diff --git a/src/lib/services/project-service.ts b/src/lib/services/project-service.ts index ac8e31a44e..6999e2af49 100644 --- a/src/lib/services/project-service.ts +++ b/src/lib/services/project-service.ts @@ -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 { @@ -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); diff --git a/src/lib/types/option.ts b/src/lib/types/option.ts index ee50b35a90..1517dac4b8 100644 --- a/src/lib/types/option.ts +++ b/src/lib/types/option.ts @@ -142,7 +142,7 @@ export interface IUnleashConfig { ui: IUIConfig; import: IImportOption; experimental: { - [key: string]: object; + [key: string]: any; }; email: IEmailOption; secureHeaders: boolean; diff --git a/src/test/e2e/services/project-service.e2e.test.ts b/src/test/e2e/services/project-service.e2e.test.ts index aad1cf5a24..0e6f26fe4f 100644 --- a/src/test/e2e/services/project-service.e2e.test.ts +++ b/src/test/e2e/services/project-service.e2e.test.ts @@ -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'); +});