From c97bcc65e636ca2bdd5d650b96307ceeabc8f1e2 Mon Sep 17 00:00:00 2001 From: Fredrik Strand Oseberg Date: Tue, 10 Oct 2023 10:43:45 +0200 Subject: [PATCH] Refactor/project overview last seen at test (#4979) Increase test coverage of last seen in project overview --- .../api/admin/project/projects.e2e.test.ts | 139 +++++++++++++++++- 1 file changed, 132 insertions(+), 7 deletions(-) diff --git a/src/test/e2e/api/admin/project/projects.e2e.test.ts b/src/test/e2e/api/admin/project/projects.e2e.test.ts index e77f05a13514..012496a4f736 100644 --- a/src/test/e2e/api/admin/project/projects.e2e.test.ts +++ b/src/test/e2e/api/admin/project/projects.e2e.test.ts @@ -1,12 +1,38 @@ -import dbInit from '../../../helpers/database-init'; -import { setupAppWithCustomConfig } from '../../../helpers/test-helper'; +import dbInit, { ITestDb } from '../../../helpers/database-init'; +import { + IUnleashTest, + setupAppWithCustomConfig, +} from '../../../helpers/test-helper'; import getLogger from '../../../../fixtures/no-logger'; -import ProjectStore from '../../../../../lib/db/project-store'; -let app; -let db; +import { IProjectStore } from 'lib/types'; +import { ProjectService } from 'lib/services'; -let projectStore: ProjectStore; +let app: IUnleashTest; +let db: ITestDb; + +let projectStore: IProjectStore; + +const insertLastSeenAt = async ( + featureName: string, + environment: string = 'default', + date: string = '2023-10-01 12:34:56', +) => { + await db.rawDatabase.raw(`INSERT INTO last_seen_at_metrics (feature_name, environment, last_seen_at) + VALUES ('${featureName}', '${environment}', '${date}');`); +}; + +const insertFeatureEnvironmentsLastSeen = async ( + featureName: string, + environment: string = 'default', + date: string = '2022-05-01 12:34:56', +) => { + await db.rawDatabase.raw(` + INSERT INTO feature_environments (feature_name, environment, last_seen_at, enabled) + VALUES ('${featureName}', '${environment}', '${date}', true) + ON CONFLICT (feature_name, environment) DO UPDATE SET last_seen_at = '${date}', enabled = true; + `); +}; beforeAll(async () => { db = await dbInit('projects_api_serial', getLogger); @@ -35,7 +61,6 @@ test('Should ONLY return default project', async () => { name: 'test', description: '', mode: 'open', - defaultStickiness: 'default', }); const { body } = await app.request @@ -62,3 +87,103 @@ test('response for default project should include created_at', async () => { .expect(200); expect(body.createdAt).toBeDefined(); }); + +test('response should include last seen at per environment', async () => { + await app.createFeature('my-new-feature-toggle'); + + await insertLastSeenAt('my-new-feature-toggle', 'default'); + await insertFeatureEnvironmentsLastSeen('my-new-feature-toggle', 'default'); + + const { body } = await app.request + .get('/api/admin/projects/default') + .expect('Content-Type', /json/) + .expect(200); + + expect(body.features[0].environments[0].lastSeenAt).toEqual( + '2022-05-01T12:34:56.000Z', + ); + + const appWithLastSeenRefactor = await setupAppWithCustomConfig( + db.stores, + { + experimental: { + flags: { + useLastSeenRefactor: true, + }, + }, + }, + db.rawDatabase, + ); + + const response = await appWithLastSeenRefactor.request + .get('/api/admin/projects/default') + .expect('Content-Type', /json/) + .expect(200); + + expect(response.body.features[0].environments[0].lastSeenAt).toEqual( + '2023-10-01T12:34:56.000Z', + ); +}); + +test('response should include last seen at per environment for multiple environments', async () => { + const appWithLastSeenRefactor = await setupAppWithCustomConfig( + db.stores, + { + experimental: { + flags: { + useLastSeenRefactor: true, + }, + }, + }, + db.rawDatabase, + ); + + await db.stores.environmentStore.create({ + name: 'development', + type: 'development', + sortOrder: 1, + enabled: true, + }); + + await db.stores.environmentStore.create({ + name: 'production', + type: 'production', + sortOrder: 2, + enabled: true, + }); + + await appWithLastSeenRefactor.services.projectService.addEnvironmentToProject( + 'default', + 'development', + ); + await appWithLastSeenRefactor.services.projectService.addEnvironmentToProject( + 'default', + 'production', + ); + + await appWithLastSeenRefactor.createFeature( + 'multiple-environment-last-seen-at', + ); + + await insertLastSeenAt('multiple-environment-last-seen-at', 'default'); + await insertLastSeenAt('multiple-environment-last-seen-at', 'development'); + await insertLastSeenAt('multiple-environment-last-seen-at', 'production'); + + const { body } = await appWithLastSeenRefactor.request + .get('/api/admin/projects/default') + .expect('Content-Type', /json/) + .expect(200); + + const featureEnvironments = body.features[1].environments; + + const [def, development, production] = featureEnvironments; + + expect(def.name).toBe('default'); + expect(def.lastSeenAt).toEqual('2023-10-01T12:34:56.000Z'); + + expect(development.name).toBe('development'); + expect(development.lastSeenAt).toEqual('2023-10-01T12:34:56.000Z'); + + expect(production.name).toBe('production'); + expect(production.lastSeenAt).toEqual('2023-10-01T12:34:56.000Z'); +});