|
| 1 | +import UserPreferences from '../methods/userPreferences'; |
| 2 | +import log from '../methods/helpers/log'; |
| 3 | +import { BIOMETRIC_TRUST_MIGRATION_V1_DONE, BIOMETRY_ENABLED_KEY } from '../constants/localAuthentication'; |
| 4 | +import { biometricTrustStore } from './index'; |
| 5 | +import { runBiometricTrustMigration } from './migration'; |
| 6 | + |
| 7 | +jest.mock('../methods/userPreferences', () => ({ |
| 8 | + __esModule: true, |
| 9 | + default: { |
| 10 | + getBool: jest.fn(), |
| 11 | + setBool: jest.fn(), |
| 12 | + getString: jest.fn(), |
| 13 | + setString: jest.fn() |
| 14 | + } |
| 15 | +})); |
| 16 | + |
| 17 | +jest.mock('../methods/helpers/log', () => ({ __esModule: true, default: jest.fn() })); |
| 18 | + |
| 19 | +jest.mock('./index', () => ({ |
| 20 | + biometricTrustStore: { |
| 21 | + enrol: jest.fn(), |
| 22 | + disenrol: jest.fn(), |
| 23 | + verify: jest.fn(), |
| 24 | + probeExists: jest.fn() |
| 25 | + } |
| 26 | +})); |
| 27 | + |
| 28 | +const mockedGetBool = UserPreferences.getBool as jest.Mock; |
| 29 | +const mockedSetBool = UserPreferences.setBool as jest.Mock; |
| 30 | +const mockedEnrol = biometricTrustStore.enrol as jest.Mock; |
| 31 | +const mockedProbeExists = biometricTrustStore.probeExists as jest.Mock; |
| 32 | +const mockedLog = log as unknown as jest.Mock; |
| 33 | + |
| 34 | +// Drives the mocked UserPreferences with the values the migration needs to see for the |
| 35 | +// branch under test, so each test reads like a state machine input row. |
| 36 | +const setPrefs = ({ biometryEnabled, migrated }: { biometryEnabled: boolean; migrated: boolean }) => { |
| 37 | + mockedGetBool.mockImplementation((key: string) => { |
| 38 | + if (key === BIOMETRY_ENABLED_KEY) return biometryEnabled; |
| 39 | + if (key === BIOMETRIC_TRUST_MIGRATION_V1_DONE) return migrated; |
| 40 | + return undefined; |
| 41 | + }); |
| 42 | +}; |
| 43 | + |
| 44 | +describe('runBiometricTrustMigration', () => { |
| 45 | + beforeEach(() => { |
| 46 | + jest.clearAllMocks(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('upgrade path: !migrated && flag && !sentinel → enrol() once and mark migrated', async () => { |
| 50 | + setPrefs({ biometryEnabled: true, migrated: false }); |
| 51 | + mockedProbeExists.mockResolvedValueOnce(false); |
| 52 | + mockedEnrol.mockResolvedValueOnce({ kind: 'success' }); |
| 53 | + |
| 54 | + await runBiometricTrustMigration(); |
| 55 | + |
| 56 | + expect(mockedEnrol).toHaveBeenCalledTimes(1); |
| 57 | + expect(mockedSetBool).toHaveBeenCalledWith(BIOMETRIC_TRUST_MIGRATION_V1_DONE, true); |
| 58 | + expect(mockedSetBool).not.toHaveBeenCalledWith(BIOMETRY_ENABLED_KEY, false); |
| 59 | + }); |
| 60 | + |
| 61 | + it('reconciliation path: migrated && flag && !sentinel → clear flag, no enrol()', async () => { |
| 62 | + setPrefs({ biometryEnabled: true, migrated: true }); |
| 63 | + mockedProbeExists.mockResolvedValueOnce(false); |
| 64 | + |
| 65 | + await runBiometricTrustMigration(); |
| 66 | + |
| 67 | + expect(mockedEnrol).not.toHaveBeenCalled(); |
| 68 | + expect(mockedSetBool).toHaveBeenCalledWith(BIOMETRY_ENABLED_KEY, false); |
| 69 | + expect(mockedSetBool).not.toHaveBeenCalledWith(BIOMETRIC_TRUST_MIGRATION_V1_DONE, expect.anything()); |
| 70 | + }); |
| 71 | + |
| 72 | + it('flag=false → no-op (no probe, no enrol, no setBool)', async () => { |
| 73 | + setPrefs({ biometryEnabled: false, migrated: false }); |
| 74 | + |
| 75 | + await runBiometricTrustMigration(); |
| 76 | + |
| 77 | + expect(mockedProbeExists).not.toHaveBeenCalled(); |
| 78 | + expect(mockedEnrol).not.toHaveBeenCalled(); |
| 79 | + expect(mockedSetBool).not.toHaveBeenCalled(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('flag=true && sentinel exists → no-op (no enrol, no flag clear)', async () => { |
| 83 | + setPrefs({ biometryEnabled: true, migrated: false }); |
| 84 | + mockedProbeExists.mockResolvedValueOnce(true); |
| 85 | + |
| 86 | + await runBiometricTrustMigration(); |
| 87 | + |
| 88 | + expect(mockedEnrol).not.toHaveBeenCalled(); |
| 89 | + expect(mockedSetBool).not.toHaveBeenCalled(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('idempotent: after successful migration, second run is a no-op', async () => { |
| 93 | + // first run: upgrade path |
| 94 | + setPrefs({ biometryEnabled: true, migrated: false }); |
| 95 | + mockedProbeExists.mockResolvedValueOnce(false); |
| 96 | + mockedEnrol.mockResolvedValueOnce({ kind: 'success' }); |
| 97 | + await runBiometricTrustMigration(); |
| 98 | + expect(mockedEnrol).toHaveBeenCalledTimes(1); |
| 99 | + |
| 100 | + // second run: sentinel now exists AND marker is set |
| 101 | + jest.clearAllMocks(); |
| 102 | + setPrefs({ biometryEnabled: true, migrated: true }); |
| 103 | + mockedProbeExists.mockResolvedValueOnce(true); |
| 104 | + |
| 105 | + await runBiometricTrustMigration(); |
| 106 | + |
| 107 | + expect(mockedEnrol).not.toHaveBeenCalled(); |
| 108 | + expect(mockedSetBool).not.toHaveBeenCalled(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('enrol() error → logged, flag untouched, marker NOT set so next boot retries', async () => { |
| 112 | + setPrefs({ biometryEnabled: true, migrated: false }); |
| 113 | + mockedProbeExists.mockResolvedValueOnce(false); |
| 114 | + const cause = new Error('keychain unavailable'); |
| 115 | + mockedEnrol.mockResolvedValueOnce({ kind: 'error', cause }); |
| 116 | + |
| 117 | + await runBiometricTrustMigration(); |
| 118 | + |
| 119 | + expect(mockedLog).toHaveBeenCalledWith(cause); |
| 120 | + expect(mockedSetBool).not.toHaveBeenCalledWith(BIOMETRIC_TRUST_MIGRATION_V1_DONE, expect.anything()); |
| 121 | + expect(mockedSetBool).not.toHaveBeenCalledWith(BIOMETRY_ENABLED_KEY, false); |
| 122 | + }); |
| 123 | + |
| 124 | + it('probeExists throws → swallowed, logged, no enrol(), no flag mutation', async () => { |
| 125 | + setPrefs({ biometryEnabled: true, migrated: false }); |
| 126 | + const boom = new Error('probe failed'); |
| 127 | + mockedProbeExists.mockRejectedValueOnce(boom); |
| 128 | + |
| 129 | + await runBiometricTrustMigration(); |
| 130 | + |
| 131 | + expect(mockedLog).toHaveBeenCalledWith(boom); |
| 132 | + expect(mockedEnrol).not.toHaveBeenCalled(); |
| 133 | + expect(mockedSetBool).not.toHaveBeenCalled(); |
| 134 | + }); |
| 135 | +}); |
0 commit comments