ToolJet/server/test/controllers/oauth/oauth-git.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

598 lines
20 KiB
TypeScript

import * as request from 'supertest';
import { INestApplication } from '@nestjs/common';
import { clearDB, createUser, createNestAppInstanceWithEnvMock, generateRedirectUrl } from '../../test.helper';
import { mocked } from 'ts-jest/utils';
import got from 'got';
import { Organization } from 'src/entities/organization.entity';
import { Repository } from 'typeorm';
import { SSOConfigs } from 'src/entities/sso_config.entity';
jest.mock('got');
const mockedGot = mocked(got);
describe('oauth controller', () => {
let app: INestApplication;
let ssoConfigsRepository: Repository<SSOConfigs>;
let orgRepository: Repository<Organization>;
const authResponseKeys = ['id', 'email', 'first_name', 'last_name', 'current_organization_id'].sort();
beforeEach(async () => {
await clearDB();
});
beforeAll(async () => {
({ app } = await createNestAppInstanceWithEnvMock());
ssoConfigsRepository = app.get('SSOConfigsRepository');
orgRepository = app.get('OrganizationRepository');
});
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});
describe('SSO Login', () => {
let current_organization: Organization;
beforeEach(async () => {
const { organization } = await createUser(app, {
email: 'anotherUser@tooljet.io',
ssoConfigs: [
{
sso: 'git',
enabled: true,
configs: { clientId: 'client-id' },
},
],
enableSignUp: true,
});
current_organization = organization;
});
describe('Multi-Workspace', () => {
describe('sign in via Git OAuth', () => {
let sso_configs;
const token = 'some-Token';
beforeEach(() => {
sso_configs = current_organization.ssoConfigs.find((conf) => conf.sso === 'git');
});
it('should return 401 if git sign in is disabled', async () => {
await ssoConfigsRepository.update(sso_configs.id, { enabled: false });
await request(app.getHttpServer())
.post('/api/oauth/sign-in/' + sso_configs.id)
.send({ token })
.expect(401);
});
it('should return 401 when the user does not exist and sign up is disabled', async () => {
await orgRepository.update(current_organization.id, { enableSignUp: false });
const gitAuthResponse = jest.fn();
gitAuthResponse.mockImplementation(() => {
return {
json: () => {
return {
access_token: 'some-access-token',
scope: 'scope',
token_type: 'bearer',
};
},
};
});
const gitGetUserResponse = jest.fn();
gitGetUserResponse.mockImplementation(() => {
return {
json: () => {
return {
name: 'SSO UserGit',
email: 'ssousergit@tooljet.io',
};
},
};
});
mockedGot.mockImplementationOnce(gitAuthResponse);
mockedGot.mockImplementationOnce(gitGetUserResponse);
await request(app.getHttpServer())
.post('/api/oauth/sign-in/' + sso_configs.id)
.send({ token })
.expect(401);
});
it('should return 401 when the user does not exist domain mismatch', async () => {
await orgRepository.update(current_organization.id, { domain: 'tooljet.io,tooljet.com' });
const gitAuthResponse = jest.fn();
gitAuthResponse.mockImplementation(() => {
return {
json: () => {
return {
access_token: 'some-access-token',
scope: 'scope',
token_type: 'bearer',
};
},
};
});
const gitGetUserResponse = jest.fn();
gitGetUserResponse.mockImplementation(() => {
return {
json: () => {
return {
name: 'SSO UserGit',
email: 'ssoUserGit@tooljett.io',
};
},
};
});
mockedGot.mockImplementationOnce(gitAuthResponse);
mockedGot.mockImplementationOnce(gitGetUserResponse);
await request(app.getHttpServer())
.post('/api/oauth/sign-in/' + sso_configs.id)
.send({ token })
.expect(401);
});
it('should return redirect url when the user does not exist and domain matches and sign up is enabled', async () => {
await orgRepository.update(current_organization.id, { domain: 'tooljet.io,tooljet.com' });
const gitAuthResponse = jest.fn();
gitAuthResponse.mockImplementation(() => {
return {
json: () => {
return {
access_token: 'some-access-token',
scope: 'scope',
token_type: 'bearer',
};
},
};
});
const gitGetUserResponse = jest.fn();
gitGetUserResponse.mockImplementation(() => {
return {
json: () => {
return {
name: 'SSO UserGit',
email: 'ssousergit@tooljet.io',
};
},
};
});
mockedGot.mockImplementationOnce(gitAuthResponse);
mockedGot.mockImplementationOnce(gitGetUserResponse);
const response = await request(app.getHttpServer())
.post('/api/oauth/sign-in/' + sso_configs.id)
.send({ token });
expect(response.statusCode).toBe(201);
const url = await generateRedirectUrl('ssousergit@tooljet.io', current_organization);
const { redirect_url } = response.body;
expect(redirect_url).toEqual(url);
});
it('should return redirect url when the user does not exist and domain includes spance matches and sign up is enabled', async () => {
await orgRepository.update(current_organization.id, {
domain: ' tooljet.io , tooljet.com, , , gmail.com',
});
const gitAuthResponse = jest.fn();
gitAuthResponse.mockImplementation(() => {
return {
json: () => {
return {
access_token: 'some-access-token',
scope: 'scope',
token_type: 'bearer',
};
},
};
});
const gitGetUserResponse = jest.fn();
gitGetUserResponse.mockImplementation(() => {
return {
json: () => {
return {
name: 'SSO UserGit',
email: 'ssousergit@tooljet.io',
};
},
};
});
mockedGot.mockImplementationOnce(gitAuthResponse);
mockedGot.mockImplementationOnce(gitGetUserResponse);
const response = await request(app.getHttpServer())
.post('/api/oauth/sign-in/' + sso_configs.id)
.send({ token });
expect(response.statusCode).toBe(201);
const url = await generateRedirectUrl('ssousergit@tooljet.io', current_organization);
const { redirect_url } = response.body;
expect(redirect_url).toEqual(url);
});
it('should return redirect url when the user does not exist and sign up is enabled', async () => {
const gitAuthResponse = jest.fn();
gitAuthResponse.mockImplementation(() => {
return {
json: () => {
return {
access_token: 'some-access-token',
scope: 'scope',
token_type: 'bearer',
};
},
};
});
const gitGetUserResponse = jest.fn();
gitGetUserResponse.mockImplementation(() => {
return {
json: () => {
return {
name: 'SSO UserGit',
email: 'ssousergit@tooljet.io',
};
},
};
});
mockedGot.mockImplementationOnce(gitAuthResponse);
mockedGot.mockImplementationOnce(gitGetUserResponse);
const response = await request(app.getHttpServer())
.post('/api/oauth/sign-in/' + sso_configs.id)
.send({ token });
expect(response.statusCode).toBe(201);
const url = await generateRedirectUrl('ssousergit@tooljet.io', current_organization);
const { redirect_url } = response.body;
expect(redirect_url).toEqual(url);
});
it('should return redirect url when the user does not exist and name not available and sign up is enabled', async () => {
const gitAuthResponse = jest.fn();
gitAuthResponse.mockImplementation(() => {
return {
json: () => {
return {
access_token: 'some-access-token',
scope: 'scope',
token_type: 'bearer',
};
},
};
});
const gitGetUserResponse = jest.fn();
gitGetUserResponse.mockImplementation(() => {
return {
json: () => {
return {
name: '',
email: 'ssousergit@tooljet.io',
};
},
};
});
mockedGot.mockImplementationOnce(gitAuthResponse);
mockedGot.mockImplementationOnce(gitGetUserResponse);
const response = await request(app.getHttpServer())
.post('/api/oauth/sign-in/' + sso_configs.id)
.send({ token });
expect(response.statusCode).toBe(201);
const url = await generateRedirectUrl('ssousergit@tooljet.io', current_organization);
const { redirect_url } = response.body;
expect(redirect_url).toEqual(url);
});
it('should return redirect url when the user does not exist and email id not available and sign up is enabled', async () => {
const gitAuthResponse = jest.fn();
gitAuthResponse.mockImplementation(() => {
return {
json: () => {
return {
access_token: 'some-access-token',
scope: 'scope',
token_type: 'bearer',
};
},
};
});
const gitGetUserResponse = jest.fn();
gitGetUserResponse.mockImplementation(() => {
return {
json: () => {
return {
name: '',
email: '',
};
},
};
});
const gitGetUserEmailResponse = jest.fn();
gitGetUserEmailResponse.mockImplementation(() => {
return {
json: () => {
return [
{
email: 'ssousergit@tooljet.io',
primary: true,
verified: true,
},
{
email: 'ssoUserGit2@tooljet.io',
primary: false,
verified: true,
},
];
},
};
});
mockedGot.mockImplementationOnce(gitAuthResponse);
mockedGot.mockImplementationOnce(gitGetUserResponse);
mockedGot.mockImplementationOnce(gitGetUserEmailResponse);
const response = await request(app.getHttpServer())
.post('/api/oauth/sign-in/' + sso_configs.id)
.send({ token });
expect(response.statusCode).toBe(201);
const url = await generateRedirectUrl('ssousergit@tooljet.io', current_organization);
const { redirect_url } = response.body;
expect(redirect_url).toEqual(url);
});
it('should return login info when the user exist', async () => {
await createUser(app, {
firstName: 'SSO',
lastName: 'userExist',
email: 'anotheruser1@tooljet.io',
groups: ['all_users'],
organization: current_organization,
status: 'active',
});
const gitAuthResponse = jest.fn();
gitAuthResponse.mockImplementation(() => {
return {
json: () => {
return {
access_token: 'some-access-token',
scope: 'scope',
token_type: 'bearer',
};
},
};
});
const gitGetUserResponse = jest.fn();
gitGetUserResponse.mockImplementation(() => {
return {
json: () => {
return {
name: 'SSO userExist',
email: 'anotheruser1@tooljet.io',
};
},
};
});
mockedGot.mockImplementationOnce(gitAuthResponse);
mockedGot.mockImplementationOnce(gitGetUserResponse);
const response = await request(app.getHttpServer())
.post('/api/oauth/sign-in/' + sso_configs.id)
.send({ token });
expect(response.statusCode).toBe(201);
expect(Object.keys(response.body).sort()).toEqual(authResponseKeys);
const { email, first_name, last_name, current_organization_id } = response.body;
expect(email).toEqual('anotheruser1@tooljet.io');
expect(first_name).toEqual('SSO');
expect(last_name).toEqual('userExist');
expect(current_organization_id).toBe(current_organization.id);
});
it('should return login info when the user exist with invited status', async () => {
const { orgUser } = await createUser(app, {
firstName: 'SSO',
lastName: 'userExist',
email: 'anotheruser1@tooljet.io',
groups: ['all_users'],
organization: current_organization,
status: 'invited',
});
const gitAuthResponse = jest.fn();
gitAuthResponse.mockImplementation(() => {
return {
json: () => {
return {
access_token: 'some-access-token',
scope: 'scope',
token_type: 'bearer',
};
},
};
});
const gitGetUserResponse = jest.fn();
gitGetUserResponse.mockImplementation(() => {
return {
json: () => {
return {
name: 'SSO userExist',
email: 'anotheruser1@tooljet.io',
};
},
};
});
mockedGot.mockImplementationOnce(gitAuthResponse);
mockedGot.mockImplementationOnce(gitGetUserResponse);
const response = await request(app.getHttpServer())
.post('/api/oauth/sign-in/' + sso_configs.id)
.send({ token });
expect(response.statusCode).toBe(201);
expect(Object.keys(response.body).sort()).toEqual(authResponseKeys);
const { email, first_name, last_name, current_organization_id } = response.body;
expect(email).toEqual('anotheruser1@tooljet.io');
expect(first_name).toEqual('SSO');
expect(last_name).toEqual('userExist');
expect(current_organization_id).toBe(current_organization.id);
await orgUser.reload();
expect(orgUser.status).toEqual('active');
});
it('should return login info when the user exist and hostname exist in configs', async () => {
await ssoConfigsRepository.update(sso_configs.id, {
configs: { clientId: 'some-client-id', hostName: 'https://github.host.com' },
});
const { orgUser } = await createUser(app, {
firstName: 'SSO',
lastName: 'userExist',
email: 'anotheruser1@tooljet.io',
groups: ['all_users'],
organization: current_organization,
});
const gitAuthResponse = jest.fn();
gitAuthResponse.mockImplementation(() => {
return {
json: () => {
return {
access_token: 'some-access-token',
scope: 'scope',
token_type: 'bearer',
};
},
};
});
const gitGetUserResponse = jest.fn();
gitGetUserResponse.mockImplementation(() => {
return {
json: () => {
return {
name: 'SSO userExist',
email: 'anotheruser1@tooljet.io',
};
},
};
});
mockedGot.mockImplementationOnce(gitAuthResponse);
mockedGot.mockImplementationOnce(gitGetUserResponse);
const response = await request(app.getHttpServer())
.post('/api/oauth/sign-in/' + sso_configs.id)
.send({ token });
expect(response.statusCode).toBe(201);
expect(gitAuthResponse).toBeCalledWith('https://github.host.com/login/oauth/access_token', expect.anything());
expect(gitGetUserResponse).toBeCalledWith('https://github.host.com/api/v3/user', expect.anything());
expect(Object.keys(response.body).sort()).toEqual(authResponseKeys);
const { email, first_name, last_name, current_organization_id } = response.body;
expect(email).toEqual('anotheruser1@tooljet.io');
expect(first_name).toEqual('SSO');
expect(last_name).toEqual('userExist');
expect(current_organization_id).toBe(current_organization.id);
await orgUser.reload();
expect(orgUser.status).toEqual('active');
});
it('should return redirect url when the user does not exist and email id not available and sign up is enabled, host name configured', async () => {
await ssoConfigsRepository.update(sso_configs.id, {
configs: { clientId: 'some-client-id', hostName: 'https://github.host.com' },
});
const gitAuthResponse = jest.fn();
gitAuthResponse.mockImplementation(() => {
return {
json: () => {
return {
access_token: 'some-access-token',
scope: 'scope',
token_type: 'bearer',
};
},
};
});
const gitGetUserResponse = jest.fn();
gitGetUserResponse.mockImplementation(() => {
return {
json: () => {
return {
name: '',
email: '',
};
},
};
});
const gitGetUserEmailResponse = jest.fn();
gitGetUserEmailResponse.mockImplementation(() => {
return {
json: () => {
return [
{
email: 'ssousergit@tooljet.io',
primary: true,
verified: true,
},
{
email: 'ssousergit2@tooljet.io',
primary: false,
verified: true,
},
];
},
};
});
mockedGot.mockImplementationOnce(gitAuthResponse);
mockedGot.mockImplementationOnce(gitGetUserResponse);
mockedGot.mockImplementationOnce(gitGetUserEmailResponse);
const response = await request(app.getHttpServer())
.post('/api/oauth/sign-in/' + sso_configs.id)
.send({ token });
expect(response.statusCode).toBe(201);
expect(gitAuthResponse).toBeCalledWith('https://github.host.com/login/oauth/access_token', expect.anything());
expect(gitGetUserResponse).toBeCalledWith('https://github.host.com/api/v3/user', expect.anything());
expect(gitGetUserEmailResponse).toBeCalledWith(
'https://github.host.com/api/v3/user/emails',
expect.anything()
);
expect(response.statusCode).toBe(201);
const url = await generateRedirectUrl('ssousergit@tooljet.io', current_organization);
const { redirect_url } = response.body;
expect(redirect_url).toEqual(url);
});
});
});
});
afterAll(async () => {
await app.close();
});
});