1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-01 00:08:27 +01:00

fix: change request enabled check should ignore disabled envs (#7869) (#7876)

This commit is contained in:
Mateusz Kwasniewski 2024-08-14 13:35:40 +02:00 committed by GitHub
parent e40975e1ed
commit 0a9b86f9bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 65 additions and 10 deletions

View File

@ -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);
});

View File

@ -70,14 +70,20 @@ export class ChangeRequestAccessReadModel
public async isChangeRequestsEnabledForProject(
project: string,
): Promise<boolean> {
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);
}
}

View File

@ -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',