Skip to content

Commit

Permalink
feat: prevent revive flag/flags in archived project (#7826)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew authored Aug 12, 2024
1 parent 624c6ce commit 67fa28f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/lib/features/feature-toggle/feature-toggle-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,18 @@ class FeatureToggleService {
}
}

private async validateActiveProject(projectId: string) {
if (this.flagResolver.isEnabled('archiveProjects')) {
const hasActiveProject =
await this.projectStore.hasActiveProject(projectId);
if (!hasActiveProject) {
throw new NotFoundError(
`Active project with id ${projectId} does not exist`,
);
}
}
}

async createFeatureToggle(
projectId: string,
value: FeatureToggleDTO,
Expand Down Expand Up @@ -2058,6 +2070,7 @@ class FeatureToggleService {
projectId: string,
auditUser: IAuditUser,
): Promise<void> {
await this.validateActiveProject(projectId);
await this.validateFeaturesContext(featureNames, projectId);

const features =
Expand Down Expand Up @@ -2091,6 +2104,11 @@ class FeatureToggleService {
featureName: string,
auditUser: IAuditUser,
): Promise<void> {
const feature = await this.featureToggleStore.get(featureName);
if (!feature) {
throw new NotFoundError(`Feature ${featureName} does not exist`);
}
await this.validateActiveProject(feature.project);
const toggle = await this.featureToggleStore.revive(featureName);
await this.featureToggleStore.disableAllEnvironmentsForFeatures([
featureName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,3 +773,40 @@ test('Should not allow to add flags to archived projects', async () => {
),
);
});

test('Should not allow to revive flags to archived projects', async () => {
const project = await stores.projectStore.create({
id: 'archivedProjectWithFlag',
name: 'archivedProjectWithFlag',
});
const flag = await service.createFeatureToggle(
project.id,
{
name: 'archiveFlag',
},
TEST_AUDIT_USER,
);

await service.archiveToggle(
flag.name,
{ email: '[email protected]' } as User,
TEST_AUDIT_USER,
);
await stores.projectStore.archive(project.id);

await expect(
service.reviveFeature(flag.name, TEST_AUDIT_USER),
).rejects.toEqual(
new NotFoundError(
`Active project with id archivedProjectWithFlag does not exist`,
),
);

await expect(
service.reviveFeatures([flag.name], project.id, TEST_AUDIT_USER),
).rejects.toEqual(
new NotFoundError(
`Active project with id archivedProjectWithFlag does not exist`,
),
);
});

0 comments on commit 67fa28f

Please sign in to comment.