1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

fix: only add project environments if enabled (#1050)

This commit is contained in:
Ivar Conradi Østhus 2021-10-21 10:14:35 +02:00 committed by GitHub
parent e345980dcf
commit 3484340cd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 13 deletions

View File

@ -59,6 +59,8 @@ export default class ProjectService {
private featureToggleService: FeatureToggleServiceV2;
private environmentsEnabled: boolean = false;
constructor(
{
projectStore,
@ -89,6 +91,8 @@ export default class ProjectService {
this.featureTypeStore = featureTypeStore;
this.featureToggleService = featureToggleService;
this.logger = config.getLogger('services/project-service.js');
this.environmentsEnabled =
config.experimental.environments?.enabled || false;
}
async getProjects(query?: IProjectQuery): Promise<IProjectWithCount[]> {
@ -122,17 +126,26 @@ export default class ProjectService {
await this.store.create(data);
const enabledEnvironments = await this.environmentStore.getAll({
enabled: true,
});
await Promise.all(
enabledEnvironments.map(async (e) => {
await this.featureEnvironmentStore.connectProject(
e.name,
data.id,
);
}),
);
if (this.environmentsEnabled) {
const enabledEnvironments = await this.environmentStore.getAll({
enabled: true,
});
// TODO: Only if enabled!
await Promise.all(
enabledEnvironments.map(async (e) => {
await this.featureEnvironmentStore.connectProject(
e.name,
data.id,
);
}),
);
} else {
await this.featureEnvironmentStore.connectProject(
'default',
data.id,
);
}
await this.accessService.createDefaultProjectRoles(user, data.id);

View File

@ -142,7 +142,7 @@ export interface IUnleashConfig {
ui: IUIConfig;
import: IImportOption;
experimental: {
[key: string]: object;
[key: string]: any;
};
email: IEmailOption;
secureHeaders: boolean;

View File

@ -30,7 +30,7 @@ beforeAll(async () => {
const config = createTestConfig({
getLogger,
// @ts-ignore
experimental: { rbac: true },
experimental: { environments: { enabled: true } },
});
accessService = new AccessService(stores, config);
featureToggleService = new FeatureToggleServiceV2(stores, config);
@ -46,6 +46,16 @@ afterAll(async () => {
await db.destroy();
});
afterEach(async () => {
const envs = await stores.environmentStore.getAll();
const deleteEnvs = envs
.filter((env) => env.name !== 'default')
.map(async (env) => {
await stores.environmentStore.delete(env.name);
});
await Promise.allSettled(deleteEnvs);
});
test('should have default project', async () => {
const project = await projectService.getProject('default');
expect(project).toBeDefined();
@ -536,3 +546,40 @@ test('A newly created project only gets connected to enabled environments', asyn
expect(connectedEnvs.some((e) => e === enabledEnv)).toBeTruthy();
expect(connectedEnvs.some((e) => e === disabledEnv)).toBeFalsy();
});
test('A newly created project only gets connected to default environment if experimental flag is disabled', async () => {
const config = createTestConfig({
getLogger,
// @ts-ignore
experimental: { environments: { enabled: false } },
});
projectService = new ProjectService(
stores,
//@ts-ignore
config,
accessService,
featureToggleService,
);
const project = {
id: 'environment-test-default',
name: 'New environment project',
description: 'Blah',
};
const enabledEnv = 'connection_test';
await db.stores.environmentStore.create({
name: enabledEnv,
type: 'test',
});
const disabledEnv = 'do_not_connect';
await db.stores.environmentStore.create({
name: disabledEnv,
type: 'test',
enabled: false,
});
await projectService.createProject(project, user);
const connectedEnvs =
await db.stores.projectStore.getEnvironmentsForProject(project.id);
expect(connectedEnvs).toHaveLength(1); // default, connection_test
expect(connectedEnvs[0]).toBe('default');
});