| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174 |
- const { expect } = require('chai')
- const async = require('async')
- const moment = require('moment')
- const Features = require('../../../app/src/infrastructure/Features')
- const User = require('./helpers/User')
- const UserHelper = require('./helpers/UserHelper')
- const UserUpdater = require('../../../app/src/Features/User/UserUpdater')
- const { db, ObjectId } = require('../../../app/src/infrastructure/mongodb')
- const MockV1ApiClass = require('./mocks/MockV1Api')
- const expectErrorResponse = require('./helpers/expectErrorResponse')
- let MockV1Api
- before(function() {
- MockV1Api = MockV1ApiClass.instance()
- })
- describe('UserEmails', function() {
- beforeEach(function(done) {
- this.timeout(20000)
- this.user = new User()
- this.user.login(done)
- })
- describe('confirming an email', function() {
- it('should confirm the email', function(done) {
- let token = null
- async.series(
- [
- cb => {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails',
- json: {
- email: 'newly-added-email@example.com'
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(204)
- cb()
- }
- )
- },
- cb => {
- this.user.request(
- { url: '/user/emails', json: true },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(200)
- expect(body[0].confirmedAt).to.not.exist
- expect(body[0].reconfirmedAt).to.not.exist
- expect(body[1].confirmedAt).to.not.exist
- expect(body[1].reconfirmedAt).to.not.exist
- cb()
- }
- )
- },
- cb => {
- db.tokens
- .find({
- use: 'email_confirmation',
- 'data.user_id': this.user._id,
- usedAt: { $exists: false }
- })
- .toArray((error, tokens) => {
- expect(error).to.not.exist
- // There should only be one confirmation token at the moment
- expect(tokens.length).to.equal(1)
- expect(tokens[0].data.email).to.equal(
- 'newly-added-email@example.com'
- )
- expect(tokens[0].data.user_id).to.equal(this.user._id)
- ;({ token } = tokens[0])
- cb()
- })
- },
- cb => {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails/confirm',
- json: {
- token
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(200)
- cb()
- }
- )
- },
- cb => {
- this.user.request(
- { url: '/user/emails', json: true },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(200)
- expect(body[0].confirmedAt).to.not.exist
- expect(body[0].reconfirmedAt).to.not.exist
- expect(body[1].confirmedAt).to.exist
- expect(body[1].reconfirmedAt).to.exist
- expect(body[1].reconfirmedAt).to.deep.equal(body[1].confirmedAt)
- cb()
- }
- )
- },
- cb => {
- db.tokens
- .find({
- use: 'email_confirmation',
- 'data.user_id': this.user._id,
- usedAt: { $exists: false }
- })
- .toArray((error, tokens) => {
- expect(error).to.not.exist
- // Token should be deleted after use
- expect(tokens.length).to.equal(0)
- cb()
- })
- }
- ],
- done
- )
- })
- it('should not allow confirmation of the email if the user has changed', function(done) {
- let token1 = null
- let token2 = null
- this.user2 = new User()
- this.email = 'duplicate-email@example.com'
- async.series(
- [
- cb => this.user2.login(cb),
- cb => {
- // Create email for first user
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails',
- json: { email: this.email }
- },
- cb
- )
- },
- cb => {
- db.tokens
- .find({
- use: 'email_confirmation',
- 'data.user_id': this.user._id,
- usedAt: { $exists: false }
- })
- .toArray((error, tokens) => {
- expect(error).to.not.exist
- // There should only be one confirmation token at the moment
- expect(tokens.length).to.equal(1)
- expect(tokens[0].data.email).to.equal(this.email)
- expect(tokens[0].data.user_id).to.equal(this.user._id)
- token1 = tokens[0].token
- cb()
- })
- },
- cb => {
- // Delete the email from the first user
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails/delete',
- json: { email: this.email }
- },
- cb
- )
- },
- cb => {
- // Create email for second user
- this.user2.request(
- {
- method: 'POST',
- url: '/user/emails',
- json: { email: this.email }
- },
- cb
- )
- },
- cb => {
- // Original confirmation token should no longer work
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails/confirm',
- json: {
- token: token1
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(404)
- cb()
- }
- )
- },
- cb => {
- db.tokens
- .find({
- use: 'email_confirmation',
- 'data.user_id': this.user2._id,
- usedAt: { $exists: false }
- })
- .toArray((error, tokens) => {
- expect(error).to.not.exist
- // The first token has been used, so this should be token2 now
- expect(tokens.length).to.equal(1)
- expect(tokens[0].data.email).to.equal(this.email)
- expect(tokens[0].data.user_id).to.equal(this.user2._id)
- token2 = tokens[0].token
- cb()
- })
- },
- cb => {
- // Second user should be able to confirm the email
- this.user2.request(
- {
- method: 'POST',
- url: '/user/emails/confirm',
- json: {
- token: token2
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(200)
- cb()
- }
- )
- },
- cb => {
- this.user2.request(
- { url: '/user/emails', json: true },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(200)
- expect(body[0].confirmedAt).to.not.exist
- expect(body[1].confirmedAt).to.exist
- cb()
- }
- )
- }
- ],
- done
- )
- })
- })
- describe('reconfirm an email', function() {
- let email, userHelper, confirmedAtDate
- beforeEach(async function() {
- userHelper = new UserHelper()
- email = userHelper.getDefaultEmail()
- userHelper = await UserHelper.createUser({ email })
- userHelper = await UserHelper.loginUser({
- email,
- password: userHelper.getDefaultPassword()
- })
- // original confirmation
- await userHelper.confirmEmail(userHelper.user._id, email)
- const user = (await UserHelper.getUser({ email })).user
- confirmedAtDate = user.emails[0].confirmedAt
- expect(user.emails[0].confirmedAt).to.exist
- expect(user.emails[0].reconfirmedAt).to.exist
- })
- it('should set reconfirmedAt and not reset confirmedAt', async function() {
- await userHelper.confirmEmail(userHelper.user._id, email)
- const user = (await UserHelper.getUser({ email })).user
- expect(user.emails[0].confirmedAt).to.exist
- expect(user.emails[0].reconfirmedAt).to.exist
- expect(user.emails[0].confirmedAt).to.deep.equal(confirmedAtDate)
- expect(user.emails[0].reconfirmedAt).to.not.deep.equal(
- user.emails[0].confirmedAt
- )
- expect(user.emails[0].reconfirmedAt > user.emails[0].confirmedAt).to.be
- .true
- })
- })
- describe('with an expired token', function() {
- it('should not confirm the email', function(done) {
- let token = null
- async.series(
- [
- cb => {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails',
- json: {
- email: (this.email = 'expired-token-email@example.com')
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(204)
- cb()
- }
- )
- },
- cb => {
- db.tokens
- .find({
- use: 'email_confirmation',
- 'data.user_id': this.user._id,
- usedAt: { $exists: false }
- })
- .toArray((error, tokens) => {
- expect(error).to.not.exist
- // There should only be one confirmation token at the moment
- expect(tokens.length).to.equal(1)
- expect(tokens[0].data.email).to.equal(this.email)
- expect(tokens[0].data.user_id).to.equal(this.user._id)
- ;({ token } = tokens[0])
- cb()
- })
- },
- cb => {
- db.tokens.update(
- {
- token
- },
- {
- $set: {
- expiresAt: new Date(Date.now() - 1000000)
- }
- },
- cb
- )
- },
- cb => {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails/confirm',
- json: {
- token
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(404)
- cb()
- }
- )
- }
- ],
- done
- )
- })
- })
- describe('resending the confirmation', function() {
- it('should generate a new token', function(done) {
- async.series(
- [
- cb => {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails',
- json: {
- email: 'reconfirmation-email@example.com'
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(204)
- cb()
- }
- )
- },
- cb => {
- db.tokens
- .find({
- use: 'email_confirmation',
- 'data.user_id': this.user._id,
- usedAt: { $exists: false }
- })
- .toArray((error, tokens) => {
- expect(error).to.not.exist
- // There should only be one confirmation token at the moment
- expect(tokens.length).to.equal(1)
- expect(tokens[0].data.email).to.equal(
- 'reconfirmation-email@example.com'
- )
- expect(tokens[0].data.user_id).to.equal(this.user._id)
- cb()
- })
- },
- cb => {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails/resend_confirmation',
- json: {
- email: 'reconfirmation-email@example.com'
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(200)
- cb()
- }
- )
- },
- cb => {
- db.tokens
- .find({
- use: 'email_confirmation',
- 'data.user_id': this.user._id,
- usedAt: { $exists: false }
- })
- .toArray((error, tokens) => {
- expect(error).to.not.exist
- // There should be two tokens now
- expect(tokens.length).to.equal(2)
- expect(tokens[0].data.email).to.equal(
- 'reconfirmation-email@example.com'
- )
- expect(tokens[0].data.user_id).to.equal(this.user._id)
- expect(tokens[1].data.email).to.equal(
- 'reconfirmation-email@example.com'
- )
- expect(tokens[1].data.user_id).to.equal(this.user._id)
- cb()
- })
- }
- ],
- done
- )
- })
- it('should create a new token if none exists', function(done) {
- // This should only be for users that have sign up with their main
- // emails before the confirmation system existed
- async.series(
- [
- cb => {
- db.tokens.remove(
- {
- use: 'email_confirmation',
- 'data.user_id': this.user._id,
- usedAt: { $exists: false }
- },
- cb
- )
- },
- cb => {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails/resend_confirmation',
- json: {
- email: this.user.email
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(200)
- cb()
- }
- )
- },
- cb => {
- db.tokens
- .find({
- use: 'email_confirmation',
- 'data.user_id': this.user._id,
- usedAt: { $exists: false }
- })
- .toArray((error, tokens) => {
- expect(error).to.not.exist
- // There should still only be one confirmation token
- expect(tokens.length).to.equal(1)
- expect(tokens[0].data.email).to.equal(this.user.email)
- expect(tokens[0].data.user_id).to.equal(this.user._id)
- cb()
- })
- }
- ],
- done
- )
- })
- it("should not allow reconfirmation if the email doesn't match the user", function(done) {
- async.series(
- [
- cb => {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails/resend_confirmation',
- json: {
- email: 'non-matching-email@example.com'
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(422)
- cb()
- }
- )
- },
- cb => {
- db.tokens
- .find({
- use: 'email_confirmation',
- 'data.user_id': this.user._id,
- usedAt: { $exists: false }
- })
- .toArray((error, tokens) => {
- expect(error).to.not.exist
- expect(tokens.length).to.equal(0)
- cb()
- })
- }
- ],
- done
- )
- })
- })
- describe('setting a default email', function() {
- it('should update confirmed emails for users not in v1', function(done) {
- async.series(
- [
- cb => {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails',
- json: {
- email: 'new-confirmed-default@example.com'
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(204)
- cb()
- }
- )
- },
- cb => {
- // Mark the email as confirmed
- db.users.updateOne(
- {
- 'emails.email': 'new-confirmed-default@example.com'
- },
- {
- $set: {
- 'emails.$.confirmedAt': new Date()
- }
- },
- cb
- )
- },
- cb => {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails/default',
- json: {
- email: 'new-confirmed-default@example.com'
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(200)
- cb()
- }
- )
- },
- cb => {
- this.user.request(
- { url: '/user/emails', json: true },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(200)
- expect(body[0].confirmedAt).to.not.exist
- expect(body[0].default).to.equal(false)
- expect(body[1].confirmedAt).to.exist
- expect(body[1].default).to.equal(true)
- cb()
- }
- )
- }
- ],
- done
- )
- })
- it('should not allow changing unconfirmed emails in v1', function(done) {
- async.series(
- [
- cb => {
- db.users.updateOne(
- {
- _id: ObjectId(this.user._id)
- },
- {
- $set: {
- 'overleaf.id': 42
- }
- },
- cb
- )
- },
- cb => {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails',
- json: {
- email: 'new-unconfirmed-default@example.com'
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(204)
- cb()
- }
- )
- },
- cb => {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails/default',
- json: {
- email: 'new-unconfirmed-default@example.com'
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(409)
- cb()
- }
- )
- },
- cb => {
- this.user.request(
- { url: '/user/emails', json: true },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(body[0].default).to.equal(true)
- expect(body[1].default).to.equal(false)
- cb()
- }
- )
- }
- ],
- done
- )
- })
- it('should not update the email in v1', function(done) {
- async.series(
- [
- cb => {
- db.users.updateOne(
- {
- _id: ObjectId(this.user._id)
- },
- {
- $set: {
- 'overleaf.id': 42
- }
- },
- cb
- )
- },
- cb => {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails',
- json: {
- email: 'new-confirmed-default-in-v1@example.com'
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(204)
- cb()
- }
- )
- },
- cb => {
- // Mark the email as confirmed
- db.users.updateOne(
- {
- 'emails.email': 'new-confirmed-default-in-v1@example.com'
- },
- {
- $set: {
- 'emails.$.confirmedAt': new Date()
- }
- },
- cb
- )
- },
- cb => {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails/default',
- json: {
- email: 'new-confirmed-default-in-v1@example.com'
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(200)
- cb()
- }
- )
- }
- ],
- error => {
- expect(error).to.not.exist
- expect(MockV1Api.updateEmail.callCount).to.equal(0)
- done()
- }
- )
- })
- it('should not return an error if the email exists in v1', function(done) {
- MockV1Api.existingEmails.push('exists-in-v1@example.com')
- async.series(
- [
- cb => {
- db.users.updateOne(
- {
- _id: ObjectId(this.user._id)
- },
- {
- $set: {
- 'overleaf.id': 42
- }
- },
- cb
- )
- },
- cb => {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails',
- json: {
- email: 'exists-in-v1@example.com'
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(204)
- cb()
- }
- )
- },
- cb => {
- // Mark the email as confirmed
- db.users.updateOne(
- {
- 'emails.email': 'exists-in-v1@example.com'
- },
- {
- $set: {
- 'emails.$.confirmedAt': new Date()
- }
- },
- cb
- )
- },
- cb => {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails/default',
- json: {
- email: 'exists-in-v1@example.com'
- }
- },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(200)
- cb()
- }
- )
- },
- cb => {
- this.user.request(
- { url: '/user/emails', json: true },
- (error, response, body) => {
- expect(error).to.not.exist
- expect(body[0].default).to.equal(false)
- expect(body[1].default).to.equal(true)
- cb()
- }
- )
- }
- ],
- done
- )
- })
- describe('audit log', function() {
- const originalEmail = 'original@overleaf.com'
- let otherEmail, response, userHelper, user, userId
- beforeEach(async function() {
- otherEmail = 'other@overleaf.com'
- userHelper = new UserHelper()
- userHelper = await UserHelper.createUser({
- email: originalEmail
- })
- userHelper = await UserHelper.loginUser({
- email: originalEmail,
- password: userHelper.getDefaultPassword()
- })
- userId = userHelper.user._id
- response = await userHelper.request.post({
- form: {
- email: otherEmail
- },
- simple: false,
- uri: '/user/emails'
- })
- expect(response.statusCode).to.equal(204)
- const token = (
- await db.tokens.findOne({
- 'data.user_id': userId.toString(),
- 'data.email': otherEmail
- })
- ).token
- response = await userHelper.request.post(`/user/emails/confirm`, {
- form: {
- token
- },
- simple: false
- })
- expect(response.statusCode).to.equal(200)
- response = await userHelper.request.post('/user/emails/default', {
- form: {
- email: otherEmail
- },
- simple: false
- })
- expect(response.statusCode).to.equal(200)
- userHelper = await UserHelper.getUser(userId)
- user = userHelper.user
- })
- it('should be updated', function() {
- const auditLog = userHelper.getAuditLogWithoutNoise()
- const entry = auditLog[auditLog.length - 1]
- expect(typeof entry.initiatorId).to.equal('object')
- expect(entry.initiatorId).to.deep.equal(user._id)
- expect(entry.ipAddress).to.equal('127.0.0.1')
- expect(entry.info).to.deep.equal({
- newPrimaryEmail: otherEmail,
- oldPrimaryEmail: originalEmail
- })
- })
- })
- describe('session cleanup', function() {
- beforeEach(function setupSecondSession(done) {
- this.userSession2 = new User()
- this.userSession2.email = this.user.email
- this.userSession2.emails = this.user.emails
- this.userSession2.password = this.user.password
- // login before adding the new email address
- // User.login() performs a mongo update and resets the .emails field.
- this.userSession2.login(done)
- })
- beforeEach(function checkSecondSessionLiveness(done) {
- this.userSession2.request(
- { method: 'GET', url: '/project', followRedirect: false },
- (error, response) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(200)
- done()
- }
- )
- })
- beforeEach(function addSecondaryEmail(done) {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails',
- json: { email: 'new-confirmed-default@example.com' }
- },
- (error, response) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(204)
- done()
- }
- )
- })
- beforeEach(function confirmSecondaryEmail(done) {
- db.users.updateOne(
- { 'emails.email': 'new-confirmed-default@example.com' },
- { $set: { 'emails.$.confirmedAt': new Date() } },
- done
- )
- })
- beforeEach(function setDefault(done) {
- this.user.request(
- {
- method: 'POST',
- url: '/user/emails/default',
- json: { email: 'new-confirmed-default@example.com' }
- },
- (error, response) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(200)
- done()
- }
- )
- })
- it('should logout the other sessions', function(done) {
- this.userSession2.request(
- { method: 'GET', url: '/project', followRedirect: false },
- (error, response) => {
- expect(error).to.not.exist
- expect(response.statusCode).to.equal(302)
- expect(response.headers)
- .to.have.property('location')
- .to.match(new RegExp('^/login'))
- done()
- }
- )
- })
- })
- })
- describe('when not logged in', function() {
- beforeEach(function(done) {
- this.anonymous = new User()
- this.anonymous.getCsrfToken(done)
- })
- it('should return a plain 403 when setting the email', function(done) {
- this.anonymous.request(
- {
- method: 'POST',
- url: '/user/emails',
- json: {
- email: 'newly-added-email@example.com'
- }
- },
- (error, response, body) => {
- if (error) {
- return done(error)
- }
- expectErrorResponse.requireLogin.json(response, body)
- done()
- }
- )
- })
- })
- describe('secondary email', function() {
- let newEmail, userHelper, userId, user
- beforeEach(async function() {
- newEmail = 'a-new-email@overleaf.com'
- userHelper = new UserHelper()
- userHelper = await UserHelper.createUser()
- userHelper = await UserHelper.loginUser({
- email: userHelper.getDefaultEmail(),
- password: userHelper.getDefaultPassword()
- })
- userId = userHelper.user._id
- await userHelper.request.post({
- form: {
- email: newEmail
- },
- simple: false,
- uri: '/user/emails'
- })
- userHelper = await UserHelper.getUser(userId)
- user = userHelper.user
- })
- it('should add the email', async function() {
- expect(user.emails[1].email).to.equal(newEmail)
- })
- it('should add to the user audit log', async function() {
- const auditLog = userHelper.getAuditLogWithoutNoise()
- expect(typeof auditLog[0].initiatorId).to.equal('object')
- expect(auditLog[0].initiatorId).to.deep.equal(user._id)
- expect(auditLog[0].info.newSecondaryEmail).to.equal(newEmail)
- expect(auditLog[0].ip).to.equal(this.user.request.ip)
- })
- })
- describe('notification period', function() {
- let defaultEmail, userHelper, email1, email2, email3
- const maxConfirmationMonths = 12
- const lastDayToReconfirm = moment()
- .subtract(maxConfirmationMonths, 'months')
- .toDate()
- const oneDayBeforeLastDayToReconfirm = moment(lastDayToReconfirm)
- .add(1, 'day')
- .toDate()
- const daysToBackdate = moment().diff(oneDayBeforeLastDayToReconfirm, 'day')
- const daysToBackdateForAfterDate = daysToBackdate + 1
- beforeEach(async function() {
- if (!Features.hasFeature('affiliations')) {
- this.skip()
- }
- userHelper = new UserHelper()
- defaultEmail = userHelper.getDefaultEmail()
- userHelper = await UserHelper.createUser({ email: defaultEmail })
- userHelper = await UserHelper.loginUser({
- email: defaultEmail,
- password: userHelper.getDefaultPassword()
- })
- const institutionId = MockV1Api.createInstitution({
- ssoEnabled: false,
- maxConfirmationMonths
- })
- const domain = 'example-affiliation.com'
- MockV1Api.addInstitutionDomain(institutionId, domain)
- email1 = `leonard@${domain}`
- email2 = `mccoy@${domain}`
- email3 = `bones@${domain}`
- })
- describe('non SSO affiliations', function() {
- beforeEach(async function() {
- // create a user with 3 affiliations at the institution.
- // all are within in the notification period
- const userId = userHelper.user._id
- await userHelper.addEmailAndConfirm(userId, email1)
- await userHelper.addEmailAndConfirm(userId, email2)
- await userHelper.addEmailAndConfirm(userId, email3)
- await userHelper.backdateConfirmation(userId, email1, daysToBackdate)
- await userHelper.backdateConfirmation(userId, email2, daysToBackdate)
- await userHelper.backdateConfirmation(
- userId,
- email3,
- daysToBackdateForAfterDate
- )
- })
- it('should flag inReconfirmNotificationPeriod for all affiliations in period', async function() {
- const response = await userHelper.request.get('/user/emails')
- expect(response.statusCode).to.equal(200)
- const fullEmails = JSON.parse(response.body)
- expect(fullEmails.length).to.equal(4)
- expect(fullEmails[0].affiliation).to.not.exist
- expect(
- fullEmails[1].affiliation.inReconfirmNotificationPeriod
- ).to.equal(true)
- expect(fullEmails[1].affiliation.pastReconfirmDate).to.equal(false)
- expect(
- fullEmails[2].affiliation.inReconfirmNotificationPeriod
- ).to.equal(true)
- expect(fullEmails[2].affiliation.pastReconfirmDate).to.equal(false)
- expect(
- fullEmails[3].affiliation.inReconfirmNotificationPeriod
- ).to.equal(true)
- expect(fullEmails[3].affiliation.pastReconfirmDate).to.equal(true)
- })
- describe('should flag emails before their confirmation expires, but within the notification period', function() {
- beforeEach(async function() {
- const dateInPeriodButNotExpired = moment()
- .subtract(maxConfirmationMonths, 'months')
- .add(14, 'days')
- .toDate()
- const backdatedDays = moment().diff(dateInPeriodButNotExpired, 'days')
- await userHelper.backdateConfirmation(
- userHelper.user._id,
- email1,
- backdatedDays
- )
- await userHelper.backdateConfirmation(
- userHelper.user._id,
- email2,
- backdatedDays
- )
- await userHelper.backdateConfirmation(
- userHelper.user._id,
- email3,
- backdatedDays
- )
- })
- it('should flag the emails', async function() {
- const response = await userHelper.request.get('/user/emails')
- expect(response.statusCode).to.equal(200)
- const fullEmails = JSON.parse(response.body)
- expect(fullEmails.length).to.equal(4)
- expect(fullEmails[0].affiliation).to.not.exist
- expect(
- fullEmails[1].affiliation.inReconfirmNotificationPeriod
- ).to.equal(true)
- expect(
- fullEmails[2].affiliation.inReconfirmNotificationPeriod
- ).to.equal(true)
- expect(
- fullEmails[3].affiliation.inReconfirmNotificationPeriod
- ).to.equal(true)
- // ensure dates are not past reconfirmation period
- function _getLastDayToReconfirm(date) {
- return moment(date).add(maxConfirmationMonths, 'months')
- }
- expect(
- moment(fullEmails[1].reconfirmedAt).isAfter(
- _getLastDayToReconfirm(fullEmails[1].reconfirmedAt)
- )
- ).to.equal(false)
- expect(
- moment(fullEmails[2].reconfirmedAt).isAfter(
- _getLastDayToReconfirm(fullEmails[2].reconfirmedAt)
- )
- ).to.equal(false)
- expect(
- moment(fullEmails[3].reconfirmedAt).isAfter(
- _getLastDayToReconfirm(fullEmails[3].reconfirmedAt)
- )
- ).to.equal(false)
- })
- })
- describe('missing reconfirmedAt', function() {
- beforeEach(async function() {
- const userId = userHelper.user._id
- const query = {
- _id: userId,
- 'emails.email': email2
- }
- const update = {
- $unset: { 'emails.$.reconfirmedAt': true }
- }
- await UserUpdater.promises.updateUser(query, update)
- })
- it('should fallback to confirmedAt for date check', async function() {
- const response = await userHelper.request.get('/user/emails')
- expect(response.statusCode).to.equal(200)
- const fullEmails = JSON.parse(response.body)
- expect(fullEmails.length).to.equal(4)
- expect(fullEmails[0].affiliation).to.not.exist
- expect(fullEmails[2].reconfirmedAt).to.not.exist
- expect(
- fullEmails[1].affiliation.inReconfirmNotificationPeriod
- ).to.equal(true)
- expect(
- fullEmails[2].affiliation.inReconfirmNotificationPeriod
- ).to.equal(true)
- expect(
- fullEmails[3].affiliation.inReconfirmNotificationPeriod
- ).to.equal(true)
- })
- })
- })
- })
- })
|