2023-11-16 09:39:43 -08:00
|
|
|
import validator from 'validator';
|
|
|
|
import type { SuperAgentTest } from 'supertest';
|
|
|
|
|
|
|
|
import type { User } from '@db/entities/User';
|
|
|
|
import { compareHash } from '@/UserManagement/UserManagementHelper';
|
|
|
|
import { UserManagementMailer } from '@/UserManagement/email/UserManagementMailer';
|
|
|
|
|
|
|
|
import Container from 'typedi';
|
|
|
|
import { UserRepository } from '@db/repositories/user.repository';
|
|
|
|
|
|
|
|
import { mockInstance } from '../shared/mocking';
|
|
|
|
import {
|
|
|
|
randomEmail,
|
|
|
|
randomInvalidPassword,
|
|
|
|
randomName,
|
|
|
|
randomValidPassword,
|
|
|
|
} from './shared/random';
|
|
|
|
import * as testDb from './shared/testDb';
|
|
|
|
import * as utils from './shared/utils/';
|
2023-11-29 04:55:41 -08:00
|
|
|
import { getGlobalAdminRole, getGlobalMemberRole } from './shared/db/roles';
|
2023-11-16 09:39:43 -08:00
|
|
|
import { createMember, createOwner, createUser, createUserShell } from './shared/db/users';
|
|
|
|
import { ExternalHooks } from '@/ExternalHooks';
|
|
|
|
import { InternalHooks } from '@/InternalHooks';
|
2023-11-29 04:55:41 -08:00
|
|
|
import type { UserInvitationResponse } from './shared/utils/users';
|
|
|
|
import {
|
|
|
|
assertInviteUserSuccessResponse,
|
|
|
|
assertInvitedUsersOnDb,
|
|
|
|
assertInviteUserErrorResponse,
|
|
|
|
} from './shared/utils/users';
|
|
|
|
import { mocked } from 'jest-mock';
|
|
|
|
import { License } from '@/License';
|
2023-11-16 09:39:43 -08:00
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
mockInstance(InternalHooks);
|
2023-11-16 09:39:43 -08:00
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
const license = mockInstance(License, {
|
|
|
|
isAdvancedPermissionsLicensed: jest.fn().mockReturnValue(true),
|
|
|
|
isWithinUsersLimit: jest.fn().mockReturnValue(true),
|
|
|
|
});
|
2023-11-16 09:39:43 -08:00
|
|
|
|
|
|
|
const externalHooks = mockInstance(ExternalHooks);
|
|
|
|
const mailer = mockInstance(UserManagementMailer, { isEmailSetUp: true });
|
|
|
|
|
|
|
|
const testServer = utils.setupTestServer({ endpointGroups: ['invitations'] });
|
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
describe('POST /invitations/:id/accept', () => {
|
|
|
|
let owner: User;
|
2023-11-16 09:39:43 -08:00
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
let authlessAgent: SuperAgentTest;
|
2023-11-16 09:39:43 -08:00
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
beforeAll(async () => {
|
|
|
|
await testDb.truncate(['User']);
|
2023-11-16 09:39:43 -08:00
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
owner = await createOwner();
|
2023-11-16 09:39:43 -08:00
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
authlessAgent = testServer.authlessAgent;
|
|
|
|
});
|
2023-11-16 09:39:43 -08:00
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
test('should fill out a member shell', async () => {
|
|
|
|
const globalMemberRole = await getGlobalMemberRole();
|
2023-11-16 09:39:43 -08:00
|
|
|
const memberShell = await createUserShell(globalMemberRole);
|
|
|
|
|
|
|
|
const memberData = {
|
|
|
|
inviterId: owner.id,
|
|
|
|
firstName: randomName(),
|
|
|
|
lastName: randomName(),
|
|
|
|
password: randomValidPassword(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const response = await authlessAgent
|
2023-11-29 04:55:41 -08:00
|
|
|
.post(`/invitations/${memberShell.id}/accept`)
|
|
|
|
.send(memberData)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
const {
|
|
|
|
id,
|
|
|
|
email,
|
|
|
|
firstName,
|
|
|
|
lastName,
|
|
|
|
personalizationAnswers,
|
|
|
|
password,
|
|
|
|
globalRole,
|
|
|
|
isPending,
|
|
|
|
apiKey,
|
2023-12-05 02:18:41 -08:00
|
|
|
globalScopes,
|
2023-11-29 04:55:41 -08:00
|
|
|
} = response.body.data;
|
|
|
|
|
|
|
|
expect(validator.isUUID(id)).toBe(true);
|
|
|
|
expect(email).toBeDefined();
|
|
|
|
expect(firstName).toBe(memberData.firstName);
|
|
|
|
expect(lastName).toBe(memberData.lastName);
|
|
|
|
expect(personalizationAnswers).toBeNull();
|
|
|
|
expect(password).toBeUndefined();
|
|
|
|
expect(isPending).toBe(false);
|
|
|
|
expect(globalRole.scope).toBe('global');
|
|
|
|
expect(globalRole.name).toBe('member');
|
|
|
|
expect(apiKey).not.toBeDefined();
|
2023-12-05 02:18:41 -08:00
|
|
|
expect(globalScopes).toBeDefined();
|
|
|
|
expect(globalScopes).not.toHaveLength(0);
|
2023-11-29 04:55:41 -08:00
|
|
|
|
|
|
|
const authToken = utils.getAuthToken(response);
|
|
|
|
expect(authToken).toBeDefined();
|
|
|
|
|
|
|
|
const storedMember = await Container.get(UserRepository).findOneByOrFail({
|
|
|
|
id: memberShell.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(storedMember.firstName).toBe(memberData.firstName);
|
|
|
|
expect(storedMember.lastName).toBe(memberData.lastName);
|
|
|
|
expect(storedMember.password).not.toBe(memberData.password);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fill out an admin shell', async () => {
|
|
|
|
const globalAdminRole = await getGlobalAdminRole();
|
|
|
|
const adminShell = await createUserShell(globalAdminRole);
|
|
|
|
|
|
|
|
const memberData = {
|
|
|
|
inviterId: owner.id,
|
|
|
|
firstName: randomName(),
|
|
|
|
lastName: randomName(),
|
|
|
|
password: randomValidPassword(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const response = await authlessAgent
|
|
|
|
.post(`/invitations/${adminShell.id}/accept`)
|
|
|
|
.send(memberData)
|
|
|
|
.expect(200);
|
2023-11-16 09:39:43 -08:00
|
|
|
|
|
|
|
const {
|
|
|
|
id,
|
|
|
|
email,
|
|
|
|
firstName,
|
|
|
|
lastName,
|
|
|
|
personalizationAnswers,
|
|
|
|
password,
|
|
|
|
globalRole,
|
|
|
|
isPending,
|
|
|
|
apiKey,
|
2023-12-05 02:18:41 -08:00
|
|
|
globalScopes,
|
2023-11-16 09:39:43 -08:00
|
|
|
} = response.body.data;
|
|
|
|
|
|
|
|
expect(validator.isUUID(id)).toBe(true);
|
|
|
|
expect(email).toBeDefined();
|
|
|
|
expect(firstName).toBe(memberData.firstName);
|
|
|
|
expect(lastName).toBe(memberData.lastName);
|
|
|
|
expect(personalizationAnswers).toBeNull();
|
|
|
|
expect(password).toBeUndefined();
|
|
|
|
expect(isPending).toBe(false);
|
2023-11-29 04:55:41 -08:00
|
|
|
expect(globalRole.scope).toBe('global');
|
|
|
|
expect(globalRole.name).toBe('admin');
|
2023-11-16 09:39:43 -08:00
|
|
|
expect(apiKey).not.toBeDefined();
|
2023-12-05 02:18:41 -08:00
|
|
|
expect(globalScopes).toBeDefined();
|
|
|
|
expect(globalScopes).not.toHaveLength(0);
|
2023-11-16 09:39:43 -08:00
|
|
|
|
|
|
|
const authToken = utils.getAuthToken(response);
|
|
|
|
expect(authToken).toBeDefined();
|
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
const storedAdmin = await Container.get(UserRepository).findOneByOrFail({
|
|
|
|
id: adminShell.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(storedAdmin.firstName).toBe(memberData.firstName);
|
|
|
|
expect(storedAdmin.lastName).toBe(memberData.lastName);
|
|
|
|
expect(storedAdmin.password).not.toBe(memberData.password);
|
2023-11-16 09:39:43 -08:00
|
|
|
});
|
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
test('should fail with invalid payloads', async () => {
|
2023-11-16 09:39:43 -08:00
|
|
|
const memberShellEmail = randomEmail();
|
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
const globalMemberRole = await getGlobalMemberRole();
|
|
|
|
|
2023-11-16 09:39:43 -08:00
|
|
|
const memberShell = await Container.get(UserRepository).save({
|
|
|
|
email: memberShellEmail,
|
|
|
|
globalRole: globalMemberRole,
|
|
|
|
});
|
|
|
|
|
|
|
|
const invalidPayloads = [
|
|
|
|
{
|
|
|
|
firstName: randomName(),
|
|
|
|
lastName: randomName(),
|
|
|
|
password: randomValidPassword(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inviterId: owner.id,
|
|
|
|
firstName: randomName(),
|
|
|
|
password: randomValidPassword(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inviterId: owner.id,
|
|
|
|
firstName: randomName(),
|
|
|
|
password: randomValidPassword(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inviterId: owner.id,
|
|
|
|
firstName: randomName(),
|
|
|
|
lastName: randomName(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inviterId: owner.id,
|
|
|
|
firstName: randomName(),
|
|
|
|
lastName: randomName(),
|
|
|
|
password: randomInvalidPassword(),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const invalidPayload of invalidPayloads) {
|
2023-11-29 04:55:41 -08:00
|
|
|
await authlessAgent
|
2023-11-16 09:39:43 -08:00
|
|
|
.post(`/invitations/${memberShell.id}/accept`)
|
2023-11-29 04:55:41 -08:00
|
|
|
.send(invalidPayload)
|
|
|
|
.expect(400);
|
2023-11-16 09:39:43 -08:00
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
const storedMemberShell = await Container.get(UserRepository).findOneOrFail({
|
2023-11-16 09:39:43 -08:00
|
|
|
where: { email: memberShellEmail },
|
|
|
|
});
|
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
expect(storedMemberShell.firstName).toBeNull();
|
|
|
|
expect(storedMemberShell.lastName).toBeNull();
|
|
|
|
expect(storedMemberShell.password).toBeNull();
|
2023-11-16 09:39:43 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fail with already accepted invite', async () => {
|
2023-11-29 04:55:41 -08:00
|
|
|
const globalMemberRole = await getGlobalMemberRole();
|
2023-11-16 09:39:43 -08:00
|
|
|
const member = await createUser({ globalRole: globalMemberRole });
|
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
const memberData = {
|
2023-11-16 09:39:43 -08:00
|
|
|
inviterId: owner.id,
|
|
|
|
firstName: randomName(),
|
|
|
|
lastName: randomName(),
|
|
|
|
password: randomValidPassword(),
|
|
|
|
};
|
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
await authlessAgent.post(`/invitations/${member.id}/accept`).send(memberData).expect(400);
|
2023-11-16 09:39:43 -08:00
|
|
|
|
|
|
|
const storedMember = await Container.get(UserRepository).findOneOrFail({
|
|
|
|
where: { email: member.email },
|
|
|
|
});
|
2023-11-29 04:55:41 -08:00
|
|
|
|
|
|
|
expect(storedMember.firstName).not.toBe(memberData.firstName);
|
|
|
|
expect(storedMember.lastName).not.toBe(memberData.lastName);
|
|
|
|
expect(storedMember.password).not.toBe(memberData.password);
|
2023-11-16 09:39:43 -08:00
|
|
|
|
|
|
|
const comparisonResult = await compareHash(member.password, storedMember.password);
|
2023-11-29 04:55:41 -08:00
|
|
|
|
2023-11-16 09:39:43 -08:00
|
|
|
expect(comparisonResult).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('POST /invitations', () => {
|
2023-11-29 04:55:41 -08:00
|
|
|
let owner: User;
|
|
|
|
let member: User;
|
|
|
|
let ownerAgent: SuperAgentTest;
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
await testDb.truncate(['User']);
|
|
|
|
|
|
|
|
owner = await createOwner();
|
|
|
|
member = await createMember();
|
|
|
|
ownerAgent = testServer.authAgentFor(owner);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fail with invalid payloads', async () => {
|
2023-11-16 09:39:43 -08:00
|
|
|
const invalidPayloads = [
|
|
|
|
randomEmail(),
|
|
|
|
[randomEmail()],
|
|
|
|
{},
|
|
|
|
[{ name: randomName() }],
|
|
|
|
[{ email: randomName() }],
|
|
|
|
];
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
invalidPayloads.map(async (invalidPayload) => {
|
2023-11-29 04:55:41 -08:00
|
|
|
await ownerAgent.post('/invitations').send(invalidPayload).expect(400);
|
|
|
|
|
|
|
|
const usersCount = await Container.get(UserRepository).count();
|
2023-11-16 09:39:43 -08:00
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
expect(usersCount).toBe(2); // DB unaffected
|
2023-11-16 09:39:43 -08:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
test('should return 200 on empty payload', async () => {
|
|
|
|
const response = await ownerAgent.post('/invitations').send([]).expect(200);
|
2023-11-16 09:39:43 -08:00
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
expect(response.body.data).toStrictEqual([]);
|
2023-11-16 09:39:43 -08:00
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
const usersCount = await Container.get(UserRepository).count();
|
2023-11-16 09:39:43 -08:00
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
expect(usersCount).toBe(2);
|
2023-11-16 09:39:43 -08:00
|
|
|
});
|
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
test('should return 200 if emailing is not set up', async () => {
|
|
|
|
mailer.invite.mockResolvedValue({ emailSent: false });
|
|
|
|
|
|
|
|
const response = await ownerAgent
|
|
|
|
.post('/invitations')
|
|
|
|
.send([{ email: randomEmail() }])
|
|
|
|
.expect(200);
|
2023-11-16 09:39:43 -08:00
|
|
|
|
|
|
|
expect(response.body.data).toBeInstanceOf(Array);
|
|
|
|
expect(response.body.data.length).toBe(1);
|
2023-11-29 04:55:41 -08:00
|
|
|
|
2023-11-16 09:39:43 -08:00
|
|
|
const { user } = response.body.data[0];
|
2023-11-29 04:55:41 -08:00
|
|
|
|
2023-11-16 09:39:43 -08:00
|
|
|
expect(user.inviteAcceptUrl).toBeDefined();
|
2023-11-29 04:55:41 -08:00
|
|
|
|
2023-11-16 09:39:43 -08:00
|
|
|
const inviteUrl = new URL(user.inviteAcceptUrl);
|
2023-11-29 04:55:41 -08:00
|
|
|
|
2023-11-16 09:39:43 -08:00
|
|
|
expect(inviteUrl.searchParams.get('inviterId')).toBe(owner.id);
|
|
|
|
expect(inviteUrl.searchParams.get('inviteeId')).toBe(user.id);
|
|
|
|
});
|
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
test('should create member shell', async () => {
|
|
|
|
mailer.invite.mockResolvedValue({ emailSent: false });
|
|
|
|
|
|
|
|
const response = await ownerAgent
|
|
|
|
.post('/invitations')
|
|
|
|
.send([{ email: randomEmail() }])
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
const [result] = response.body.data as UserInvitationResponse[];
|
|
|
|
|
|
|
|
const storedUser = await Container.get(UserRepository).findOneByOrFail({
|
|
|
|
id: result.user.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
assertInvitedUsersOnDb(storedUser);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should create admin shell if licensed', async () => {
|
|
|
|
mailer.invite.mockResolvedValue({ emailSent: false });
|
|
|
|
|
|
|
|
const response = await ownerAgent
|
|
|
|
.post('/invitations')
|
|
|
|
.send([{ email: randomEmail(), role: 'admin' }])
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
const [result] = response.body.data as UserInvitationResponse[];
|
|
|
|
|
|
|
|
const storedUser = await Container.get(UserRepository).findOneByOrFail({
|
|
|
|
id: result.user.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
assertInvitedUsersOnDb(storedUser);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fail to create admin shell if not licensed', async () => {
|
|
|
|
license.isAdvancedPermissionsLicensed.mockReturnValue(false);
|
|
|
|
mailer.invite.mockResolvedValue({ emailSent: false });
|
|
|
|
|
|
|
|
await ownerAgent
|
|
|
|
.post('/invitations')
|
|
|
|
.send([{ email: randomEmail(), role: 'admin' }])
|
|
|
|
.expect(403);
|
|
|
|
});
|
|
|
|
|
2023-11-16 09:39:43 -08:00
|
|
|
test('should email invites and create user shells but ignore existing', async () => {
|
2023-11-29 04:55:41 -08:00
|
|
|
externalHooks.run.mockClear();
|
2023-11-16 09:39:43 -08:00
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
mailer.invite.mockResolvedValue({ emailSent: true });
|
2023-11-16 09:39:43 -08:00
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
const globalMemberRole = await getGlobalMemberRole();
|
2023-11-16 09:39:43 -08:00
|
|
|
const memberShell = await createUserShell(globalMemberRole);
|
|
|
|
|
|
|
|
const newUser = randomEmail();
|
|
|
|
|
|
|
|
const shellUsers = [memberShell.email];
|
|
|
|
const usersToInvite = [newUser, ...shellUsers];
|
|
|
|
const usersToCreate = [newUser];
|
|
|
|
const existingUsers = [member.email];
|
|
|
|
|
|
|
|
const testEmails = [...usersToInvite, ...existingUsers];
|
|
|
|
|
|
|
|
const payload = testEmails.map((email) => ({ email }));
|
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
const response = await ownerAgent.post('/invitations').send(payload).expect(200);
|
2023-11-16 09:39:43 -08:00
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
const internalHooks = Container.get(InternalHooks);
|
2023-11-16 09:39:43 -08:00
|
|
|
|
|
|
|
expect(internalHooks.onUserTransactionalEmail).toHaveBeenCalledTimes(usersToInvite.length);
|
|
|
|
|
|
|
|
expect(externalHooks.run).toHaveBeenCalledTimes(1);
|
2023-11-29 04:55:41 -08:00
|
|
|
|
2023-11-16 09:39:43 -08:00
|
|
|
const [hookName, hookData] = externalHooks.run.mock.calls[0];
|
2023-11-29 04:55:41 -08:00
|
|
|
|
2023-11-16 09:39:43 -08:00
|
|
|
expect(hookName).toBe('user.invited');
|
|
|
|
expect(hookData?.[0]).toStrictEqual(usersToCreate);
|
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
const result = response.body.data as UserInvitationResponse[];
|
|
|
|
|
|
|
|
for (const invitationResponse of result) {
|
|
|
|
assertInviteUserSuccessResponse(invitationResponse);
|
|
|
|
|
2023-11-16 09:39:43 -08:00
|
|
|
const storedUser = await Container.get(UserRepository).findOneByOrFail({
|
|
|
|
id: invitationResponse.user.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
assertInvitedUsersOnDb(storedUser);
|
|
|
|
}
|
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
const calls = mocked(internalHooks).onUserTransactionalEmail.mock.calls;
|
|
|
|
|
|
|
|
for (const [onUserTransactionalEmailParameter] of calls) {
|
2023-11-16 09:39:43 -08:00
|
|
|
expect(onUserTransactionalEmailParameter.user_id).toBeDefined();
|
|
|
|
expect(onUserTransactionalEmailParameter.message_type).toBe('New user invite');
|
|
|
|
expect(onUserTransactionalEmailParameter.public_api).toBe(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
test('should return 200 when invite method throws error', async () => {
|
2023-11-16 09:39:43 -08:00
|
|
|
mailer.invite.mockImplementation(async () => {
|
2023-11-29 04:55:41 -08:00
|
|
|
throw new Error('failed to send email');
|
2023-11-16 09:39:43 -08:00
|
|
|
});
|
|
|
|
|
2023-11-29 04:55:41 -08:00
|
|
|
const response = await ownerAgent
|
|
|
|
.post('/invitations')
|
|
|
|
.send([{ email: randomEmail() }])
|
|
|
|
.expect(200);
|
2023-11-16 09:39:43 -08:00
|
|
|
|
|
|
|
expect(response.body.data).toBeInstanceOf(Array);
|
|
|
|
expect(response.body.data.length).toBe(1);
|
2023-11-29 04:55:41 -08:00
|
|
|
|
|
|
|
const [invitationResponse] = response.body.data;
|
2023-11-16 09:39:43 -08:00
|
|
|
|
|
|
|
assertInviteUserErrorResponse(invitationResponse);
|
|
|
|
});
|
|
|
|
});
|