|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import sinon from 'sinon'; |
| 6 | +import { AppError as error } from '@fxa/accounts/errors'; |
| 7 | +import * as authMethods from './authMethods'; |
| 8 | + |
| 9 | +const MOCK_ACCOUNT = { |
| 10 | + uid: 'abcdef123456', |
| 11 | +}; |
| 12 | + |
| 13 | +function mockDB() { |
| 14 | + return { |
| 15 | + totpToken: sinon.stub(), |
| 16 | + // Add other DB methods as needed |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +describe('availableAuthenticationMethods', () => { |
| 21 | + let mockDbInstance: ReturnType<typeof mockDB>; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + mockDbInstance = mockDB(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('returns [`pwd`,`email`] for non-TOTP-enabled accounts', async () => { |
| 28 | + mockDbInstance.totpToken = sinon.stub().rejects(error.totpTokenNotFound()); |
| 29 | + const amr = await authMethods.availableAuthenticationMethods( |
| 30 | + mockDbInstance as any, |
| 31 | + MOCK_ACCOUNT as any |
| 32 | + ); |
| 33 | + expect(mockDbInstance.totpToken.calledWithExactly(MOCK_ACCOUNT.uid)).toBe(true); |
| 34 | + expect(Array.from(amr).sort()).toEqual(['email', 'pwd']); |
| 35 | + }); |
| 36 | + |
| 37 | + it('returns [`pwd`,`email`,`otp`] for TOTP-enabled accounts', async () => { |
| 38 | + mockDbInstance.totpToken = sinon.stub().resolves({ |
| 39 | + verified: true, |
| 40 | + enabled: true, |
| 41 | + sharedSecret: 'secret!', |
| 42 | + }); |
| 43 | + const amr = await authMethods.availableAuthenticationMethods( |
| 44 | + mockDbInstance as any, |
| 45 | + MOCK_ACCOUNT as any |
| 46 | + ); |
| 47 | + expect(mockDbInstance.totpToken.calledWithExactly(MOCK_ACCOUNT.uid)).toBe(true); |
| 48 | + expect(Array.from(amr).sort()).toEqual(['email', 'otp', 'pwd']); |
| 49 | + }); |
| 50 | + |
| 51 | + it('returns [`pwd`,`email`] when TOTP token is not yet enabled', async () => { |
| 52 | + mockDbInstance.totpToken = sinon.stub().resolves({ |
| 53 | + verified: true, |
| 54 | + enabled: false, |
| 55 | + sharedSecret: 'secret!', |
| 56 | + }); |
| 57 | + const amr = await authMethods.availableAuthenticationMethods( |
| 58 | + mockDbInstance as any, |
| 59 | + MOCK_ACCOUNT as any |
| 60 | + ); |
| 61 | + expect(mockDbInstance.totpToken.calledWithExactly(MOCK_ACCOUNT.uid)).toBe(true); |
| 62 | + expect(Array.from(amr).sort()).toEqual(['email', 'pwd']); |
| 63 | + }); |
| 64 | + |
| 65 | + it('rethrows unexpected DB errors', async () => { |
| 66 | + mockDbInstance.totpToken = sinon.stub().rejects(error.serviceUnavailable()); |
| 67 | + try { |
| 68 | + await authMethods.availableAuthenticationMethods( |
| 69 | + mockDbInstance as any, |
| 70 | + MOCK_ACCOUNT as any |
| 71 | + ); |
| 72 | + throw new Error('error should have been re-thrown'); |
| 73 | + } catch (err: any) { |
| 74 | + expect(mockDbInstance.totpToken.calledWithExactly(MOCK_ACCOUNT.uid)).toBe(true); |
| 75 | + expect(err.errno).toBe(error.ERRNO.SERVER_BUSY); |
| 76 | + } |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | +describe('verificationMethodToAMR', () => { |
| 81 | + it('maps `email` to `email`', () => { |
| 82 | + expect(authMethods.verificationMethodToAMR('email')).toBe('email'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('maps `email-captcha` to `email`', () => { |
| 86 | + expect(authMethods.verificationMethodToAMR('email-captcha')).toBe('email'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('maps `email-2fa` to `email`', () => { |
| 90 | + expect(authMethods.verificationMethodToAMR('email-2fa')).toBe('email'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('maps `totp-2fa` to `otp`', () => { |
| 94 | + expect(authMethods.verificationMethodToAMR('totp-2fa')).toBe('otp'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('maps `recovery-code` to `otp`', () => { |
| 98 | + expect(authMethods.verificationMethodToAMR('recovery-code')).toBe('otp'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('throws when given an unknown verification method', () => { |
| 102 | + expect(() => { |
| 103 | + authMethods.verificationMethodToAMR('email-gotcha' as any); |
| 104 | + }).toThrow(/unknown verificationMethod/); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe('maximumAssuranceLevel', () => { |
| 109 | + it('returns 0 when no authentication methods are used', () => { |
| 110 | + expect(authMethods.maximumAssuranceLevel([])).toBe(0); |
| 111 | + expect(authMethods.maximumAssuranceLevel(new Set())).toBe(0); |
| 112 | + }); |
| 113 | + |
| 114 | + it('returns 1 when only `pwd` auth is used', () => { |
| 115 | + expect(authMethods.maximumAssuranceLevel(['pwd'])).toBe(1); |
| 116 | + }); |
| 117 | + |
| 118 | + it('returns 1 when only `email` auth is used', () => { |
| 119 | + expect(authMethods.maximumAssuranceLevel(['email'])).toBe(1); |
| 120 | + }); |
| 121 | + |
| 122 | + it('returns 1 when only `otp` auth is used', () => { |
| 123 | + expect(authMethods.maximumAssuranceLevel(['otp'])).toBe(1); |
| 124 | + }); |
| 125 | + |
| 126 | + it('returns 1 when only things-you-know auth mechanisms are used', () => { |
| 127 | + expect(authMethods.maximumAssuranceLevel(['email', 'pwd'])).toBe(1); |
| 128 | + }); |
| 129 | + |
| 130 | + it('returns 2 when both `pwd` and `otp` methods are used', () => { |
| 131 | + expect(authMethods.maximumAssuranceLevel(['pwd', 'otp'])).toBe(2); |
| 132 | + }); |
| 133 | +}); |
0 commit comments