2021-07-23 06:43:50 +00:00
|
|
|
import * as request from 'supertest';
|
|
|
|
|
import { INestApplication } from '@nestjs/common';
|
2023-01-09 12:00:32 +00:00
|
|
|
import {
|
|
|
|
|
clearDB,
|
|
|
|
|
createApplication,
|
|
|
|
|
createUser,
|
|
|
|
|
createNestAppInstance,
|
|
|
|
|
generateAppDefaults,
|
2023-04-06 11:12:58 +00:00
|
|
|
authenticateUser,
|
|
|
|
|
logoutUser,
|
2023-01-09 12:00:32 +00:00
|
|
|
} from '../test.helper';
|
2021-07-23 06:43:50 +00:00
|
|
|
|
|
|
|
|
describe('app_users controller', () => {
|
|
|
|
|
let app: INestApplication;
|
|
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
await clearDB();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
|
app = await createNestAppInstance();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should allow only authenticated users to create new app users', async () => {
|
2021-10-15 09:05:11 +00:00
|
|
|
await request(app.getHttpServer()).post('/api/app_users').expect(401);
|
2021-07-23 06:43:50 +00:00
|
|
|
});
|
|
|
|
|
|
2023-01-09 12:00:32 +00:00
|
|
|
it('should be able to create a new app user if admin of same organization', async () => {
|
2021-10-11 15:15:58 +00:00
|
|
|
const adminUserData = await createUser(app, {
|
|
|
|
|
email: 'admin@tooljet.io',
|
|
|
|
|
groups: ['all_users', 'admin'],
|
|
|
|
|
});
|
2023-04-06 11:12:58 +00:00
|
|
|
|
|
|
|
|
const loggedUser = await authenticateUser(app);
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const developerUserData = await createUser(app, {
|
|
|
|
|
email: 'dev@tooljet.io',
|
2021-10-11 15:15:58 +00:00
|
|
|
groups: ['all_users', 'developer'],
|
2021-09-21 13:48:28 +00:00
|
|
|
organization: adminUserData.organization,
|
|
|
|
|
});
|
2023-01-09 12:00:32 +00:00
|
|
|
const { application } = await generateAppDefaults(app, adminUserData.user, {});
|
2021-07-23 06:43:50 +00:00
|
|
|
|
|
|
|
|
const response = await request(app.getHttpServer())
|
2021-10-15 09:05:11 +00:00
|
|
|
.post(`/api/app_users`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', adminUserData.user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', loggedUser.tokenCookie)
|
2021-09-21 13:48:28 +00:00
|
|
|
.send({
|
2021-07-23 06:43:50 +00:00
|
|
|
app_id: application.id,
|
|
|
|
|
org_user_id: developerUserData.orgUser.id,
|
2021-10-11 15:15:58 +00:00
|
|
|
groups: ['all_users', 'admin'],
|
2023-01-09 12:00:32 +00:00
|
|
|
role: '',
|
2021-07-23 06:43:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(response.statusCode).toBe(201);
|
2023-04-06 11:12:58 +00:00
|
|
|
|
|
|
|
|
await logoutUser(app, loggedUser.tokenCookie, adminUserData.user.defaultOrganizationId);
|
2021-07-23 06:43:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not be able to create new app user if admin of another organization', async () => {
|
2021-10-11 15:15:58 +00:00
|
|
|
const adminUserData = await createUser(app, {
|
|
|
|
|
email: 'admin@tooljet.io',
|
|
|
|
|
groups: ['all_users', 'admin'],
|
|
|
|
|
});
|
2021-09-21 13:48:28 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
|
const developerUserData = await createUser(app, {
|
|
|
|
|
email: 'dev@tooljet.io',
|
2021-10-11 15:15:58 +00:00
|
|
|
groups: ['all_users', 'developer'],
|
2021-09-21 13:48:28 +00:00
|
|
|
organization: adminUserData.organization,
|
|
|
|
|
});
|
2021-10-11 15:15:58 +00:00
|
|
|
const anotherOrgAdminUserData = await createUser(app, {
|
|
|
|
|
email: 'another@tooljet.io',
|
|
|
|
|
groups: ['all_users', 'admin'],
|
|
|
|
|
});
|
|
|
|
|
const application = await createApplication(app, {
|
|
|
|
|
name: 'name',
|
|
|
|
|
user: adminUserData.user,
|
|
|
|
|
});
|
2021-07-23 06:43:50 +00:00
|
|
|
|
2023-04-06 11:12:58 +00:00
|
|
|
const loggedUser = await authenticateUser(app, 'another@tooljet.io');
|
|
|
|
|
|
2021-07-23 06:43:50 +00:00
|
|
|
const response = await request(app.getHttpServer())
|
2021-10-15 09:05:11 +00:00
|
|
|
.post(`/api/app_users`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', anotherOrgAdminUserData.user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', loggedUser.tokenCookie)
|
2021-09-21 13:48:28 +00:00
|
|
|
.send({
|
2021-07-23 06:43:50 +00:00
|
|
|
app_id: application.id,
|
|
|
|
|
org_user_id: adminUserData.orgUser.id,
|
2021-10-11 15:15:58 +00:00
|
|
|
groups: ['all_users', 'admin'],
|
2021-07-23 06:43:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(response.statusCode).toBe(403);
|
2023-04-06 11:12:58 +00:00
|
|
|
|
|
|
|
|
await logoutUser(app, loggedUser.tokenCookie, anotherOrgAdminUserData.user.defaultOrganizationId);
|
2021-07-23 06:43:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not allow developers and viewers to create app users', async () => {
|
2021-10-11 15:15:58 +00:00
|
|
|
const adminUserData = await createUser(app, {
|
|
|
|
|
email: 'admin@tooljet.io',
|
|
|
|
|
groups: ['all_users', 'admin'],
|
|
|
|
|
});
|
|
|
|
|
const application = await createApplication(app, {
|
|
|
|
|
name: 'name',
|
|
|
|
|
user: adminUserData.user,
|
|
|
|
|
});
|
2021-07-23 06:43:50 +00:00
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const developerUserData = await createUser(app, {
|
|
|
|
|
email: 'dev@tooljet.io',
|
2021-10-11 15:15:58 +00:00
|
|
|
groups: ['all_users', 'developer'],
|
2021-09-21 13:48:28 +00:00
|
|
|
organization: adminUserData.organization,
|
|
|
|
|
});
|
|
|
|
|
const viewerUserData = await createUser(app, {
|
|
|
|
|
email: 'viewer@tooljet.io',
|
2021-10-11 15:15:58 +00:00
|
|
|
groups: ['all_users', 'viewer'],
|
2021-09-21 13:48:28 +00:00
|
|
|
organization: adminUserData.organization,
|
|
|
|
|
});
|
2021-07-23 06:43:50 +00:00
|
|
|
|
2023-04-06 11:12:58 +00:00
|
|
|
const loggedUser = await authenticateUser(app, 'dev@tooljet.io');
|
|
|
|
|
|
2021-07-23 06:43:50 +00:00
|
|
|
let response = await request(app.getHttpServer())
|
2021-10-15 09:05:11 +00:00
|
|
|
.post(`/api/app_users/`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', adminUserData.user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', loggedUser.tokenCookie)
|
2021-09-21 13:48:28 +00:00
|
|
|
.send({
|
2021-07-23 06:43:50 +00:00
|
|
|
app_id: application.id,
|
|
|
|
|
org_user_id: viewerUserData.orgUser.id,
|
2021-10-11 15:15:58 +00:00
|
|
|
groups: ['all_users', 'admin'],
|
2021-07-23 06:43:50 +00:00
|
|
|
});
|
|
|
|
|
expect(response.statusCode).toBe(403);
|
|
|
|
|
|
2023-04-06 11:12:58 +00:00
|
|
|
await logoutUser(app, loggedUser.tokenCookie, developerUserData.user.defaultOrganizationId);
|
|
|
|
|
const loggedDeveloperUser = await authenticateUser(app, 'viewer@tooljet.io');
|
|
|
|
|
|
2021-07-23 06:43:50 +00:00
|
|
|
response = response = await request(app.getHttpServer())
|
2021-10-15 09:05:11 +00:00
|
|
|
.post(`/api/app_users/`)
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('Cookie', loggedDeveloperUser.tokenCookie)
|
2021-09-21 13:48:28 +00:00
|
|
|
.send({
|
|
|
|
|
app_id: application.id,
|
|
|
|
|
org_user_id: developerUserData.orgUser.id,
|
2021-10-11 15:15:58 +00:00
|
|
|
groups: ['all_users', 'admin'],
|
2021-09-21 13:48:28 +00:00
|
|
|
});
|
2021-07-23 06:43:50 +00:00
|
|
|
|
2023-04-06 11:12:58 +00:00
|
|
|
await logoutUser(app, loggedDeveloperUser.tokenCookie, viewerUserData.user.defaultOrganizationId);
|
2021-07-23 06:43:50 +00:00
|
|
|
await application.reload();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
|
await app.close();
|
|
|
|
|
});
|
|
|
|
|
});
|