|
| 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 * as Sentry from '@sentry/node'; |
| 6 | +import { OAuthDb } from './registeredClients'; |
| 7 | +import { getRegisteredClientIds, reset } from './registeredClients'; |
| 8 | + |
| 9 | +jest.mock('@sentry/node', () => ({ |
| 10 | + captureException: jest.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +/** |
| 14 | + * Creates a mock OAuthDb whose `getAllClientIds` resolves with the given |
| 15 | + * hex-encoded client ID strings converted to Buffer rows. |
| 16 | + */ |
| 17 | +function makeDb(ids: string[]): OAuthDb { |
| 18 | + return { |
| 19 | + mysql: { |
| 20 | + getAllClientIds: jest |
| 21 | + .fn() |
| 22 | + .mockResolvedValue(ids.map((id) => ({ id: Buffer.from(id, 'hex') }))), |
| 23 | + }, |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +describe('getRegisteredClientIds', () => { |
| 28 | + beforeEach(() => { |
| 29 | + jest.useFakeTimers(); |
| 30 | + jest.clearAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + reset(); |
| 35 | + jest.useRealTimers(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('fetches from DB on first call and returns client IDs', async () => { |
| 39 | + const db = makeDb(['aabbccdd11223344', 'deadbeefdeadbeef']); |
| 40 | + |
| 41 | + const result = await getRegisteredClientIds(db); |
| 42 | + |
| 43 | + expect(db.mysql.getAllClientIds).toHaveBeenCalledTimes(1); |
| 44 | + expect(result).toEqual(new Set(['aabbccdd11223344', 'deadbeefdeadbeef'])); |
| 45 | + }); |
| 46 | + |
| 47 | + it('returns cached IDs on second call within 60 seconds', async () => { |
| 48 | + const db = makeDb(['aabbccdd11223344']); |
| 49 | + |
| 50 | + await getRegisteredClientIds(db); |
| 51 | + jest.advanceTimersByTime(30_000); |
| 52 | + const result = await getRegisteredClientIds(db); |
| 53 | + |
| 54 | + expect(db.mysql.getAllClientIds).toHaveBeenCalledTimes(1); |
| 55 | + expect(result).toEqual(new Set(['aabbccdd11223344'])); |
| 56 | + }); |
| 57 | + |
| 58 | + it('refetches from DB after 60 seconds', async () => { |
| 59 | + const db = makeDb(['aabbccdd11223344']); |
| 60 | + |
| 61 | + await getRegisteredClientIds(db); |
| 62 | + jest.advanceTimersByTime(61_000); |
| 63 | + await getRegisteredClientIds(db); |
| 64 | + |
| 65 | + expect(db.mysql.getAllClientIds).toHaveBeenCalledTimes(2); |
| 66 | + }); |
| 67 | + |
| 68 | + it('returns empty Set and captures to Sentry when DB fails on first call', async () => { |
| 69 | + const error = new Error('DB unavailable'); |
| 70 | + const db: OAuthDb = { |
| 71 | + mysql: { getAllClientIds: jest.fn().mockRejectedValue(error) }, |
| 72 | + }; |
| 73 | + |
| 74 | + const result = await getRegisteredClientIds(db); |
| 75 | + |
| 76 | + expect(result).toEqual(new Set()); |
| 77 | + expect(Sentry.captureException).toHaveBeenCalledWith(error); |
| 78 | + }); |
| 79 | + |
| 80 | + it('returns last successful Set and captures to Sentry when DB fails on subsequent call', async () => { |
| 81 | + const db = makeDb(['aabbccdd11223344']); |
| 82 | + |
| 83 | + // Successful first fetch |
| 84 | + await getRegisteredClientIds(db); |
| 85 | + |
| 86 | + // DB fails on refetch |
| 87 | + jest.advanceTimersByTime(61_000); |
| 88 | + const error = new Error('DB unavailable'); |
| 89 | + (db.mysql.getAllClientIds as jest.Mock).mockRejectedValueOnce(error); |
| 90 | + const result = await getRegisteredClientIds(db); |
| 91 | + |
| 92 | + expect(result).toEqual(new Set(['aabbccdd11223344'])); |
| 93 | + expect(Sentry.captureException).toHaveBeenCalledWith(error); |
| 94 | + }); |
| 95 | + |
| 96 | + it('returns an empty Set when DB returns no clients', async () => { |
| 97 | + const db = makeDb([]); |
| 98 | + |
| 99 | + const result = await getRegisteredClientIds(db); |
| 100 | + |
| 101 | + expect(result).toEqual(new Set()); |
| 102 | + }); |
| 103 | + |
| 104 | + it('re-fetches exactly at the 60-second boundary', async () => { |
| 105 | + const db = makeDb(['aabbccdd11223344']); |
| 106 | + |
| 107 | + await getRegisteredClientIds(db); |
| 108 | + jest.advanceTimersByTime(60_001); |
| 109 | + await getRegisteredClientIds(db); |
| 110 | + |
| 111 | + expect(db.mysql.getAllClientIds).toHaveBeenCalledTimes(2); |
| 112 | + }); |
| 113 | +}); |
0 commit comments