1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-23 20:07:40 +02:00
unleash.unleash/src/test/e2e/api/admin/feature.custom-auth.e2e.test.ts
Christopher Kolstad ff7be7696c
fix: Stores as typescript and with interfaces. (#902)
Co-authored-by: Ivar Conradi Østhus <ivarconr@gmail.com>
2021-08-12 15:04:37 +02:00

70 lines
2.0 KiB
TypeScript

import { setupAppWithCustomAuth } from '../../helpers/test-helper';
import AuthenticationRequired from '../../../../lib/types/authentication-required';
import dbInit from '../../helpers/database-init';
import getLogger from '../../../fixtures/no-logger';
let stores;
let db;
beforeAll(async () => {
db = await dbInit('feature_api_custom_auth', getLogger);
stores = db.stores;
});
afterAll(async () => {
if (db) {
await db.destroy();
}
});
test('should require authenticated user', async () => {
expect.assertions(0);
const preHook = (app) => {
app.use('/api/admin/', (req, res) =>
res
.status('401')
.json(
new AuthenticationRequired({
path: '/api/admin/login',
type: 'custom',
message: 'You have to identify yourself.',
}),
)
.end(),
);
};
const { request, destroy } = await setupAppWithCustomAuth(stores, preHook);
await request.get('/api/admin/features').expect(401);
await destroy();
});
test('creates new feature toggle with createdBy', async () => {
expect.assertions(1);
const email = 'custom-user@mail.com';
const preHook = (app, config, { userService }) => {
app.use('/api/admin/', async (req, res, next) => {
req.user = await userService.loginUserWithoutPassword(email, true);
next();
});
};
const { request, destroy } = await setupAppWithCustomAuth(stores, preHook);
// create toggle
await request
.post('/api/admin/features')
.send({
name: 'com.test.Username',
enabled: false,
strategies: [{ name: 'default' }],
})
.expect(201);
await request.get('/api/admin/events/com.test.Username').expect((res) => {
expect(res.body.events[0].createdBy).toBe(email);
});
await destroy();
});