| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970 |
- import { vi, assert, expect } from 'vitest'
- import { setTimeout } from 'node:timers/promises'
- import MockRequest from '../helpers/MockRequest.mjs'
- import MockResponse from '../helpers/MockResponse.mjs'
- import Errors from '../../../../app/src/Features/Errors/Errors.js'
- const modulePath = '../../../../app/src/Features/User/UserEmailsController.mjs'
- vi.mock('../../../../app/src/Features/Errors/Errors.js', () =>
- vi.importActual('../../../../app/src/Features/Errors/Errors.js')
- )
- describe('UserEmailsController', function () {
- beforeEach(async function (ctx) {
- ctx.req = new MockRequest(vi)
- ctx.req.sessionID = Math.random().toString()
- ctx.res = new MockResponse(vi)
- ctx.next = vi.fn()
- ctx.user = {
- _id: 'mock-user-id',
- email: 'example@overleaf.com',
- emails: [],
- }
- ctx.UserGetter = {
- getUser: vi.fn().mockImplementation((userId, projection, callback) => {
- callback?.(null, ctx.user)
- }),
- getUserFullEmails: vi.fn(),
- promises: {
- ensureUniqueEmailAddress: vi.fn().mockResolvedValue(undefined),
- getUser: vi.fn().mockResolvedValue(ctx.user),
- getUserByAnyEmail: vi.fn(),
- },
- }
- ctx.SessionManager = {
- getSessionUser: vi.fn().mockReturnValue(ctx.user),
- getLoggedInUserId: vi.fn().mockReturnValue(ctx.user._id),
- setInSessionUser: vi.fn(),
- }
- ctx.Features = {
- hasFeature: vi.fn(),
- }
- ctx.UserSessionsManager = {
- promises: {
- removeSessionsFromRedis: vi.fn().mockResolvedValue(undefined),
- },
- }
- ctx.UserUpdater = {
- addEmailAddress: vi.fn(),
- updateV1AndSetDefaultEmailAddress: vi.fn(),
- promises: {
- addEmailAddress: vi.fn().mockResolvedValue(undefined),
- confirmEmail: vi.fn().mockResolvedValue(undefined),
- removeEmailAddress: vi.fn(),
- setDefaultEmailAddress: vi.fn().mockResolvedValue(undefined),
- },
- }
- ctx.EmailHelper = { parseEmail: vi.fn() }
- ctx.endorseAffiliation = vi.fn((userId, email, role, dept, callback) =>
- callback()
- )
- ctx.InstitutionsAPI = {
- endorseAffiliation: ctx.endorseAffiliation,
- }
- ctx.HttpErrorHandler = { conflict: vi.fn() }
- ctx.AnalyticsManager = {
- recordEventForSession: vi.fn(),
- recordEventForMongoUserInBackground: vi.fn(),
- }
- ctx.UserAuditLogHandler = {
- addEntry: vi.fn((userId, op, initiatorId, ip, info, callback) =>
- callback()
- ),
- promises: {
- addEntry: vi.fn().mockResolvedValue(undefined),
- },
- }
- ctx.rateLimiter = {
- consume: vi.fn().mockResolvedValue(undefined),
- }
- ctx.RateLimiter = {
- RateLimiter: vi.fn().mockImplementation(function RateLimiter() {
- this.consume = ctx.rateLimiter.consume
- }),
- }
- ctx.AuthenticationController = {
- getRedirectFromSession: vi.fn().mockReturnValue(null),
- }
- vi.doMock(
- '../../../../app/src/Features/Authentication/AuthenticationController',
- () => ({
- default: ctx.AuthenticationController,
- })
- )
- vi.doMock(
- '../../../../app/src/Features/Authentication/SessionManager',
- () => ({
- default: ctx.SessionManager,
- })
- )
- vi.doMock('../../../../app/src/infrastructure/Features', () => ({
- default: ctx.Features,
- }))
- vi.doMock('../../../../app/src/Features/User/UserSessionsManager', () => ({
- default: ctx.UserSessionsManager,
- }))
- vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
- default: ctx.UserGetter,
- }))
- vi.doMock('../../../../app/src/Features/User/UserUpdater', () => ({
- default: ctx.UserUpdater,
- }))
- vi.doMock('../../../../app/src/Features/Email/EmailHandler', () => ({
- default: (ctx.EmailHandler = {
- promises: {
- sendEmail: vi.fn().mockResolvedValue(undefined),
- },
- }),
- }))
- vi.doMock('../../../../app/src/Features/Helpers/EmailHelper', () => ({
- default: ctx.EmailHelper,
- }))
- vi.doMock(
- '../../../../app/src/Features/User/UserEmailsConfirmationHandler',
- () => ({
- default: (ctx.UserEmailsConfirmationHandler = {
- promises: {},
- }),
- })
- )
- vi.doMock(
- '../../../../app/src/Features/Institutions/InstitutionsAPI',
- () => ({
- default: ctx.InstitutionsAPI,
- })
- )
- vi.doMock('../../../../app/src/Features/Errors/HttpErrorHandler', () => ({
- default: ctx.HttpErrorHandler,
- }))
- vi.doMock(
- '../../../../app/src/Features/Analytics/AnalyticsManager',
- () => ({
- default: ctx.AnalyticsManager,
- })
- )
- vi.doMock('../../../../app/src/Features/User/UserAuditLogHandler', () => ({
- default: ctx.UserAuditLogHandler,
- }))
- vi.doMock(
- '../../../../app/src/infrastructure/RateLimiter',
- () => ctx.RateLimiter
- )
- ctx.UserEmailsController = (await import(modulePath)).default
- })
- describe('List', function () {
- beforeEach(function () {})
- it('lists emails', async function (ctx) {
- expect.assertions(1)
- const fullEmails = [{ some: 'data' }]
- ctx.UserGetter.getUserFullEmails.mockImplementation(
- (userId, callback) => {
- callback(null, fullEmails)
- }
- )
- await ctx.UserEmailsController.list(ctx.req, {
- json: response => {
- assert.deepEqual(response, fullEmails)
- expect(ctx.UserGetter.getUserFullEmails).toHaveBeenCalledWith(
- ctx.user._id,
- expect.any(Function)
- )
- },
- })
- })
- })
- describe('addWithConfirmationCode', function () {
- beforeEach(function (ctx) {
- ctx.newEmail = 'new_email@baz.com'
- ctx.req.body = {
- email: ctx.newEmail,
- }
- ctx.EmailHelper.parseEmail.mockReturnValue(ctx.newEmail)
- ctx.UserEmailsConfirmationHandler.promises.sendConfirmationCode = vi
- .fn()
- .mockResolvedValue({
- confirmCode: '123456',
- confirmCodeExpiresTimestamp: new Date(),
- })
- })
- it('sends an email confirmation', async function (ctx) {
- expect.assertions(2)
- await ctx.UserEmailsController.addWithConfirmationCode(ctx.req, {
- sendStatus: code => {
- expect(code).to.equal(200)
- expect(
- ctx.UserEmailsConfirmationHandler.promises.sendConfirmationCode
- ).toHaveBeenCalledWith(ctx.newEmail, false)
- },
- })
- })
- it('handles email parse error', async function (ctx) {
- expect.assertions(1)
- ctx.EmailHelper.parseEmail.mockReturnValue(null)
- await ctx.UserEmailsController.addWithConfirmationCode(ctx.req, {
- sendStatus: code => {
- expect(code).to.equal(422)
- },
- })
- })
- it('handles when the email already exists', async function (ctx) {
- expect.assertions(1)
- ctx.UserGetter.promises.ensureUniqueEmailAddress.mockRejectedValue(
- new Errors.EmailExistsError()
- )
- await ctx.UserEmailsController.addWithConfirmationCode(ctx.req, {
- status: code => {
- expect(code).to.equal(409)
- return { json: () => {} }
- },
- })
- })
- it('should fail to add new emails when the limit has been reached', async function (ctx) {
- expect.assertions(2)
- ctx.user.emails = []
- for (let i = 0; i < 10; i++) {
- ctx.user.emails.push({ email: `example${i}@overleaf.com` })
- }
- await ctx.UserEmailsController.addWithConfirmationCode(ctx.req, {
- status: code => {
- expect(code).to.equal(422)
- return {
- json: error => {
- expect(error.message).to.equal('secondary email limit exceeded')
- },
- }
- },
- })
- })
- })
- describe('checkNewSecondaryEmailConfirmationCode', function () {
- beforeEach(function (ctx) {
- ctx.newEmail = 'new_email@baz.com'
- ctx.req.session.pendingSecondaryEmail = {
- confirmCode: '123456',
- email: ctx.newEmail,
- confirmCodeExpiresTimestamp: new Date(Math.max),
- }
- })
- describe('with a valid confirmation code', function () {
- beforeEach(function (ctx) {
- ctx.req.body = {
- code: '123456',
- }
- })
- it('adds the email', async function (ctx) {
- expect.assertions(2)
- await ctx.UserEmailsController.checkNewSecondaryEmailConfirmationCode(
- ctx.req,
- {
- json: () => {
- expect(
- ctx.UserUpdater.promises.addEmailAddress
- ).toHaveBeenCalledWith(ctx.user._id, ctx.newEmail, undefined, {
- initiatorId: 'mock-user-id',
- ipAddress: '42.42.42.42',
- })
- expect(
- ctx.UserUpdater.promises.confirmEmail
- ).toHaveBeenCalledWith(ctx.user._id, ctx.newEmail, undefined)
- },
- }
- )
- })
- it('redirects to /project', async function (ctx) {
- expect.assertions(1)
- await ctx.UserEmailsController.checkNewSecondaryEmailConfirmationCode(
- ctx.req,
- {
- json: ({ redir }) => {
- expect(redir).to.equal('/project')
- },
- }
- )
- })
- it('sends a security alert email', async function (ctx) {
- expect.assertions(4)
- ctx.req.session.pendingSecondaryEmail = {
- confirmCode: '123456',
- email: ctx.newEmail,
- confirmCodeExpiresTimestamp: new Date(Math.max),
- affiliationOptions: {},
- }
- ctx.req.body.code = '123456'
- await ctx.UserEmailsController.checkNewSecondaryEmailConfirmationCode(
- ctx.req,
- {
- json: vi.fn().mockResolvedValue(undefined),
- }
- )
- const emailCall = ctx.EmailHandler.promises.sendEmail.mock.calls[0]
- expect(emailCall[0]).to.equal('securityAlert')
- expect(emailCall[1].to).to.equal(ctx.user.email)
- expect(emailCall[1].actionDescribed).to.contain(
- 'a secondary email address'
- )
- expect(emailCall[1].message[0]).to.contain(ctx.newEmail)
- })
- })
- describe('with an invalid confirmation code', function () {
- beforeEach(function (ctx) {
- ctx.req.body = {
- code: '999999',
- }
- })
- it('does not add the email', async function (ctx) {
- expect.assertions(2)
- await ctx.UserEmailsController.checkNewSecondaryEmailConfirmationCode(
- ctx.req,
- {
- status: () => {
- expect(
- ctx.UserUpdater.promises.addEmailAddress
- ).not.toHaveBeenCalled()
- expect(
- ctx.UserUpdater.promises.confirmEmail
- ).not.toHaveBeenCalled()
- return { json: ctx.next }
- },
- }
- )
- })
- it('responds with a 403', async function (ctx) {
- expect.assertions(1)
- await ctx.UserEmailsController.checkNewSecondaryEmailConfirmationCode(
- ctx.req,
- {
- status: code => {
- expect(code).to.equal(403)
- return { json: ctx.next }
- },
- }
- )
- })
- })
- })
- describe('resendNewSecondaryEmailConfirmationCode', function () {
- beforeEach(function (ctx) {
- ctx.newEmail = 'new_email@baz.com'
- ctx.req.session.pendingSecondaryEmail = {
- confirmCode: '123456',
- email: ctx.newEmail,
- confirmCodeExpiresTimestamp: new Date(Math.max),
- }
- ctx.UserEmailsConfirmationHandler.promises.sendConfirmationCode = vi
- .fn()
- .mockResolvedValue({
- confirmCode: '123456',
- confirmCodeExpiresTimestamp: new Date(),
- })
- })
- it('should send the email', async function (ctx) {
- expect.assertions(2)
- await ctx.UserEmailsController.resendNewSecondaryEmailConfirmationCode(
- ctx.req,
- {
- status: code => {
- expect(code).to.equal(200)
- expect(
- ctx.UserEmailsConfirmationHandler.promises.sendConfirmationCode
- ).toHaveBeenCalledWith(ctx.newEmail, false)
- return { json: ctx.next }
- },
- }
- )
- })
- })
- describe('remove', function () {
- beforeEach(function (ctx) {
- ctx.email = 'email_to_remove@bar.com'
- ctx.req.body.email = ctx.email
- ctx.EmailHelper.parseEmail.mockReturnValue(ctx.email)
- })
- it('removes email', async function (ctx) {
- expect.assertions(3)
- const auditLog = {
- initiatorId: ctx.user._id,
- ipAddress: ctx.req.ip,
- }
- ctx.UserUpdater.promises.removeEmailAddress.mockResolvedValue(undefined)
- await ctx.UserEmailsController.remove(ctx.req, {
- sendStatus: code => {
- expect(code).to.equal(200)
- expect(ctx.EmailHelper.parseEmail).toHaveBeenCalledWith(ctx.email)
- expect(
- ctx.UserUpdater.promises.removeEmailAddress
- ).toHaveBeenCalledWith(ctx.user._id, ctx.email, auditLog)
- },
- })
- })
- it('handles email parse error', async function (ctx) {
- expect.assertions(2)
- ctx.EmailHelper.parseEmail.mockReturnValue(null)
- await ctx.UserEmailsController.remove(ctx.req, {
- sendStatus: code => {
- expect(code).to.equal(422)
- expect(
- ctx.UserUpdater.promises.removeEmailAddress
- ).not.toHaveBeenCalled()
- },
- })
- })
- })
- describe('setDefault', function () {
- beforeEach(function (ctx) {
- ctx.email = 'email_to_set_default@bar.com'
- ctx.req.body.email = ctx.email
- ctx.EmailHelper.parseEmail.mockReturnValue(ctx.email)
- ctx.SessionManager.setInSessionUser.mockReturnValue(null)
- })
- it('sets default email', async function (ctx) {
- expect.assertions(4)
- await ctx.UserEmailsController.setDefault(ctx.req, {
- sendStatus: code => {
- expect(code).to.equal(200)
- expect(ctx.EmailHelper.parseEmail).toHaveBeenCalledWith(ctx.email)
- expect(ctx.SessionManager.setInSessionUser).toHaveBeenCalledWith(
- ctx.req.session,
- {
- email: ctx.email,
- }
- )
- expect(
- ctx.UserUpdater.promises.setDefaultEmailAddress
- ).toHaveBeenCalledWith(
- ctx.user._id,
- ctx.email,
- false,
- { initiatorId: 'mock-user-id', ipAddress: '42.42.42.42' },
- true,
- false
- )
- },
- })
- })
- it('deletes unconfirmed primary if delete-unconfirmed-primary is set', async function (ctx) {
- expect.assertions(1)
- ctx.user.emails = [{ email: 'example@overleaf.com' }]
- ctx.req.query['delete-unconfirmed-primary'] = ''
- await ctx.UserEmailsController.setDefault(ctx.req, {
- sendStatus: () => {
- expect(
- ctx.UserUpdater.promises.removeEmailAddress
- ).toHaveBeenCalledWith(ctx.user._id, 'example@overleaf.com', {
- initiatorId: ctx.user._id,
- ipAddress: ctx.req.ip,
- extraInfo: {
- info: 'removed unconfirmed email after setting new primary',
- },
- })
- },
- })
- })
- it('doesnt delete a confirmed primary', async function (ctx) {
- expect.assertions(1)
- ctx.user.emails = [
- { email: 'example@overleaf.com', confirmedAt: '2000-01-01' },
- ]
- ctx.req.query['delete-unconfirmed-primary'] = ''
- await ctx.UserEmailsController.setDefault(ctx.req, {
- sendStatus: () => {
- expect(
- ctx.UserUpdater.promises.removeEmailAddress
- ).not.toHaveBeenCalled()
- },
- })
- })
- it('doesnt delete primary if delete-unconfirmed-primary is not set', async function (ctx) {
- await ctx.UserEmailsController.setDefault(ctx.req, {
- sendStatus: () => {
- expect(
- ctx.UserUpdater.promises.removeEmailAddress
- ).not.toHaveBeenCalled()
- },
- })
- })
- it('handles email parse error', async function (ctx) {
- expect.assertions(2)
- ctx.EmailHelper.parseEmail.mockReturnValue(null)
- await ctx.UserEmailsController.setDefault(ctx.req, {
- sendStatus: code => {
- expect(code).to.equal(422)
- expect(
- ctx.UserUpdater.promises.setDefaultEmailAddress
- ).not.toHaveBeenCalled()
- },
- })
- })
- it('should reset the users other sessions', async function (ctx) {
- await ctx.UserEmailsController.setDefault(ctx.req, ctx.res)
- expect(
- ctx.UserSessionsManager.promises.removeSessionsFromRedis
- ).toHaveBeenCalledWith(ctx.user, ctx.req.sessionID)
- })
- it('handles error from revoking sessions and returns 200', async function (ctx) {
- const redisError = new Error('redis error')
- ctx.UserSessionsManager.promises.removeSessionsFromRedis = vi
- .fn()
- .mockRejectedValue(redisError)
- await ctx.UserEmailsController.setDefault(ctx.req, ctx.res)
- expect(ctx.res.statusCode).to.equal(200)
- // give revoke process time to run
- await setTimeout(0)
- expect(ctx.logger.warn).toHaveBeenCalledWith(
- expect.objectContaining({ err: redisError }),
- 'failed revoking secondary sessions after changing default email'
- )
- })
- })
- describe('endorse', function () {
- beforeEach(function (ctx) {
- ctx.email = 'email_to_endorse@bar.com'
- ctx.req.body.email = ctx.email
- ctx.EmailHelper.parseEmail.mockReturnValue(ctx.email)
- })
- it('endorses affiliation', async function (ctx) {
- expect.assertions(2)
- ctx.req.body.role = 'Role'
- ctx.req.body.department = 'Department'
- await ctx.UserEmailsController.endorse(ctx.req, {
- sendStatus: code => {
- expect(code).to.equal(204)
- expect(ctx.endorseAffiliation).toHaveBeenCalledWith(
- ctx.user._id,
- ctx.email,
- 'Role',
- 'Department',
- expect.any(Function)
- )
- },
- })
- })
- })
- describe('confirm', function () {
- beforeEach(function (ctx) {
- ctx.UserEmailsConfirmationHandler.confirmEmailFromToken = vi
- .fn()
- .mockImplementation((req, token, callback) => {
- callback(null, { userId: ctx.user._id, email: ctx.user.email })
- })
- ctx.token = 'mock-token'
- ctx.req.body = { token: ctx.token }
- ctx.req.ip = '0.0.0.0'
- ctx.next = vi.fn()
- ctx.res = new MockResponse(vi)
- })
- describe('successfully', function () {
- beforeEach(function (ctx) {
- ctx.UserEmailsController.confirm(ctx.req, ctx.res, ctx.next)
- })
- it('should confirm the email from the token', function (ctx) {
- expect(
- ctx.UserEmailsConfirmationHandler.confirmEmailFromToken
- ).toHaveBeenCalledWith(ctx.req, ctx.token, expect.any(Function))
- })
- it('should return a 200 status', function (ctx) {
- expect(ctx.res.sendStatus).toHaveBeenCalledWith(200)
- })
- it('should log the confirmation to the audit log', function (ctx) {
- expect(ctx.UserAuditLogHandler.addEntry).toHaveBeenCalledWith(
- ctx.user._id,
- 'confirm-email',
- ctx.user._id,
- ctx.req.ip,
- {
- token: ctx.token.substring(0, 10),
- email: ctx.user.email,
- },
- expect.any(Function)
- )
- })
- })
- describe('without a token', function () {
- beforeEach(function (ctx) {
- ctx.req.body.token = null
- ctx.UserEmailsController.confirm(ctx.req, ctx.res, ctx.next)
- })
- it('should return a 422 status', function (ctx) {
- expect(ctx.res.status).toHaveBeenCalledWith(422)
- })
- })
- describe('when confirming fails', function () {
- beforeEach(function (ctx) {
- ctx.UserEmailsConfirmationHandler.confirmEmailFromToken = vi
- .fn()
- .mockImplementation((req, token, callback) => {
- callback(new Errors.NotFoundError('not found'))
- })
- })
- it('should return a 404 error code with a message', function (ctx) {
- ctx.UserEmailsController.confirm(ctx.req, ctx.res, ctx.next)
- expect(ctx.res.status).toHaveBeenCalledWith(404)
- expect(ctx.res.json).toHaveBeenCalledWith({
- message: ctx.req.i18n.translate('confirmation_token_invalid'),
- })
- })
- })
- })
- describe('sendExistingEmailConfirmationCode', function () {
- beforeEach(function (ctx) {
- ctx.email = 'existing-email@example.com'
- ctx.req.body.email = ctx.email
- ctx.EmailHelper.parseEmail.mockReturnValue(ctx.email)
- ctx.UserGetter.promises.getUserByAnyEmail.mockResolvedValue({
- _id: ctx.user._id,
- email: ctx.email,
- })
- ctx.UserEmailsConfirmationHandler.promises.sendConfirmationCode = vi
- .fn()
- .mockResolvedValue({
- confirmCode: '123456',
- confirmCodeExpiresTimestamp: new Date(),
- })
- })
- it('should send confirmation code for existing email', async function (ctx) {
- expect.assertions(2)
- await ctx.UserEmailsController.sendExistingEmailConfirmationCode(
- ctx.req,
- {
- sendStatus: code => {
- expect(code).to.equal(204)
- expect(
- ctx.UserEmailsConfirmationHandler.promises.sendConfirmationCode
- ).toHaveBeenCalledWith(ctx.email, false)
- },
- }
- )
- })
- it('should store confirmation code in session', async function (ctx) {
- const confirmCode = '123456'
- const confirmCodeExpiresTimestamp = new Date()
- ctx.UserEmailsConfirmationHandler.promises.sendConfirmationCode.mockResolvedValue(
- {
- confirmCode,
- confirmCodeExpiresTimestamp,
- }
- )
- await ctx.UserEmailsController.sendExistingEmailConfirmationCode(
- ctx.req,
- { sendStatus: vi.fn() }
- )
- expect(ctx.req.session.pendingExistingEmail).to.deep.equal({
- email: ctx.email,
- confirmCode,
- confirmCodeExpiresTimestamp,
- affiliationOptions: undefined,
- })
- })
- it('should handle invalid email', async function (ctx) {
- expect.assertions(2)
- ctx.EmailHelper.parseEmail.mockReturnValue(null)
- await ctx.UserEmailsController.sendExistingEmailConfirmationCode(
- ctx.req,
- {
- sendStatus: code => {
- expect(code).to.equal(400)
- expect(
- ctx.UserEmailsConfirmationHandler.promises.sendConfirmationCode
- ).not.toHaveBeenCalled()
- },
- }
- )
- })
- it('should handle email not belonging to user', async function (ctx) {
- expect.assertions(2)
- ctx.UserGetter.promises.getUserByAnyEmail.mockResolvedValue({
- _id: 'another-user-id',
- })
- await ctx.UserEmailsController.sendExistingEmailConfirmationCode(
- ctx.req,
- {
- sendStatus: code => {
- expect(code).to.equal(422)
- expect(
- ctx.UserEmailsConfirmationHandler.promises.sendConfirmationCode
- ).not.toHaveBeenCalled()
- },
- }
- )
- })
- })
- describe('checkExistingEmailConfirmationCode', function () {
- beforeEach(function (ctx) {
- ctx.email = 'existing-email@example.com'
- ctx.req.session.pendingExistingEmail = {
- confirmCode: '123456',
- email: ctx.email,
- confirmCodeExpiresTimestamp: new Date(Math.max),
- }
- ctx.UserUpdater.promises.confirmEmail.mockResolvedValue(undefined)
- ctx.res = new MockResponse(vi)
- })
- describe('with a valid confirmation code', function () {
- beforeEach(function (ctx) {
- ctx.req.body = { code: '123456' }
- })
- it('confirms the email', async function (ctx) {
- const mockRes = new MockResponse(vi)
- await ctx.UserEmailsController.checkExistingEmailConfirmationCode(
- ctx.req,
- mockRes
- )
- expect(ctx.UserUpdater.promises.confirmEmail).toHaveBeenCalledWith(
- ctx.user._id,
- ctx.email,
- undefined
- )
- })
- it('adds audit log entry', async function (ctx) {
- await ctx.UserEmailsController.checkExistingEmailConfirmationCode(
- ctx.req,
- { json: vi.fn() }
- )
- expect(ctx.UserAuditLogHandler.promises.addEntry).toHaveBeenCalledWith(
- ctx.user._id,
- 'confirm-email-via-code',
- ctx.user._id,
- ctx.req.ip,
- { email: ctx.email }
- )
- })
- it('records analytics event', async function (ctx) {
- await ctx.UserEmailsController.checkExistingEmailConfirmationCode(
- ctx.req,
- { json: vi.fn() }
- )
- expect(ctx.AnalyticsManager.recordEventForSession).toHaveBeenCalledWith(
- ctx.req.session,
- 'email-verified',
- {
- provider: 'email',
- verification_type: 'token',
- isPrimary: ctx.user.email === ctx.email,
- }
- )
- })
- it('removes pendingExistingEmail from session', async function (ctx) {
- await ctx.UserEmailsController.checkExistingEmailConfirmationCode(
- ctx.req,
- { json: vi.fn() }
- )
- expect(ctx.req.session.pendingExistingEmail).to.be.undefined
- })
- })
- describe('with an invalid confirmation code', function () {
- beforeEach(function (ctx) {
- ctx.req.body = { code: '999999' }
- })
- it('does not confirm the email', async function (ctx) {
- expect.assertions(1)
- await ctx.UserEmailsController.checkExistingEmailConfirmationCode(
- ctx.req,
- {
- status: () => {
- expect(
- ctx.UserUpdater.promises.confirmEmail
- ).not.toHaveBeenCalled()
- return { json: ctx.next }
- },
- }
- )
- })
- it('responds with a 403', async function (ctx) {
- expect.assertions(1)
- await ctx.UserEmailsController.checkExistingEmailConfirmationCode(
- ctx.req,
- {
- status: code => {
- expect(code).to.equal(403)
- return { json: ctx.next }
- },
- }
- )
- })
- })
- describe('with an expired confirmation code', function () {
- beforeEach(function (ctx) {
- ctx.req.session.pendingExistingEmail.confirmCodeExpiresTimestamp =
- new Date(0)
- ctx.req.body = { code: '123456' }
- })
- it('responds with a 403', async function (ctx) {
- expect.assertions(1)
- await ctx.UserEmailsController.checkExistingEmailConfirmationCode(
- ctx.req,
- {
- status: code => {
- expect(code).to.equal(403)
- return { json: ctx.next }
- },
- }
- )
- })
- })
- })
- describe('resendExistingSecondaryEmailConfirmationCode', function () {
- beforeEach(function (ctx) {
- ctx.email = 'existing-email@example.com'
- ctx.req.session.pendingExistingEmail = {
- confirmCode: '123456',
- email: ctx.email,
- confirmCodeExpiresTimestamp: new Date(Math.max),
- }
- ctx.res.status = vi.fn().mockReturnValue({ json: vi.fn() })
- ctx.UserEmailsConfirmationHandler.promises.sendConfirmationCode = vi
- .fn()
- .mockResolvedValue({
- confirmCode: '654321',
- confirmCodeExpiresTimestamp: new Date(),
- })
- })
- it('should resend confirmation code', async function (ctx) {
- expect.assertions(2)
- await ctx.UserEmailsController.resendExistingSecondaryEmailConfirmationCode(
- ctx.req,
- {
- status: code => {
- expect(code).to.equal(200)
- expect(
- ctx.UserEmailsConfirmationHandler.promises.sendConfirmationCode
- ).toHaveBeenCalledWith(ctx.email, false)
- return { json: vi.fn() }
- },
- }
- )
- })
- it('should update session with new code', async function (ctx) {
- const newCode = '654321'
- const newExpiryTime = new Date()
- ctx.UserEmailsConfirmationHandler.promises.sendConfirmationCode.mockResolvedValue(
- {
- confirmCode: newCode,
- confirmCodeExpiresTimestamp: newExpiryTime,
- }
- )
- await ctx.UserEmailsController.resendExistingSecondaryEmailConfirmationCode(
- ctx.req,
- { status: () => ({ json: vi.fn() }) }
- )
- expect(ctx.req.session.pendingExistingEmail.confirmCode).to.equal(newCode)
- expect(
- ctx.req.session.pendingExistingEmail.confirmCodeExpiresTimestamp
- ).to.equal(newExpiryTime)
- })
- it('should add audit log entry', async function (ctx) {
- await ctx.UserEmailsController.resendExistingSecondaryEmailConfirmationCode(
- ctx.req,
- { status: () => ({ json: vi.fn() }) }
- )
- expect(ctx.UserAuditLogHandler.promises.addEntry).toHaveBeenCalledWith(
- ctx.user._id,
- 'resend-confirm-email-code',
- ctx.user._id,
- ctx.req.ip,
- { email: ctx.email }
- )
- })
- it('should handle rate limiting', async function (ctx) {
- expect.assertions(1)
- ctx.rateLimiter.consume.mockRejectedValue({ remainingPoints: 0 })
- await ctx.UserEmailsController.resendExistingSecondaryEmailConfirmationCode(
- ctx.req,
- {
- status: code => {
- expect(code).to.equal(429)
- return { json: vi.fn() }
- },
- }
- )
- })
- })
- })
|