2021-09-21 13:48:28 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
2021-07-20 11:25:51 +00:00
|
|
|
import * as request from 'supertest';
|
2022-01-06 10:38:18 +00:00
|
|
|
import { BadRequestException, INestApplication } from '@nestjs/common';
|
2023-04-06 11:12:58 +00:00
|
|
|
import { authHeaderForUser, clearDB, createUser, createNestAppInstance, authenticateUser } from '../test.helper';
|
2021-07-20 11:25:51 +00:00
|
|
|
|
|
|
|
|
describe('organization users controller', () => {
|
|
|
|
|
let app: INestApplication;
|
|
|
|
|
|
2021-07-22 07:25:29 +00:00
|
|
|
beforeEach(async () => {
|
|
|
|
|
await clearDB();
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-20 11:25:51 +00:00
|
|
|
beforeAll(async () => {
|
2021-07-22 14:22:14 +00:00
|
|
|
app = await createNestAppInstance();
|
2021-07-20 11:25:51 +00:00
|
|
|
});
|
|
|
|
|
|
2021-09-03 04:22:07 +00:00
|
|
|
it('should allow only admin to be able to invite new users', async () => {
|
|
|
|
|
// setup a pre existing user of different organization
|
2022-01-06 10:38:18 +00:00
|
|
|
await createUser(app, {
|
|
|
|
|
email: 'someUser@tooljet.io',
|
|
|
|
|
groups: ['admin', 'all_users'],
|
|
|
|
|
});
|
2021-09-03 04:22:07 +00:00
|
|
|
|
|
|
|
|
// setup organization and user setup to test against
|
|
|
|
|
const adminUserData = await createUser(app, {
|
|
|
|
|
email: 'admin@tooljet.io',
|
2021-10-11 15:15:58 +00:00
|
|
|
groups: ['admin', 'all_users'],
|
2021-09-03 04:22:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const organization = adminUserData.organization;
|
|
|
|
|
|
2023-04-06 11:12:58 +00:00
|
|
|
let loggedUser = await authenticateUser(app);
|
|
|
|
|
adminUserData['tokenCookie'] = loggedUser.tokenCookie;
|
|
|
|
|
|
2021-09-03 04:22:07 +00:00
|
|
|
const developerUserData = await createUser(app, {
|
|
|
|
|
email: 'developer@tooljet.io',
|
2021-10-11 15:15:58 +00:00
|
|
|
groups: ['developer', 'all_users'],
|
2021-09-03 04:22:07 +00:00
|
|
|
organization,
|
|
|
|
|
});
|
|
|
|
|
|
2023-04-06 11:12:58 +00:00
|
|
|
loggedUser = await authenticateUser(app, 'developer@tooljet.io');
|
|
|
|
|
developerUserData['tokenCookie'] = loggedUser.tokenCookie;
|
|
|
|
|
|
2021-09-03 04:22:07 +00:00
|
|
|
const viewerUserData = await createUser(app, {
|
|
|
|
|
email: 'viewer@tooljet.io',
|
2021-10-11 15:15:58 +00:00
|
|
|
groups: ['viewer', 'all_users'],
|
2021-09-03 04:22:07 +00:00
|
|
|
organization,
|
|
|
|
|
});
|
|
|
|
|
|
2023-04-06 11:12:58 +00:00
|
|
|
loggedUser = await authenticateUser(app, 'viewer@tooljet.io');
|
|
|
|
|
viewerUserData['tokenCookie'] = loggedUser.tokenCookie;
|
|
|
|
|
|
2021-09-03 04:22:07 +00:00
|
|
|
await request(app.getHttpServer())
|
2021-12-10 03:13:05 +00:00
|
|
|
.post(`/api/organization_users/`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', adminUserData.user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', adminUserData['tokenCookie'])
|
2022-05-05 07:08:42 +00:00
|
|
|
.send({ email: 'test@tooljet.io' })
|
2021-09-03 04:22:07 +00:00
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
|
|
await request(app.getHttpServer())
|
2021-12-10 03:13:05 +00:00
|
|
|
.post(`/api/organization_users/`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', developerUserData.user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', developerUserData['tokenCookie'])
|
2022-05-05 07:08:42 +00:00
|
|
|
.send({ email: 'test2@tooljet.io' })
|
2021-09-03 04:22:07 +00:00
|
|
|
.expect(403);
|
|
|
|
|
|
|
|
|
|
await request(app.getHttpServer())
|
2021-12-10 03:13:05 +00:00
|
|
|
.post(`/api/organization_users/`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', viewerUserData.user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', viewerUserData['tokenCookie'])
|
2022-05-05 07:08:42 +00:00
|
|
|
.send({ email: 'test3@tooljet.io' })
|
2021-09-03 04:22:07 +00:00
|
|
|
.expect(403);
|
|
|
|
|
});
|
|
|
|
|
|
2022-01-06 10:38:18 +00:00
|
|
|
describe('POST /api/organization_users/:id/archive', () => {
|
|
|
|
|
it('should allow only authenticated users to archive org users', async () => {
|
|
|
|
|
await request(app.getHttpServer()).post('/api/organization_users/random-id/archive/').expect(401);
|
2021-09-03 04:22:07 +00:00
|
|
|
});
|
2021-07-22 07:25:29 +00:00
|
|
|
|
2022-01-06 10:38:18 +00:00
|
|
|
it('should throw error when trying to remove last active admin', async () => {
|
|
|
|
|
const adminUserData = await createUser(app, {
|
|
|
|
|
email: 'admin@tooljet.io',
|
|
|
|
|
groups: ['admin', 'all_users'],
|
|
|
|
|
status: 'active',
|
|
|
|
|
});
|
2023-04-06 11:12:58 +00:00
|
|
|
|
|
|
|
|
const loggedUser = await authenticateUser(app);
|
|
|
|
|
adminUserData['tokenCookie'] = loggedUser.tokenCookie;
|
|
|
|
|
|
2022-01-06 10:38:18 +00:00
|
|
|
const organization = adminUserData.organization;
|
|
|
|
|
const anotherAdminUserData = await createUser(app, {
|
|
|
|
|
email: 'another-admin@tooljet.io',
|
|
|
|
|
groups: ['admin', 'all_users'],
|
|
|
|
|
status: 'active',
|
|
|
|
|
organization,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const _archivedAdmin = await createUser(app, {
|
|
|
|
|
email: 'archived-admin@tooljet.io',
|
|
|
|
|
groups: ['admin', 'all_users'],
|
|
|
|
|
status: 'archived',
|
|
|
|
|
organization,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
|
.post(`/api/organization_users/${anotherAdminUserData.orgUser.id}/archive/`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', adminUserData.user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', adminUserData['tokenCookie'])
|
2022-01-06 10:38:18 +00:00
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
|
.post(`/api/organization_users/${adminUserData.orgUser.id}/archive/`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', adminUserData.user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', adminUserData['tokenCookie']);
|
2022-01-06 10:38:18 +00:00
|
|
|
|
|
|
|
|
expect(response.statusCode).toEqual(400);
|
|
|
|
|
expect(response.body.message).toEqual('Atleast one active admin is required.');
|
|
|
|
|
});
|
2021-07-22 07:25:29 +00:00
|
|
|
|
2022-01-06 10:38:18 +00:00
|
|
|
it('should allow only admin users to archive org users', async () => {
|
|
|
|
|
const adminUserData = await createUser(app, {
|
|
|
|
|
email: 'admin@tooljet.io',
|
|
|
|
|
groups: ['admin', 'all_users'],
|
|
|
|
|
});
|
2023-04-06 11:12:58 +00:00
|
|
|
|
|
|
|
|
let loggedUser = await authenticateUser(app);
|
|
|
|
|
adminUserData['tokenCookie'] = loggedUser.tokenCookie;
|
|
|
|
|
|
2022-01-06 10:38:18 +00:00
|
|
|
const organization = adminUserData.organization;
|
|
|
|
|
const developerUserData = await createUser(app, {
|
|
|
|
|
email: 'developer@tooljet.io',
|
|
|
|
|
groups: ['developer', 'all_users'],
|
|
|
|
|
organization,
|
|
|
|
|
});
|
2023-04-06 11:12:58 +00:00
|
|
|
|
|
|
|
|
loggedUser = await authenticateUser(app, 'developer@tooljet.io');
|
|
|
|
|
developerUserData['tokenCookie'] = loggedUser.tokenCookie;
|
|
|
|
|
|
2022-01-06 10:38:18 +00:00
|
|
|
const viewerUserData = await createUser(app, {
|
|
|
|
|
email: 'viewer@tooljet.io',
|
|
|
|
|
groups: ['viewer', 'all_users'],
|
|
|
|
|
organization,
|
2022-05-05 07:08:42 +00:00
|
|
|
status: 'invited',
|
2022-01-06 10:38:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
|
.post(`/api/organization_users/${viewerUserData.orgUser.id}/archive/`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', developerUserData.user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', developerUserData['tokenCookie'])
|
2022-01-06 10:38:18 +00:00
|
|
|
.expect(403);
|
|
|
|
|
|
|
|
|
|
await viewerUserData.orgUser.reload();
|
|
|
|
|
expect(viewerUserData.orgUser.status).toBe('invited');
|
|
|
|
|
|
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
|
.post(`/api/organization_users/${viewerUserData.orgUser.id}/archive/`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', adminUserData.user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', adminUserData['tokenCookie'])
|
2022-01-06 10:38:18 +00:00
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
|
|
await viewerUserData.orgUser.reload();
|
|
|
|
|
expect(viewerUserData.orgUser.status).toBe('archived');
|
|
|
|
|
});
|
2021-07-22 07:25:29 +00:00
|
|
|
});
|
|
|
|
|
|
2021-12-30 20:41:10 +00:00
|
|
|
describe('POST /api/organization_users/:id/unarchive', () => {
|
|
|
|
|
it('should allow only authenticated users to unarchive org users', async () => {
|
|
|
|
|
await request(app.getHttpServer()).post('/api/organization_users/random-id/unarchive/').expect(401);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should allow only admin users to unarchive org users', async () => {
|
|
|
|
|
const adminUserData = await createUser(app, {
|
|
|
|
|
email: 'admin@tooljet.io',
|
|
|
|
|
status: 'active',
|
|
|
|
|
groups: ['admin', 'all_users'],
|
|
|
|
|
});
|
2023-04-06 11:12:58 +00:00
|
|
|
|
|
|
|
|
let loggedUser = await authenticateUser(app);
|
|
|
|
|
adminUserData['tokenCookie'] = loggedUser.tokenCookie;
|
|
|
|
|
|
2021-12-30 20:41:10 +00:00
|
|
|
const organization = adminUserData.organization;
|
|
|
|
|
const developerUserData = await createUser(app, {
|
|
|
|
|
email: 'developer@tooljet.io',
|
|
|
|
|
status: 'active',
|
|
|
|
|
groups: ['developer', 'all_users'],
|
|
|
|
|
organization,
|
|
|
|
|
});
|
2023-04-06 11:12:58 +00:00
|
|
|
|
|
|
|
|
loggedUser = await authenticateUser(app, 'developer@tooljet.io');
|
|
|
|
|
developerUserData['tokenCookie'] = loggedUser.tokenCookie;
|
|
|
|
|
|
2021-12-30 20:41:10 +00:00
|
|
|
const viewerUserData = await createUser(app, {
|
|
|
|
|
email: 'viewer@tooljet.io',
|
|
|
|
|
status: 'archived',
|
|
|
|
|
groups: ['viewer', 'all_users'],
|
|
|
|
|
organization,
|
|
|
|
|
});
|
|
|
|
|
|
2023-04-06 11:12:58 +00:00
|
|
|
loggedUser = await authenticateUser(app, 'viewer@tooljet.io');
|
|
|
|
|
viewerUserData['tokenCookie'] = loggedUser.tokenCookie;
|
|
|
|
|
|
2021-12-30 20:41:10 +00:00
|
|
|
await request(app.getHttpServer())
|
|
|
|
|
.post(`/api/organization_users/${viewerUserData.orgUser.id}/unarchive/`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', developerUserData.user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', developerUserData['tokenCookie'])
|
2021-12-30 20:41:10 +00:00
|
|
|
.expect(403);
|
|
|
|
|
|
|
|
|
|
await viewerUserData.orgUser.reload();
|
|
|
|
|
expect(viewerUserData.orgUser.status).toBe('archived');
|
|
|
|
|
|
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
|
.post(`/api/organization_users/${viewerUserData.orgUser.id}/unarchive/`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', developerUserData.user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', developerUserData['tokenCookie'])
|
2021-12-30 20:41:10 +00:00
|
|
|
.expect(403);
|
|
|
|
|
|
|
|
|
|
await viewerUserData.orgUser.reload();
|
|
|
|
|
expect(viewerUserData.orgUser.status).toBe('archived');
|
|
|
|
|
|
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
|
.post(`/api/organization_users/${viewerUserData.orgUser.id}/unarchive/`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', adminUserData.user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', adminUserData['tokenCookie'])
|
2021-12-30 20:41:10 +00:00
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
|
|
await viewerUserData.orgUser.reload();
|
|
|
|
|
await viewerUserData.user.reload();
|
|
|
|
|
expect(viewerUserData.orgUser.status).toBe('invited');
|
2022-05-05 07:08:42 +00:00
|
|
|
expect(viewerUserData.user.invitationToken).not.toBe('');
|
2021-12-30 20:41:10 +00:00
|
|
|
expect(viewerUserData.user.password).not.toBe('old-password');
|
|
|
|
|
});
|
|
|
|
|
|
2022-06-17 11:39:13 +00:00
|
|
|
it('should not allow unarchive if user status is not archived', async () => {
|
2021-12-30 20:41:10 +00:00
|
|
|
const adminUserData = await createUser(app, {
|
|
|
|
|
email: 'admin@tooljet.io',
|
|
|
|
|
status: 'active',
|
|
|
|
|
groups: ['admin', 'all_users'],
|
|
|
|
|
});
|
2023-04-06 11:12:58 +00:00
|
|
|
|
|
|
|
|
const loggedUser = await authenticateUser(app);
|
|
|
|
|
adminUserData['tokenCookie'] = loggedUser.tokenCookie;
|
|
|
|
|
|
2021-12-30 20:41:10 +00:00
|
|
|
const organization = adminUserData.organization;
|
|
|
|
|
const developerUserData = await createUser(app, {
|
|
|
|
|
email: 'developer@tooljet.io',
|
|
|
|
|
status: 'active',
|
|
|
|
|
groups: ['developer', 'all_users'],
|
|
|
|
|
organization,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
|
.post(`/api/organization_users/${developerUserData.orgUser.id}/unarchive/`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', adminUserData.user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', adminUserData['tokenCookie'])
|
2022-06-17 11:39:13 +00:00
|
|
|
.expect(400);
|
2021-12-30 20:41:10 +00:00
|
|
|
|
|
|
|
|
await developerUserData.orgUser.reload();
|
|
|
|
|
expect(developerUserData.orgUser.status).toBe('active');
|
|
|
|
|
});
|
2022-06-17 11:39:13 +00:00
|
|
|
|
|
|
|
|
it('should not allow unarchive if user status is not archived', async () => {
|
|
|
|
|
const adminUserData = await createUser(app, {
|
|
|
|
|
email: 'admin@tooljet.io',
|
|
|
|
|
status: 'active',
|
|
|
|
|
groups: ['admin', 'all_users'],
|
|
|
|
|
});
|
|
|
|
|
const organization = adminUserData.organization;
|
|
|
|
|
const developerUserData = await createUser(app, {
|
|
|
|
|
email: 'developer@tooljet.io',
|
|
|
|
|
status: 'invited',
|
|
|
|
|
groups: ['developer', 'all_users'],
|
|
|
|
|
organization,
|
|
|
|
|
});
|
|
|
|
|
|
2023-04-06 11:12:58 +00:00
|
|
|
const loggedUser = await authenticateUser(app);
|
|
|
|
|
adminUserData['tokenCookie'] = loggedUser.tokenCookie;
|
|
|
|
|
|
2022-06-17 11:39:13 +00:00
|
|
|
await request(app.getHttpServer())
|
|
|
|
|
.post(`/api/organization_users/${developerUserData.orgUser.id}/unarchive/`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', adminUserData.user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', adminUserData['tokenCookie'])
|
2022-06-17 11:39:13 +00:00
|
|
|
.expect(400);
|
|
|
|
|
|
|
|
|
|
await developerUserData.orgUser.reload();
|
|
|
|
|
expect(developerUserData.orgUser.status).toBe('invited');
|
|
|
|
|
});
|
2021-12-30 20:41:10 +00:00
|
|
|
});
|
|
|
|
|
|
2021-07-20 11:25:51 +00:00
|
|
|
afterAll(async () => {
|
|
|
|
|
await app.close();
|
|
|
|
|
});
|
2021-07-22 07:25:29 +00:00
|
|
|
});
|