diff --git a/src/lib/features/export-import-toggles/export-import.e2e.test.ts b/src/lib/features/export-import-toggles/export-import.e2e.test.ts index ece621a082..6c77430cdb 100644 --- a/src/lib/features/export-import-toggles/export-import.e2e.test.ts +++ b/src/lib/features/export-import-toggles/export-import.e2e.test.ts @@ -128,26 +128,15 @@ const createSegment = (postData: UpsertSegmentSchema): Promise => { }; const createContextField = async (contextField: IContextFieldDto) => { - await app.request.post(`/api/admin/context`).send(contextField).expect(201); + await app.createContextField(contextField); }; const createFeature = async (featureName: string) => { - await app.request - .post(`/api/admin/projects/${DEFAULT_PROJECT}/features`) - .send({ - name: featureName, - }) - .set('Content-Type', 'application/json') - .expect(201); + await app.createFeature(featureName); }; const archiveFeature = async (featureName: string) => { - await app.request - .delete( - `/api/admin/projects/${DEFAULT_PROJECT}/features/${featureName}`, - ) - .set('Content-Type', 'application/json') - .expect(202); + await app.archiveFeature(featureName); }; const unArchiveFeature = async (featureName: string) => { diff --git a/src/test/e2e/helpers/test-helper.ts b/src/test/e2e/helpers/test-helper.ts index 752b70e8fc..0ca7725d98 100644 --- a/src/test/e2e/helpers/test-helper.ts +++ b/src/test/e2e/helpers/test-helper.ts @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import supertest from 'supertest'; import EventEmitter from 'events'; @@ -7,19 +6,83 @@ import { createTestConfig } from '../../config/test-config'; import { IAuthType, IUnleashConfig } from '../../../lib/types/option'; import { createServices } from '../../../lib/services'; import sessionDb from '../../../lib/middleware/session-db'; -import { IUnleashStores } from '../../../lib/types'; +import { DEFAULT_PROJECT, IUnleashStores } from '../../../lib/types'; import { IUnleashServices } from '../../../lib/types/services'; import { Db } from '../../../lib/db/db'; +import { IContextFieldDto } from 'lib/types/stores/context-field-store'; process.env.NODE_ENV = 'test'; -export interface IUnleashTest { +export interface IUnleashTest extends IUnleashHttpAPI { request: supertest.SuperAgentTest; destroy: () => Promise; services: IUnleashServices; config: IUnleashConfig; } +export interface IUnleashHttpAPI { + createFeature( + name: string, + project?: string, + expectedResponseCode?: number, + ): supertest.Test; + archiveFeature( + name: string, + project?: string, + expectedResponseCode?: number, + ): supertest.Test; + createContextField( + contextField: IContextFieldDto, + expectedResponseCode?: number, + ): supertest.Test; +} + +function httpApis( + request: supertest.SuperAgentTest, + config: IUnleashConfig, +): IUnleashHttpAPI { + const base = config.server.baseUriPath || ''; + + return { + createFeature: ( + name: string, + project: string = DEFAULT_PROJECT, + expectedResponseCode: number = 201, + ) => { + return request + .post(`${base}/api/admin/projects/${project}/features`) + .send({ + name, + }) + .set('Content-Type', 'application/json') + .expect(expectedResponseCode); + }, + + archiveFeature( + name: string, + project: string = DEFAULT_PROJECT, + expectedResponseCode: number = 202, + ): supertest.Test { + return request + .delete( + `${base}/api/admin/projects/${project}/features/${name}`, + ) + .set('Content-Type', 'application/json') + .expect(expectedResponseCode); + }, + + createContextField( + contextField: IContextFieldDto, + expectedResponseCode: number = 201, + ): supertest.Test { + return request + .post(`${base}/api/admin/context`) + .send(contextField) + .expect(expectedResponseCode); + }, + }; +} + async function createApp( stores, adminAuthentication = IAuthType.NONE, @@ -52,7 +115,13 @@ async function createApp( }; // TODO: use create from server-impl instead? - return { request, destroy, services, config }; + return { + request, + destroy, + services, + config, + ...httpApis(request, config), + }; } export async function setupApp(stores: IUnleashStores): Promise { @@ -61,6 +130,7 @@ export async function setupApp(stores: IUnleashStores): Promise { export async function setupAppWithCustomConfig( stores: IUnleashStores, + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types customOptions: any, db?: Db, ): Promise { @@ -69,6 +139,7 @@ export async function setupAppWithCustomConfig( export async function setupAppWithAuth( stores: IUnleashStores, + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types customOptions?: any, db?: Db, ): Promise {