mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	refactor: add tests for /api/client/features (#5057)
add more robust tests for /api/client/features
This commit is contained in:
		
							parent
							
								
									675ec2e836
								
							
						
					
					
						commit
						0064c9e1be
					
				@ -11,6 +11,51 @@ let app: IUnleashTest;
 | 
				
			|||||||
let db: ITestDb;
 | 
					let db: ITestDb;
 | 
				
			||||||
let dummyAdmin;
 | 
					let dummyAdmin;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const apiClientResponse = [
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        name: 'test1',
 | 
				
			||||||
 | 
					        type: 'release',
 | 
				
			||||||
 | 
					        enabled: false,
 | 
				
			||||||
 | 
					        project: 'default',
 | 
				
			||||||
 | 
					        stale: false,
 | 
				
			||||||
 | 
					        strategies: [
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                name: 'flexibleRollout',
 | 
				
			||||||
 | 
					                constraints: [],
 | 
				
			||||||
 | 
					                parameters: { rollout: '100' },
 | 
				
			||||||
 | 
					                variants: [],
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					        variants: [],
 | 
				
			||||||
 | 
					        description: null,
 | 
				
			||||||
 | 
					        impressionData: false,
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        name: 'test2',
 | 
				
			||||||
 | 
					        type: 'release',
 | 
				
			||||||
 | 
					        enabled: false,
 | 
				
			||||||
 | 
					        project: 'default',
 | 
				
			||||||
 | 
					        stale: false,
 | 
				
			||||||
 | 
					        strategies: [
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                name: 'default',
 | 
				
			||||||
 | 
					                constraints: [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        contextName: 'userId',
 | 
				
			||||||
 | 
					                        operator: 'IN',
 | 
				
			||||||
 | 
					                        values: ['123'],
 | 
				
			||||||
 | 
					                    },
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                parameters: {},
 | 
				
			||||||
 | 
					                variants: [],
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					        variants: [],
 | 
				
			||||||
 | 
					        description: null,
 | 
				
			||||||
 | 
					        impressionData: false,
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
beforeAll(async () => {
 | 
					beforeAll(async () => {
 | 
				
			||||||
    db = await dbInit('client_feature_toggles', getLogger);
 | 
					    db = await dbInit('client_feature_toggles', getLogger);
 | 
				
			||||||
    app = await setupAppWithCustomConfig(
 | 
					    app = await setupAppWithCustomConfig(
 | 
				
			||||||
@ -107,3 +152,88 @@ test('should support filtering on project', async () => {
 | 
				
			|||||||
            expect(res.body.features[0].name).toBe('ab_test1');
 | 
					            expect(res.body.features[0].name).toBe('ab_test1');
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test('should return correct data structure from /api/client/features', async () => {
 | 
				
			||||||
 | 
					    await db.rawDatabase.raw('DELETE FROM features');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await app.createFeature('test1', 'default');
 | 
				
			||||||
 | 
					    await app.createFeature('test2', 'default');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await app.addStrategyToFeatureEnv(
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            name: 'flexibleRollout',
 | 
				
			||||||
 | 
					            constraints: [],
 | 
				
			||||||
 | 
					            parameters: { rollout: '100' },
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        DEFAULT_ENV,
 | 
				
			||||||
 | 
					        'test1',
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					    await app.addStrategyToFeatureEnv(
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            name: 'default',
 | 
				
			||||||
 | 
					            constraints: [
 | 
				
			||||||
 | 
					                { contextName: 'userId', operator: 'IN', values: ['123'] },
 | 
				
			||||||
 | 
					            ],
 | 
				
			||||||
 | 
					            parameters: {},
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        DEFAULT_ENV,
 | 
				
			||||||
 | 
					        'test2',
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const result = await app.request
 | 
				
			||||||
 | 
					        .get('/api/client/features')
 | 
				
			||||||
 | 
					        .expect('Content-Type', /json/)
 | 
				
			||||||
 | 
					        .expect(200);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    expect(result.body.features).toEqual(apiClientResponse);
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test('should return correct data structure from /api/client/features | separateAdminClientApi', async () => {
 | 
				
			||||||
 | 
					    const appWithFeatureFlag = await setupAppWithCustomConfig(
 | 
				
			||||||
 | 
					        db.stores,
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            experimental: {
 | 
				
			||||||
 | 
					                flags: {
 | 
				
			||||||
 | 
					                    strictSchemaValidation: true,
 | 
				
			||||||
 | 
					                    dependentFeatures: true,
 | 
				
			||||||
 | 
					                    separateAdminClientApi: true,
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        db.rawDatabase,
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await db.rawDatabase.raw('DELETE FROM features');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await appWithFeatureFlag.createFeature('test1', 'default');
 | 
				
			||||||
 | 
					    await appWithFeatureFlag.createFeature('test2', 'default');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await appWithFeatureFlag.addStrategyToFeatureEnv(
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            name: 'flexibleRollout',
 | 
				
			||||||
 | 
					            constraints: [],
 | 
				
			||||||
 | 
					            parameters: { rollout: '100' },
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        DEFAULT_ENV,
 | 
				
			||||||
 | 
					        'test1',
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await appWithFeatureFlag.addStrategyToFeatureEnv(
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            name: 'default',
 | 
				
			||||||
 | 
					            constraints: [
 | 
				
			||||||
 | 
					                { contextName: 'userId', operator: 'IN', values: ['123'] },
 | 
				
			||||||
 | 
					            ],
 | 
				
			||||||
 | 
					            parameters: {},
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        DEFAULT_ENV,
 | 
				
			||||||
 | 
					        'test2',
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const result = await appWithFeatureFlag.request
 | 
				
			||||||
 | 
					        .get('/api/client/features')
 | 
				
			||||||
 | 
					        .expect('Content-Type', /json/)
 | 
				
			||||||
 | 
					        .expect(200);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    expect(result.body.features).toEqual(apiClientResponse);
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
@ -11,7 +11,11 @@ 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';
 | 
					import { IContextFieldDto } from 'lib/types/stores/context-field-store';
 | 
				
			||||||
import { DEFAULT_ENV } from '../../../lib/util';
 | 
					import { DEFAULT_ENV } from '../../../lib/util';
 | 
				
			||||||
import { CreateFeatureSchema, ImportTogglesSchema } from '../../../lib/openapi';
 | 
					import {
 | 
				
			||||||
 | 
					    CreateFeatureSchema,
 | 
				
			||||||
 | 
					    CreateFeatureStrategySchema,
 | 
				
			||||||
 | 
					    ImportTogglesSchema,
 | 
				
			||||||
 | 
					} from '../../../lib/openapi';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
process.env.NODE_ENV = 'test';
 | 
					process.env.NODE_ENV = 'test';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -28,6 +32,14 @@ export interface IUnleashTest extends IUnleashHttpAPI {
 | 
				
			|||||||
 * All functions return a supertest.Test object, which can be used to compose more assertions on the response.
 | 
					 * All functions return a supertest.Test object, which can be used to compose more assertions on the response.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
export interface IUnleashHttpAPI {
 | 
					export interface IUnleashHttpAPI {
 | 
				
			||||||
 | 
					    addStrategyToFeatureEnv(
 | 
				
			||||||
 | 
					        postData: CreateFeatureStrategySchema,
 | 
				
			||||||
 | 
					        envName: string,
 | 
				
			||||||
 | 
					        featureName: string,
 | 
				
			||||||
 | 
					        project?: string,
 | 
				
			||||||
 | 
					        expectStatusCode?: number,
 | 
				
			||||||
 | 
					    ): supertest.Test;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    createFeature(
 | 
					    createFeature(
 | 
				
			||||||
        feature: string | CreateFeatureSchema,
 | 
					        feature: string | CreateFeatureSchema,
 | 
				
			||||||
        project?: string,
 | 
					        project?: string,
 | 
				
			||||||
@ -74,6 +86,16 @@ function httpApis(
 | 
				
			|||||||
    const base = config.server.baseUriPath || '';
 | 
					    const base = config.server.baseUriPath || '';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
 | 
					        addStrategyToFeatureEnv: (
 | 
				
			||||||
 | 
					            postData: CreateFeatureStrategySchema,
 | 
				
			||||||
 | 
					            envName: string,
 | 
				
			||||||
 | 
					            featureName: string,
 | 
				
			||||||
 | 
					            project: string = DEFAULT_PROJECT,
 | 
				
			||||||
 | 
					            expectStatusCode: number = 200,
 | 
				
			||||||
 | 
					        ) => {
 | 
				
			||||||
 | 
					            const url = `${base}/api/admin/projects/${project}/features/${featureName}/environments/${envName}/strategies`;
 | 
				
			||||||
 | 
					            return request.post(url).send(postData).expect(expectStatusCode);
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
        createFeature: (
 | 
					        createFeature: (
 | 
				
			||||||
            feature: string | CreateFeatureSchema,
 | 
					            feature: string | CreateFeatureSchema,
 | 
				
			||||||
            project: string = DEFAULT_PROJECT,
 | 
					            project: string = DEFAULT_PROJECT,
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user