2022-09-26 12:06:30 +02:00
|
|
|
import { setupAppWithCustomAuth } from '../../helpers/test-helper';
|
|
|
|
import dbInit from '../../helpers/database-init';
|
|
|
|
import getLogger from '../../../fixtures/no-logger';
|
|
|
|
import { RoleName } from '../../../../lib/types/model';
|
|
|
|
import { PublicSignupTokenCreateSchema } from '../../../../lib/openapi/spec/public-signup-token-create-schema';
|
|
|
|
|
|
|
|
let stores;
|
|
|
|
let db;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
db = await dbInit('test', getLogger);
|
|
|
|
stores = db.stores;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await stores.publicSignupTokenStore.deleteAll();
|
|
|
|
await stores.eventStore.deleteAll();
|
|
|
|
await stores.userStore.deleteAll();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
if (db) {
|
|
|
|
await db.destroy();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const expireAt = (addDays: number = 7): Date => {
|
|
|
|
let now = new Date();
|
|
|
|
now.setDate(now.getDate() + addDays);
|
|
|
|
return now;
|
|
|
|
};
|
|
|
|
|
|
|
|
test('admin users should be able to create a token', async () => {
|
|
|
|
expect.assertions(3);
|
|
|
|
|
|
|
|
const preHook = (app, config, { userService, accessService }) => {
|
|
|
|
app.use('/api/admin/', async (req, res, next) => {
|
|
|
|
const role = await accessService.getRootRole(RoleName.ADMIN);
|
|
|
|
const user = await userService.createUser({
|
|
|
|
email: 'admin@example.com',
|
|
|
|
rootRole: role.id,
|
|
|
|
});
|
|
|
|
req.user = user;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const { request, destroy } = await setupAppWithCustomAuth(stores, preHook);
|
|
|
|
|
|
|
|
const tokenCreate: PublicSignupTokenCreateSchema = {
|
|
|
|
name: 'some-name',
|
|
|
|
expiresAt: expireAt().toISOString(),
|
|
|
|
};
|
|
|
|
|
|
|
|
await request
|
|
|
|
.post('/api/admin/invite-link/tokens')
|
|
|
|
.send(tokenCreate)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(201)
|
|
|
|
.expect((res) => {
|
|
|
|
expect(res.body.name).toBe('some-name');
|
|
|
|
expect(res.body.secret).not.toBeNull();
|
|
|
|
expect(res.body.url).not.toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
await destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('no permission to validate a token', async () => {
|
|
|
|
const preHook = (app, config, { userService, accessService }) => {
|
|
|
|
app.use('/api/admin/', async (req, res, next) => {
|
|
|
|
const admin = await accessService.getRootRole(RoleName.ADMIN);
|
|
|
|
await userService.createUser({
|
|
|
|
email: 'admin@example.com',
|
|
|
|
username: 'admin@example.com',
|
|
|
|
rootRole: admin.id,
|
|
|
|
});
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const { request, destroy } = await setupAppWithCustomAuth(stores, preHook);
|
|
|
|
|
|
|
|
await stores.publicSignupTokenStore.insert({
|
|
|
|
name: 'some-name',
|
|
|
|
expiresAt: expireAt(),
|
|
|
|
secret: 'some-secret',
|
|
|
|
createAt: new Date(),
|
|
|
|
createdBy: 'admin@example.com',
|
|
|
|
roleId: 3,
|
|
|
|
});
|
2022-09-30 13:01:32 +02:00
|
|
|
await request.get('/invite/some-secret/validate').expect(200);
|
2022-09-26 12:06:30 +02:00
|
|
|
|
|
|
|
await destroy();
|
|
|
|
});
|
|
|
|
|
2022-09-30 13:01:32 +02:00
|
|
|
test('should return 400 if token can not be validate', async () => {
|
2022-09-26 12:06:30 +02:00
|
|
|
const preHook = (app, config, { userService, accessService }) => {
|
|
|
|
app.use('/api/admin/', async (req, res, next) => {
|
|
|
|
const admin = await accessService.getRootRole(RoleName.ADMIN);
|
|
|
|
await userService.createUser({
|
|
|
|
email: 'admin@example.com',
|
|
|
|
username: 'admin@example.com',
|
|
|
|
rootRole: admin.id,
|
|
|
|
});
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const { request, destroy } = await setupAppWithCustomAuth(stores, preHook);
|
|
|
|
|
2022-09-30 13:01:32 +02:00
|
|
|
await request.get('/invite/some-invalid-secret/validate').expect(400);
|
2022-09-26 12:06:30 +02:00
|
|
|
|
|
|
|
await destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('users can signup with invite-link', async () => {
|
|
|
|
expect.assertions(1);
|
|
|
|
|
|
|
|
const preHook = (app, config, { userService, accessService }) => {
|
|
|
|
app.use('/api/admin/', async (req, res, next) => {
|
|
|
|
const admin = await accessService.getRootRole(RoleName.ADMIN);
|
|
|
|
await userService.createUser({
|
|
|
|
email: 'admin@example.com',
|
|
|
|
username: 'admin@example.com',
|
|
|
|
rootRole: admin.id,
|
|
|
|
});
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const { request, destroy } = await setupAppWithCustomAuth(stores, preHook);
|
|
|
|
|
|
|
|
await stores.publicSignupTokenStore.insert({
|
|
|
|
name: 'some-name',
|
|
|
|
expiresAt: expireAt(),
|
|
|
|
secret: 'some-secret',
|
2022-09-30 13:01:32 +02:00
|
|
|
url: 'http://localhost:4242/invite/some-secret/signup',
|
2022-09-26 12:06:30 +02:00
|
|
|
createAt: new Date(),
|
|
|
|
createdBy: 'admin@example.com',
|
|
|
|
roleId: 3,
|
|
|
|
});
|
|
|
|
|
2022-09-30 13:01:32 +02:00
|
|
|
const createUser = {
|
|
|
|
name: 'some-username',
|
2022-09-26 12:06:30 +02:00
|
|
|
email: 'some@example.com',
|
|
|
|
password: 'eweggwEG',
|
|
|
|
};
|
|
|
|
|
|
|
|
await request
|
2022-09-30 13:01:32 +02:00
|
|
|
.post('/invite/some-secret/signup')
|
2022-09-26 12:06:30 +02:00
|
|
|
.send(createUser)
|
|
|
|
.expect(201)
|
|
|
|
.expect((res) => {
|
|
|
|
const user = res.body;
|
2022-09-30 13:01:32 +02:00
|
|
|
expect(user.name).toBe('some-username');
|
2022-09-26 12:06:30 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
await destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('can get a token with users', async () => {
|
|
|
|
expect.assertions(1);
|
|
|
|
|
|
|
|
const preHook = (app, config, { userService, accessService }) => {
|
|
|
|
app.use('/api/admin/', async (req, res, next) => {
|
|
|
|
const role = await accessService.getRootRole(RoleName.ADMIN);
|
|
|
|
const user = await userService.createUser({
|
|
|
|
email: 'admin@example.com',
|
|
|
|
rootRole: role.id,
|
|
|
|
});
|
|
|
|
req.user = user;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const { request, destroy } = await setupAppWithCustomAuth(stores, preHook);
|
|
|
|
|
|
|
|
await stores.publicSignupTokenStore.insert({
|
|
|
|
name: 'some-name',
|
|
|
|
expiresAt: expireAt(),
|
|
|
|
secret: 'some-secret',
|
|
|
|
createAt: new Date(),
|
|
|
|
createdBy: 'admin@example.com',
|
|
|
|
roleId: 3,
|
|
|
|
});
|
|
|
|
|
|
|
|
const user = await stores.userStore.insert({
|
|
|
|
username: 'some-username',
|
|
|
|
email: 'some@example.com',
|
|
|
|
password: 'eweggwEG',
|
|
|
|
sendEmail: false,
|
|
|
|
rootRole: 3,
|
|
|
|
});
|
|
|
|
|
|
|
|
await stores.publicSignupTokenStore.addTokenUser('some-secret', user.id);
|
|
|
|
|
|
|
|
await request
|
|
|
|
.get('/api/admin/invite-link/tokens/some-secret')
|
|
|
|
.expect(200)
|
|
|
|
.expect((res) => {
|
|
|
|
const token = res.body;
|
|
|
|
expect(token.users.length).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
await destroy();
|
|
|
|
});
|
2022-11-01 10:38:18 +01:00
|
|
|
|
|
|
|
test('should not be able to set expiry further than 1 month', async () => {
|
|
|
|
const preHook = (app, config, { userService, accessService }) => {
|
|
|
|
app.use('/api/admin/', async (req, res, next) => {
|
|
|
|
const role = await accessService.getRootRole(RoleName.ADMIN);
|
|
|
|
const user = await userService.createUser({
|
|
|
|
email: 'admin@example.com',
|
|
|
|
rootRole: role.id,
|
|
|
|
});
|
|
|
|
req.user = user;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const { request, destroy } = await setupAppWithCustomAuth(stores, preHook);
|
|
|
|
|
|
|
|
const tokenCreate: PublicSignupTokenCreateSchema = {
|
|
|
|
name: 'some-name',
|
|
|
|
expiresAt: expireAt(100).toISOString(),
|
|
|
|
};
|
|
|
|
|
|
|
|
await request
|
|
|
|
.post('/api/admin/invite-link/tokens')
|
|
|
|
.send(tokenCreate)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(201)
|
|
|
|
.expect((res) => {
|
|
|
|
expect(new Date(res.body.expiresAt).getTime()).toBeLessThan(
|
|
|
|
expireAt(31).getTime(),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
await destroy();
|
|
|
|
});
|