UNPKG

unleash-server

Version:

Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.

173 lines • 5.77 kB
import { setupAppWithAuth, } from '../../helpers/test-helper.js'; import { RoleName } from '../../../../lib/types/index.js'; import dbInit from '../../helpers/database-init.js'; import getLogger from '../../../fixtures/no-logger.js'; let app; let db; let stores; let accessService; let editorRole; const regularUserName = 'favorites-user'; const createFeature = async (featureName) => { await app.request .post('/api/admin/projects/default/features') .send({ name: featureName, }) .set('Content-Type', 'application/json') .expect(201); }; const loginRegularUser = () => app.request .post('/auth/demo/login') .send({ email: `${regularUserName}@getunleash.io`, }) .expect(200); const createUserEditorAccess = async (name, email) => { const { userStore } = stores; const user = await userStore.insert({ name, email }); await accessService.addUserToRole(user.id, editorRole.id, 'default'); return user; }; const favoriteFeature = async (featureName) => { await app.request .post(`/api/admin/projects/default/features/${featureName}/favorites`) .set('Content-Type', 'application/json') .expect(200); }; const unfavoriteFeature = async (featureName) => { await app.request .delete(`/api/admin/projects/default/features/${featureName}/favorites`) .set('Content-Type', 'application/json') .expect(200); }; const favoriteProject = async (projectName = 'default') => { await app.request .post(`/api/admin/projects/${projectName}/favorites`) .set('Content-Type', 'application/json') .expect(200); }; const unfavoriteProject = async (projectName = 'default') => { await app.request .delete(`/api/admin/projects/${projectName}/favorites`) .set('Content-Type', 'application/json') .expect(200); }; const getProject = async (projectName = 'default') => { return app.request .get(`/api/admin/projects/${projectName}/overview`) .set('Content-Type', 'application/json') .expect(200); }; const getProjects = async () => { return app.request .get('/api/admin/projects') .set('Content-Type', 'application/json') .expect(200); }; beforeAll(async () => { db = await dbInit('favorites_api_serial', getLogger); app = await setupAppWithAuth(db.stores, { experimental: { flags: { strictSchemaValidation: true, }, }, }, db.rawDatabase); stores = db.stores; accessService = app.services.accessService; const roles = await accessService.getRootRoles(); editorRole = roles.find((role) => role.name === RoleName.EDITOR); await createUserEditorAccess(regularUserName, `${regularUserName}@getunleash.io`); }); afterAll(async () => { await app.destroy(); await db.destroy(); }); afterEach(async () => { await db.stores.favoriteFeaturesStore.deleteAll(); await db.stores.featureToggleStore.deleteAll(); }); beforeEach(async () => { await loginRegularUser(); }); test('feature should not be favorited by default', async () => { const featureName = 'test-feature'; await createFeature(featureName); const { body } = await app.request .get(`/api/admin/projects/default/features/${featureName}`) .set('Content-Type', 'application/json') .expect(200); expect(body).toMatchObject({ name: featureName, favorite: false, }); }); test('should be favorited in admin endpoint', async () => { const featureName = 'test-feature'; await createFeature(featureName); await favoriteFeature(featureName); const { body } = await app.request .get('/api/admin/projects/default/features') .set('Content-Type', 'application/json') .expect(200); expect(body.features).toHaveLength(1); expect(body.features[0]).toMatchObject({ name: featureName, favorite: true, }); }); test('should be favorited in project single feature endpoint', async () => { const featureName = 'test-feature'; await createFeature(featureName); await favoriteFeature(featureName); const { body } = await app.request .get(`/api/admin/projects/default/features/${featureName}`) .set('Content-Type', 'application/json') .expect(200); expect(body).toMatchObject({ name: featureName, favorite: true, }); }); test('should be able to unfavorite feature', async () => { const featureName = 'test-feature'; await createFeature(featureName); await favoriteFeature(featureName); await unfavoriteFeature(featureName); const { body } = await app.request .get(`/api/admin/projects/default/features/${featureName}`) .set('Content-Type', 'application/json') .expect(200); expect(body).toMatchObject({ name: featureName, favorite: false, }); }); test('should be favorited in projects list', async () => { await favoriteProject(); const { body } = await getProjects(); expect(body.projects).toHaveLength(1); expect(body.projects[0]).toMatchObject({ name: 'Default', favorite: true, }); }); test('should be favorited in single project endpoint', async () => { await favoriteProject(); const { body } = await getProject(); expect(body).toMatchObject({ name: 'Default', favorite: true, }); }); test('project should not be favorited by default', async () => { await favoriteProject(); await unfavoriteProject(); const { body } = await getProject(); expect(body).toMatchObject({ name: 'Default', favorite: false, }); }); //# sourceMappingURL=favorites.e2e.test.js.map