|
| 1 | +import 'reflect-metadata'; |
| 2 | +import { RequestMethod } from '@nestjs/common'; |
| 3 | +import { METHOD_METADATA, PATH_METADATA } from '@nestjs/common/constants'; |
| 4 | +import { MemberVisibilityService } from '../settings/member-visibility.service'; |
| 5 | +import { AuditController } from './audit.controller'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Member visibility matrix of every GET route of AuditController: |
| 9 | + * ADMIN / EDITOR / VIEWER x open / restricted. Open workspaces and admins must |
| 10 | + * reach AuditService with exactly today's arguments; a restricted member gets |
| 11 | + * the same call plus their own user id. AuditService is mocked here; the |
| 12 | + * queries themselves are covered in audit.member-visibility.spec.ts and, on a |
| 13 | + * real database, in audit.member-visibility.integration.spec.ts. |
| 14 | + */ |
| 15 | +describe('AuditController member visibility', () => { |
| 16 | + const ORG = 'org-1'; |
| 17 | + const ME = 'user-me'; |
| 18 | + |
| 19 | + let audit: Record<string, jest.Mock>; |
| 20 | + let orgSettings: { get: jest.Mock }; |
| 21 | + let roles: { getAllowedToolIds: jest.Mock }; |
| 22 | + let controller: AuditController; |
| 23 | + |
| 24 | + const req = (role: string) => ({ user: { sub: ME, role , organizationId: ORG, email: '[email protected]' } }); |
| 25 | + |
| 26 | + /** |
| 27 | + * One entry per GET route: how to call it, the AuditService method it |
| 28 | + * reaches, and the arguments of the open path (today) and the restricted path. |
| 29 | + */ |
| 30 | + const ROUTES: Record< |
| 31 | + string, |
| 32 | + { |
| 33 | + path: string; |
| 34 | + service: string; |
| 35 | + call: (c: AuditController, role: string) => Promise<unknown>; |
| 36 | + open: unknown[]; |
| 37 | + restricted: unknown[]; |
| 38 | + } |
| 39 | + > = { |
| 40 | + listInvocations: { |
| 41 | + path: 'invocations', |
| 42 | + service: 'getRecentInvocations', |
| 43 | + call: (c, role) => c.listInvocations(req(role), '20', '40', 'tool-1', 'ERROR', 'crm', 'conn-1', 'srv-1'), |
| 44 | + open: [ |
| 45 | + 20, |
| 46 | + 40, |
| 47 | + { toolId: 'tool-1', status: 'ERROR', search: 'crm', connectorId: 'conn-1', mcpServerId: 'srv-1', organizationId: ORG }, |
| 48 | + ], |
| 49 | + restricted: [ |
| 50 | + 20, |
| 51 | + 40, |
| 52 | + { |
| 53 | + toolId: 'tool-1', |
| 54 | + status: 'ERROR', |
| 55 | + search: 'crm', |
| 56 | + connectorId: 'conn-1', |
| 57 | + mcpServerId: 'srv-1', |
| 58 | + organizationId: ORG, |
| 59 | + onlyUserId: ME, |
| 60 | + }, |
| 61 | + ], |
| 62 | + }, |
| 63 | + getStats: { |
| 64 | + path: 'stats', |
| 65 | + service: 'getStats', |
| 66 | + call: (c, role) => c.getStats(req(role)), |
| 67 | + open: [ORG], |
| 68 | + restricted: [ORG, ME], |
| 69 | + }, |
| 70 | + getAnalytics: { |
| 71 | + path: 'analytics', |
| 72 | + service: 'getAnalytics', |
| 73 | + call: (c, role) => c.getAnalytics(req(role), { days: 30 }), |
| 74 | + open: [ORG, 30], |
| 75 | + restricted: [ORG, 30, ME], |
| 76 | + }, |
| 77 | + getToolPerformance: { |
| 78 | + path: 'tool-performance', |
| 79 | + service: 'getToolPerformance', |
| 80 | + call: (c, role) => c.getToolPerformance(req(role), { hours: 48, connectorId: 'conn-1', mcpServerId: 'srv-1' }), |
| 81 | + open: [ORG, { hours: 48, connectorId: 'conn-1', mcpServerId: 'srv-1' }], |
| 82 | + restricted: [ORG, { hours: 48, connectorId: 'conn-1', mcpServerId: 'srv-1' }, ME], |
| 83 | + }, |
| 84 | + getBreakdowns: { |
| 85 | + path: 'breakdowns', |
| 86 | + service: 'getBreakdowns', |
| 87 | + call: (c, role) => c.getBreakdowns(req(role), '14'), |
| 88 | + open: [ORG, 14], |
| 89 | + restricted: [ORG, 14, ME], |
| 90 | + }, |
| 91 | + }; |
| 92 | + |
| 93 | + beforeEach(() => { |
| 94 | + audit = { |
| 95 | + getRecentInvocations: jest.fn().mockResolvedValue([]), |
| 96 | + getStats: jest.fn().mockResolvedValue({}), |
| 97 | + getAnalytics: jest.fn().mockResolvedValue({}), |
| 98 | + getToolPerformance: jest.fn().mockResolvedValue({ items: [], hasMore: false }), |
| 99 | + getBreakdowns: jest.fn().mockResolvedValue({}), |
| 100 | + }; |
| 101 | + orgSettings = { get: jest.fn().mockResolvedValue(null) }; |
| 102 | + roles = { getAllowedToolIds: jest.fn().mockResolvedValue(null) }; |
| 103 | + const visibility = new MemberVisibilityService(orgSettings as any, roles as any); |
| 104 | + controller = new AuditController(audit as any, visibility); |
| 105 | + }); |
| 106 | + |
| 107 | + it('declares a visibility rule for every GET route of the controller', () => { |
| 108 | + const proto = AuditController.prototype as unknown as Record<string, unknown>; |
| 109 | + const getRoutes = Object.getOwnPropertyNames(proto).filter( |
| 110 | + (name) => |
| 111 | + name !== 'constructor' && |
| 112 | + typeof proto[name] === 'function' && |
| 113 | + Reflect.getMetadata(METHOD_METADATA, proto[name] as object) === RequestMethod.GET, |
| 114 | + ); |
| 115 | + expect(getRoutes.sort()).toEqual(Object.keys(ROUTES).sort()); |
| 116 | + for (const name of getRoutes) { |
| 117 | + expect(Reflect.getMetadata(PATH_METADATA, proto[name] as object)).toBe(ROUTES[name].path); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + describe.each(Object.entries(ROUTES))('%s', (_name, route) => { |
| 122 | + const expectCall = (args: unknown[]) => { |
| 123 | + const mock = audit[route.service]; |
| 124 | + expect(mock).toHaveBeenCalledTimes(1); |
| 125 | + // Exact arity: the open path must not pass even an undefined extra argument. |
| 126 | + expect(mock.mock.calls[0]).toEqual(args); |
| 127 | + expect(mock.mock.calls[0]).toHaveLength(args.length); |
| 128 | + }; |
| 129 | + |
| 130 | + it.each(['open', 'restricted'])('ADMIN in a %s workspace: today’s call, no settings read', async (mode) => { |
| 131 | + orgSettings.get.mockResolvedValue(mode); |
| 132 | + await route.call(controller, 'ADMIN'); |
| 133 | + expectCall(route.open); |
| 134 | + expect(orgSettings.get).not.toHaveBeenCalled(); |
| 135 | + expect(roles.getAllowedToolIds).not.toHaveBeenCalled(); |
| 136 | + }); |
| 137 | + |
| 138 | + it.each(['EDITOR', 'VIEWER'])('%s in an open workspace: today’s call', async (role) => { |
| 139 | + orgSettings.get.mockResolvedValue('open'); |
| 140 | + await route.call(controller, role); |
| 141 | + expectCall(route.open); |
| 142 | + expect(roles.getAllowedToolIds).not.toHaveBeenCalled(); |
| 143 | + }); |
| 144 | + |
| 145 | + it.each(['EDITOR', 'VIEWER'])('%s in a workspace that never set the mode: today’s call', async (role) => { |
| 146 | + orgSettings.get.mockResolvedValue(null); |
| 147 | + await route.call(controller, role); |
| 148 | + expectCall(route.open); |
| 149 | + }); |
| 150 | + |
| 151 | + it.each(['EDITOR', 'VIEWER'])('%s in a restricted workspace: own calls only', async (role) => { |
| 152 | + orgSettings.get.mockResolvedValue('restricted'); |
| 153 | + await route.call(controller, role); |
| 154 | + expectCall(route.restricted); |
| 155 | + }); |
| 156 | + |
| 157 | + it.each([[['tool-1']], [[]]])( |
| 158 | + 'a restricted member with a tool whitelist (%j) still gets own calls only', |
| 159 | + async (allowed) => { |
| 160 | + orgSettings.get.mockResolvedValue('restricted'); |
| 161 | + roles.getAllowedToolIds.mockResolvedValue(allowed); |
| 162 | + await route.call(controller, 'VIEWER'); |
| 163 | + expectCall(route.restricted); |
| 164 | + }, |
| 165 | + ); |
| 166 | + |
| 167 | + it('fails closed when the role lookup throws', async () => { |
| 168 | + orgSettings.get.mockResolvedValue('restricted'); |
| 169 | + roles.getAllowedToolIds.mockRejectedValue(new Error('database down')); |
| 170 | + await expect(route.call(controller, 'VIEWER')).rejects.toThrow('database down'); |
| 171 | + expect(audit[route.service]).not.toHaveBeenCalled(); |
| 172 | + }); |
| 173 | + |
| 174 | + it('fails closed when the mode lookup throws for a member', async () => { |
| 175 | + orgSettings.get.mockRejectedValue(new Error('database down')); |
| 176 | + await expect(route.call(controller, 'EDITOR')).rejects.toThrow('database down'); |
| 177 | + expect(audit[route.service]).not.toHaveBeenCalled(); |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + it.each([ |
| 182 | + ['toolId', [undefined, undefined, 'tool-x']], |
| 183 | + ['status', [undefined, undefined, undefined, 'SUCCESS']], |
| 184 | + ['search', [undefined, undefined, undefined, undefined, 'export']], |
| 185 | + ['connectorId', [undefined, undefined, undefined, undefined, undefined, 'conn-x']], |
| 186 | + ['mcpServerId', [undefined, undefined, undefined, undefined, undefined, undefined, 'srv-x']], |
| 187 | + ] as const)('a restricted member filtering invocations by %s keeps the own-calls filter', async (_f, args) => { |
| 188 | + orgSettings.get.mockResolvedValue('restricted'); |
| 189 | + await (controller.listInvocations as any)(req('VIEWER'), ...args); |
| 190 | + const filters = audit.getRecentInvocations.mock.calls[0][2]; |
| 191 | + expect(filters).toMatchObject({ organizationId: ORG, onlyUserId: ME }); |
| 192 | + }); |
| 193 | +}); |
0 commit comments