Skip to content

Commit

Permalink
fix: change request enabled check should ignore disabled envs (#7869) (
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew authored Aug 14, 2024
1 parent e40975e commit 0a9b86f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 0a9b86f

Please sign in to comment.