|
| 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 { Test, TestingModule } from '@nestjs/testing'; |
| 6 | +import { ConfigService } from '@nestjs/config'; |
| 7 | +import { LOGGER_PROVIDER } from '@fxa/shared/log'; |
| 8 | +import { PasskeyConfig } from './passkey.config'; |
| 9 | +import { PasskeyConfigProvider, RawPasskeyConfig } from './passkey.provider'; |
| 10 | + |
| 11 | +const VALID_RAW_CONFIG: RawPasskeyConfig = { |
| 12 | + enabled: true, |
| 13 | + rpId: 'accounts.firefox.com', |
| 14 | + rpName: null, |
| 15 | + allowedOrigins: ['https://accounts.firefox.com'], |
| 16 | + challengeTimeout: 60000, |
| 17 | + maxPasskeysPerUser: 10, |
| 18 | + userVerification: 'required', |
| 19 | + residentKey: 'required', |
| 20 | + authenticatorAttachment: null, |
| 21 | +}; |
| 22 | + |
| 23 | +function buildModule(rawPasskeys: unknown) { |
| 24 | + const mockConfigService = { |
| 25 | + get: jest.fn().mockReturnValue(rawPasskeys), |
| 26 | + }; |
| 27 | + const mockLogger = { |
| 28 | + error: jest.fn(), |
| 29 | + warn: jest.fn(), |
| 30 | + log: jest.fn(), |
| 31 | + }; |
| 32 | + |
| 33 | + return Test.createTestingModule({ |
| 34 | + providers: [ |
| 35 | + PasskeyConfigProvider, |
| 36 | + { provide: ConfigService, useValue: mockConfigService }, |
| 37 | + { provide: LOGGER_PROVIDER, useValue: mockLogger }, |
| 38 | + ], |
| 39 | + }) |
| 40 | + .compile() |
| 41 | + .then((module: TestingModule) => ({ |
| 42 | + config: module.get(PasskeyConfig), |
| 43 | + logger: mockLogger, |
| 44 | + })); |
| 45 | +} |
| 46 | + |
| 47 | +describe('PasskeyConfigProvider', () => { |
| 48 | + describe('when passkeys.enabled is false', () => { |
| 49 | + it('returns { enabled: false } without validation', async () => { |
| 50 | + const { config } = await buildModule({ enabled: false }); |
| 51 | + expect(config.enabled).toBe(false); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('when config is valid', () => { |
| 56 | + it('returns a PasskeyConfig instance', async () => { |
| 57 | + const { config } = await buildModule(VALID_RAW_CONFIG); |
| 58 | + expect(config).toBeInstanceOf(PasskeyConfig); |
| 59 | + }); |
| 60 | + |
| 61 | + it('copies all fields correctly', async () => { |
| 62 | + const { config } = await buildModule(VALID_RAW_CONFIG); |
| 63 | + expect(config.enabled).toBe(true); |
| 64 | + expect(config.rpId).toBe('accounts.firefox.com'); |
| 65 | + expect(config.allowedOrigins).toEqual(['https://accounts.firefox.com']); |
| 66 | + expect(config.challengeTimeout).toBe(60000); |
| 67 | + expect(config.maxPasskeysPerUser).toBe(10); |
| 68 | + expect(config.userVerification).toBe('required'); |
| 69 | + expect(config.residentKey).toBe('required'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('maps rpName null to rpId', async () => { |
| 73 | + const { config } = await buildModule(VALID_RAW_CONFIG); |
| 74 | + expect(config.rpName).toBe('accounts.firefox.com'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('maps authenticatorAttachment null to undefined', async () => { |
| 78 | + const { config } = await buildModule(VALID_RAW_CONFIG); |
| 79 | + expect(config.authenticatorAttachment).toBeUndefined(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('does not log an error', async () => { |
| 83 | + const { logger } = await buildModule(VALID_RAW_CONFIG); |
| 84 | + expect(logger.error).not.toHaveBeenCalled(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('when config is invalid', () => { |
| 89 | + it('returns { enabled: false }', async () => { |
| 90 | + const { config } = await buildModule({ |
| 91 | + ...VALID_RAW_CONFIG, |
| 92 | + rpId: '', // fails @IsString() non-empty check |
| 93 | + allowedOrigins: ['not-a-valid-origin'], |
| 94 | + }); |
| 95 | + expect(config.enabled).toBe(false); |
| 96 | + }); |
| 97 | + |
| 98 | + it('logs an error with the validation message', async () => { |
| 99 | + const { logger } = await buildModule({ |
| 100 | + ...VALID_RAW_CONFIG, |
| 101 | + allowedOrigins: ['not-a-valid-origin'], |
| 102 | + }); |
| 103 | + expect(logger.error).toHaveBeenCalledWith( |
| 104 | + 'passkey.config.invalid', |
| 105 | + expect.objectContaining({ |
| 106 | + message: expect.stringContaining( |
| 107 | + 'Passkeys disabled due to malformed config' |
| 108 | + ), |
| 109 | + }) |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + it('rejects allowedOrigins with trailing path', async () => { |
| 114 | + const { config, logger } = await buildModule({ |
| 115 | + ...VALID_RAW_CONFIG, |
| 116 | + allowedOrigins: ['https://accounts.firefox.com/path'], |
| 117 | + }); |
| 118 | + expect(config.enabled).toBe(false); |
| 119 | + expect(logger.error).toHaveBeenCalled(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('rejects empty allowedOrigins array', async () => { |
| 123 | + const { config, logger } = await buildModule({ |
| 124 | + ...VALID_RAW_CONFIG, |
| 125 | + allowedOrigins: [], |
| 126 | + }); |
| 127 | + expect(config.enabled).toBe(false); |
| 128 | + expect(logger.error).toHaveBeenCalled(); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments