2023-07-13 01:14:48 -07:00
|
|
|
import type { SuperAgentTest } from 'supertest';
|
2023-06-21 04:22:00 -07:00
|
|
|
import validator from 'validator';
|
|
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
import type { Role } from '@db/entities/Role';
|
|
|
|
import { License } from '@/License';
|
2023-06-21 04:22:00 -07:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
import { randomApiKey } from '../shared/random';
|
|
|
|
import * as utils from '../shared/utils/';
|
2023-06-21 04:22:00 -07:00
|
|
|
import * as testDb from '../shared/testDb';
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
utils.mockInstance(License, {
|
|
|
|
getUsersLimit: jest.fn().mockReturnValue(-1),
|
|
|
|
});
|
|
|
|
|
|
|
|
const testServer = utils.setupTestServer({ endpointGroups: ['publicApi'] });
|
2023-06-21 04:22:00 -07:00
|
|
|
|
|
|
|
let globalOwnerRole: Role;
|
|
|
|
let globalMemberRole: Role;
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-07-13 01:14:48 -07:00
|
|
|
[globalOwnerRole, globalMemberRole] = await testDb.getAllRoles();
|
2023-06-21 04:22:00 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
2023-07-13 01:14:48 -07:00
|
|
|
await testDb.truncate(['SharedCredentials', 'SharedWorkflow', 'Workflow', 'Credentials', 'User']);
|
2023-06-21 04:22:00 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('With license unlimited quota:users', () => {
|
2023-07-13 01:14:48 -07:00
|
|
|
describe('GET /users', () => {
|
|
|
|
test('should fail due to missing API Key', async () => {
|
|
|
|
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
|
|
|
|
const authOwnerAgent = testServer.publicApiAgentFor(owner);
|
|
|
|
await authOwnerAgent.get('/users').expect(401);
|
2023-06-21 04:22:00 -07:00
|
|
|
});
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
test('should fail due to invalid API Key', async () => {
|
|
|
|
const owner = await testDb.createUser({
|
|
|
|
globalRole: globalOwnerRole,
|
|
|
|
apiKey: randomApiKey(),
|
|
|
|
});
|
|
|
|
owner.apiKey = 'invalid-key';
|
|
|
|
const authOwnerAgent = testServer.publicApiAgentFor(owner);
|
|
|
|
await authOwnerAgent.get('/users').expect(401);
|
2023-06-21 04:22:00 -07:00
|
|
|
});
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
test('should fail due to member trying to access owner only endpoint', async () => {
|
|
|
|
const member = await testDb.createUser({ apiKey: randomApiKey() });
|
|
|
|
const authMemberAgent = testServer.publicApiAgentFor(member);
|
|
|
|
await authMemberAgent.get('/users').expect(403);
|
|
|
|
});
|
2023-06-21 04:22:00 -07:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
test('should return all users', async () => {
|
|
|
|
const owner = await testDb.createUser({
|
|
|
|
globalRole: globalOwnerRole,
|
|
|
|
apiKey: randomApiKey(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const authOwnerAgent = testServer.publicApiAgentFor(owner);
|
|
|
|
|
|
|
|
await testDb.createUser();
|
|
|
|
|
|
|
|
const response = await authOwnerAgent.get('/users').expect(200);
|
|
|
|
expect(response.body.data.length).toBe(2);
|
|
|
|
expect(response.body.nextCursor).toBeNull();
|
|
|
|
|
|
|
|
for (const user of response.body.data) {
|
|
|
|
const {
|
|
|
|
id,
|
|
|
|
email,
|
|
|
|
firstName,
|
|
|
|
lastName,
|
|
|
|
personalizationAnswers,
|
|
|
|
globalRole,
|
|
|
|
password,
|
|
|
|
isPending,
|
|
|
|
createdAt,
|
|
|
|
updatedAt,
|
|
|
|
} = user;
|
|
|
|
|
|
|
|
expect(validator.isUUID(id)).toBe(true);
|
|
|
|
expect(email).toBeDefined();
|
|
|
|
expect(firstName).toBeDefined();
|
|
|
|
expect(lastName).toBeDefined();
|
|
|
|
expect(personalizationAnswers).toBeUndefined();
|
|
|
|
expect(password).toBeUndefined();
|
|
|
|
expect(isPending).toBe(false);
|
|
|
|
expect(globalRole).toBeUndefined();
|
|
|
|
expect(createdAt).toBeDefined();
|
|
|
|
expect(updatedAt).toBeDefined();
|
|
|
|
}
|
|
|
|
});
|
2023-06-21 04:22:00 -07:00
|
|
|
});
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
describe('GET /users/:id', () => {
|
|
|
|
test('should fail due to missing API Key', async () => {
|
|
|
|
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
|
|
|
|
const authOwnerAgent = testServer.publicApiAgentFor(owner);
|
|
|
|
await authOwnerAgent.get(`/users/${owner.id}`).expect(401);
|
2023-06-21 04:22:00 -07:00
|
|
|
});
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
test('should fail due to invalid API Key', async () => {
|
|
|
|
const owner = await testDb.createUser({
|
|
|
|
globalRole: globalOwnerRole,
|
|
|
|
apiKey: randomApiKey(),
|
|
|
|
});
|
|
|
|
owner.apiKey = 'invalid-key';
|
|
|
|
const authOwnerAgent = testServer.publicApiAgentFor(owner);
|
|
|
|
await authOwnerAgent.get(`/users/${owner.id}`).expect(401);
|
|
|
|
});
|
2023-06-21 04:22:00 -07:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
test('should fail due to member trying to access owner only endpoint', async () => {
|
|
|
|
const member = await testDb.createUser({ apiKey: randomApiKey() });
|
|
|
|
const authMemberAgent = testServer.publicApiAgentFor(member);
|
|
|
|
await authMemberAgent.get(`/users/${member.id}`).expect(403);
|
|
|
|
});
|
|
|
|
test('should return 404 for non-existing id ', async () => {
|
|
|
|
const owner = await testDb.createUser({
|
|
|
|
globalRole: globalOwnerRole,
|
|
|
|
apiKey: randomApiKey(),
|
|
|
|
});
|
|
|
|
const authOwnerAgent = testServer.publicApiAgentFor(owner);
|
|
|
|
await authOwnerAgent.get(`/users/${uuid()}`).expect(404);
|
2023-06-21 04:22:00 -07:00
|
|
|
});
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
test('should return a pending user', async () => {
|
|
|
|
const owner = await testDb.createUser({
|
|
|
|
globalRole: globalOwnerRole,
|
|
|
|
apiKey: randomApiKey(),
|
|
|
|
});
|
2023-06-21 04:22:00 -07:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
const { id: memberId } = await testDb.createUserShell(globalMemberRole);
|
2023-06-21 04:22:00 -07:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
const authOwnerAgent = testServer.publicApiAgentFor(owner);
|
|
|
|
const response = await authOwnerAgent.get(`/users/${memberId}`).expect(200);
|
2023-06-21 04:22:00 -07:00
|
|
|
|
|
|
|
const {
|
|
|
|
id,
|
|
|
|
email,
|
|
|
|
firstName,
|
|
|
|
lastName,
|
|
|
|
personalizationAnswers,
|
|
|
|
globalRole,
|
|
|
|
password,
|
|
|
|
isPending,
|
|
|
|
createdAt,
|
|
|
|
updatedAt,
|
2023-07-13 01:14:48 -07:00
|
|
|
} = response.body;
|
2023-06-21 04:22:00 -07:00
|
|
|
|
|
|
|
expect(validator.isUUID(id)).toBe(true);
|
|
|
|
expect(email).toBeDefined();
|
|
|
|
expect(firstName).toBeDefined();
|
|
|
|
expect(lastName).toBeDefined();
|
|
|
|
expect(personalizationAnswers).toBeUndefined();
|
|
|
|
expect(password).toBeUndefined();
|
|
|
|
expect(globalRole).toBeUndefined();
|
|
|
|
expect(createdAt).toBeDefined();
|
2023-07-13 01:14:48 -07:00
|
|
|
expect(isPending).toBeDefined();
|
|
|
|
expect(isPending).toBeTruthy();
|
2023-06-21 04:22:00 -07:00
|
|
|
expect(updatedAt).toBeDefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
describe('GET /users/:email', () => {
|
|
|
|
test('with non-existing email should return 404', async () => {
|
|
|
|
const owner = await testDb.createUser({
|
|
|
|
globalRole: globalOwnerRole,
|
|
|
|
apiKey: randomApiKey(),
|
|
|
|
});
|
|
|
|
const authOwnerAgent = testServer.publicApiAgentFor(owner);
|
|
|
|
await authOwnerAgent.get('/users/jhondoe@gmail.com').expect(404);
|
2023-06-21 04:22:00 -07:00
|
|
|
});
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
test('should return a user', async () => {
|
|
|
|
const owner = await testDb.createUser({
|
|
|
|
globalRole: globalOwnerRole,
|
|
|
|
apiKey: randomApiKey(),
|
|
|
|
});
|
2023-06-21 04:22:00 -07:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
const authOwnerAgent = testServer.publicApiAgentFor(owner);
|
|
|
|
const response = await authOwnerAgent.get(`/users/${owner.email}`).expect(200);
|
2023-06-21 04:22:00 -07:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
const {
|
|
|
|
id,
|
|
|
|
email,
|
|
|
|
firstName,
|
|
|
|
lastName,
|
|
|
|
personalizationAnswers,
|
|
|
|
globalRole,
|
|
|
|
password,
|
|
|
|
isPending,
|
|
|
|
createdAt,
|
|
|
|
updatedAt,
|
|
|
|
} = response.body;
|
2023-06-21 04:22:00 -07:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
expect(validator.isUUID(id)).toBe(true);
|
|
|
|
expect(email).toBeDefined();
|
|
|
|
expect(firstName).toBeDefined();
|
|
|
|
expect(lastName).toBeDefined();
|
|
|
|
expect(personalizationAnswers).toBeUndefined();
|
|
|
|
expect(password).toBeUndefined();
|
|
|
|
expect(isPending).toBe(false);
|
|
|
|
expect(globalRole).toBeUndefined();
|
|
|
|
expect(createdAt).toBeDefined();
|
|
|
|
expect(updatedAt).toBeDefined();
|
2023-06-21 04:22:00 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('With license without quota:users', () => {
|
2023-07-13 01:14:48 -07:00
|
|
|
let authOwnerAgent: SuperAgentTest;
|
|
|
|
|
2023-06-21 04:22:00 -07:00
|
|
|
beforeEach(async () => {
|
|
|
|
utils.mockInstance(License, { getUsersLimit: jest.fn().mockReturnValue(null) });
|
2023-07-13 01:14:48 -07:00
|
|
|
|
|
|
|
const owner = await testDb.createUser({
|
|
|
|
globalRole: globalOwnerRole,
|
|
|
|
apiKey: randomApiKey(),
|
|
|
|
});
|
|
|
|
authOwnerAgent = testServer.publicApiAgentFor(owner);
|
2023-06-21 04:22:00 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('GET /users should fail due to invalid license', async () => {
|
2023-07-13 01:14:48 -07:00
|
|
|
await authOwnerAgent.get('/users').expect(403);
|
2023-06-21 04:22:00 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('GET /users/:id should fail due to invalid license', async () => {
|
2023-07-13 01:14:48 -07:00
|
|
|
await authOwnerAgent.get(`/users/${uuid()}`).expect(403);
|
2023-06-21 04:22:00 -07:00
|
|
|
});
|
|
|
|
});
|