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';
|
2021-09-21 13:48:28 +00:00
|
|
|
import { authHeaderForUser, clearDB, createUser, createNestAppInstance } from '../test.helper';
|
2021-12-30 20:41:10 +00:00
|
|
|
import Preview from 'twilio/lib/rest/Preview';
|
2022-01-06 10:38:18 +00:00
|
|
|
import { response } from 'express';
|
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;
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await request(app.getHttpServer())
|
2021-12-10 03:13:05 +00:00
|
|
|
.post(`/api/organization_users/`)
|
2021-09-03 04:22:07 +00:00
|
|
|
.set('Authorization', authHeaderForUser(adminUserData.user))
|
2021-10-11 15:15:58 +00:00
|
|
|
.send({ email: 'test@tooljet.io', groups: ['Viewer', 'all_users'] })
|
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/`)
|
2021-09-03 04:22:07 +00:00
|
|
|
.set('Authorization', authHeaderForUser(developerUserData.user))
|
2021-10-11 15:15:58 +00:00
|
|
|
.send({ email: 'test2@tooljet.io', groups: ['Viewer', 'all_users'] })
|
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/`)
|
2021-09-03 04:22:07 +00:00
|
|
|
.set('Authorization', authHeaderForUser(viewerUserData.user))
|
2021-10-11 15:15:58 +00:00
|
|
|
.send({ email: 'test3@tooljet.io', groups: ['Viewer', 'all_users'] })
|
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',
|
|
|
|
|
});
|
|
|
|
|
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/`)
|
|
|
|
|
.set('Authorization', authHeaderForUser(adminUserData.user))
|
|
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
|
.post(`/api/organization_users/${adminUserData.orgUser.id}/archive/`)
|
|
|
|
|
.set('Authorization', authHeaderForUser(adminUserData.user));
|
|
|
|
|
|
|
|
|
|
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'],
|
|
|
|
|
status: 'active',
|
|
|
|
|
});
|
|
|
|
|
const organization = adminUserData.organization;
|
|
|
|
|
const developerUserData = await createUser(app, {
|
|
|
|
|
email: 'developer@tooljet.io',
|
|
|
|
|
groups: ['developer', 'all_users'],
|
|
|
|
|
organization,
|
|
|
|
|
});
|
|
|
|
|
const viewerUserData = await createUser(app, {
|
|
|
|
|
email: 'viewer@tooljet.io',
|
|
|
|
|
groups: ['viewer', 'all_users'],
|
|
|
|
|
organization,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
|
.post(`/api/organization_users/${viewerUserData.orgUser.id}/archive/`)
|
|
|
|
|
.set('Authorization', authHeaderForUser(developerUserData.user))
|
|
|
|
|
.expect(403);
|
|
|
|
|
|
|
|
|
|
await viewerUserData.orgUser.reload();
|
|
|
|
|
expect(viewerUserData.orgUser.status).toBe('invited');
|
|
|
|
|
|
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
|
.post(`/api/organization_users/${viewerUserData.orgUser.id}/archive/`)
|
|
|
|
|
.set('Authorization', authHeaderForUser(adminUserData.user))
|
|
|
|
|
.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'],
|
|
|
|
|
});
|
|
|
|
|
const organization = adminUserData.organization;
|
|
|
|
|
const developerUserData = await createUser(app, {
|
|
|
|
|
email: 'developer@tooljet.io',
|
|
|
|
|
status: 'active',
|
|
|
|
|
groups: ['developer', 'all_users'],
|
|
|
|
|
organization,
|
|
|
|
|
});
|
|
|
|
|
const viewerUserData = await createUser(app, {
|
|
|
|
|
email: 'viewer@tooljet.io',
|
|
|
|
|
status: 'archived',
|
|
|
|
|
invitationToken: 'old-token',
|
|
|
|
|
password: 'old-password',
|
|
|
|
|
groups: ['viewer', 'all_users'],
|
|
|
|
|
organization,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
|
.post(`/api/organization_users/${viewerUserData.orgUser.id}/unarchive/`)
|
2022-01-07 09:16:23 +00:00
|
|
|
.set('Authorization', authHeaderForUser(developerUserData.user))
|
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/`)
|
|
|
|
|
.set('Authorization', authHeaderForUser(developerUserData.user))
|
|
|
|
|
.expect(403);
|
|
|
|
|
|
|
|
|
|
await viewerUserData.orgUser.reload();
|
|
|
|
|
expect(viewerUserData.orgUser.status).toBe('archived');
|
|
|
|
|
|
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
|
.post(`/api/organization_users/${viewerUserData.orgUser.id}/unarchive/`)
|
|
|
|
|
.set('Authorization', authHeaderForUser(adminUserData.user))
|
|
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
|
|
await viewerUserData.orgUser.reload();
|
|
|
|
|
await viewerUserData.user.reload();
|
|
|
|
|
expect(viewerUserData.orgUser.status).toBe('invited');
|
|
|
|
|
expect(viewerUserData.user.invitationToken).not.toBe('old-token');
|
|
|
|
|
expect(viewerUserData.user.password).not.toBe('old-password');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should allow unarchive if user is already 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: 'active',
|
|
|
|
|
groups: ['developer', 'all_users'],
|
|
|
|
|
organization,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
|
.post(`/api/organization_users/${developerUserData.orgUser.id}/unarchive/`)
|
|
|
|
|
.set('Authorization', authHeaderForUser(adminUserData.user))
|
|
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
|
|
await developerUserData.orgUser.reload();
|
|
|
|
|
expect(developerUserData.orgUser.status).toBe('active');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-20 11:25:51 +00:00
|
|
|
afterAll(async () => {
|
|
|
|
|
await app.close();
|
|
|
|
|
});
|
2021-07-22 07:25:29 +00:00
|
|
|
});
|