2023-03-31 04:51:38 -07:00
|
|
|
import Container from 'typedi';
|
2023-03-17 09:24:05 -07:00
|
|
|
import type { SuperAgentTest } from 'supertest';
|
|
|
|
import type { User } from '@db/entities/User';
|
|
|
|
import { setSamlLoginEnabled } from '@/sso/saml/samlHelpers';
|
2023-03-24 09:46:06 -07:00
|
|
|
import { getCurrentAuthenticationMethod, setCurrentAuthenticationMethod } from '@/sso/ssoHelpers';
|
2023-03-31 04:51:38 -07:00
|
|
|
import { License } from '@/License';
|
2023-03-03 01:05:30 -08:00
|
|
|
import { randomEmail, randomName, randomValidPassword } from '../shared/random';
|
|
|
|
import * as testDb from '../shared/testDb';
|
|
|
|
import * as utils from '../shared/utils';
|
2023-03-24 09:46:06 -07:00
|
|
|
import { sampleConfig } from './sampleMetadata';
|
2023-03-03 01:05:30 -08:00
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
let owner: User;
|
|
|
|
let authOwnerAgent: SuperAgentTest;
|
2023-03-03 01:05:30 -08:00
|
|
|
|
2023-03-23 08:54:35 -07:00
|
|
|
async function enableSaml(enable: boolean) {
|
|
|
|
await setSamlLoginEnabled(enable);
|
2023-03-03 01:05:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-03-31 04:51:38 -07:00
|
|
|
Container.get(License).isSamlEnabled = () => true;
|
2023-03-24 09:46:06 -07:00
|
|
|
const app = await utils.initTestServer({ endpointGroups: ['me', 'saml'] });
|
2023-03-17 09:24:05 -07:00
|
|
|
owner = await testDb.createOwner();
|
|
|
|
authOwnerAgent = utils.createAuthAgent(app)(owner);
|
2023-03-03 01:05:30 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
2023-03-31 04:51:38 -07:00
|
|
|
Container.reset();
|
2023-03-03 01:05:30 -08:00
|
|
|
await testDb.terminate();
|
|
|
|
});
|
|
|
|
|
2023-03-16 07:34:28 -07:00
|
|
|
describe('Instance owner', () => {
|
2023-03-17 09:24:05 -07:00
|
|
|
describe('PATCH /me', () => {
|
|
|
|
test('should succeed with valid inputs', async () => {
|
2023-03-23 08:54:35 -07:00
|
|
|
await enableSaml(false);
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerAgent
|
|
|
|
.patch('/me')
|
|
|
|
.send({
|
|
|
|
email: randomEmail(),
|
|
|
|
firstName: randomName(),
|
|
|
|
lastName: randomName(),
|
|
|
|
password: randomValidPassword(),
|
|
|
|
})
|
|
|
|
.expect(200);
|
2023-03-03 01:05:30 -08:00
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
test('should throw BadRequestError if email is changed when SAML is enabled', async () => {
|
2023-03-23 08:54:35 -07:00
|
|
|
await enableSaml(true);
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerAgent
|
|
|
|
.patch('/me')
|
|
|
|
.send({
|
|
|
|
email: randomEmail(),
|
|
|
|
firstName: randomName(),
|
|
|
|
lastName: randomName(),
|
|
|
|
})
|
|
|
|
.expect(400, { code: 400, message: 'SAML user may not change their email' });
|
2023-03-03 01:05:30 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-17 09:24:05 -07:00
|
|
|
describe('PATCH /password', () => {
|
|
|
|
test('should throw BadRequestError if password is changed when SAML is enabled', async () => {
|
2023-03-23 08:54:35 -07:00
|
|
|
await enableSaml(true);
|
2023-03-17 09:24:05 -07:00
|
|
|
await authOwnerAgent
|
|
|
|
.patch('/me/password')
|
|
|
|
.send({
|
|
|
|
password: randomValidPassword(),
|
|
|
|
})
|
|
|
|
.expect(400, {
|
|
|
|
code: 400,
|
|
|
|
message: 'With SAML enabled, users need to use their SAML provider to change passwords',
|
|
|
|
});
|
2023-03-03 01:05:30 -08:00
|
|
|
});
|
|
|
|
});
|
2023-03-24 09:46:06 -07:00
|
|
|
|
|
|
|
describe('POST /sso/saml/config', () => {
|
|
|
|
test('should post saml config', async () => {
|
|
|
|
await authOwnerAgent
|
|
|
|
.post('/sso/saml/config')
|
|
|
|
.send({
|
|
|
|
...sampleConfig,
|
|
|
|
loginEnabled: true,
|
|
|
|
})
|
|
|
|
.expect(200);
|
|
|
|
expect(getCurrentAuthenticationMethod()).toBe('saml');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('POST /sso/saml/config/toggle', () => {
|
|
|
|
test('should toggle saml as default authentication method', async () => {
|
|
|
|
await enableSaml(true);
|
|
|
|
expect(getCurrentAuthenticationMethod()).toBe('saml');
|
|
|
|
|
|
|
|
await authOwnerAgent
|
|
|
|
.post('/sso/saml/config/toggle')
|
|
|
|
.send({
|
|
|
|
loginEnabled: false,
|
|
|
|
})
|
|
|
|
.expect(200);
|
|
|
|
expect(getCurrentAuthenticationMethod()).toBe('email');
|
|
|
|
|
|
|
|
await authOwnerAgent
|
|
|
|
.post('/sso/saml/config/toggle')
|
|
|
|
.send({
|
|
|
|
loginEnabled: true,
|
|
|
|
})
|
|
|
|
.expect(200);
|
|
|
|
expect(getCurrentAuthenticationMethod()).toBe('saml');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('POST /sso/saml/config/toggle', () => {
|
|
|
|
test('should fail enable saml if default authentication is not email', async () => {
|
|
|
|
await enableSaml(true);
|
|
|
|
|
|
|
|
await authOwnerAgent
|
|
|
|
.post('/sso/saml/config/toggle')
|
|
|
|
.send({
|
|
|
|
loginEnabled: false,
|
|
|
|
})
|
|
|
|
.expect(200);
|
|
|
|
expect(getCurrentAuthenticationMethod()).toBe('email');
|
|
|
|
|
|
|
|
await setCurrentAuthenticationMethod('ldap');
|
|
|
|
expect(getCurrentAuthenticationMethod()).toBe('ldap');
|
|
|
|
|
|
|
|
await authOwnerAgent
|
|
|
|
.post('/sso/saml/config/toggle')
|
|
|
|
.send({
|
|
|
|
loginEnabled: true,
|
|
|
|
})
|
2023-04-04 05:28:29 -07:00
|
|
|
.expect(500);
|
2023-03-24 09:46:06 -07:00
|
|
|
|
|
|
|
expect(getCurrentAuthenticationMethod()).toBe('ldap');
|
|
|
|
});
|
|
|
|
});
|
2023-03-03 01:05:30 -08:00
|
|
|
});
|