Skip to content

Commit

Permalink
feat: prevent adding flags to archived project (#7811)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew authored Aug 9, 2024
1 parent e8d682c commit bde81b9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 17 deletions.
11 changes: 9 additions & 2 deletions src/lib/features/feature-toggle/feature-toggle-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,12 @@ class FeatureToggleService {
await this.validateName(value.name);
await this.validateFeatureFlagNameAgainstPattern(value.name, projectId);

const projectExists = await this.projectStore.hasProject(projectId);
let projectExists: boolean;
if (this.flagResolver.isEnabled('archiveProjects')) {
projectExists = await this.projectStore.hasActiveProject(projectId);
} else {
projectExists = await this.projectStore.hasProject(projectId);
}

if (await this.projectStore.isFeatureLimitReached(projectId)) {
throw new InvalidOperationError(
Expand Down Expand Up @@ -1322,7 +1327,9 @@ class FeatureToggleService {

return createdToggle;
}
throw new NotFoundError(`Project with id ${projectId} does not exist`);
throw new NotFoundError(
`Active project with id ${projectId} does not exist`,
);
}

async checkFeatureFlagNamesAgainstProjectPattern(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import {
TEST_AUDIT_USER,
} from '../../../types';
import EnvironmentService from '../../project-environments/environment-service';
import { ForbiddenError, PatternError, PermissionError } from '../../../error';
import {
ForbiddenError,
NotFoundError,
PatternError,
PermissionError,
} from '../../../error';
import type { ISegmentService } from '../../segment/segment-service-interface';
import { createFeatureToggleService, createSegmentService } from '../..';
import { insertLastSeenAt } from '../../../../test/e2e/helpers/test-helper';
Expand All @@ -41,7 +46,9 @@ const mockConstraints = (): IConstraint[] => {
const irrelevantDate = new Date();

beforeAll(async () => {
const config = createTestConfig();
const config = createTestConfig({
experimental: { flags: { archiveProjects: true } },
});
db = await dbInit(
'feature_toggle_service_v2_service_serial',
config.getLogger,
Expand Down Expand Up @@ -744,3 +751,25 @@ test('Should return "default" for stickiness when creating a flexibleRollout str
strategies: [{ parameters: { stickiness: 'default' } }],
});
});

test('Should not allow to add flags to archived projects', async () => {
const project = await stores.projectStore.create({
id: 'archivedProject',
name: 'archivedProject',
});
await stores.projectStore.archive(project.id);

await expect(
service.createFeatureToggle(
project.id,
{
name: 'irrelevant',
},
TEST_AUDIT_USER,
),
).rejects.toEqual(
new NotFoundError(
`Active project with id archivedProject does not exist`,
),
);
});
2 changes: 2 additions & 0 deletions src/lib/features/project/project-store-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export interface IProjectApplicationsSearchParams {
export interface IProjectStore extends Store<IProject, string> {
hasProject(id: string): Promise<boolean>;

hasActiveProject(id: string): Promise<boolean>;

updateHealth(healthUpdate: IProjectHealthUpdate): Promise<void>;

create(project: IProjectInsert): Promise<IProject>;
Expand Down
27 changes: 18 additions & 9 deletions src/lib/features/project/project-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,6 @@ class ProjectStore implements IProjectStore {

destroy(): void {}

async exists(id: string): Promise<boolean> {
const result = await this.db.raw(
`SELECT EXISTS(SELECT 1 FROM ${TABLE} WHERE id = ?) AS present`,
[id],
);
const { present } = result.rows[0];
return present;
}

async isFeatureLimitReached(id: string): Promise<boolean> {
const result = await this.db.raw(
`SELECT EXISTS(SELECT 1
Expand Down Expand Up @@ -252,6 +243,15 @@ class ProjectStore implements IProjectStore {
.then(this.mapRow);
}

async exists(id: string): Promise<boolean> {
const result = await this.db.raw(
`SELECT EXISTS(SELECT 1 FROM ${TABLE} WHERE id = ?) AS present`,
[id],
);
const { present } = result.rows[0];
return present;
}

async hasProject(id: string): Promise<boolean> {
const result = await this.db.raw(
`SELECT EXISTS(SELECT 1 FROM ${TABLE} WHERE id = ?) AS present`,
Expand All @@ -261,6 +261,15 @@ class ProjectStore implements IProjectStore {
return present;
}

async hasActiveProject(id: string): Promise<boolean> {
const result = await this.db.raw(
`SELECT EXISTS(SELECT 1 FROM ${TABLE} WHERE id = ? and archived_at IS NULL) AS present`,
[id],
);
const { present } = result.rows[0];
return present;
}

async updateHealth(healthUpdate: IProjectHealthUpdate): Promise<void> {
await this.db(TABLE).where({ id: healthUpdate.id }).update({
health: healthUpdate.health,
Expand Down
14 changes: 10 additions & 4 deletions src/test/fixtures/fake-project-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ export default class FakeProjectStore implements IProjectStore {
return this.projects.length;
}

async exists(key: string): Promise<boolean> {
return this.projects.some((project) => project.id === key);
}

async get(key: string): Promise<IProject> {
const project = this.projects.find((p) => p.id === key);
if (project) {
Expand All @@ -129,10 +125,20 @@ export default class FakeProjectStore implements IProjectStore {
return Promise.resolve(0);
}

async exists(key: string): Promise<boolean> {
return this.projects.some((project) => project.id === key);
}

async hasProject(id: string): Promise<boolean> {
return this.exists(id);
}

async hasActiveProject(id: string): Promise<boolean> {
return this.projects.some(
(project) => project.id === id && project.archivedAt === null,
);
}

async importProjects(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
projects: IProjectInsert[],
Expand Down

0 comments on commit bde81b9

Please sign in to comment.