1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00

chore: PoC test API helper (DRY) (#3342)

## About the changes
New way of centralizing API calls while doing E2E tests to avoid repeating ourselves.
This commit is contained in:
Gastón Fournier 2023-03-20 10:27:19 +01:00 committed by GitHub
parent d6faf01a4f
commit af42347a7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 18 deletions

View File

@ -128,26 +128,15 @@ const createSegment = (postData: UpsertSegmentSchema): Promise<ISegment> => {
};
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) => {

View File

@ -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<void>;
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<IUnleashTest> {
@ -61,6 +130,7 @@ export async function setupApp(stores: IUnleashStores): Promise<IUnleashTest> {
export async function setupAppWithCustomConfig(
stores: IUnleashStores,
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
customOptions: any,
db?: Db,
): Promise<IUnleashTest> {
@ -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<IUnleashTest> {