2021-08-12 15:04:37 +02:00
|
|
|
import { setupAppWithCustomAuth } from '../../helpers/test-helper';
|
|
|
|
import AuthenticationRequired from '../../../../lib/types/authentication-required';
|
2017-11-16 16:45:01 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
import dbInit from '../../helpers/database-init';
|
|
|
|
import getLogger from '../../../fixtures/no-logger';
|
2019-10-03 15:01:33 +02:00
|
|
|
|
|
|
|
let stores;
|
2021-03-19 22:25:21 +01:00
|
|
|
let db;
|
2019-10-03 15:01:33 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
beforeAll(async () => {
|
2021-03-19 22:25:21 +01:00
|
|
|
db = await dbInit('feature_api_custom_auth', getLogger);
|
2019-10-03 15:01:33 +02:00
|
|
|
stores = db.stores;
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
afterAll(async () => {
|
|
|
|
if (db) {
|
|
|
|
await db.destroy();
|
|
|
|
}
|
2019-10-03 15:01:33 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should require authenticated user', async () => {
|
|
|
|
expect.assertions(0);
|
2021-08-12 15:04:37 +02:00
|
|
|
const preHook = (app) => {
|
2017-11-16 16:45:01 +01:00
|
|
|
app.use('/api/admin/', (req, res) =>
|
|
|
|
res
|
2023-01-18 13:22:58 +01:00
|
|
|
.status(401)
|
2017-11-16 16:45:01 +01:00
|
|
|
.json(
|
|
|
|
new AuthenticationRequired({
|
2021-10-26 23:04:44 +02:00
|
|
|
path: '/auth/demo/login',
|
2017-11-16 16:45:01 +01:00
|
|
|
type: 'custom',
|
2021-02-23 06:20:10 +01:00
|
|
|
message: 'You have to identify yourself.',
|
2020-04-14 22:29:11 +02:00
|
|
|
}),
|
2017-11-16 16:45:01 +01:00
|
|
|
)
|
2020-04-14 22:29:11 +02:00
|
|
|
.end(),
|
2017-11-16 16:45:01 +01:00
|
|
|
);
|
|
|
|
};
|
2021-05-28 11:10:24 +02:00
|
|
|
const { request, destroy } = await setupAppWithCustomAuth(stores, preHook);
|
|
|
|
await request.get('/api/admin/features').expect(401);
|
|
|
|
await destroy();
|
2017-11-16 16:45:01 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('creates new feature toggle with createdBy', async () => {
|
|
|
|
expect.assertions(1);
|
2021-04-12 20:25:03 +02:00
|
|
|
const email = 'custom-user@mail.com';
|
2017-11-16 16:45:01 +01:00
|
|
|
|
2021-04-12 20:25:03 +02:00
|
|
|
const preHook = (app, config, { userService }) => {
|
|
|
|
app.use('/api/admin/', async (req, res, next) => {
|
|
|
|
req.user = await userService.loginUserWithoutPassword(email, true);
|
2017-11-16 16:45:01 +01:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
2021-05-28 11:10:24 +02:00
|
|
|
const { request, destroy } = await setupAppWithCustomAuth(stores, preHook);
|
2017-11-16 16:45:01 +01:00
|
|
|
|
|
|
|
// create toggle
|
2021-01-18 12:32:19 +01:00
|
|
|
await request
|
2023-04-26 10:45:00 +02:00
|
|
|
.post('/api/admin/projects/default/features')
|
2021-01-18 12:32:19 +01:00
|
|
|
.send({
|
|
|
|
name: 'com.test.Username',
|
|
|
|
enabled: false,
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
})
|
|
|
|
.expect(201);
|
2017-11-16 16:45:01 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
await request.get('/api/admin/events/com.test.Username').expect((res) => {
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(res.body.events[0].createdBy).toBe(email);
|
2019-10-03 15:01:33 +02:00
|
|
|
});
|
2021-05-28 11:10:24 +02:00
|
|
|
|
|
|
|
await destroy();
|
2017-11-16 16:45:01 +01:00
|
|
|
});
|