ToolJet/server/test/controllers/organization_users.e2e-spec.ts

223 lines
7.8 KiB
TypeScript
Raw Normal View History

/* eslint-disable @typescript-eslint/no-unused-vars */
import * as request from 'supertest';
import { BadRequestException, INestApplication } from '@nestjs/common';
import { authHeaderForUser, clearDB, createUser, createNestAppInstance } from '../test.helper';
import Preview from 'twilio/lib/rest/Preview';
import { response } from 'express';
describe('organization users controller', () => {
let app: INestApplication;
beforeEach(async () => {
await clearDB();
});
beforeAll(async () => {
app = await createNestAppInstance();
});
it('should allow only admin to be able to invite new users', async () => {
// setup a pre existing user of different organization
await createUser(app, {
email: 'someUser@tooljet.io',
groups: ['admin', 'all_users'],
});
// setup organization and user setup to test against
const adminUserData = await createUser(app, {
email: 'admin@tooljet.io',
Feature: User access management 🔥 (#918) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * Feature: User access permission group usage (#883) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * revise migrations * hide roles usage * setup group permissions for apps and users * fix defaultChecked * fix update permission checkbox * fix casl ability check to have params passed * fix casl apps abilities to check with app specific permission * add ability to delete groups * conditionally render edit and delete options for all and admin users * fix user role to group migration * revise group management pages to disallow updating default group * move manage users and groups to navbar dropdown * show only addable apps and users on dropdowns * rename header as profile settings * scope addable apps and users by organization * scope viewable apps on homepage * hide manage groups link from non admins * make permissions to be used with radio input * add loading state for add apps/users buttons * revise unit tests * revise migrations * fix e2e tests * comment out dead code * fix seeds script * handle folder count * captalize error toast * hide manage users dropdown for non admins * show fobidden error on blank homepage * fix folder app count * fix invalid state set * make group name clickable for edit instead * users with edit permission can deploy apps * not show edit link on homepage if user dont have update permission * remove unused entity from merge * remove roles usage from manage org users page * fix folder count and blank slate on homepage * disable add buttons if there is no selections * humanize default groups on view * make app added onto groups have read permission by default * not show app menu if user is not admin * remove admin users from group user addition dropdown * create default permissions for app cloned * fix querying index page without page params * fix admin scoped out from group add * remove apps from header * fix invitation url not shown * scope admin deletion check by org * scope public apps by organization * add specs for group permissions e2e * removed unused entity and add group permissions spec * remove console logs * remove unused permission * scope public app count by org * remove console log * refactor manage group permission resources component * update group permssion in org scope
2021-10-11 15:15:58 +00:00
groups: ['admin', 'all_users'],
});
const organization = adminUserData.organization;
const developerUserData = await createUser(app, {
email: 'developer@tooljet.io',
Feature: User access management 🔥 (#918) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * Feature: User access permission group usage (#883) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * revise migrations * hide roles usage * setup group permissions for apps and users * fix defaultChecked * fix update permission checkbox * fix casl ability check to have params passed * fix casl apps abilities to check with app specific permission * add ability to delete groups * conditionally render edit and delete options for all and admin users * fix user role to group migration * revise group management pages to disallow updating default group * move manage users and groups to navbar dropdown * show only addable apps and users on dropdowns * rename header as profile settings * scope addable apps and users by organization * scope viewable apps on homepage * hide manage groups link from non admins * make permissions to be used with radio input * add loading state for add apps/users buttons * revise unit tests * revise migrations * fix e2e tests * comment out dead code * fix seeds script * handle folder count * captalize error toast * hide manage users dropdown for non admins * show fobidden error on blank homepage * fix folder app count * fix invalid state set * make group name clickable for edit instead * users with edit permission can deploy apps * not show edit link on homepage if user dont have update permission * remove unused entity from merge * remove roles usage from manage org users page * fix folder count and blank slate on homepage * disable add buttons if there is no selections * humanize default groups on view * make app added onto groups have read permission by default * not show app menu if user is not admin * remove admin users from group user addition dropdown * create default permissions for app cloned * fix querying index page without page params * fix admin scoped out from group add * remove apps from header * fix invitation url not shown * scope admin deletion check by org * scope public apps by organization * add specs for group permissions e2e * removed unused entity and add group permissions spec * remove console logs * remove unused permission * scope public app count by org * remove console log * refactor manage group permission resources component * update group permssion in org scope
2021-10-11 15:15:58 +00:00
groups: ['developer', 'all_users'],
organization,
});
const viewerUserData = await createUser(app, {
email: 'viewer@tooljet.io',
Feature: User access management 🔥 (#918) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * Feature: User access permission group usage (#883) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * revise migrations * hide roles usage * setup group permissions for apps and users * fix defaultChecked * fix update permission checkbox * fix casl ability check to have params passed * fix casl apps abilities to check with app specific permission * add ability to delete groups * conditionally render edit and delete options for all and admin users * fix user role to group migration * revise group management pages to disallow updating default group * move manage users and groups to navbar dropdown * show only addable apps and users on dropdowns * rename header as profile settings * scope addable apps and users by organization * scope viewable apps on homepage * hide manage groups link from non admins * make permissions to be used with radio input * add loading state for add apps/users buttons * revise unit tests * revise migrations * fix e2e tests * comment out dead code * fix seeds script * handle folder count * captalize error toast * hide manage users dropdown for non admins * show fobidden error on blank homepage * fix folder app count * fix invalid state set * make group name clickable for edit instead * users with edit permission can deploy apps * not show edit link on homepage if user dont have update permission * remove unused entity from merge * remove roles usage from manage org users page * fix folder count and blank slate on homepage * disable add buttons if there is no selections * humanize default groups on view * make app added onto groups have read permission by default * not show app menu if user is not admin * remove admin users from group user addition dropdown * create default permissions for app cloned * fix querying index page without page params * fix admin scoped out from group add * remove apps from header * fix invitation url not shown * scope admin deletion check by org * scope public apps by organization * add specs for group permissions e2e * removed unused entity and add group permissions spec * remove console logs * remove unused permission * scope public app count by org * remove console log * refactor manage group permission resources component * update group permssion in org scope
2021-10-11 15:15:58 +00:00
groups: ['viewer', 'all_users'],
organization,
});
await request(app.getHttpServer())
.post(`/api/organization_users/`)
.set('Authorization', authHeaderForUser(adminUserData.user))
Feature: User access management 🔥 (#918) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * Feature: User access permission group usage (#883) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * revise migrations * hide roles usage * setup group permissions for apps and users * fix defaultChecked * fix update permission checkbox * fix casl ability check to have params passed * fix casl apps abilities to check with app specific permission * add ability to delete groups * conditionally render edit and delete options for all and admin users * fix user role to group migration * revise group management pages to disallow updating default group * move manage users and groups to navbar dropdown * show only addable apps and users on dropdowns * rename header as profile settings * scope addable apps and users by organization * scope viewable apps on homepage * hide manage groups link from non admins * make permissions to be used with radio input * add loading state for add apps/users buttons * revise unit tests * revise migrations * fix e2e tests * comment out dead code * fix seeds script * handle folder count * captalize error toast * hide manage users dropdown for non admins * show fobidden error on blank homepage * fix folder app count * fix invalid state set * make group name clickable for edit instead * users with edit permission can deploy apps * not show edit link on homepage if user dont have update permission * remove unused entity from merge * remove roles usage from manage org users page * fix folder count and blank slate on homepage * disable add buttons if there is no selections * humanize default groups on view * make app added onto groups have read permission by default * not show app menu if user is not admin * remove admin users from group user addition dropdown * create default permissions for app cloned * fix querying index page without page params * fix admin scoped out from group add * remove apps from header * fix invitation url not shown * scope admin deletion check by org * scope public apps by organization * add specs for group permissions e2e * removed unused entity and add group permissions spec * remove console logs * remove unused permission * scope public app count by org * remove console log * refactor manage group permission resources component * update group permssion in org scope
2021-10-11 15:15:58 +00:00
.send({ email: 'test@tooljet.io', groups: ['Viewer', 'all_users'] })
.expect(201);
await request(app.getHttpServer())
.post(`/api/organization_users/`)
.set('Authorization', authHeaderForUser(developerUserData.user))
Feature: User access management 🔥 (#918) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * Feature: User access permission group usage (#883) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * revise migrations * hide roles usage * setup group permissions for apps and users * fix defaultChecked * fix update permission checkbox * fix casl ability check to have params passed * fix casl apps abilities to check with app specific permission * add ability to delete groups * conditionally render edit and delete options for all and admin users * fix user role to group migration * revise group management pages to disallow updating default group * move manage users and groups to navbar dropdown * show only addable apps and users on dropdowns * rename header as profile settings * scope addable apps and users by organization * scope viewable apps on homepage * hide manage groups link from non admins * make permissions to be used with radio input * add loading state for add apps/users buttons * revise unit tests * revise migrations * fix e2e tests * comment out dead code * fix seeds script * handle folder count * captalize error toast * hide manage users dropdown for non admins * show fobidden error on blank homepage * fix folder app count * fix invalid state set * make group name clickable for edit instead * users with edit permission can deploy apps * not show edit link on homepage if user dont have update permission * remove unused entity from merge * remove roles usage from manage org users page * fix folder count and blank slate on homepage * disable add buttons if there is no selections * humanize default groups on view * make app added onto groups have read permission by default * not show app menu if user is not admin * remove admin users from group user addition dropdown * create default permissions for app cloned * fix querying index page without page params * fix admin scoped out from group add * remove apps from header * fix invitation url not shown * scope admin deletion check by org * scope public apps by organization * add specs for group permissions e2e * removed unused entity and add group permissions spec * remove console logs * remove unused permission * scope public app count by org * remove console log * refactor manage group permission resources component * update group permssion in org scope
2021-10-11 15:15:58 +00:00
.send({ email: 'test2@tooljet.io', groups: ['Viewer', 'all_users'] })
.expect(403);
await request(app.getHttpServer())
.post(`/api/organization_users/`)
.set('Authorization', authHeaderForUser(viewerUserData.user))
Feature: User access management 🔥 (#918) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * Feature: User access permission group usage (#883) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * revise migrations * hide roles usage * setup group permissions for apps and users * fix defaultChecked * fix update permission checkbox * fix casl ability check to have params passed * fix casl apps abilities to check with app specific permission * add ability to delete groups * conditionally render edit and delete options for all and admin users * fix user role to group migration * revise group management pages to disallow updating default group * move manage users and groups to navbar dropdown * show only addable apps and users on dropdowns * rename header as profile settings * scope addable apps and users by organization * scope viewable apps on homepage * hide manage groups link from non admins * make permissions to be used with radio input * add loading state for add apps/users buttons * revise unit tests * revise migrations * fix e2e tests * comment out dead code * fix seeds script * handle folder count * captalize error toast * hide manage users dropdown for non admins * show fobidden error on blank homepage * fix folder app count * fix invalid state set * make group name clickable for edit instead * users with edit permission can deploy apps * not show edit link on homepage if user dont have update permission * remove unused entity from merge * remove roles usage from manage org users page * fix folder count and blank slate on homepage * disable add buttons if there is no selections * humanize default groups on view * make app added onto groups have read permission by default * not show app menu if user is not admin * remove admin users from group user addition dropdown * create default permissions for app cloned * fix querying index page without page params * fix admin scoped out from group add * remove apps from header * fix invitation url not shown * scope admin deletion check by org * scope public apps by organization * add specs for group permissions e2e * removed unused entity and add group permissions spec * remove console logs * remove unused permission * scope public app count by org * remove console log * refactor manage group permission resources component * update group permssion in org scope
2021-10-11 15:15:58 +00:00
.send({ email: 'test3@tooljet.io', groups: ['Viewer', 'all_users'] })
.expect(403);
});
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);
});
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.');
});
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');
});
});
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/`)
.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(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');
});
});
afterAll(async () => {
await app.close();
});
});