2021-07-24 18:44:44 +00:00
import * as request from 'supertest' ;
import { INestApplication } from '@nestjs/common' ;
2021-09-21 04:50:02 +00:00
import {
clearDB ,
createApplication ,
createUser ,
createNestAppInstance ,
createDataSource ,
2022-05-02 13:12:14 +00:00
createDataQuery ,
2021-10-11 15:15:58 +00:00
createAppGroupPermission ,
2022-01-04 08:04:12 +00:00
createApplicationVersion ,
2023-01-09 12:00:32 +00:00
generateAppDefaults ,
2023-04-06 11:12:58 +00:00
authenticateUser ,
2025-02-25 06:52:50 +00:00
createDatasourceGroupPermission ,
2021-09-21 04:50:02 +00:00
} from '../test.helper' ;
import { Credential } from 'src/entities/credential.entity' ;
2023-01-09 12:00:32 +00:00
import { getManager , getRepository } from 'typeorm' ;
2021-10-11 15:15:58 +00:00
import { GroupPermission } from 'src/entities/group_permission.entity' ;
2023-01-09 12:00:32 +00:00
import { DataSource } from 'src/entities/data_source.entity' ;
2021-07-24 18:44:44 +00:00
describe ( 'data sources controller' , ( ) = > {
let app : INestApplication ;
beforeEach ( async ( ) = > {
await clearDB ( ) ;
} ) ;
beforeAll ( async ( ) = > {
app = await createNestAppInstance ( ) ;
} ) ;
2025-02-25 06:52:50 +00:00
it ( 'should be able to create data sources only if user has admin group or app update permission in same organization or has instance user type' , async ( ) = > {
2021-09-21 04:50:02 +00:00
const adminUserData = await createUser ( app , {
email : 'admin@tooljet.io' ,
2021-10-11 15:15:58 +00:00
groups : [ 'all_users' , 'admin' ] ,
2021-09-21 04:50:02 +00:00
} ) ;
2025-02-25 06:52:50 +00:00
const superAdminUserData = await createUser ( app , {
email : 'superadmin@tooljet.io' ,
groups : [ 'all_users' , 'admin' ] ,
userType : 'instance' ,
} ) ;
2021-09-21 04:50:02 +00:00
const developerUserData = await createUser ( app , {
email : 'developer@tooljet.io' ,
2021-10-11 15:15:58 +00:00
groups : [ 'all_users' , 'developer' ] ,
2021-09-21 04:50:02 +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' ] ,
2021-09-21 04:50:02 +00:00
organization : adminUserData.organization ,
} ) ;
const anotherOrgAdminUserData = await createUser ( app , {
email : 'another@tooljet.io' ,
2021-10-11 15:15:58 +00:00
groups : [ 'all_users' , 'admin' ] ,
2021-09-21 04:50:02 +00:00
} ) ;
2023-01-09 12:00:32 +00:00
2023-04-06 11:12:58 +00:00
let loggedUser = await authenticateUser ( app , adminUserData . user . email ) ;
adminUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
loggedUser = await authenticateUser ( app , developerUserData . user . email ) ;
developerUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
loggedUser = await authenticateUser ( app , viewerUserData . user . email ) ;
viewerUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
loggedUser = await authenticateUser ( app , anotherOrgAdminUserData . user . email ) ;
anotherOrgAdminUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
2025-02-25 06:52:50 +00:00
loggedUser = await authenticateUser (
app ,
superAdminUserData . user . email ,
'password' ,
adminUserData . user . defaultOrganizationId
) ;
superAdminUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
2023-04-06 11:12:58 +00:00
2023-01-09 12:00:32 +00:00
const { application , appVersion : applicationVersion } = await generateAppDefaults ( app , adminUserData . user , {
isDataSourceNeeded : false ,
isQueryNeeded : false ,
2021-09-21 04:50:02 +00:00
} ) ;
2022-05-05 07:08:42 +00:00
const developerUserGroup = await getRepository ( GroupPermission ) . findOneOrFail ( {
2022-01-28 06:14:44 +00:00
where : {
group : 'developer' ,
} ,
2021-10-11 15:15:58 +00:00
} ) ;
await createAppGroupPermission ( app , application , developerUserGroup . id , {
read : false ,
update : true ,
delete : false ,
} ) ;
2021-09-21 04:50:02 +00:00
const dataSourceParams = {
name : 'name' ,
options : [ { key : 'foo' , value : 'bar' , encrypted : 'true' } ] ,
kind : 'postgres' ,
2022-01-04 08:04:12 +00:00
app_version_id : applicationVersion.id ,
2021-09-21 04:50:02 +00:00
} ;
2025-02-25 06:52:50 +00:00
for ( const userData of [ adminUserData , developerUserData , superAdminUserData ] ) {
2021-07-24 18:44:44 +00:00
const response = await request ( app . getHttpServer ( ) )
2021-10-15 09:05:11 +00:00
. post ( ` /api/data_sources ` )
2025-02-25 06:52:50 +00:00
. set ( 'tj-workspace-id' , adminUserData . user . defaultOrganizationId )
2023-04-06 11:12:58 +00:00
. set ( 'Cookie' , userData [ 'tokenCookie' ] )
2021-09-21 04:50:02 +00:00
. send ( dataSourceParams ) ;
2021-07-24 18:44:44 +00:00
expect ( response . statusCode ) . toBe ( 201 ) ;
2022-01-04 08:04:12 +00:00
expect ( response . body . id ) . toBeDefined ( ) ;
expect ( response . body . app_version_id ) . toBe ( applicationVersion . id ) ;
expect ( response . body . kind ) . toBe ( 'postgres' ) ;
expect ( response . body . name ) . toBe ( 'name' ) ;
expect ( response . body . created_at ) . toBeDefined ( ) ;
expect ( response . body . updated_at ) . toBeDefined ( ) ;
2021-07-24 18:44:44 +00:00
}
2021-09-21 04:50:02 +00:00
// encrypted data source options will create credentials
2025-02-25 06:52:50 +00:00
expect ( await Credential . count ( ) ) . toBe ( 9 ) ;
2021-09-21 04:50:02 +00:00
2021-07-24 18:44:44 +00:00
// Should not update if viewer or if user of another org
2021-09-21 04:50:02 +00:00
for ( const userData of [ anotherOrgAdminUserData , viewerUserData ] ) {
2021-07-24 18:44:44 +00:00
const response = await request ( app . getHttpServer ( ) )
2021-10-15 09:05:11 +00:00
. post ( ` /api/data_sources ` )
2023-04-06 11:12:58 +00:00
. set ( 'tj-workspace-id' , userData . user . defaultOrganizationId )
. set ( 'Cookie' , userData [ 'tokenCookie' ] )
2021-09-21 04:50:02 +00:00
. send ( dataSourceParams ) ;
2021-07-24 18:44:44 +00:00
expect ( response . statusCode ) . toBe ( 403 ) ;
}
} ) ;
2025-02-25 06:52:50 +00:00
it ( 'should be able to update data sources only if user has group admin or app update permission in same organization or has instance user type' , async ( ) = > {
2021-09-21 04:50:02 +00:00
const adminUserData = await createUser ( app , {
email : 'admin@tooljet.io' ,
2021-10-11 15:15:58 +00:00
groups : [ 'all_users' , 'admin' ] ,
2021-09-21 04:50:02 +00:00
} ) ;
2025-02-25 06:52:50 +00:00
const superAdminUserData = await createUser ( app , {
email : 'superadmin@tooljet.io' ,
groups : [ 'all_users' , 'admin' ] ,
userType : 'instance' ,
} ) ;
2021-09-21 04:50:02 +00:00
const developerUserData = await createUser ( app , {
email : 'developer@tooljet.io' ,
2021-10-11 15:15:58 +00:00
groups : [ 'all_users' , 'developer' ] ,
2021-09-21 04:50:02 +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 04:50:02 +00:00
organization : adminUserData.organization ,
} ) ;
const anotherOrgAdminUserData = await createUser ( app , {
email : 'another@tooljet.io' ,
2021-10-11 15:15:58 +00:00
groups : [ 'all_users' , 'admin' ] ,
2021-09-21 04:50:02 +00:00
} ) ;
2023-04-06 11:12:58 +00:00
let loggedUser = await authenticateUser ( app , adminUserData . user . email ) ;
adminUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
loggedUser = await authenticateUser ( app , developerUserData . user . email ) ;
developerUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
loggedUser = await authenticateUser ( app , viewerUserData . user . email ) ;
viewerUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
loggedUser = await authenticateUser ( app , anotherOrgAdminUserData . user . email ) ;
anotherOrgAdminUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
2025-02-25 06:52:50 +00:00
loggedUser = await authenticateUser (
app ,
superAdminUserData . user . email ,
'password' ,
adminUserData . user . defaultOrganizationId
) ;
superAdminUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
const { application , dataSource , appEnvironments } = await generateAppDefaults ( app , adminUserData . user , {
2023-01-09 12:00:32 +00:00
isQueryNeeded : false ,
dsOptions : [ { key : 'foo' , value : 'bar' , encrypted : 'true' } ] ,
dsKind : 'postgres' ,
2021-09-21 04:50:02 +00:00
} ) ;
2022-05-05 07:08:42 +00:00
const developerUserGroup = await getRepository ( GroupPermission ) . findOneOrFail ( {
2022-01-28 06:14:44 +00:00
where : {
group : 'developer' ,
} ,
2021-10-11 15:15:58 +00:00
} ) ;
await createAppGroupPermission ( app , application , developerUserGroup . id , {
read : false ,
update : true ,
delete : false ,
} ) ;
2021-09-21 04:50:02 +00:00
// encrypted data source options will create credentials
2025-02-25 06:52:50 +00:00
expect ( await Credential . count ( ) ) . toBe ( 3 ) ;
2021-09-21 04:50:02 +00:00
2025-02-25 06:52:50 +00:00
for ( const userData of [ adminUserData , developerUserData , superAdminUserData ] ) {
2021-09-21 04:50:02 +00:00
const newOptions = [
{ key : 'email' , value : userData.user.email } ,
{ key : 'foo' , value : 'baz' , encrypted : 'true' } ,
] ;
2021-07-24 18:44:44 +00:00
const response = await request ( app . getHttpServer ( ) )
2021-10-15 09:05:11 +00:00
. put ( ` /api/data_sources/ ${ dataSource . id } ` )
2025-02-25 06:52:50 +00:00
. set ( 'tj-workspace-id' , adminUserData . user . defaultOrganizationId )
2023-04-06 11:12:58 +00:00
. set ( 'Cookie' , userData [ 'tokenCookie' ] )
2021-07-24 18:44:44 +00:00
. send ( {
2021-09-21 04:50:02 +00:00
options : newOptions ,
} ) ;
2021-07-24 18:44:44 +00:00
2023-01-09 12:00:32 +00:00
const updatedDs = await getManager ( )
. createQueryBuilder ( DataSource , 'data_source' )
. innerJoinAndSelect ( 'data_source.dataSourceOptions' , 'dataSourceOptions' )
. where ( 'data_source.id = :dataSourceId' , { dataSourceId : dataSource.id } )
. getOneOrFail ( ) ;
2025-02-25 06:52:50 +00:00
const updatedOptions = updatedDs . dataSourceOptions . find (
( option ) = > option . environmentId === appEnvironments . find ( ( env ) = > env . isDefault ) . id
) ;
2021-07-24 18:44:44 +00:00
expect ( response . statusCode ) . toBe ( 200 ) ;
2025-02-25 06:52:50 +00:00
expect ( updatedOptions . options [ 'email' ] [ 'value' ] ) . toBe ( userData . user . email ) ;
2021-07-24 18:44:44 +00:00
}
2021-09-21 04:50:02 +00:00
// new credentials will not be created upon data source update
2025-02-25 06:52:50 +00:00
expect ( await Credential . count ( ) ) . toBe ( 3 ) ;
2021-09-21 04:50:02 +00:00
2021-07-24 18:44:44 +00:00
// Should not update if viewer or if user of another org
2021-09-21 04:50:02 +00:00
for ( const userData of [ anotherOrgAdminUserData , viewerUserData ] ) {
const newOptions = [
{ key : 'email' , value : userData.user.email } ,
{ key : 'foo' , value : 'baz' , encrypted : 'true' } ,
] ;
2021-07-24 18:44:44 +00:00
const response = await request ( app . getHttpServer ( ) )
2021-10-15 09:05:11 +00:00
. put ( ` /api/data_sources/ ${ dataSource . id } ` )
2023-04-06 11:12:58 +00:00
. set ( 'tj-workspace-id' , userData . user . defaultOrganizationId )
. set ( 'Cookie' , userData [ 'tokenCookie' ] )
2021-07-24 18:44:44 +00:00
. send ( {
2021-09-21 04:50:02 +00:00
options : newOptions ,
} ) ;
2021-07-24 18:44:44 +00:00
expect ( response . statusCode ) . toBe ( 403 ) ;
}
} ) ;
2025-02-25 06:52:50 +00:00
it ( 'should be able to list (get) datasources for an app by all users of same organization or has instance user type' , async ( ) = > {
2021-09-21 04:50:02 +00:00
const adminUserData = await createUser ( app , {
email : 'admin@tooljet.io' ,
2021-10-11 15:15:58 +00:00
groups : [ 'all_users' , 'admin' ] ,
2021-09-21 04:50:02 +00:00
} ) ;
2025-02-25 06:52:50 +00:00
const superAdminUserData = await createUser ( app , {
email : 'superadmin@tooljet.io' ,
groups : [ 'all_users' , 'admin' ] ,
userType : 'instance' ,
organization : adminUserData.organization ,
} ) ;
2021-09-21 04:50:02 +00:00
const developerUserData = await createUser ( app , {
email : 'developer@tooljet.io' ,
2021-10-11 15:15:58 +00:00
groups : [ 'all_users' ] ,
2021-09-21 04:50:02 +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' ] ,
2021-09-21 04:50:02 +00:00
organization : adminUserData.organization ,
} ) ;
const anotherOrgAdminUserData = await createUser ( app , {
email : 'another@tooljet.io' ,
2021-10-11 15:15:58 +00:00
groups : [ 'all_users' , 'admin' ] ,
2021-09-21 04:50:02 +00:00
} ) ;
2023-01-09 12:00:32 +00:00
2023-04-06 11:12:58 +00:00
let loggedUser = await authenticateUser ( app , adminUserData . user . email ) ;
adminUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
loggedUser = await authenticateUser ( app , developerUserData . user . email ) ;
developerUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
loggedUser = await authenticateUser ( app , viewerUserData . user . email ) ;
viewerUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
loggedUser = await authenticateUser ( app , anotherOrgAdminUserData . user . email ) ;
anotherOrgAdminUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
2025-02-25 06:52:50 +00:00
loggedUser = await authenticateUser ( app , superAdminUserData . user . email , 'password' , adminUserData . organization . id ) ;
superAdminUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
2023-04-06 11:12:58 +00:00
2025-02-25 06:52:50 +00:00
const { application , appVersion , dataSource } = await generateAppDefaults ( app , adminUserData . user , {
2023-01-09 12:00:32 +00:00
isQueryNeeded : false ,
2021-09-21 04:50:02 +00:00
} ) ;
2022-05-05 07:08:42 +00:00
const allUserGroup = await getRepository ( GroupPermission ) . findOneOrFail ( {
2022-01-28 06:14:44 +00:00
where : {
group : 'all_users' ,
organizationId : adminUserData.organization.id ,
} ,
2021-10-11 15:15:58 +00:00
} ) ;
await createAppGroupPermission ( app , application , allUserGroup . id , {
read : true ,
update : true ,
delete : false ,
} ) ;
2025-02-25 06:52:50 +00:00
await createDatasourceGroupPermission ( app , dataSource . id , allUserGroup . id , {
read : true ,
update : false ,
delete : false ,
} ) ;
2021-09-21 04:50:02 +00:00
for ( const userData of [ adminUserData , developerUserData , viewerUserData ] ) {
2021-07-24 18:44:44 +00:00
const response = await request ( app . getHttpServer ( ) )
2023-01-09 12:00:32 +00:00
. get ( ` /api/data_sources?app_version_id= ${ appVersion . id } ` )
2023-04-06 11:12:58 +00:00
. set ( 'tj-workspace-id' , userData . user . defaultOrganizationId )
. set ( 'Cookie' , userData [ 'tokenCookie' ] ) ;
2021-07-24 18:44:44 +00:00
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 ( ) )
2023-01-09 12:00:32 +00:00
. get ( ` /api/data_sources?app_version_id= ${ appVersion . id } ` )
2023-04-06 11:12:58 +00:00
. set ( 'tj-workspace-id' , anotherOrgAdminUserData . user . defaultOrganizationId )
. set ( 'Cookie' , anotherOrgAdminUserData [ 'tokenCookie' ] ) ;
2021-07-24 18:44:44 +00:00
2021-09-21 04:50:02 +00:00
expect ( response . statusCode ) . toBe ( 403 ) ;
2021-07-24 18:44:44 +00:00
} ) ;
2025-02-25 06:52:50 +00:00
it ( 'should be able to delete data sources of an app only if admin/developer of same organization or the user is a super admin' , async ( ) = > {
2022-05-02 13:12:14 +00:00
const adminUserData = await createUser ( app , {
email : 'admin@tooljet.io' ,
groups : [ 'all_users' , 'admin' ] ,
} ) ;
2025-02-25 06:52:50 +00:00
const superAdminUserData = await createUser ( app , {
email : 'superadmin@tooljet.io' ,
groups : [ 'all_users' , 'admin' ] ,
userType : 'instance' ,
organization : adminUserData.organization ,
} ) ;
2022-05-02 13:12:14 +00:00
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' ] ,
} ) ;
2023-01-09 12:00:32 +00:00
2023-04-06 11:12:58 +00:00
let loggedUser = await authenticateUser ( app , adminUserData . user . email ) ;
adminUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
loggedUser = await authenticateUser ( app , developerUserData . user . email ) ;
developerUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
loggedUser = await authenticateUser ( app , viewerUserData . user . email ) ;
viewerUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
loggedUser = await authenticateUser ( app , anotherOrgAdminUserData . user . email ) ;
anotherOrgAdminUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
2025-02-25 06:52:50 +00:00
loggedUser = await authenticateUser (
app ,
superAdminUserData . user . email ,
'password' ,
adminUserData . user . defaultOrganizationId
) ;
superAdminUserData [ 'tokenCookie' ] = loggedUser . tokenCookie ;
2023-04-06 11:12:58 +00:00
2023-01-09 12:00:32 +00:00
const { application , appVersion } = await generateAppDefaults ( app , adminUserData . user , {
isQueryNeeded : false ,
isDataSourceNeeded : false ,
2022-05-02 13:12:14 +00:00
} ) ;
// setup app permissions for developer
const developerUserGroup = await getRepository ( GroupPermission ) . findOne ( {
where : {
group : 'developer' ,
} ,
} ) ;
await createAppGroupPermission ( app , application , developerUserGroup . id , {
read : true ,
update : true ,
delete : false ,
} ) ;
2025-02-25 06:52:50 +00:00
for ( const userData of [ adminUserData , developerUserData , superAdminUserData ] ) {
2022-05-02 13:12:14 +00:00
const dataSource = await createDataSource ( app , {
name : 'name' ,
options : [ { key : 'foo' , value : 'bar' , encrypted : 'true' } ] ,
kind : 'postgres' ,
2023-01-09 12:00:32 +00:00
appVersion ,
2022-05-02 13:12:14 +00:00
} ) ;
const response = await request ( app . getHttpServer ( ) )
. delete ( ` /api/data_sources/ ${ dataSource . id } ` )
2023-04-06 11:12:58 +00:00
. set ( 'tj-workspace-id' , userData . user . defaultOrganizationId )
. set ( 'Cookie' , userData [ 'tokenCookie' ] )
2023-01-09 12:00:32 +00:00
. send ( ) ;
2022-05-02 13:12:14 +00:00
expect ( response . statusCode ) . toBe ( 200 ) ;
}
// Should not delete if viewer or if user of another org
for ( const userData of [ anotherOrgAdminUserData , viewerUserData ] ) {
const dataSource = await createDataSource ( app , {
name : 'name' ,
options : [ { key : 'foo' , value : 'bar' , encrypted : 'true' } ] ,
kind : 'postgres' ,
2023-01-09 12:00:32 +00:00
appVersion ,
2022-05-02 13:12:14 +00:00
} ) ;
const response = await request ( app . getHttpServer ( ) )
. delete ( ` /api/data_sources/ ${ dataSource . id } ` )
2023-04-06 11:12:58 +00:00
. set ( 'tj-workspace-id' , userData . user . defaultOrganizationId )
. set ( 'Cookie' , userData [ 'tokenCookie' ] )
2023-01-09 12:00:32 +00:00
. send ( ) ;
2022-05-02 13:12:14 +00:00
expect ( response . statusCode ) . toBe ( 403 ) ;
}
} ) ;
it ( 'should be able to a delete data sources from a specific version of an app' , async ( ) = > {
const adminUserData = await createUser ( app , {
email : 'admin@tooljet.io' ,
groups : [ 'all_users' , 'admin' ] ,
} ) ;
const application = await createApplication ( app , {
name : 'name' ,
user : adminUserData.user ,
} ) ;
const appVersion1 = await createApplicationVersion ( app , application ) ;
const dataSource1 = await createDataSource ( app , {
name : 'api' ,
kind : 'restapi' ,
appVersion : appVersion1 ,
} ) ;
await createDataQuery ( app , {
dataSource : dataSource1 ,
options : {
method : 'get' ,
url : 'https://api.github.com/repos/tooljet/tooljet/stargazers' ,
url_params : [ ] ,
headers : [ ] ,
body : [ ] ,
} ,
} ) ;
2022-06-28 07:06:28 +00:00
const appVersion2 = await createApplicationVersion ( app , application , { name : 'v2' , definition : null } ) ;
2022-05-02 13:12:14 +00:00
const dataSource2 = await createDataSource ( app , {
name : 'api2' ,
kind : 'restapi' ,
appVersion : appVersion2 ,
} ) ;
const dataSource2Temp = dataSource2 ;
const query2 = await createDataQuery ( app , {
2023-01-09 12:00:32 +00:00
name : 'restapi2' ,
2022-05-02 13:12:14 +00:00
dataSource : dataSource2 ,
options : {
method : 'get' ,
url : 'https://api.github.com/repos/tooljet/tooljet/stargazers' ,
url_params : [ ] ,
headers : [ ] ,
body : [ ] ,
} ,
} ) ;
const dataQuery2Temp = query2 ;
2023-04-06 11:12:58 +00:00
const loggedUser = await authenticateUser ( app , adminUserData . user . email ) ;
2022-05-02 13:12:14 +00:00
const response = await request ( app . getHttpServer ( ) )
. delete ( ` /api/data_sources/ ${ dataSource1 . id } ` )
2023-04-06 11:12:58 +00:00
. set ( 'tj-workspace-id' , adminUserData . user . defaultOrganizationId )
. set ( 'Cookie' , loggedUser . tokenCookie )
2022-05-02 13:12:14 +00:00
. send ( ) ;
expect ( response . statusCode ) . toBe ( 200 ) ;
await dataSource2 . reload ( ) ;
await query2 . reload ( ) ;
expect ( dataSource2 . id ) . toBe ( dataSource2Temp . id ) ;
expect ( query2 . id ) . toBe ( dataQuery2Temp . id ) ;
} ) ;
2022-01-04 08:04:12 +00:00
it ( 'should be able to search data sources with application version id' , async ( ) = > {
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 , adminUserData . user . email ) ;
2023-01-09 12:00:32 +00:00
const { dataSource } = await generateAppDefaults ( app , adminUserData . user , {
isQueryNeeded : false ,
2022-01-04 08:04:12 +00:00
} ) ;
let response = await request ( app . getHttpServer ( ) )
2023-01-09 12:00:32 +00:00
. get ( ` /api/data_sources?app_version_id= ${ dataSource . appVersionId } ` )
2023-04-06 11:12:58 +00:00
. set ( 'tj-workspace-id' , adminUserData . user . defaultOrganizationId )
. set ( 'Cookie' , loggedUser . tokenCookie ) ;
2022-01-04 08:04:12 +00:00
expect ( response . statusCode ) . toBe ( 200 ) ;
expect ( response . body . data_sources . length ) . toBe ( 1 ) ;
response = await request ( app . getHttpServer ( ) )
2023-01-09 12:00:32 +00:00
. get ( ` /api/data_sources?app_version_id=62929ad6-11ae-4655-bb3e-2d2465b58950 ` )
2023-04-06 11:12:58 +00:00
. set ( 'tj-workspace-id' , adminUserData . user . defaultOrganizationId )
. set ( 'Cookie' , loggedUser . tokenCookie ) ;
2022-01-04 08:04:12 +00:00
2023-01-09 12:00:32 +00:00
expect ( response . statusCode ) . toBe ( 500 ) ;
2022-01-04 08:04:12 +00:00
} ) ;
2021-07-25 17:46:44 +00:00
it ( 'should not be able to authorize OAuth code for a REST API source if user of another organization' , async ( ) = > {
2021-09-21 04:50:02 +00:00
const adminUserData = await createUser ( app , {
email : 'admin@tooljet.io' ,
2021-10-11 15:15:58 +00:00
groups : [ 'all_users' , 'admin' ] ,
2021-09-21 04:50:02 +00:00
} ) ;
const anotherOrgAdminUserData = await createUser ( app , {
email : 'another@tooljet.io' ,
2021-10-11 15:15:58 +00:00
groups : [ 'all_users' , 'admin' ] ,
2021-09-21 04:50:02 +00:00
} ) ;
2023-01-09 12:00:32 +00:00
const { dataSource } = await generateAppDefaults ( app , adminUserData . user , {
isQueryNeeded : false ,
2021-09-21 04:50:02 +00:00
} ) ;
2021-07-25 17:46:44 +00:00
2023-04-06 11:12:58 +00:00
const loggedUser = await authenticateUser ( app , anotherOrgAdminUserData . user . email ) ;
2021-07-25 17:46:44 +00:00
// Should not update if user of another org
const response = await request ( app . getHttpServer ( ) )
2021-10-15 09:05:11 +00:00
. post ( ` /api/data_sources/ ${ dataSource . id } /authorize_oauth2 ` )
2023-04-06 11:12:58 +00:00
. set ( 'tj-workspace-id' , anotherOrgAdminUserData . user . defaultOrganizationId )
. set ( 'Cookie' , loggedUser . tokenCookie )
2021-07-25 17:46:44 +00:00
. send ( {
2021-09-21 04:50:02 +00:00
code : 'oauth-auth-code' ,
} ) ;
2021-07-25 17:46:44 +00:00
2023-04-14 10:07:42 +00:00
expect ( response . statusCode ) . toBe ( 400 ) ;
2021-07-25 17:46:44 +00:00
} ) ;
2023-04-06 11:12:58 +00:00
afterAll ( async ( ) = > {
await app . close ( ) ;
} ) ;
2021-09-21 04:50:02 +00:00
} ) ;