ToolJet/server/test/controllers/organizations.e2e-spec.ts
Muhsin Shah C P 32740743e1
[Improvement] URLs scoped with workspace id (#5487)
* add: new URL prefix

* fix: working on home page

* add: profile path

* playing with rxjs

* removed context part

* working on path changes

* changing routes
- TODO: replace the workspaceId with actual id

* redo: public apps path

* initial commit

* added authorize API

* remove privileges from auth response

* fixed some api issue
- added subscriptions

* fix: redirect url workspace-id null issue

* fix: switch workspace

* fix: organization list mapping
- menu item paths

* fix: preview url
- editor, viewer permission mapping

* jwt fix

* fix: some url issue
- permission mappings
- workspace login

* fixed some issues
- user invite workspace-id
- org settings menu item default selected item issue

* app viewer fixes

* fixing workspace login issues

* fix

* fixing issues
- tooljet db
- path issues
- refatoring the code

* fix: workspace vars permissions

* fix: multi-page handle

* fix: create app from template

* fix: bulk user upload

* fix: import app
- clone app
- upload profile image

* fix: onboarding

* fix: log out

* fixed multi-workspace logout issue

* fix: launch btn

* fix: oauth2

* fixes

* fix: sso login

* fix: workspace sso login

* fixing sso issues

* fix: moved list of orgs to rxjs
- fixed switching issues

* reverting some changes

* fixed some minor bugs

* fixing sso redirect url issues

* fix: switching network timing issues

* fix: back to workspace-id

* fix: tj-database
- refactored the code - removed org id from some pages
- will get the org id from the service file only

* fix: multi-pages

* fix: infinite loop issue

* fixing workspace switching issue

* fixes
- comment link
- logout & private route redirect url

* fix: wrong uuid error

* fixing subpath
- fixed most of the places
- need to test & fix workspace login, sso, new account

* fix: subpath workspace login

* fix: rxjs handle bug

* Revert "fix: tj-database"

This reverts commit 9632ec2ff0.

* fix: reverted tj-db changes

* fix: subpath sso

* typo fix

* fix: existing session issues

* new: switch workspace page

* fix: modal dark-mode

* added default sso support

* fixes
- subpath workspace switching
- handle wrong routes

* fix: manager user button
- refactored the code

* removed SINGLE Workspace feature

* rebase

* add: change modal text

* fix: added validation

* fixed private app 401 issue

* initial commit

* fix: logged out session multi-tab issue

* refactoring the code

* fix: redirect url issue

* added auth-token in cookies

* Fix: failing e2e specs

* added session API

* fix: backend session guard

* fix: removing user details from local storage

* fix: null wid

* undo and redo

* fix: login page

* fix: viewer login redirection

* fix: login page redirection

* fix: public apps logout issue

* added session storage and scheduler

* added profile api

* fix: sso login
- switch workspace
- login page
- setup admin

* working on fixes

* fix: socket issue

* fix: setup admin api

* connected profile & logout apis

* fix: malfunctioned auth token case

* fix: realtime avatar

* fix: profile avatar

* fix: Realtime cursors avatar

* setting max age for auth token cookie

* add: Go to login page if logout api returns 401

* fix: subpath login

* fix

* fix: app logout [viewer]

* fix: authorize page

* remove expiry from jwt

* fix: integrations route
- session api

* small fix

* fix: updated profile

* fix: workspace login [logged user]

* fix: oauth and another workspace page issue

* fixed app preview logout issue

* subpath fix

* fix: subpath app id

* fix: selected state didnt change for apps page [subpath]

* fix

* add cookie parser to test app

* specs added

* increased user session expiry time

* test: session & new apis

* working on test cases

* fix: onboarding issue

* fixing specs

* fix: test cases

* fix: removing profile api calls

* some fixes

* fixing rebase issues

* fix: global ds issues

* fix: app is crashing

* fix: back to text

* fix: oauth test cases

* fix: test-helper

* fix: onboarding test cases

* fix: tests again

* refactoring the code

* latest develop merging precautions
- fixed a minor null issue

* fix: typo

* fix :menu issues due to the merging

* fix: - clicking on tooljet logo didnt redirect to login page for public apps
- private app preview doesnt load after login

* subpath fixes

* fixed back to issue

* PR changes

* fix: spec fixes for EE

* doc: URL scoped for workspace

---------

Co-authored-by: gsmithun4 <gsmithun4@gmail.com>
Co-authored-by: Shubhendra <withshubh@gmail.com>
2023-04-06 16:42:58 +05:30

439 lines
17 KiB
TypeScript

import * as request from 'supertest';
import { INestApplication } from '@nestjs/common';
import { clearDB, createUser, createNestAppInstanceWithEnvMock, authenticateUser } from '../test.helper';
import { Repository } from 'typeorm';
import { SSOConfigs } from 'src/entities/sso_config.entity';
import { User } from 'src/entities/user.entity';
describe('organizations controller', () => {
let app: INestApplication;
let ssoConfigsRepository: Repository<SSOConfigs>;
let userRepository: Repository<User>;
let mockConfig;
beforeEach(async () => {
await clearDB();
});
beforeAll(async () => {
({ app, mockConfig } = await createNestAppInstanceWithEnvMock());
ssoConfigsRepository = app.get('SSOConfigsRepository');
userRepository = app.get('UserRepository');
});
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});
describe('list organization users', () => {
it('should allow only authenticated users to list org users', async () => {
await request(app.getHttpServer()).get('/api/organizations/users').expect(401);
});
it('should list organization users', async () => {
const userData = await createUser(app, { email: 'admin@tooljet.io' });
const { user, orgUser } = userData;
const loggedUser = await authenticateUser(app);
userData['tokenCookie'] = loggedUser.tokenCookie;
const response = await request(app.getHttpServer())
.get('/api/organizations/users?page=1')
.set('tj-workspace-id', user.defaultOrganizationId)
.set('Cookie', userData['tokenCookie']);
expect(response.statusCode).toBe(200);
expect(response.body.users.length).toBe(1);
await orgUser.reload();
expect(response.body.users[0]).toStrictEqual({
email: user.email,
user_id: user.id,
first_name: user.firstName,
id: orgUser.id,
last_name: user.lastName,
name: `${user.firstName} ${user.lastName}`,
role: orgUser.role,
status: orgUser.status,
avatar_id: user.avatarId,
});
});
describe('create organization', () => {
it('should allow only authenticated users to create organization', async () => {
await request(app.getHttpServer()).post('/api/organizations').send({ name: 'My workspace' }).expect(401);
});
it('should create new organization if Multi-Workspace supported', async () => {
const { user, organization } = await createUser(app, {
email: 'admin@tooljet.io',
});
const loggedUser = await authenticateUser(app);
const response = await request(app.getHttpServer())
.post('/api/organizations')
.send({ name: 'My workspace' })
.set('tj-workspace-id', user.defaultOrganizationId)
.set('Cookie', loggedUser.tokenCookie);
expect(response.statusCode).toBe(201);
expect(response.body.current_organization_id).not.toBe(organization.id);
const newUser = await userRepository.findOneOrFail({ where: { id: user.id } });
expect(newUser.defaultOrganizationId).toBe(response.body.current_organization_id);
});
it('should throw error if name is empty', async () => {
const { user } = await createUser(app, { email: 'admin@tooljet.io' });
const loggedUser = await authenticateUser(app);
const response = await request(app.getHttpServer())
.post('/api/organizations')
.send({ name: '' })
.set('tj-workspace-id', user.defaultOrganizationId)
.set('Cookie', loggedUser.tokenCookie);
expect(response.statusCode).toBe(400);
});
it('should throw error if name is longer than 25 characters', async () => {
const { user } = await createUser(app, { email: 'admin@tooljet.io' });
const loggedUser = await authenticateUser(app);
const response = await request(app.getHttpServer())
.post('/api/organizations')
.send({ name: 'xxxxxxxxxxxxxxxxxxxxxxxxxx' })
.set('tj-workspace-id', user.defaultOrganizationId)
.set('Cookie', loggedUser.tokenCookie);
expect(response.statusCode).toBe(400);
});
it('should create new organization if Multi-Workspace supported and user logged in via SSO', async () => {
const { user, organization } = await createUser(app, {
email: 'admin@tooljet.io',
});
const loggedUser = await authenticateUser(app);
const response = await request(app.getHttpServer())
.post('/api/organizations')
.send({ name: 'My workspace' })
.set('tj-workspace-id', user.defaultOrganizationId)
.set('Cookie', loggedUser.tokenCookie);
expect(response.statusCode).toBe(201);
expect(response.body.current_organization_id).not.toBe(organization.id);
});
});
describe('update organization', () => {
it('should change organization params if changes are done by admin', async () => {
const { user, organization } = await createUser(app, {
email: 'admin@tooljet.io',
});
const loggedUser = await authenticateUser(app);
const response = await request(app.getHttpServer())
.patch('/api/organizations')
.send({ name: 'new name', domain: 'tooljet.io', enableSignUp: true })
.set('tj-workspace-id', user.defaultOrganizationId)
.set('Cookie', loggedUser.tokenCookie);
expect(response.statusCode).toBe(200);
await organization.reload();
expect(organization.name).toBe('new name');
expect(organization.domain).toBe('tooljet.io');
expect(organization.enableSignUp).toBeTruthy();
});
it('should throw error if name is longer than 25 characters', async () => {
const { user } = await createUser(app, { email: 'admin@tooljet.io' });
const loggedUser = await authenticateUser(app);
const response = await request(app.getHttpServer())
.post('/api/organizations')
.send({ name: 'xxxxxxxxxxxxxxxxxxxxxxxxxx' })
.set('tj-workspace-id', user.defaultOrganizationId)
.set('Cookie', loggedUser.tokenCookie);
expect(response.statusCode).toBe(400);
});
it('should not change organization params if changes are not done by admin', async () => {
const { organization } = await createUser(app, { email: 'admin@tooljet.io' });
const developerUserData = await createUser(app, {
email: 'developer@tooljet.io',
groups: ['all_users'],
organization,
});
const loggedUser = await authenticateUser(app, 'developer@tooljet.io');
const response = await request(app.getHttpServer())
.patch('/api/organizations')
.send({ name: 'new name', domain: 'tooljet.io', enableSignUp: true })
.set('tj-workspace-id', developerUserData.user.defaultOrganizationId)
.set('Cookie', loggedUser.tokenCookie);
expect(response.statusCode).toBe(403);
});
});
describe('update organization configs', () => {
it('should change organization configs if changes are done by admin', async () => {
const { user } = await createUser(app, {
email: 'admin@tooljet.io',
});
const loggedUser = await authenticateUser(app);
const response = await request(app.getHttpServer())
.patch('/api/organizations/configs')
.send({ type: 'git', configs: { clientId: 'client-id', clientSecret: 'client-secret' }, enabled: true })
.set('tj-workspace-id', user.defaultOrganizationId)
.set('Cookie', loggedUser.tokenCookie);
expect(response.statusCode).toBe(200);
const ssoConfigs = await ssoConfigsRepository.findOneOrFail({ where: { id: response.body.id } });
expect(ssoConfigs.sso).toBe('git');
expect(ssoConfigs.enabled).toBeTruthy();
expect(ssoConfigs.configs.clientId).toBe('client-id');
expect(ssoConfigs.configs['clientSecret']).not.toBe('client-secret');
});
it('should not change organization configs if changes are not done by admin', async () => {
const { user } = await createUser(app, {
email: 'admin@tooljet.io',
groups: ['all_users'],
});
const loggedUser = await authenticateUser(app);
const response = await request(app.getHttpServer())
.patch('/api/organizations/configs')
.send({ type: 'git', configs: { clientId: 'client-id', clientSecret: 'client-secret' }, enabled: true })
.set('tj-workspace-id', user.defaultOrganizationId)
.set('Cookie', loggedUser.tokenCookie);
expect(response.statusCode).toBe(403);
});
});
describe('get organization configs', () => {
it('should get organization details if requested by admin', async () => {
const { user, organization } = await createUser(app, {
email: 'admin@tooljet.io',
});
const loggedUser = await authenticateUser(app);
const response = await request(app.getHttpServer())
.patch('/api/organizations/configs')
.send({ type: 'git', configs: { clientId: 'client-id', clientSecret: 'client-secret' }, enabled: true })
.set('tj-workspace-id', user.defaultOrganizationId)
.set('Cookie', loggedUser.tokenCookie);
expect(response.statusCode).toBe(200);
const getResponse = await request(app.getHttpServer())
.get('/api/organizations/configs')
.set('tj-workspace-id', user.defaultOrganizationId)
.set('Cookie', loggedUser.tokenCookie);
expect(getResponse.statusCode).toBe(200);
expect(getResponse.body.organization_details.id).toBe(organization.id);
expect(getResponse.body.organization_details.name).toBe(organization.name);
expect(getResponse.body.organization_details.sso_configs.length).toBe(2);
expect(getResponse.body.organization_details.sso_configs.find((ob) => ob.sso === 'form').organization_id).toBe(
organization.id
);
expect(getResponse.body.organization_details.sso_configs.find((ob) => ob.sso === 'git').enabled).toBeTruthy();
expect(getResponse.body.organization_details.sso_configs.find((ob) => ob.sso === 'git').configs).toEqual({
client_id: 'client-id',
client_secret: 'client-secret',
});
});
it('should not get organization configs if request not done by admin', async () => {
const { user } = await createUser(app, {
email: 'admin@tooljet.io',
groups: ['all_users'],
});
const loggedUser = await authenticateUser(app);
const response = await request(app.getHttpServer())
.get('/api/organizations/configs')
.set('tj-workspace-id', user.defaultOrganizationId)
.set('Cookie', loggedUser.tokenCookie);
expect(response.statusCode).toBe(403);
});
});
describe('get public organization configs', () => {
it('should get organization specific details for all users for multiple organization deployment', async () => {
const { user, organization } = await createUser(app, {
email: 'admin@tooljet.io',
});
const loggedUser = await authenticateUser(app);
const response = await request(app.getHttpServer())
.patch('/api/organizations/configs')
.send({ type: 'git', configs: { clientId: 'client-id', clientSecret: 'client-secret' }, enabled: true })
.set('tj-workspace-id', user.defaultOrganizationId)
.set('Cookie', loggedUser.tokenCookie);
expect(response.statusCode).toBe(200);
const getResponse = await request(app.getHttpServer()).get(
`/api/organizations/${organization.id}/public-configs`
);
expect(getResponse.statusCode).toBe(200);
const authGetResponse = await request(app.getHttpServer())
.get('/api/organizations/configs')
.set('tj-workspace-id', user.defaultOrganizationId)
.set('Cookie', loggedUser.tokenCookie);
expect(authGetResponse.statusCode).toBe(200);
expect(getResponse.statusCode).toBe(200);
expect(getResponse.body).toEqual({
sso_configs: {
name: 'Test Organization',
enable_sign_up: false,
form: {
config_id: authGetResponse.body.organization_details.sso_configs.find((ob) => ob.sso === 'form').id,
sso: 'form',
configs: {},
enabled: true,
},
git: {
config_id: authGetResponse.body.organization_details.sso_configs.find((ob) => ob.sso === 'git').id,
sso: 'git',
configs: { client_id: 'client-id', client_secret: '' },
enabled: true,
},
},
});
});
it('should get organization specific details with instance level sso and override it with organization sso configs for all users for multiple organization deployment', async () => {
jest.spyOn(mockConfig, 'get').mockImplementation((key: string) => {
switch (key) {
case 'SSO_GOOGLE_OAUTH2_CLIENT_ID':
return 'google-client-id';
case 'SSO_GIT_OAUTH2_CLIENT_ID':
return 'git-client-id';
case 'SSO_GIT_OAUTH2_CLIENT_SECRET':
return 'git-secret';
default:
return process.env[key];
}
});
const { user, organization } = await createUser(app, {
email: 'admin@tooljet.io',
});
const loggedUser = await authenticateUser(app);
const response = await request(app.getHttpServer())
.patch('/api/organizations/configs')
.send({ type: 'git', configs: { clientId: 'org-client-id', clientSecret: 'client-secret' }, enabled: true })
.set('tj-workspace-id', user.defaultOrganizationId)
.set('Cookie', loggedUser.tokenCookie);
expect(response.statusCode).toBe(200);
const getResponse = await request(app.getHttpServer()).get(
`/api/organizations/${organization.id}/public-configs`
);
expect(getResponse.statusCode).toBe(200);
const authGetResponse = await request(app.getHttpServer())
.get('/api/organizations/configs')
.set('tj-workspace-id', user.defaultOrganizationId)
.set('Cookie', loggedUser.tokenCookie);
expect(authGetResponse.statusCode).toBe(200);
expect(getResponse.statusCode).toBe(200);
expect(getResponse.body).toEqual({
sso_configs: {
name: 'Test Organization',
enable_sign_up: false,
form: {
config_id: authGetResponse.body.organization_details.sso_configs.find((ob) => ob.sso === 'form').id,
sso: 'form',
configs: {},
enabled: true,
},
git: {
config_id: authGetResponse.body.organization_details.sso_configs.find((ob) => ob.sso === 'git').id,
sso: 'git',
configs: { client_id: 'org-client-id', client_secret: '' },
enabled: true,
},
google: {
sso: 'google',
configs: { client_id: 'google-client-id', client_secret: '' },
enabled: true,
},
},
});
});
it('should get organization specific details with instance level sso for all users for multiple organization deployment', async () => {
jest.spyOn(mockConfig, 'get').mockImplementation((key: string) => {
switch (key) {
case 'SSO_GOOGLE_OAUTH2_CLIENT_ID':
return 'google-client-id';
case 'SSO_GIT_OAUTH2_CLIENT_ID':
return 'git-client-id';
case 'SSO_GIT_OAUTH2_CLIENT_SECRET':
return 'git-secret';
default:
return process.env[key];
}
});
const { user, organization } = await createUser(app, {
email: 'admin@tooljet.io',
});
const loggedUser = await authenticateUser(app);
const getResponse = await request(app.getHttpServer()).get(
`/api/organizations/${organization.id}/public-configs`
);
expect(getResponse.statusCode).toBe(200);
const authGetResponse = await request(app.getHttpServer())
.get('/api/organizations/configs')
.set('tj-workspace-id', user.defaultOrganizationId)
.set('Cookie', loggedUser.tokenCookie);
expect(authGetResponse.statusCode).toBe(200);
expect(getResponse.statusCode).toBe(200);
expect(getResponse.body).toEqual({
sso_configs: {
name: 'Test Organization',
enable_sign_up: false,
form: {
config_id: authGetResponse.body.organization_details.sso_configs.find((ob) => ob.sso === 'form').id,
sso: 'form',
configs: {},
enabled: true,
},
git: {
sso: 'git',
configs: {
client_id: 'git-client-id',
client_secret: '',
},
enabled: true,
},
google: {
sso: 'google',
configs: { client_id: 'google-client-id', client_secret: '' },
enabled: true,
},
},
});
});
});
});
afterAll(async () => {
await app.close();
});
});