mirror of
https://github.com/Unleash/unleash.git
synced 2025-07-21 13:47:39 +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:
parent
d6faf01a4f
commit
af42347a7b
@ -128,26 +128,15 @@ const createSegment = (postData: UpsertSegmentSchema): Promise<ISegment> => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const createContextField = async (contextField: IContextFieldDto) => {
|
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) => {
|
const createFeature = async (featureName: string) => {
|
||||||
await app.request
|
await app.createFeature(featureName);
|
||||||
.post(`/api/admin/projects/${DEFAULT_PROJECT}/features`)
|
|
||||||
.send({
|
|
||||||
name: featureName,
|
|
||||||
})
|
|
||||||
.set('Content-Type', 'application/json')
|
|
||||||
.expect(201);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const archiveFeature = async (featureName: string) => {
|
const archiveFeature = async (featureName: string) => {
|
||||||
await app.request
|
await app.archiveFeature(featureName);
|
||||||
.delete(
|
|
||||||
`/api/admin/projects/${DEFAULT_PROJECT}/features/${featureName}`,
|
|
||||||
)
|
|
||||||
.set('Content-Type', 'application/json')
|
|
||||||
.expect(202);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const unArchiveFeature = async (featureName: string) => {
|
const unArchiveFeature = async (featureName: string) => {
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
||||||
import supertest from 'supertest';
|
import supertest from 'supertest';
|
||||||
|
|
||||||
import EventEmitter from 'events';
|
import EventEmitter from 'events';
|
||||||
@ -7,19 +6,83 @@ import { createTestConfig } from '../../config/test-config';
|
|||||||
import { IAuthType, IUnleashConfig } from '../../../lib/types/option';
|
import { IAuthType, IUnleashConfig } from '../../../lib/types/option';
|
||||||
import { createServices } from '../../../lib/services';
|
import { createServices } from '../../../lib/services';
|
||||||
import sessionDb from '../../../lib/middleware/session-db';
|
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 { IUnleashServices } from '../../../lib/types/services';
|
||||||
import { Db } from '../../../lib/db/db';
|
import { Db } from '../../../lib/db/db';
|
||||||
|
import { IContextFieldDto } from 'lib/types/stores/context-field-store';
|
||||||
|
|
||||||
process.env.NODE_ENV = 'test';
|
process.env.NODE_ENV = 'test';
|
||||||
|
|
||||||
export interface IUnleashTest {
|
export interface IUnleashTest extends IUnleashHttpAPI {
|
||||||
request: supertest.SuperAgentTest;
|
request: supertest.SuperAgentTest;
|
||||||
destroy: () => Promise<void>;
|
destroy: () => Promise<void>;
|
||||||
services: IUnleashServices;
|
services: IUnleashServices;
|
||||||
config: IUnleashConfig;
|
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(
|
async function createApp(
|
||||||
stores,
|
stores,
|
||||||
adminAuthentication = IAuthType.NONE,
|
adminAuthentication = IAuthType.NONE,
|
||||||
@ -52,7 +115,13 @@ async function createApp(
|
|||||||
};
|
};
|
||||||
|
|
||||||
// TODO: use create from server-impl instead?
|
// 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> {
|
export async function setupApp(stores: IUnleashStores): Promise<IUnleashTest> {
|
||||||
@ -61,6 +130,7 @@ export async function setupApp(stores: IUnleashStores): Promise<IUnleashTest> {
|
|||||||
|
|
||||||
export async function setupAppWithCustomConfig(
|
export async function setupAppWithCustomConfig(
|
||||||
stores: IUnleashStores,
|
stores: IUnleashStores,
|
||||||
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||||
customOptions: any,
|
customOptions: any,
|
||||||
db?: Db,
|
db?: Db,
|
||||||
): Promise<IUnleashTest> {
|
): Promise<IUnleashTest> {
|
||||||
@ -69,6 +139,7 @@ export async function setupAppWithCustomConfig(
|
|||||||
|
|
||||||
export async function setupAppWithAuth(
|
export async function setupAppWithAuth(
|
||||||
stores: IUnleashStores,
|
stores: IUnleashStores,
|
||||||
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||||
customOptions?: any,
|
customOptions?: any,
|
||||||
db?: Db,
|
db?: Db,
|
||||||
): Promise<IUnleashTest> {
|
): Promise<IUnleashTest> {
|
||||||
|
Loading…
Reference in New Issue
Block a user