ToolJet/server/test/controllers/data_sources.e2e-spec.ts
Akshay 7023f72d1d
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 20:45:58 +05:30

257 lines
8.3 KiB
TypeScript

import * as request from 'supertest';
import { INestApplication } from '@nestjs/common';
import {
authHeaderForUser,
clearDB,
createApplication,
createUser,
createNestAppInstance,
createDataSource,
createAppGroupPermission,
} from '../test.helper';
import { Credential } from 'src/entities/credential.entity';
import { getRepository } from 'typeorm';
import { GroupPermission } from 'src/entities/group_permission.entity';
describe('data sources controller', () => {
let app: INestApplication;
beforeEach(async () => {
await clearDB();
});
beforeAll(async () => {
app = await createNestAppInstance();
});
it('should be able to create data sources only if user has admin group or app update permission in same organization', async () => {
const adminUserData = await createUser(app, {
email: 'admin@tooljet.io',
groups: ['all_users', 'admin'],
});
const developerUserData = await createUser(app, {
email: 'developer@tooljet.io',
groups: ['all_users', 'developer'],
organization: adminUserData.organization,
});
const viewerUserData = await createUser(app, {
email: 'viewer@tooljet.io',
groups: ['all_users'],
organization: adminUserData.organization,
});
const anotherOrgAdminUserData = await createUser(app, {
email: 'another@tooljet.io',
groups: ['all_users', 'admin'],
});
const application = await createApplication(app, {
name: 'name',
user: adminUserData.user,
});
const developerUserGroup = await getRepository(GroupPermission).findOne({
group: 'developer',
});
await createAppGroupPermission(app, application, developerUserGroup.id, {
read: false,
update: true,
delete: false,
});
const dataSourceParams = {
name: 'name',
options: [{ key: 'foo', value: 'bar', encrypted: 'true' }],
kind: 'postgres',
app_id: application.id,
};
for (const userData of [adminUserData, developerUserData]) {
const response = await request(app.getHttpServer())
.post(`/data_sources`)
.set('Authorization', authHeaderForUser(userData.user))
.send(dataSourceParams);
expect(response.statusCode).toBe(201);
}
// encrypted data source options will create credentials
expect(await Credential.count()).toBe(2);
// Should not update if viewer or if user of another org
for (const userData of [anotherOrgAdminUserData, viewerUserData]) {
const response = await request(app.getHttpServer())
.post(`/data_sources`)
.set('Authorization', authHeaderForUser(userData.user))
.send(dataSourceParams);
expect(response.statusCode).toBe(403);
}
});
it('should be able to update data sources only if user has group admin or app update permission in same organization', async () => {
const adminUserData = await createUser(app, {
email: 'admin@tooljet.io',
groups: ['all_users', 'admin'],
});
const developerUserData = await createUser(app, {
email: 'developer@tooljet.io',
groups: ['all_users', 'developer'],
organization: adminUserData.organization,
});
const viewerUserData = await createUser(app, {
email: 'viewer@tooljet.io',
groups: ['all_users', 'viewer'],
organization: adminUserData.organization,
});
const anotherOrgAdminUserData = await createUser(app, {
email: 'another@tooljet.io',
groups: ['all_users', 'admin'],
});
const application = await createApplication(app, {
name: 'name',
user: adminUserData.user,
});
const developerUserGroup = await getRepository(GroupPermission).findOne({
group: 'developer',
});
await createAppGroupPermission(app, application, developerUserGroup.id, {
read: false,
update: true,
delete: false,
});
const dataSource = await createDataSource(app, {
name: 'name',
options: [{ key: 'foo', value: 'bar', encrypted: 'true' }],
kind: 'postgres',
application: application,
user: adminUserData.user,
});
// encrypted data source options will create credentials
expect(await Credential.count()).toBe(1);
for (const userData of [adminUserData, developerUserData]) {
const newOptions = [
{ key: 'email', value: userData.user.email },
{ key: 'foo', value: 'baz', encrypted: 'true' },
];
const response = await request(app.getHttpServer())
.put(`/data_sources/${dataSource.id}`)
.set('Authorization', authHeaderForUser(userData.user))
.send({
options: newOptions,
});
expect(response.statusCode).toBe(200);
await dataSource.reload();
expect(dataSource.options['email']['value']).toBe(userData.user.email);
}
// new credentials will not be created upon data source update
expect(await Credential.count()).toBe(1);
// Should not update if viewer or if user of another org
for (const userData of [anotherOrgAdminUserData, viewerUserData]) {
const newOptions = [
{ key: 'email', value: userData.user.email },
{ key: 'foo', value: 'baz', encrypted: 'true' },
];
const response = await request(app.getHttpServer())
.put(`/data_sources/${dataSource.id}`)
.set('Authorization', authHeaderForUser(userData.user))
.send({
options: newOptions,
});
expect(response.statusCode).toBe(403);
}
});
it('should be able to list (get) datasources for an app by all users of same organization', async () => {
const adminUserData = await createUser(app, {
email: 'admin@tooljet.io',
groups: ['all_users', 'admin'],
});
const developerUserData = await createUser(app, {
email: 'developer@tooljet.io',
groups: ['all_users'],
organization: adminUserData.organization,
});
const viewerUserData = await createUser(app, {
email: 'viewer@tooljet.io',
groups: ['all_users'],
organization: adminUserData.organization,
});
const application = await createApplication(app, {
name: 'name',
user: adminUserData.user,
});
const anotherOrgAdminUserData = await createUser(app, {
email: 'another@tooljet.io',
groups: ['all_users', 'admin'],
});
await createDataSource(app, {
name: 'name',
kind: 'postgres',
application: application,
user: adminUserData.user,
});
const allUserGroup = await getRepository(GroupPermission).findOne({
group: 'all_users',
organizationId: adminUserData.organization.id,
});
await createAppGroupPermission(app, application, allUserGroup.id, {
read: true,
update: true,
delete: false,
});
for (const userData of [adminUserData, developerUserData, viewerUserData]) {
const response = await request(app.getHttpServer())
.get(`/data_sources?app_id=${application.id}`)
.set('Authorization', authHeaderForUser(userData.user));
expect(response.statusCode).toBe(200);
expect(response.body.data_sources.length).toBe(1);
}
// Forbidden if user of another organization
const response = await request(app.getHttpServer())
.get(`/data_sources?app_id=${application.id}`)
.set('Authorization', authHeaderForUser(anotherOrgAdminUserData.user));
expect(response.statusCode).toBe(403);
});
it('should not be able to authorize OAuth code for a REST API source if user of another organization', async () => {
const adminUserData = await createUser(app, {
email: 'admin@tooljet.io',
groups: ['all_users', 'admin'],
});
const anotherOrgAdminUserData = await createUser(app, {
email: 'another@tooljet.io',
groups: ['all_users', 'admin'],
});
const application = await createApplication(app, {
name: 'name',
user: adminUserData.user,
});
const dataSource = await createDataSource(app, {
name: 'name',
options: [],
kind: 'restapi',
application: application,
user: adminUserData.user,
});
// Should not update if user of another org
const response = await request(app.getHttpServer())
.post(`/data_sources/${dataSource.id}/authorize_oauth2`)
.set('Authorization', authHeaderForUser(anotherOrgAdminUserData.user))
.send({
code: 'oauth-auth-code',
});
expect(response.statusCode).toBe(403);
});
});