Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(session): pin the key scope on the sessions stats overview
Yudhi Armyndharisd30ee334ffc2
diff --git a/src/modules/session/session.controller.spec.ts b/src/modules/session/session.controller.spec.tsindex 1f4585f8f..cc498632d 100644--- a/src/modules/session/session.controller.spec.ts+++ b/src/modules/session/session.controller.spec.ts@@ -407,6 +407,49 @@ describe('SessionController findAll name filter', () => {});});+// GET /sessions/stats/overview names no :sessionId, so the guard has nothing to scope it against and the+// key's allowlist handed to the service is the whole fence: without it a key confined to one session reads+// the gateway-wide totals and status distribution.+describe('SessionController getStats scope', () => {+ const stats = {+ total: 1,+ active: 0,+ ready: 1,+ disconnected: 0,+ byStatus: { ready: 1 },+ memoryUsage: { heapUsed: 1, heapTotal: 2, rss: 3 },+ };+ let sessionService: { getStats: jest.Mock };+ let controller: SessionController;++ beforeEach(() => {+ sessionService = { getStats: jest.fn().mockResolvedValue(stats) };+ controller = new SessionControllerClass(+ sessionService as unknown as SessionService,+ { logInfo: jest.fn() } as unknown as AuditService,+ new ChatScopeService(),+ );+ });++ it("hands the key's allowedSessions to the service and returns its answer", async () => {+ const apiKey = { allowedSessions: ['sess-uuid-1', 'sess-uuid-2'] } as unknown as ApiKey;++ await expect(controller.getStats(apiKey)).resolves.toBe(stats);++ expect(sessionService.getStats).toHaveBeenCalledTimes(1);+ expect(sessionService.getStats).toHaveBeenCalledWith(['sess-uuid-1', 'sess-uuid-2']);+ });++ it('hands over no scope for a key without an allowlist, or for no key', async () => {+ await controller.getStats({ allowedSessions: null } as unknown as ApiKey);+ await controller.getStats(undefined);++ // null, undefined and [] all mean "every session" to the service; any list would narrow the totals.+ const scopes = sessionService.getStats.mock.calls.map((call: unknown[]) => call[0] ?? []);+ expect(scopes).toEqual([[], []]);+ });+});+// The pin route forwards a boolean the engine can refuse. The value worth pinning is that the// engine's `false` reaches the caller: WhatsApp caps pinned chats at three, and a controller that// hard-coded `{ success: true }` would report a refused pin as done.diff --git a/test/global-routes-session-scope.e2e-spec.ts b/test/global-routes-session-scope.e2e-spec.tsindex 2c0a80f3c..d5a4ebfca 100644--- a/test/global-routes-session-scope.e2e-spec.ts+++ b/test/global-routes-session-scope.e2e-spec.ts@@ -103,6 +103,23 @@ describe('Global read and create routes reject session-scoped keys (e2e)', () =>expect(res.status).toBe(200);});+ it('counts only its own sessions for a scoped key on GET /api/sessions/stats/overview', async () => {+ // The route names no :sessionId, so the guard cannot scope it: the key's allowlist handed to the+ // service is the only thing keeping the gateway-wide totals from a scoped key. A session outside the+ // allowlist makes the two answers differ; the control proves it is really counted.+ const sessionRepo: Repository<Session> = app.get(getRepositoryToken(Session, 'data'));+ await sessionRepo.save(sessionRepo.create({ name: `e2e-global-scope-stats-${Date.now()}` }));+ const overview = (key: string) =>+ request(app.getHttpServer())+ .get('/api/sessions/stats/overview')+ .set('X-API-Key', key)+ .expect(200)+ .then(res => res.body as { total: number; byStatus: Record<string, number> });++ expect(await overview(scopedOperatorKey)).toMatchObject({ total: 1, byStatus: { created: 1 } });+ expect((await overview(adminKey)).total).toBeGreaterThanOrEqual(2);+ });+it('denies a session-scoped key per-session stats for a session outside its scope', async () => {// The param-scope check refuses a live key, so it answers 403 like the fence above, and the audit// row names the key that was refused.