2021-09-21 13:48:28 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
2021-07-08 05:40:27 +00:00
|
|
|
import * as request from 'supertest';
|
2021-07-08 16:43:23 +00:00
|
|
|
import { INestApplication } from '@nestjs/common';
|
|
|
|
|
import { Repository } from 'typeorm';
|
2021-07-12 10:36:53 +00:00
|
|
|
import { User } from 'src/entities/user.entity';
|
2022-01-07 09:16:23 +00:00
|
|
|
import { clearDB, createUser, createNestAppInstance, authHeaderForUser } from '../test.helper';
|
|
|
|
|
import { OrganizationUser } from 'src/entities/organization_user.entity';
|
2021-07-08 05:40:27 +00:00
|
|
|
|
2021-07-08 16:43:23 +00:00
|
|
|
describe('Authentication', () => {
|
2021-07-08 05:40:27 +00:00
|
|
|
let app: INestApplication;
|
2021-07-08 16:43:23 +00:00
|
|
|
let userRepository: Repository<User>;
|
2022-01-07 09:16:23 +00:00
|
|
|
let orgUserRepository: Repository<OrganizationUser>;
|
2021-12-15 17:23:07 +00:00
|
|
|
const originalEnv = process.env;
|
2021-07-22 07:25:29 +00:00
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
await clearDB();
|
2021-07-22 09:41:50 +00:00
|
|
|
await createUser(app, { email: 'admin@tooljet.io' });
|
2021-07-22 07:25:29 +00:00
|
|
|
});
|
2021-07-08 05:40:27 +00:00
|
|
|
|
2021-07-08 16:43:23 +00:00
|
|
|
beforeAll(async () => {
|
2021-07-22 14:22:14 +00:00
|
|
|
app = await createNestAppInstance();
|
2021-07-08 16:43:23 +00:00
|
|
|
|
|
|
|
|
userRepository = app.get('UserRepository');
|
2022-01-07 09:16:23 +00:00
|
|
|
orgUserRepository = app.get('OrganizationUserRepository');
|
2021-07-08 05:40:27 +00:00
|
|
|
});
|
|
|
|
|
|
2021-07-20 09:10:11 +00:00
|
|
|
it('should create new users', async () => {
|
2021-10-15 09:05:11 +00:00
|
|
|
const response = await request(app.getHttpServer()).post('/api/signup').send({ email: 'test@tooljet.io' });
|
2021-07-20 09:10:11 +00:00
|
|
|
expect(response.statusCode).toBe(201);
|
|
|
|
|
|
|
|
|
|
const id = response.body['id'];
|
2021-12-06 19:07:19 +00:00
|
|
|
const user = await userRepository.findOne(id, {
|
|
|
|
|
relations: ['organization'],
|
|
|
|
|
});
|
2021-07-20 09:10:11 +00:00
|
|
|
|
2021-12-06 19:07:19 +00:00
|
|
|
expect(user.organization.name).toBe('Untitled organization');
|
2021-10-11 15:15:58 +00:00
|
|
|
|
|
|
|
|
const groupPermissions = await user.groupPermissions;
|
|
|
|
|
const groupNames = groupPermissions.map((x) => x.group);
|
|
|
|
|
|
|
|
|
|
expect(new Set(['all_users', 'admin'])).toEqual(new Set(groupNames));
|
2021-12-06 19:07:19 +00:00
|
|
|
|
|
|
|
|
const adminGroup = groupPermissions.find((x) => x.group == 'admin');
|
|
|
|
|
expect(adminGroup.appCreate).toBeTruthy();
|
|
|
|
|
expect(adminGroup.appDelete).toBeTruthy();
|
|
|
|
|
expect(adminGroup.folderCreate).toBeTruthy();
|
|
|
|
|
|
|
|
|
|
const allUserGroup = groupPermissions.find((x) => x.group == 'all_users');
|
|
|
|
|
expect(allUserGroup.appCreate).toBeFalsy();
|
|
|
|
|
expect(allUserGroup.appDelete).toBeFalsy();
|
|
|
|
|
expect(allUserGroup.folderCreate).toBeFalsy();
|
2021-07-20 09:10:11 +00:00
|
|
|
});
|
|
|
|
|
|
2021-10-11 15:15:58 +00:00
|
|
|
it('authenticate if valid credentials', async () => {
|
|
|
|
|
await request(app.getHttpServer())
|
2021-10-15 09:05:11 +00:00
|
|
|
.post('/api/authenticate')
|
2021-07-22 07:25:29 +00:00
|
|
|
.send({ email: 'admin@tooljet.io', password: 'password' })
|
2021-09-21 13:48:28 +00:00
|
|
|
.expect(201);
|
2021-07-08 16:43:23 +00:00
|
|
|
});
|
|
|
|
|
|
2022-01-07 09:16:23 +00:00
|
|
|
it('throw 401 if user is archived', async () => {
|
|
|
|
|
await createUser(app, { email: 'user@tooljet.io', status: 'archived' });
|
|
|
|
|
|
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
|
.post('/api/authenticate')
|
|
|
|
|
.send({ email: 'user@tooljet.io', password: 'password' })
|
|
|
|
|
.expect(401);
|
|
|
|
|
|
|
|
|
|
const adminUser = await userRepository.findOne({ email: 'admin@tooljet.io' });
|
|
|
|
|
await orgUserRepository.update({ userId: adminUser.id }, { status: 'archived' });
|
|
|
|
|
|
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
|
.get('/api/organizations/users')
|
|
|
|
|
.set('Authorization', authHeaderForUser(adminUser))
|
|
|
|
|
.expect(401);
|
|
|
|
|
});
|
|
|
|
|
|
2021-10-11 15:15:58 +00:00
|
|
|
it('throw 401 if invalid credentials', async () => {
|
|
|
|
|
await request(app.getHttpServer())
|
2021-10-15 09:05:11 +00:00
|
|
|
.post('/api/authenticate')
|
2021-10-11 15:15:58 +00:00
|
|
|
.send({ email: 'amdin@tooljet.io', password: 'pwd' })
|
2021-09-21 13:48:28 +00:00
|
|
|
.expect(401);
|
2021-07-10 07:01:13 +00:00
|
|
|
});
|
|
|
|
|
|
2021-12-15 17:23:07 +00:00
|
|
|
describe('if password login is disabled', () => {
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
|
process.env = { ...originalEnv, DISABLE_PASSWORD_LOGIN: 'true' };
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not create new users', async () => {
|
|
|
|
|
const response = await request(app.getHttpServer()).post('/api/signup').send({ email: 'test@tooljet.io' });
|
|
|
|
|
expect(response.statusCode).toBe(403);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('does authenticate if valid credentials', async () => {
|
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
|
.post('/api/authenticate')
|
|
|
|
|
.send({ email: 'admin@tooljet.io', password: 'password' })
|
|
|
|
|
.expect(403);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
|
process.env = { ...originalEnv };
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-08 16:43:23 +00:00
|
|
|
afterAll(async () => {
|
|
|
|
|
await app.close();
|
2021-07-08 05:40:27 +00:00
|
|
|
});
|
2021-09-21 13:48:28 +00:00
|
|
|
});
|