From 2615a71474e46faafecf1473ee7a5ec176fc9e3c Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Wed, 6 Nov 2024 14:30:45 +0100 Subject: [PATCH] test: add tests for project resources data (#8675) Adds a test that checks all the values in the project status' resources object. --- .../projects-status.e2e.test.ts | 83 ++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/src/lib/features/project-status/projects-status.e2e.test.ts b/src/lib/features/project-status/projects-status.e2e.test.ts index f19171d2f209..857abeb49aec 100644 --- a/src/lib/features/project-status/projects-status.e2e.test.ts +++ b/src/lib/features/project-status/projects-status.e2e.test.ts @@ -4,11 +4,17 @@ import { setupAppWithCustomConfig, } from '../../../test/e2e/helpers/test-helper'; import getLogger from '../../../test/fixtures/no-logger'; -import { FEATURE_CREATED, type IUnleashConfig } from '../../types'; +import { + FEATURE_CREATED, + RoleName, + type IAuditUser, + type IUnleashConfig, +} from '../../types'; import type { EventService } from '../../services'; import { createEventsService } from '../events/createEventsService'; import { createTestConfig } from '../../../test/config/test-config'; import { randomId } from '../../util'; +import { ApiTokenType } from '../../types/models/api-token'; let app: IUnleashTest; let db: ITestDb; @@ -47,6 +53,10 @@ afterAll(async () => { await db.destroy(); }); +beforeEach(async () => { + await db.stores.clientMetricsStoreV2.deleteAll(); +}); + test('project insights should return correct count for each day', async () => { await eventService.storeEvent({ type: FEATURE_CREATED, @@ -145,3 +155,74 @@ test('project status should return environments with connected SDKs', async () = expect(body.resources.connectedEnvironments).toBe(1); }); + +test('project resources should contain the right data', async () => { + const { body: noResourcesBody } = await app.request + .get('/api/admin/projects/default/status') + .expect('Content-Type', /json/) + .expect(200); + + expect(noResourcesBody.resources).toMatchObject({ + members: 0, + apiTokens: 0, + segments: 0, + connectedEnvironments: 0, + }); + + const flagName = randomId(); + await app.createFeature(flagName); + + const environment = 'default'; + await db.stores.clientMetricsStoreV2.batchInsertMetrics([ + { + featureName: flagName, + appName: `web2`, + environment, + timestamp: new Date(), + yes: 5, + no: 2, + }, + ]); + + await app.services.apiTokenService.createApiTokenWithProjects({ + tokenName: 'test-token', + projects: ['default'], + type: ApiTokenType.CLIENT, + environment: 'default', + }); + + await app.services.segmentService.create( + { + name: 'test-segment', + project: 'default', + constraints: [], + }, + {} as IAuditUser, + ); + + const admin = await app.services.userService.createUser({ + username: 'admin', + rootRole: RoleName.ADMIN, + }); + const user = await app.services.userService.createUser({ + username: 'test-user', + rootRole: RoleName.EDITOR, + }); + + await app.services.projectService.addAccess('default', [4], [], [user.id], { + ...admin, + ip: '', + } as IAuditUser); + + const { body } = await app.request + .get('/api/admin/projects/default/status') + .expect('Content-Type', /json/) + .expect(200); + + expect(body.resources).toMatchObject({ + members: 1, + apiTokens: 1, + segments: 1, + connectedEnvironments: 1, + }); +});