Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(stats): assert the unscoped-key fence on global stats routes
Yudhi Armyndharis85c19cdd0144
diff --git a/src/modules/stats/stats.controller.spec.ts b/src/modules/stats/stats.controller.spec.tsindex 3bacc9a1e..7ed99ad7b 100644--- a/src/modules/stats/stats.controller.spec.ts+++ b/src/modules/stats/stats.controller.spec.ts@@ -1,12 +1,13 @@import { Reflector } from '@nestjs/core';import { StatsController } from './stats.controller';-import { REQUIRED_ROLE_KEY } from '../auth/decorators/auth.decorators';+import { REQUIRED_ROLE_KEY, UNSCOPED_KEY } from '../auth/decorators/auth.decorators';import { ApiKeyRole } from '../auth/entities/api-key.entity';// the global stats routes aggregate across EVERY session and carry no scope param, so the-// ApiKeyGuard's allowedSessions fence doesn't apply. They must require ADMIN so a VIEWER / a-// session-restricted key can't read cross-tenant activity. The per-session route is left ungated:-// it carries :sessionId, so the guard already scopes a restricted key to its own sessions.+// ApiKeyGuard's allowedSessions fence doesn't apply. They require ADMIN to keep VIEWER/OPERATOR keys+// out, and @RequireUnscopedKey to keep a session-restricted key out: role and session scope are+// independent, so ADMIN alone does not do that. The per-session route is left ungated: it carries+// :sessionId, so the guard already scopes a restricted key to its own sessions.describe('StatsController access control', () => {const reflector = new Reflector();// Opaque-object view of the prototype so the lint unbound-method rule doesn't fire on a@@ -18,6 +19,10 @@ describe('StatsController access control', () => {expect(role).toBe(ApiKeyRole.ADMIN);});+ it.each(['getOverview', 'getMessageStats'] as const)('global stats route %s requires an unscoped key', method => {+ expect(reflector.get<boolean | undefined>(UNSCOPED_KEY, proto[method])).toBe(true);+ });+it('per-session stats is not globally ADMIN-gated (scope-enforced by its :sessionId param)', () => {const role = reflector.get<ApiKeyRole | undefined>(REQUIRED_ROLE_KEY, proto.getSessionStats);expect(role).toBeUndefined();