From 0a9b86f9bbcdc45e0f0a3b51a4b5a1453def8e12 Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Wed, 14 Aug 2024 13:35:40 +0200 Subject: [PATCH] fix: change request enabled check should ignore disabled envs (#7869) (#7876) --- ...l-change-request-access-read-model.test.ts | 49 +++++++++++++++++++ .../sql-change-request-access-read-model.ts | 24 +++++---- ...e-request-segment-usage-read-model.test.ts | 2 +- 3 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 src/lib/features/change-request-access-service/sql-change-request-access-read-model.test.ts diff --git a/src/lib/features/change-request-access-service/sql-change-request-access-read-model.test.ts b/src/lib/features/change-request-access-service/sql-change-request-access-read-model.test.ts new file mode 100644 index 000000000000..1054aece2e7f --- /dev/null +++ b/src/lib/features/change-request-access-service/sql-change-request-access-read-model.test.ts @@ -0,0 +1,49 @@ +import dbInit, { type ITestDb } from '../../../test/e2e/helpers/database-init'; +import getLogger from '../../../test/fixtures/no-logger'; +import { createChangeRequestAccessReadModel } from './createChangeRequestAccessReadModel'; +import { createTestConfig } from '../../../test/config/test-config'; +import type { IChangeRequestAccessReadModel } from './change-request-access-read-model'; + +let db: ITestDb; + +let readModel: IChangeRequestAccessReadModel; + +beforeAll(async () => { + db = await dbInit('change_request_access_read_model', getLogger); + + const config = createTestConfig({ + getLogger, + }); + + readModel = createChangeRequestAccessReadModel(db.rawDatabase, config); +}); + +afterAll(async () => { + await db.destroy(); +}); + +test(`Should indicate change request enabled status`, async () => { + // no change requests + const defaultStatus = + await readModel.isChangeRequestsEnabledForProject('default'); + expect(defaultStatus).toBe(false); + + // change request enabled in enabled environment + await db.rawDatabase('change_request_settings').insert({ + project: 'default', + environment: 'default', + required_approvals: 1, + }); + const enabledStatus = + await readModel.isChangeRequestsEnabledForProject('default'); + expect(enabledStatus).toBe(true); + + // change request enabled in disabled environment + await db.stores.projectStore.deleteEnvironmentForProject( + 'default', + 'default', + ); + const disabledStatus = + await readModel.isChangeRequestsEnabledForProject('default'); + expect(disabledStatus).toBe(false); +}); diff --git a/src/lib/features/change-request-access-service/sql-change-request-access-read-model.ts b/src/lib/features/change-request-access-service/sql-change-request-access-read-model.ts index da562e30ae57..0c2431620c04 100644 --- a/src/lib/features/change-request-access-service/sql-change-request-access-read-model.ts +++ b/src/lib/features/change-request-access-service/sql-change-request-access-read-model.ts @@ -70,14 +70,20 @@ export class ChangeRequestAccessReadModel public async isChangeRequestsEnabledForProject( project: string, ): Promise { - const result = await this.db.raw( - `SELECT EXISTS(SELECT 1 - FROM change_request_settings - WHERE project = ? - ) AS present`, - [project], - ); - const { present } = result.rows[0]; - return present; + const result = await this.db('change_request_settings') + .join('project_environments', function () { + return this.on( + 'change_request_settings.project', + 'project_environments.project_id', + ).andOn( + 'change_request_settings.environment', + 'project_environments.environment_name', + ); + }) + .where('change_request_settings.project', project) + .select('change_request_settings.project') + .first(); + + return Boolean(result); } } diff --git a/src/lib/features/change-request-segment-usage-service/change-request-segment-usage-read-model.test.ts b/src/lib/features/change-request-segment-usage-service/change-request-segment-usage-read-model.test.ts index 6eea3e093c03..78ed1d5a4d3b 100644 --- a/src/lib/features/change-request-segment-usage-service/change-request-segment-usage-read-model.test.ts +++ b/src/lib/features/change-request-segment-usage-service/change-request-segment-usage-read-model.test.ts @@ -18,7 +18,7 @@ const FLAG_NAME = 'crarm-test-flag'; let readModel: IChangeRequestSegmentUsageReadModel; beforeAll(async () => { - db = await dbInit('change_request_access_read_model_serial', getLogger); + db = await dbInit('change_request_segment_usage_read_model', getLogger); user = await db.stores.userStore.insert({ username: 'cr-creator',