refactor(core): Move all user DB access to UserRepository (#6910)

Prep for https://linear.app/n8n/issue/PAY-646
This commit is contained in:
Iván Ovejero
2023-08-22 15:58:05 +02:00
committed by GitHub
parent 67b88f75f4
commit 96a9de68a0
20 changed files with 209 additions and 184 deletions

View File

@@ -4,23 +4,24 @@ import { mock, anyObject, captor } from 'jest-mock-extended';
import type { ILogger } from 'n8n-workflow';
import type { IExternalHooksClass, IInternalHooksClass } from '@/Interfaces';
import type { User } from '@db/entities/User';
import type { UserRepository } from '@db/repositories';
import { MeController } from '@/controllers';
import { AUTH_COOKIE_NAME } from '@/constants';
import { BadRequestError } from '@/ResponseHelper';
import type { AuthenticatedRequest, MeRequest } from '@/requests';
import { badPasswords } from '../shared/testData';
import { UserService } from '@/services/user.service';
import Container from 'typedi';
describe('MeController', () => {
const logger = mock<ILogger>();
const externalHooks = mock<IExternalHooksClass>();
const internalHooks = mock<IInternalHooksClass>();
const userRepository = mock<UserRepository>();
const userService = mock<UserService>();
Container.set(UserService, userService);
const controller = new MeController({
logger,
externalHooks,
internalHooks,
repositories: { User: userRepository },
});
describe('updateCurrentUser', () => {
@@ -48,12 +49,12 @@ describe('MeController', () => {
const reqBody = { email: 'valid@email.com', firstName: 'John', lastName: 'Potato' };
const req = mock<MeRequest.UserUpdate>({ user, body: reqBody });
const res = mock<Response>();
userRepository.findOneOrFail.mockResolvedValue(user);
userService.findOneOrFail.mockResolvedValue(user);
jest.spyOn(jwt, 'sign').mockImplementation(() => 'signed-token');
await controller.updateCurrentUser(req, res);
expect(userRepository.update).toHaveBeenCalled();
expect(userService.update).toHaveBeenCalled();
const cookieOptions = captor<CookieOptions>();
expect(res.cookie).toHaveBeenCalledWith(AUTH_COOKIE_NAME, 'signed-token', cookieOptions);
@@ -76,7 +77,7 @@ describe('MeController', () => {
const reqBody = { email: 'valid@email.com', firstName: 'John', lastName: 'Potato' };
const req = mock<MeRequest.UserUpdate>({ user, body: reqBody });
const res = mock<Response>();
userRepository.findOneOrFail.mockResolvedValue(user);
userService.findOneOrFail.mockResolvedValue(user);
jest.spyOn(jwt, 'sign').mockImplementation(() => 'signed-token');
// Add invalid data to the request payload
@@ -84,9 +85,9 @@ describe('MeController', () => {
await controller.updateCurrentUser(req, res);
expect(userRepository.update).toHaveBeenCalled();
expect(userService.update).toHaveBeenCalled();
const updatedUser = userRepository.update.mock.calls[0][1];
const updatedUser = userService.update.mock.calls[0][1];
expect(updatedUser.email).toBe(reqBody.email);
expect(updatedUser.firstName).toBe(reqBody.firstName);
expect(updatedUser.lastName).toBe(reqBody.lastName);
@@ -138,7 +139,7 @@ describe('MeController', () => {
body: { currentPassword: 'old_password', newPassword: 'NewPassword123' },
});
const res = mock<Response>();
userRepository.save.calledWith(req.user).mockResolvedValue(req.user);
userService.save.calledWith(req.user).mockResolvedValue(req.user);
jest.spyOn(jwt, 'sign').mockImplementation(() => 'new-signed-token');
await controller.updatePassword(req, res);
@@ -171,7 +172,7 @@ describe('MeController', () => {
describe('createAPIKey', () => {
it('should create and save an API key', async () => {
const { apiKey } = await controller.createAPIKey(req);
expect(userRepository.update).toHaveBeenCalledWith(req.user.id, { apiKey });
expect(userService.update).toHaveBeenCalledWith(req.user.id, { apiKey });
});
});
@@ -185,7 +186,7 @@ describe('MeController', () => {
describe('deleteAPIKey', () => {
it('should delete the API key', async () => {
await controller.deleteAPIKey(req);
expect(userRepository.update).toHaveBeenCalledWith(req.user.id, { apiKey: null });
expect(userService.update).toHaveBeenCalledWith(req.user.id, { apiKey: null });
});
});
});