|
| 1 | +import { Reflector } from '@nestjs/core'; |
| 2 | +import type { BrowserCredentialStorageService } from './browser-credential-storage.service'; |
| 3 | +import { BrowserAuthTotpController } from './browser-auth-totp.controller'; |
| 4 | + |
| 5 | +// The controller imports the auth guards, which pull in the real Prisma client |
| 6 | +// and better-auth (ESM) at module load. Stub those so importing the controller |
| 7 | +// doesn't open a DB connection or fail to parse — the guards aren't executed here |
| 8 | +// (we test the controller methods + their RBAC metadata directly). |
| 9 | +jest.mock('@db', () => ({ |
| 10 | + db: {}, |
| 11 | + // The DTO module (transitively imported) decorates fields with |
| 12 | + // @IsEnum(TaskFrequency), so the enum must exist at module-eval time. |
| 13 | + TaskFrequency: { |
| 14 | + daily: 'daily', |
| 15 | + weekly: 'weekly', |
| 16 | + monthly: 'monthly', |
| 17 | + quarterly: 'quarterly', |
| 18 | + yearly: 'yearly', |
| 19 | + }, |
| 20 | +})); |
| 21 | +jest.mock('../auth/auth.server', () => ({ |
| 22 | + auth: { api: { getSession: jest.fn() } }, |
| 23 | +})); |
| 24 | +jest.mock('@trycompai/auth', () => ({ |
| 25 | + statement: {}, |
| 26 | + BUILT_IN_ROLE_PERMISSIONS: {}, |
| 27 | +})); |
| 28 | + |
| 29 | +// Mirrors PERMISSIONS_KEY in ../auth/permission.guard — imported by value rather |
| 30 | +// than by reference so the test doesn't pull the guard's better-auth (ESM) chain |
| 31 | +// into Jest. Keep in sync with the guard. |
| 32 | +const PERMISSIONS_KEY = 'required_permissions'; |
| 33 | + |
| 34 | +describe('BrowserAuthTotpController', () => { |
| 35 | + const service = { |
| 36 | + getOrgTotpStatuses: jest.fn(), |
| 37 | + getProfileTotpStatus: jest.fn(), |
| 38 | + setProfileTotp: jest.fn(), |
| 39 | + clearProfileTotp: jest.fn(), |
| 40 | + }; |
| 41 | + let controller: BrowserAuthTotpController; |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + jest.clearAllMocks(); |
| 45 | + controller = new BrowserAuthTotpController( |
| 46 | + service as unknown as BrowserCredentialStorageService, |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('delegation (organization + profile scoping reaches storage)', () => { |
| 51 | + it('getOrgTotpStatuses passes the org id and wraps the map', async () => { |
| 52 | + service.getOrgTotpStatuses.mockResolvedValue({ bap_1: true }); |
| 53 | + |
| 54 | + const res = await controller.getOrgTotpStatuses('org_1'); |
| 55 | + |
| 56 | + expect(service.getOrgTotpStatuses).toHaveBeenCalledWith('org_1'); |
| 57 | + expect(res).toEqual({ statuses: { bap_1: true } }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('getProfileTotp passes the org + profile ids', async () => { |
| 61 | + service.getProfileTotpStatus.mockResolvedValue({ configured: true }); |
| 62 | + |
| 63 | + const res = await controller.getProfileTotp('org_1', 'bap_1'); |
| 64 | + |
| 65 | + expect(service.getProfileTotpStatus).toHaveBeenCalledWith({ |
| 66 | + organizationId: 'org_1', |
| 67 | + profileId: 'bap_1', |
| 68 | + }); |
| 69 | + expect(res).toEqual({ configured: true }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('setProfileTotp forwards the seed with org + profile ids', async () => { |
| 73 | + service.setProfileTotp.mockResolvedValue({ configured: true }); |
| 74 | + |
| 75 | + await controller.setProfileTotp('org_1', 'bap_1', { |
| 76 | + totpSeed: 'JBSWY3DPEHPK3PXP', |
| 77 | + }); |
| 78 | + |
| 79 | + expect(service.setProfileTotp).toHaveBeenCalledWith({ |
| 80 | + organizationId: 'org_1', |
| 81 | + profileId: 'bap_1', |
| 82 | + totpSeed: 'JBSWY3DPEHPK3PXP', |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('clearProfileTotp passes the org + profile ids', async () => { |
| 87 | + service.clearProfileTotp.mockResolvedValue({ configured: false }); |
| 88 | + |
| 89 | + await controller.clearProfileTotp('org_1', 'bap_1'); |
| 90 | + |
| 91 | + expect(service.clearProfileTotp).toHaveBeenCalledWith({ |
| 92 | + organizationId: 'org_1', |
| 93 | + profileId: 'bap_1', |
| 94 | + }); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('RBAC — reads require integration:read, writes require integration:update', () => { |
| 99 | + const reflector = new Reflector(); |
| 100 | + const permsOf = (method: object) => |
| 101 | + reflector.get(PERMISSIONS_KEY, method as never); |
| 102 | + |
| 103 | + it('gates read routes with integration:read', () => { |
| 104 | + expect(permsOf(BrowserAuthTotpController.prototype.getOrgTotpStatuses)).toEqual( |
| 105 | + [{ resource: 'integration', actions: ['read'] }], |
| 106 | + ); |
| 107 | + expect(permsOf(BrowserAuthTotpController.prototype.getProfileTotp)).toEqual([ |
| 108 | + { resource: 'integration', actions: ['read'] }, |
| 109 | + ]); |
| 110 | + }); |
| 111 | + |
| 112 | + it('gates write routes with integration:update', () => { |
| 113 | + expect(permsOf(BrowserAuthTotpController.prototype.setProfileTotp)).toEqual([ |
| 114 | + { resource: 'integration', actions: ['update'] }, |
| 115 | + ]); |
| 116 | + expect(permsOf(BrowserAuthTotpController.prototype.clearProfileTotp)).toEqual( |
| 117 | + [{ resource: 'integration', actions: ['update'] }], |
| 118 | + ); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments