2024-02-28 04:12:28 -08:00
|
|
|
import Container from 'typedi';
|
|
|
|
import type { Response } from 'express';
|
|
|
|
import { anyObject, mock } from 'jest-mock-extended';
|
2023-02-17 01:59:09 -08:00
|
|
|
import jwt from 'jsonwebtoken';
|
2024-02-28 04:12:28 -08:00
|
|
|
|
|
|
|
import type { AuthService } from '@/auth/auth.service';
|
2023-12-27 02:50:43 -08:00
|
|
|
import config from '@/config';
|
2023-11-08 07:29:39 -08:00
|
|
|
import { OwnerController } from '@/controllers/owner.controller';
|
2024-02-28 04:12:28 -08:00
|
|
|
import type { User } from '@db/entities/User';
|
|
|
|
import type { SettingsRepository } from '@db/repositories/settings.repository';
|
|
|
|
import type { UserRepository } from '@db/repositories/user.repository';
|
|
|
|
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
|
|
|
|
import type { InternalHooks } from '@/InternalHooks';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { License } from '@/License';
|
2024-02-28 04:12:28 -08:00
|
|
|
import type { OwnerRequest } from '@/requests';
|
|
|
|
import type { UserService } from '@/services/user.service';
|
|
|
|
import { PasswordUtility } from '@/services/password.utility';
|
2023-11-10 06:04:26 -08:00
|
|
|
|
|
|
|
import { mockInstance } from '../../shared/mocking';
|
|
|
|
import { badPasswords } from '../shared/testData';
|
2023-02-17 01:59:09 -08:00
|
|
|
|
|
|
|
describe('OwnerController', () => {
|
2023-12-27 02:50:43 -08:00
|
|
|
const configGetSpy = jest.spyOn(config, 'getEnv');
|
|
|
|
const internalHooks = mock<InternalHooks>();
|
2024-02-28 04:12:28 -08:00
|
|
|
const authService = mock<AuthService>();
|
|
|
|
const userService = mock<UserService>();
|
|
|
|
const userRepository = mock<UserRepository>();
|
2023-04-12 01:59:14 -07:00
|
|
|
const settingsRepository = mock<SettingsRepository>();
|
2023-11-10 06:04:26 -08:00
|
|
|
mockInstance(License).isWithinUsersLimit.mockReturnValue(true);
|
2023-08-25 04:23:22 -07:00
|
|
|
const controller = new OwnerController(
|
2023-10-25 07:35:22 -07:00
|
|
|
mock(),
|
2023-02-17 01:59:09 -08:00
|
|
|
internalHooks,
|
2023-08-25 04:23:22 -07:00
|
|
|
settingsRepository,
|
2024-02-28 04:12:28 -08:00
|
|
|
authService,
|
2023-08-25 04:23:22 -07:00
|
|
|
userService,
|
2023-12-11 09:23:42 -08:00
|
|
|
Container.get(PasswordUtility),
|
2023-12-27 02:50:43 -08:00
|
|
|
mock(),
|
2024-01-02 08:53:24 -08:00
|
|
|
userRepository,
|
2023-08-25 04:23:22 -07:00
|
|
|
);
|
2023-02-17 01:59:09 -08:00
|
|
|
|
|
|
|
describe('setupOwner', () => {
|
|
|
|
it('should throw a BadRequestError if the instance owner is already setup', async () => {
|
2023-12-27 02:50:43 -08:00
|
|
|
configGetSpy.mockReturnValue(true);
|
2023-05-23 17:01:45 -07:00
|
|
|
await expect(controller.setupOwner(mock(), mock())).rejects.toThrowError(
|
2023-02-17 01:59:09 -08:00
|
|
|
new BadRequestError('Instance owner already setup'),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw a BadRequestError if the email is invalid', async () => {
|
2023-12-27 02:50:43 -08:00
|
|
|
configGetSpy.mockReturnValue(false);
|
2023-02-17 01:59:09 -08:00
|
|
|
const req = mock<OwnerRequest.Post>({ body: { email: 'invalid email' } });
|
2023-05-23 17:01:45 -07:00
|
|
|
await expect(controller.setupOwner(req, mock())).rejects.toThrowError(
|
2023-02-17 01:59:09 -08:00
|
|
|
new BadRequestError('Invalid email address'),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('should throw if the password is invalid', () => {
|
|
|
|
Object.entries(badPasswords).forEach(([password, errorMessage]) => {
|
|
|
|
it(password, async () => {
|
2023-12-27 02:50:43 -08:00
|
|
|
configGetSpy.mockReturnValue(false);
|
2023-02-17 01:59:09 -08:00
|
|
|
const req = mock<OwnerRequest.Post>({ body: { email: 'valid@email.com', password } });
|
2023-05-23 17:01:45 -07:00
|
|
|
await expect(controller.setupOwner(req, mock())).rejects.toThrowError(
|
2023-02-17 01:59:09 -08:00
|
|
|
new BadRequestError(errorMessage),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw a BadRequestError if firstName & lastName are missing ', async () => {
|
2023-12-27 02:50:43 -08:00
|
|
|
configGetSpy.mockReturnValue(false);
|
2023-02-17 01:59:09 -08:00
|
|
|
const req = mock<OwnerRequest.Post>({
|
|
|
|
body: { email: 'valid@email.com', password: 'NewPassword123', firstName: '', lastName: '' },
|
|
|
|
});
|
2023-05-23 17:01:45 -07:00
|
|
|
await expect(controller.setupOwner(req, mock())).rejects.toThrowError(
|
2023-02-17 01:59:09 -08:00
|
|
|
new BadRequestError('First and last names are mandatory'),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should setup the instance owner successfully', async () => {
|
|
|
|
const user = mock<User>({
|
|
|
|
id: 'userId',
|
2024-01-24 04:38:57 -08:00
|
|
|
role: 'global:owner',
|
2023-02-17 01:59:09 -08:00
|
|
|
authIdentities: [],
|
|
|
|
});
|
2024-04-09 02:20:35 -07:00
|
|
|
const browserId = 'test-browser-id';
|
2023-02-17 01:59:09 -08:00
|
|
|
const req = mock<OwnerRequest.Post>({
|
|
|
|
body: {
|
|
|
|
email: 'valid@email.com',
|
|
|
|
password: 'NewPassword123',
|
|
|
|
firstName: 'Jane',
|
|
|
|
lastName: 'Doe',
|
|
|
|
},
|
|
|
|
user,
|
2024-04-09 02:20:35 -07:00
|
|
|
browserId,
|
2023-02-17 01:59:09 -08:00
|
|
|
});
|
|
|
|
const res = mock<Response>();
|
2023-12-27 02:50:43 -08:00
|
|
|
configGetSpy.mockReturnValue(false);
|
2024-02-28 08:02:18 -08:00
|
|
|
userRepository.findOneOrFail.calledWith(anyObject()).mockResolvedValue(user);
|
2024-01-02 08:53:24 -08:00
|
|
|
userRepository.save.calledWith(anyObject()).mockResolvedValue(user);
|
2023-02-17 01:59:09 -08:00
|
|
|
jest.spyOn(jwt, 'sign').mockImplementation(() => 'signed-token');
|
|
|
|
|
|
|
|
await controller.setupOwner(req, res);
|
|
|
|
|
2024-02-28 08:02:18 -08:00
|
|
|
expect(userRepository.findOneOrFail).toHaveBeenCalledWith({
|
|
|
|
where: { role: 'global:owner' },
|
|
|
|
});
|
2024-02-20 07:34:54 -08:00
|
|
|
expect(userRepository.save).toHaveBeenCalledWith(user, { transaction: false });
|
2024-04-09 02:20:35 -07:00
|
|
|
expect(authService.issueCookie).toHaveBeenCalledWith(res, user, browserId);
|
2023-02-17 01:59:09 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|