From 2fa9d3958c4c11fb9de430418bb88d7d8cfee7ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vincent=20Lain=C3=A9?= Date: Tue, 17 Jan 2023 14:23:47 +0100 Subject: [PATCH] Fix: Bugfix Typeorm v0.3 :bug: --- back/package.json | 2 +- back/src/admin/admin.controller.spec.ts | 103 ---------- back/src/admin/admin.service.spec.ts | 18 -- back/src/admin/admin.service.ts | 12 +- back/src/auth/auth.controller.spec.ts | 48 ----- back/src/auth/auth.service.spec.ts | 116 ----------- back/src/auth/auth.service.ts | 6 +- back/src/chatbot/chatbot.controller.spec.ts | 54 ------ back/src/chatbot/chatbot.service.spec.ts | 182 ------------------ back/src/chatbot/chatbot.service.ts | 4 +- back/src/core/guards/roles.guard.spec.ts | 59 ------ back/src/health/health.controller.spec.ts | 18 -- back/src/shared/services/mail.service.spec.ts | 58 ------ back/src/user/user.controller.spec.ts | 65 ------- back/src/user/user.service.spec.ts | 149 -------------- back/src/user/user.service.ts | 8 +- front/package.json | 2 +- 17 files changed, 21 insertions(+), 883 deletions(-) delete mode 100644 back/src/admin/admin.controller.spec.ts delete mode 100644 back/src/admin/admin.service.spec.ts delete mode 100644 back/src/auth/auth.controller.spec.ts delete mode 100644 back/src/auth/auth.service.spec.ts delete mode 100644 back/src/chatbot/chatbot.controller.spec.ts delete mode 100644 back/src/chatbot/chatbot.service.spec.ts delete mode 100644 back/src/core/guards/roles.guard.spec.ts delete mode 100644 back/src/health/health.controller.spec.ts delete mode 100644 back/src/shared/services/mail.service.spec.ts delete mode 100644 back/src/user/user.controller.spec.ts delete mode 100644 back/src/user/user.service.spec.ts diff --git a/back/package.json b/back/package.json index a957a9f..f862874 100644 --- a/back/package.json +++ b/back/package.json @@ -1,6 +1,6 @@ { "name": "fabrique-chatbot-back", - "version": "1.17.0", + "version": "1.17.1", "description": "", "author": "", "license": "MIT", diff --git a/back/src/admin/admin.controller.spec.ts b/back/src/admin/admin.controller.spec.ts deleted file mode 100644 index 4a30e9a..0000000 --- a/back/src/admin/admin.controller.spec.ts +++ /dev/null @@ -1,103 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { AdminController } from './admin.controller'; -import { User } from "@entity/user.entity"; -import { usersMock } from "@mock/users.mock"; -import cloneDeep = require('lodash/cloneDeep'); -import { Chatbot } from "@entity/chatbot.entity"; -import { chatbotsMock } from "@mock/chatbots.mock"; -import { UpdateChatbotDto } from "@dto/update-chatbot.dto"; -import { ChatbotStatus } from "@enum/chatbot-status.enum"; -import { LaunchUpdateChatbotDto } from "@dto/launch-update-chatbot.dto"; - -describe('Admin Controller', () => { - - const users: User[] = cloneDeep(usersMock); - const chatbots: Chatbot[] = cloneDeep(chatbotsMock); - - let adminController: AdminController; - - let userServiceStub = { - findAll: () => users, - deleteUser: (email: string) => { - } - }; - let chatbotServiceStub = { - findAll: () => chatbots, - delete: (id: number) => { - }, - update: (id: number, updateChatbot: UpdateChatbotDto) => { - }, - findOneWithParam: (id: number, status: ChatbotStatus): Chatbot => { - return null; - } - }; - let chatbotGenerationServiceStub = { - updateChatbotRepos: () => { - }, - updateChatbot: () => { - }, - }; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - controllers: [AdminController], - providers: [ - {provide: 'UserService', useValue: userServiceStub}, - {provide: 'ChatbotService', useValue: chatbotServiceStub}, - {provide: 'ChatbotGenerationService', useValue: chatbotGenerationServiceStub}, - ] - }).compile(); - - adminController = module.get(AdminController); - }); - - it('should be defined', () => { - expect(adminController).toBeDefined(); - }); - - it('should get all chatbots', async () => { - const allChatbots = await adminController.getChatbots(); - expect(allChatbots.length).toEqual(1); - }); - - it('should get all users', async () => { - const allUsers = await adminController.getUsers(); - expect(allUsers.length).toEqual(2); - }); - - it('should call delete user', async () => { - spyOn(userServiceStub, 'deleteUser'); - await adminController.deleteUser('bruce@wayne.fr'); - expect(userServiceStub.deleteUser).toHaveBeenCalledWith('bruce@wayne.fr'); - }); - - it('should call delete chatbot', async () => { - spyOn(chatbotServiceStub, 'delete'); - await adminController.deleteChatbot(2); - expect(chatbotServiceStub.delete).toHaveBeenCalledWith(2); - }); - - it('should call update chatbot', async () => { - spyOn(chatbotServiceStub, 'update'); - await adminController.update(2, {name: 'Bruce'}); - expect(chatbotServiceStub.update).toHaveBeenCalledWith(2, {name: 'Bruce'}); - }); - - describe('on launch update chatbot', () => { - - it('should throw error if there is no chatbot running', async () => { - jest.spyOn(chatbotServiceStub, 'findOneWithParam').mockReturnValue(null); - await expect(adminController.updateChatbot(2, new LaunchUpdateChatbotDto())).rejects.toBeTruthy(); - }); - - it('should call update repos & update chatbot', async () => { - spyOn(chatbotGenerationServiceStub, 'updateChatbotRepos'); - spyOn(chatbotGenerationServiceStub, 'updateChatbot'); - jest.spyOn(chatbotServiceStub, 'findOneWithParam').mockReturnValue(chatbots[0]); - await adminController.updateChatbot(2, new LaunchUpdateChatbotDto(true, true, false)); - expect(chatbotGenerationServiceStub.updateChatbotRepos).toHaveBeenCalled(); - expect(chatbotGenerationServiceStub.updateChatbot).toHaveBeenCalledWith(chatbots[0], {updateFront: true, updateBack: true, updateRasa: false}); - }); - - }) -}); diff --git a/back/src/admin/admin.service.spec.ts b/back/src/admin/admin.service.spec.ts deleted file mode 100644 index 5e5e153..0000000 --- a/back/src/admin/admin.service.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { AdminService } from './admin.service'; - -describe('AdminService', () => { - let service: AdminService; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [AdminService], - }).compile(); - - service = module.get(AdminService); - }); - - it('should be defined', () => { - expect(service).toBeDefined(); - }); -}); diff --git a/back/src/admin/admin.service.ts b/back/src/admin/admin.service.ts index d6df706..ca128bb 100644 --- a/back/src/admin/admin.service.ts +++ b/back/src/admin/admin.service.ts @@ -47,8 +47,10 @@ export class AdminService { this._logger.log('Update Chatbot Domain Name ...', chatbotId.toString()); const chatbot: Chatbot = await this._chatbotService.findOneWithParam({ - id: chatbotId, - status: ChatbotStatus.running + where :{ + id: chatbotId, + status: ChatbotStatus.running + } }); if (!chatbot) { throw new HttpException(`Ce chatbot n'existe pas ou n'est pas en fonctionnement.`, HttpStatus.INTERNAL_SERVER_ERROR); @@ -75,8 +77,10 @@ export class AdminService { this._logger.log('Update Chatbot...', chatbotId.toString()); const chatbot: Chatbot = await this._chatbotService.findOneWithParam({ - id: chatbotId, - status: ChatbotStatus.running + where: { + id: chatbotId, + status: ChatbotStatus.running + } }); if (!chatbot) { throw new HttpException(`Ce chatbot n'existe pas ou n'est pas en fonctionnement.`, HttpStatus.INTERNAL_SERVER_ERROR); diff --git a/back/src/auth/auth.controller.spec.ts b/back/src/auth/auth.controller.spec.ts deleted file mode 100644 index 611c9c6..0000000 --- a/back/src/auth/auth.controller.spec.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { AuthController } from './auth.controller'; -import { AuthService } from "./auth.service"; - -describe('Auth Controller', () => { - let authController: AuthController; - let authService: AuthService; - - let authServiceStub = { - sendEmailPasswordToken: () => {}, - resetPassword: () => {}, - login: () => {return {}} - }; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - controllers: [AuthController], - providers: [ - {provide: 'AuthService', useValue: authServiceStub}, - ] - }).compile(); - - authController = module.get(AuthController); - authService = module.get(AuthService); - }); - - it('should be defined', () => { - expect(authController).toBeDefined(); - }); - - it('should send email password token', async () => { - jest.spyOn(authService, 'sendEmailPasswordToken'); - await authController.forgotPassword(''); - expect(authService.sendEmailPasswordToken).toHaveBeenCalled(); - }); - - it('should reset password', async () => { - jest.spyOn(authService, 'resetPassword'); - await authController.resetPassword({password: '', token: ''}); - expect(authService.resetPassword).toHaveBeenCalled(); - }); - - it('should login', async () => { - jest.spyOn(authService, 'login'); - await authController.login({email: '', password: ''}); - expect(authService.login).toHaveBeenCalled(); - }); -}); diff --git a/back/src/auth/auth.service.spec.ts b/back/src/auth/auth.service.spec.ts deleted file mode 100644 index 8ac80f7..0000000 --- a/back/src/auth/auth.service.spec.ts +++ /dev/null @@ -1,116 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { AuthService } from './auth.service'; -import { UserService } from "../user/user.service"; -import cloneDeep = require('lodash/cloneDeep'); -import { User } from "@entity/user.entity"; -import { usersMock } from "@mock/users.mock"; -import { MailService } from "../shared/services/mail.service"; -import { mailServiceStub } from "../../test/stubs/mail.service.stub"; - -describe('AuthService', () => { - let authService: AuthService; - let userService: UserService; - let mailService: MailService; - let jwtServiceStub = { - sign: (data) => data - }; - let userServiceStub = { - findOne: () => new Promise(() => { - }), - setPasswordResetToken: (user) => new Promise(() => { - }), - findOneWithParam: (params) => new Promise(() => { - }), - findAndUpdate: (email, params) => new Promise(() => { - }), - }; - - const users: User[] = cloneDeep(usersMock); - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [ - AuthService, - {provide: 'MailService', useValue: mailServiceStub}, - {provide: 'JwtService', useValue: jwtServiceStub}, - {provide: 'UserService', useValue: userServiceStub} - ], - }).compile(); - - authService = module.get(AuthService); - userService = module.get(UserService); - mailService = module.get(MailService); - }); - - it('should be defined', () => { - expect(authService).toBeDefined(); - }); - - describe('Login', () => { - it('should throw exception if there is no user', async () => { - jest.spyOn(userService, 'findOne').mockResolvedValueOnce(null); - - await expect(authService.login({email: 'batman@gotham.fr', password: 'WayneCorp'})).rejects.toBeTruthy(); - expect(userService.findOne).toHaveBeenCalled(); - }); - - it('should throw exception if passwords are not the same', async () => { - jest.spyOn(userService, 'findOne').mockResolvedValueOnce(users[0]); - - await expect(authService.login({email: 'batman@gotham.fr', password: 'WayneCorpBis'})).rejects.toBeTruthy(); - expect(userService.findOne).toHaveBeenCalled(); - }); - - it('should return token if there is a user with same password', async () => { - jest.spyOn(userService, 'findOne').mockResolvedValueOnce(users[0]); - - expect((await authService.login({ - email: 'batman@gotham.fr', - password: 'WayneCorp' - })).chatbotFactoryToken).toBeDefined(); - expect(userService.findOne).toHaveBeenCalled(); - }); - }); - - describe('Password Forgot', () => { - it('should return null if there is no user', async () => { - jest.spyOn(userService, 'findOne').mockResolvedValueOnce(null); - jest.spyOn(mailService, 'sendEmail').mockResolvedValueOnce(null); - - expect((await authService.sendEmailPasswordToken('batman@gotham.fr'))).toEqual(undefined); - expect(mailService.sendEmail).not.toHaveBeenCalled(); - }); - - it('should send mail if there is an user', async () => { - jest.spyOn(userService, 'findOne').mockResolvedValueOnce(users[0]); - jest.spyOn(mailService, 'sendEmail').mockResolvedValueOnce(null); - jest.spyOn(userService, 'setPasswordResetToken').mockResolvedValueOnce(users[0]); - - await authService.sendEmailPasswordToken('batman@gotham.fr'); - expect(userService.setPasswordResetToken).toHaveBeenCalled(); - expect(mailService.sendEmail).toHaveBeenCalled(); - }); - }); - - describe('Password Reset', () => { - it('should throw exception if there is no user', async () => { - jest.spyOn(userService, 'findOneWithParam').mockResolvedValueOnce(null); - jest.spyOn(mailService, 'sendEmail').mockResolvedValueOnce(null); - - await expect(authService.resetPassword({password: 'WayneCorp', token: 'token'})).rejects.toBeTruthy(); - expect(userService.findOneWithParam).toHaveBeenCalled(); - expect(mailService.sendEmail).toHaveBeenCalled(); - }); - - it('should hash password, save it and send email', async () => { - jest.spyOn(userService, 'findOneWithParam').mockResolvedValueOnce(users[0]); - jest.spyOn(userService, 'findAndUpdate').mockResolvedValueOnce(users[0]); - jest.spyOn(mailService, 'sendEmail').mockResolvedValueOnce(null); - - await authService.resetPassword({password: 'WayneCorp', token: 'token'}); - expect(userService.findOneWithParam).toHaveBeenCalled(); - expect(userService.findAndUpdate).toHaveBeenCalled(); - expect(mailService.sendEmail).toHaveBeenCalled(); - }); - }); -}); diff --git a/back/src/auth/auth.service.ts b/back/src/auth/auth.service.ts index 1f9cb97..d80eaf9 100644 --- a/back/src/auth/auth.service.ts +++ b/back/src/auth/auth.service.ts @@ -48,8 +48,10 @@ export class AuthService { async resetPassword(resetPassword: ResetPasswordDto) { const userWithoutPassword = await this._userService.findOneWithParam({ - reset_password_token: resetPassword.token, - reset_password_expires: MoreThan(new Date()) + where: { + reset_password_token: resetPassword.token, + reset_password_expires: MoreThan(new Date()) + } }); if (!userWithoutPassword) { throw new HttpException('Cet utilisateur n\'existe pas.', HttpStatus.INTERNAL_SERVER_ERROR); diff --git a/back/src/chatbot/chatbot.controller.spec.ts b/back/src/chatbot/chatbot.controller.spec.ts deleted file mode 100644 index c865da9..0000000 --- a/back/src/chatbot/chatbot.controller.spec.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { ChatbotController } from './chatbot.controller'; -import { Chatbot } from "@entity/chatbot.entity"; -import cloneDeep = require('lodash/cloneDeep'); -import { chatbotsMock } from "@mock/chatbots.mock"; -import { CreateChatbotDto } from "@dto/create-chatbot.dto"; -import { ChatbotService } from "./chatbot.service"; - -describe('Chatbot Controller', () => { - - const chatbots: Chatbot[] = cloneDeep(chatbotsMock); - - let chatbotController: ChatbotController; - let chatbotService: ChatbotService; - - let chatbotServiceStub = { - checkTemplateFile: () => { return {questionsNumber: 8, errors: {}} }, - create: () => chatbots[0] - }; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - controllers: [ChatbotController], - providers: [ - {provide: 'ChatbotService', useValue: chatbotServiceStub}, - ] - }).compile(); - - chatbotController = module.get(ChatbotController); - chatbotService = module.get(ChatbotService); - }); - - it('should be defined', () => { - expect(chatbotController).toBeDefined(); - }); - - it('should get check template with uploaded file', async () => { - const resume = await chatbotController.checkTemplateFile({}); - expect(resume.questionsNumber).toEqual(8); - }); - - it('should create chatbot if there is no error in template file', async () => { - jest.spyOn(chatbotService, 'create'); - - await chatbotController.create({file: [], icon: []}, {}, {}); - expect(chatbotService.create).toHaveBeenCalled(); - }); - - it('should not create chatbot if there is errors in template file', async () => { - jest.spyOn(chatbotService, 'checkTemplateFile').mockReturnValue({questionsNumber: 8, errors: {'1': 'fail'}, categories: [], warnings: {}}); - - await expect(chatbotController.create({file: [], icon: []}, {}, {})).rejects.toBeTruthy(); - }); -}); diff --git a/back/src/chatbot/chatbot.service.spec.ts b/back/src/chatbot/chatbot.service.spec.ts deleted file mode 100644 index e3f301e..0000000 --- a/back/src/chatbot/chatbot.service.spec.ts +++ /dev/null @@ -1,182 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { ChatbotService } from './chatbot.service'; -import { Repository } from "typeorm"; -import { Chatbot } from "@entity/chatbot.entity"; -import { getRepositoryToken } from "@nestjs/typeorm"; -const XLSX = require('xlsx'); -import cloneDeep = require('lodash/cloneDeep'); -import { chatbotsMock } from "@mock/chatbots.mock"; -import { OvhStorageService } from "../shared/services/ovh-storage.service"; -import { FileModel } from "@model/file.model"; -import { ovhStorageServiceStub } from "../../test/stubs/ovh-storage.service.stub"; - -describe('ChatbotService', () => { - let chatbotService: ChatbotService; - let ovhStorageService: OvhStorageService; - let chatbotRepository: Repository; - let workbook: any; - - const chatbots: Chatbot[] = cloneDeep(chatbotsMock); - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [ - ChatbotService, - {provide: getRepositoryToken(Chatbot), useClass: Repository}, - {provide: OvhStorageService, useValue: ovhStorageServiceStub}, - ], - }).compile(); - - chatbotService = module.get(ChatbotService); - chatbotRepository = module.get>(getRepositoryToken(Chatbot)); - ovhStorageService = module.get(OvhStorageService); - }); - - it('should be defined', () => { - expect(chatbotService).toBeDefined(); - }); - - describe('repository calls', () => { - it('should call find when findAll', async () => { - jest.spyOn(chatbotRepository, 'find').mockResolvedValueOnce(chatbots); - expect(await chatbotService.findAll()).toEqual(chatbots); - }); - - it('should call findOne when findOne', async () => { - jest.spyOn(chatbotRepository, 'findOne').mockResolvedValueOnce(chatbots[0]); - expect(await chatbotService.findOne(chatbots[0].id)).toEqual(chatbots[0]); - }); - - it('should call findOne when findOneWithParam', async () => { - jest.spyOn(chatbotRepository, 'findOne').mockResolvedValueOnce(chatbots[0]); - expect(await chatbotService.findOneWithParam(chatbots[0].id)).toEqual(chatbots[0]); - }); - - it('should call create', async () => { - jest.spyOn(chatbotRepository, 'save').mockResolvedValueOnce(chatbots[0]); - await chatbotService.create(chatbots[0]); - expect(chatbotRepository.save).toHaveBeenCalledWith(chatbots[0]); - }); - - it('should not call save when no file or no icon', async () => { - jest.spyOn(chatbotRepository, 'save').mockResolvedValue(chatbots[0]); - await chatbotService.create(chatbots[0], {}); - expect(chatbotRepository.save).toHaveBeenCalledTimes(1); - - await chatbotService.create(chatbots[0], null, {}); - expect(chatbotRepository.save).toHaveBeenCalledTimes(2); - }); - - - it('should call save two time and ovh storage service when file & icon', async () => { - jest.spyOn(chatbotRepository, 'save').mockResolvedValue(chatbots[0]); - jest.spyOn(ovhStorageService, 'set').mockResolvedValue(true); - - await chatbotService.create(chatbots[0], {originalname: 'file.xlsx'}, {originalname: 'icon.png'}); - expect(chatbotRepository.save).toHaveBeenCalledTimes(2); - expect(ovhStorageService.set).toHaveBeenCalledTimes(2); - }); - - it('should call save and update if the chatbot exists', async () => { - jest.spyOn(chatbotRepository, 'findOne').mockResolvedValueOnce(chatbots[0]); - jest.spyOn(chatbotRepository, 'save').mockResolvedValueOnce(chatbots[0]); - - await chatbotService.findAndUpdate(chatbots[0].id, {primary_color: '#000'}); - expect(chatbotRepository.findOne).toHaveBeenCalled(); - const chatbotUptaded = cloneDeep(chatbots[0]); - chatbotUptaded.primary_color = '#000'; - expect(chatbotRepository.save).toHaveBeenCalledWith(chatbotUptaded); - }); - - it('should throw an error on save and update if the chatbot doest not exists', async () => { - jest.spyOn(chatbotRepository, 'findOne').mockResolvedValueOnce(null); - jest.spyOn(chatbotRepository, 'save').mockResolvedValueOnce(chatbots[0]); - - await expect(chatbotService.findAndUpdate(chatbots[0].id, {primary_color: '#000'})).rejects.toBeTruthy(); - expect(chatbotRepository.save).not.toHaveBeenCalled(); - }); - }); - - describe('Check template file', () => { - beforeEach(async () => { - workbook = await XLSX.readFile('./test/files/TEMPLATE_CHATBOT_TEST.xlsx'); - }); - - it('should throw exception if file is not an excel', async () => { - expect(() => {chatbotService.checkTemplateFile(null)}).toThrow(); - }); - - it('should convert template file to json', async () => { - // @ts-ignore - jest.spyOn(chatbotService._xlsx, 'read').mockReturnValue(workbook); - // @ts-ignore - jest.spyOn(chatbotService, '_computeTemplateFile').mockReturnValue(null); - // @ts-ignore - jest.spyOn(chatbotService, '_checkFile').mockReturnValue(null); - - const result = chatbotService.checkTemplateFile({}); - expect(result).toBeDefined(); - }); - - it('should compute template file', async () => { - // @ts-ignore - jest.spyOn(chatbotService._xlsx, 'read').mockReturnValue(workbook); - // @ts-ignore - jest.spyOn(chatbotService, '_checkFile').mockReturnValue(null); - - const result = chatbotService.checkTemplateFile({}); - expect(result.categories.length).toEqual(2); - expect(result.questionsNumber).toEqual(12); - }); - - it('should check file', async () => { - // @ts-ignore - jest.spyOn(chatbotService._xlsx, 'read').mockReturnValue(workbook); - - const result = chatbotService.checkTemplateFile({}); - expect(Object.keys(result.warnings).length).toBeGreaterThan(0); - expect(Object.keys(result.errors).length).toBeGreaterThan(0); - }); - }); - - describe('File filers', () => { - it('should return an error if it is not an excel', async () => { - expect(ChatbotService.excelFileFilter(null, {originalname: 'test.doc'}, (error, success) => error)).toBeDefined(); - expect(ChatbotService.excelFileFilter(null, {originalname: 'xls.doc'}, (error, success) => error)).toBeDefined(); - expect(ChatbotService.excelFileFilter(null, {originalname: 'xlsx.doc'}, (error, success) => error)).toBeDefined(); - - expect(ChatbotService.multipleFileFilters(null, {originalname: 'xlsx.doc', fieldname: 'file'}, (error, success) => error)).toBeDefined(); - }); - - it('should return true if it is an excel', async () => { - expect(ChatbotService.excelFileFilter(null, {originalname: 'doc.xlsx'}, (error, success) => error)).toEqual(null); - expect(ChatbotService.excelFileFilter(null, {originalname: 'doc.xls'}, (error, success) => error)).toEqual(null); - - expect(ChatbotService.excelFileFilter(null, {originalname: 'doc.xlsx'}, (error, success) => success)).toEqual(true); - expect(ChatbotService.excelFileFilter(null, {originalname: 'doc.xls'}, (error, success) => success)).toEqual(true); - - expect(ChatbotService.multipleFileFilters(null, {originalname: 'doc.xls', fieldname: 'file'}, (error, success) => success)).toEqual(true); - }); - }); - - - describe('Image filers', () => { - it('should return an error if it is not an jpg or png', async () => { - expect(await ChatbotService.imageFileFilter(null, {originalname: 'image.gif'}, (error, success) => error)).toBeDefined(); - expect(await ChatbotService.imageFileFilter(null, {originalname: 'jpg.doc'}, (error, success) => error)).toBeDefined(); - expect(await ChatbotService.imageFileFilter(null, {originalname: 'png.doc'}, (error, success) => error)).toBeDefined(); - - expect(ChatbotService.multipleFileFilters(null, {originalname: 'png.doc', fieldname: 'icon'}, (error, success) => error)).toBeDefined(); - }); - - it('should return true if it is an image', async () => { - expect(await ChatbotService.imageFileFilter(null, {originalname: 'doc.jpg'}, (error, success) => error)).toEqual(null); - expect(await ChatbotService.imageFileFilter(null, {originalname: 'doc.png'}, (error, success) => error)).toEqual(null); - - expect(await ChatbotService.imageFileFilter(null, {originalname: 'doc.jpg'}, (error, success) => success)).toEqual(true); - expect(await ChatbotService.imageFileFilter(null, {originalname: 'doc.png'}, (error, success) => success)).toEqual(true); - - expect(ChatbotService.multipleFileFilters(null, {originalname: 'doc.png', fieldname: 'icon'}, (error, success) => success)).toEqual(true); - }); - }); -}); diff --git a/back/src/chatbot/chatbot.service.ts b/back/src/chatbot/chatbot.service.ts index 793a2b5..1b7fefb 100644 --- a/back/src/chatbot/chatbot.service.ts +++ b/back/src/chatbot/chatbot.service.ts @@ -3,7 +3,7 @@ import { Sheet2JSONOpts, WorkBook, WorkSheet } from "xlsx"; import { TemplateFileDto, TemplateResponseType } from "@dto/template-file.dto"; import { TemplateFileCheckResumeDto } from "@dto/template-file-check-resume.dto"; import { InjectRepository } from "@nestjs/typeorm"; -import { Repository } from "typeorm"; +import { FindOneOptions, Repository } from "typeorm"; import { Chatbot } from "@entity/chatbot.entity"; import { ChatbotModel } from "@model/chatbot.model"; import { FileModel } from "@model/file.model"; @@ -45,7 +45,7 @@ export class ChatbotService { }); } - findOneWithParam(param: any): Promise { + findOneWithParam(param: FindOneOptions): Promise { return this._chatbotsRepository.findOne(param); } diff --git a/back/src/core/guards/roles.guard.spec.ts b/back/src/core/guards/roles.guard.spec.ts deleted file mode 100644 index 1068241..0000000 --- a/back/src/core/guards/roles.guard.spec.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { Test, TestingModule } from "@nestjs/testing"; -import { RolesGuard } from "@guard/roles.guard"; -import { ExecutionContext } from "@nestjs/common"; - -describe('RolesGuard', () => { - let rolesGuard: RolesGuard; - - let roles = null; - let reflectorStub = { - get: (name, context) => { - return roles - } - }; - let contextStub: ExecutionContext = { - getHandler: () => { - }, - switchToHttp: () => { - return { - getRequest: () => { - return { - user: { - role: 'user' - } - } - } - } - } - }; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [ - RolesGuard, - {provide: 'Reflector', useValue: reflectorStub}, - ], - }).compile(); - - rolesGuard = module.get(RolesGuard); - roles = null; - }); - - it('should be defined', () => { - expect(rolesGuard).toBeDefined(); - }); - - it('should return true if there is no roles', () => { - expect(rolesGuard.canActivate(contextStub)).toEqual(true); - }); - - it('should return true if user has the role', () => { - roles = ['robin', 'user']; - expect(rolesGuard.canActivate(contextStub)).toEqual(true); - }); - - it('should return false if user hasnt the role', () => { - roles = ['batman', 'admin']; - expect(rolesGuard.canActivate(contextStub)).toEqual(false); - }); -}); diff --git a/back/src/health/health.controller.spec.ts b/back/src/health/health.controller.spec.ts deleted file mode 100644 index 114ea26..0000000 --- a/back/src/health/health.controller.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { HealthController } from './health.controller'; - -describe('Health Controller', () => { - let controller: HealthController; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - controllers: [HealthController], - }).compile(); - - controller = module.get(HealthController); - }); - - it('should be defined', () => { - expect(controller).toBeDefined(); - }); -}); diff --git a/back/src/shared/services/mail.service.spec.ts b/back/src/shared/services/mail.service.spec.ts deleted file mode 100644 index 7012e6f..0000000 --- a/back/src/shared/services/mail.service.spec.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { MailService } from './mail.service'; -import { MailerService } from "@nestjs-modules/mailer"; - -describe('MailService', () => { - let mailService: MailService; - let mailerService: MailerService; - let mailerServiceStub = { - sendMail: (options: any) => new Promise(() => { - }) - }; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [ - MailService, - {provide: 'MailerService', useValue: mailerServiceStub} - ], - }).compile(); - - mailService = module.get(MailService); - mailerService = module.get(MailerService); - }); - - it('should be defined', () => { - expect(mailService).toBeDefined(); - }); - - it('should call mailer service on send mail with the rights values', () => { - process.env.MAIL_USER = 'batman@gotham.fr'; - jest.spyOn(mailerService, 'sendMail'); - mailService.sendEmail('police@gotham.fr', 'Batmobile volée', 'batmobile-steal', {suspect: 'Robin'}).then(); - - expect(mailerService.sendMail).toHaveBeenCalledWith({ - to: 'police@gotham.fr', - from: 'batman@gotham.fr', - subject: 'Batmobile volée', - template: 'batmobile-steal', - context: {suspect: 'Robin'}, - }); - }); - - it('should log call to mail server', async () => { - jest.spyOn(mailerService, 'sendMail').mockResolvedValue(true); - jest.spyOn(console, 'log'); - await mailService.sendEmail('police@gotham.fr', 'Batmobile volée', 'batmobile-steal', {suspect: 'Robin'}).then(); - - expect(console.log).toHaveBeenCalled(); - }); - - it('should log error if call crash', async () => { - jest.spyOn(mailerService, 'sendMail').mockRejectedValue(true); - jest.spyOn(console, 'error'); - - await expect(mailService.sendEmail('police@gotham.fr', 'Batmobile volée', 'batmobile-steal', {suspect: 'Robin'}).then()).rejects.toBeTruthy(); - expect(console.error).toHaveBeenCalled(); - }); -}); diff --git a/back/src/user/user.controller.spec.ts b/back/src/user/user.controller.spec.ts deleted file mode 100644 index 35a0ded..0000000 --- a/back/src/user/user.controller.spec.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { UserController } from './user.controller'; -import { User } from "@entity/user.entity"; -import { usersMock } from "@mock/users.mock"; -import cloneDeep = require('lodash/cloneDeep'); -import { CreateUserDto } from "@dto/create-user.dto"; -import camelcaseKeys = require("camelcase-keys"); -import { UserDto } from "@dto/user.dto"; -import { plainToClass } from "class-transformer"; -import { UpdateUserDto } from "@dto/update-user.dto"; -import { UserRole } from "@enum/user-role.enum"; -import { UserService } from "./user.service"; - -describe('User Controller', () => { - - const users: User[] = cloneDeep(usersMock); - - let userController: UserController; - let userService: UserService; - let userServiceStub = { - create: (user) => users[0], - delete: (email) => true, - findAndUpdate: (email: string, data: any) => users[0] - }; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - controllers: [UserController], - providers: [ - {provide: 'UserService', useValue: userServiceStub}, - ] - }).compile(); - - userController = module.get(UserController); - userService = module.get(UserService); - }); - - it('should be defined', () => { - expect(userController).toBeDefined(); - }); - - it('should create user', async () => { - const userCreated = await userController.create( {}); - expect(userCreated).toEqual(plainToClass(UserDto, camelcaseKeys(users[0], {deep: true}))); - }); - - describe('on update user', () => { - it('should return error if the user is not admin and the user updated is not current user', async () => { - const userUpdateRequest = userController.update('bruce@gotham.fr', new UpdateUserDto(), {user: {role: UserRole.user, email: 'gordon@gotham.fr'}}); - await expect(userUpdateRequest).rejects.toBeTruthy(); - }); - - it('should delete role update if the user is not admin', async () => { - jest.spyOn(userService, 'findAndUpdate').mockResolvedValue(users[0]); - await userController.update('gordon@gotham.fr', {role: UserRole.admin, firstName: 'Gordon'}, {user: {role: UserRole.user, email: 'gordon@gotham.fr'}}); - expect(userService.findAndUpdate).toHaveBeenCalledWith('gordon@gotham.fr', {first_name: 'Gordon'}); - }); - - it('should keep role update if the user is admin', async () => { - jest.spyOn(userService, 'findAndUpdate').mockResolvedValue(users[0]); - await userController.update('gordon@gotham.fr', {role: UserRole.admin, firstName: 'Gordon'}, {user: {role: UserRole.admin, email: 'gordon@gotham.fr'}}); - expect(userService.findAndUpdate).toHaveBeenCalledWith('gordon@gotham.fr', {first_name: 'Gordon', role: UserRole.admin}); - }); - }) -}); diff --git a/back/src/user/user.service.spec.ts b/back/src/user/user.service.spec.ts deleted file mode 100644 index ae30553..0000000 --- a/back/src/user/user.service.spec.ts +++ /dev/null @@ -1,149 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { UserService } from './user.service'; -import { MailService } from "../shared/services/mail.service"; -import { getRepositoryToken } from "@nestjs/typeorm"; -import { Repository } from "typeorm"; -import { User } from "@entity/user.entity"; -import cloneDeep = require('lodash/cloneDeep'); -import { usersMock } from "@mock/users.mock"; -import { mailServiceStub } from "../../test/stubs/mail.service.stub"; - -describe('UserService', () => { - let userService: UserService; - let userRepository: Repository; - let mailService: MailService; - - const users: User[] = cloneDeep(usersMock); - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [ - UserService, - {provide: 'MailService', useValue: mailServiceStub}, - {provide: getRepositoryToken(User), useClass: Repository} - ], - }).compile(); - - userService = module.get(UserService); - userRepository = module.get>(getRepositoryToken(User)); - mailService = module.get(MailService); - }); - - beforeEach(() => { - // RESET USER VALUES - users[0].reset_password_token = null; - users[0].reset_password_expires = null; - }); - - it('should be defined', () => { - expect(userService).toBeDefined(); - }); - - describe('repository calls', () => { - it('should call find when findAll', async () => { - jest.spyOn(userRepository, 'find').mockResolvedValueOnce(users); - expect(await userService.findAll()).toEqual(users); - }); - - it('should call findOne with no password', async () => { - jest.spyOn(userRepository, 'findOne').mockResolvedValueOnce(users[0]); - userService.findOne(users[0].email); - expect(userRepository.findOne).toHaveBeenCalledWith({where: {email: users[0].email}}); - }); - - it('should call findOne with password', async () => { - jest.spyOn(userRepository, 'findOne').mockResolvedValueOnce(users[0]); - userService.findOne(users[0].email, true); - expect(userRepository.findOne).toHaveBeenCalledWith({ - select: ['email', 'password', 'first_name', 'last_name', 'chatbot_theme', 'role'], - where: {email: users[0].email} - }); - }); - - it('should call findOne with params', async () => { - jest.spyOn(userRepository, 'findOne').mockResolvedValueOnce(users[0]); - userService.findOneWithParam(users[0].email); - expect(userRepository.findOne).toHaveBeenCalledWith(users[0].email); - }); - - it('should call save and update if the user exists', async () => { - jest.spyOn(userRepository, 'findOne').mockResolvedValueOnce(users[0]); - jest.spyOn(userRepository, 'save').mockResolvedValueOnce(users[0]); - - await userService.findAndUpdate(users[0].email, {first_name: 'Robin'}); - expect(userRepository.findOne).toHaveBeenCalled(); - const userUpdated = cloneDeep(users[0]); - userUpdated.first_name = 'Robin'; - expect(userRepository.save).toHaveBeenCalledWith(userUpdated); - }); - - it('should throw an error on save and update if the user doest not exists', async () => { - jest.spyOn(userRepository, 'findOne').mockResolvedValueOnce(null); - jest.spyOn(userRepository, 'save').mockResolvedValueOnce(users[0]); - - await expect(userService.findAndUpdate(users[0].email, {first_name: 'Robin'})).rejects.toBeTruthy(); - expect(userRepository.save).not.toHaveBeenCalled(); - }); - - it('should call delete', async () => { - jest.spyOn(userRepository, 'delete').mockResolvedValueOnce({raw: ''}); - userService.delete(users[0].email); - expect(userRepository.delete).toHaveBeenCalledWith(users[0].email); - }); - }); - - describe('create user', () => { - it('should create user if it does not exists and send email', async () => { - jest.spyOn(userRepository, 'findOne').mockResolvedValueOnce(null); - jest.spyOn(userRepository, 'save').mockResolvedValueOnce(users[0]); - jest.spyOn(userService, 'sendEmailPasswordToken').mockResolvedValueOnce(null); - - await userService.create(users[0]); - expect(userRepository.save).toHaveBeenCalledWith(users[0]); - }); - - it('should not create user if it already exists', async () => { - jest.spyOn(userRepository, 'findOne').mockResolvedValueOnce(users[0]); - jest.spyOn(userRepository, 'save').mockResolvedValueOnce(users[0]); - - await expect(userService.create(users[0])).rejects.toBeTruthy(); - expect(userRepository.save).not.toHaveBeenCalled(); - }); - - it('should send mail and generate token when user is created', async () => { - jest.spyOn(userRepository, 'findOne').mockResolvedValueOnce(users[0]); - jest.spyOn(userRepository, 'save').mockResolvedValueOnce(users[0]); - jest.spyOn(mailService, 'sendEmail').mockResolvedValueOnce(true); - - await userService.sendEmailPasswordToken(users[0]); - expect(userRepository.save).toHaveBeenCalled(); - expect(mailService.sendEmail).toHaveBeenCalled(); - }); - }); - - describe('delete user', () => { - it('should throw an error on delete if the user doest not exists', async () => { - jest.spyOn(userRepository, 'findOne').mockResolvedValueOnce(null); - jest.spyOn(userRepository, 'delete').mockResolvedValueOnce(null); - - await expect(userService.deleteUser(users[0].email)).rejects.toBeTruthy(); - expect(userRepository.delete).not.toHaveBeenCalled(); - }); - - it('should throw an error if the user to delete is admin', async () => { - jest.spyOn(userRepository, 'findOne').mockResolvedValueOnce(users[1]); - jest.spyOn(userRepository, 'delete').mockResolvedValueOnce(null); - - await expect(userService.deleteUser(users[1].email)).rejects.toBeTruthy(); - expect(userRepository.delete).not.toHaveBeenCalled(); - }); - - it('should call delete if the user is not admin', async () => { - jest.spyOn(userRepository, 'findOne').mockResolvedValueOnce(users[0]); - jest.spyOn(userRepository, 'delete').mockResolvedValueOnce(null); - - await userService.deleteUser(users[0].email); - expect(userRepository.delete).toHaveBeenCalled(); - }); - }) -}); diff --git a/back/src/user/user.service.ts b/back/src/user/user.service.ts index 1638be8..53608c1 100644 --- a/back/src/user/user.service.ts +++ b/back/src/user/user.service.ts @@ -1,6 +1,6 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { InjectRepository } from "@nestjs/typeorm"; -import { DeleteResult, Repository } from "typeorm"; +import { DeleteResult, FindOneOptions, Repository } from "typeorm"; import { User } from "@entity/user.entity"; import { UserModel } from "@model/user.model"; import { MailService } from "../shared/services/mail.service"; @@ -28,7 +28,7 @@ export class UserService { }); } - findOneWithParam(param: any): Promise { + findOneWithParam(param: FindOneOptions): Promise { return this._usersRepository.findOne(param); } @@ -36,7 +36,9 @@ export class UserService { const userExists = await this.findOne(user.email); if (!userExists) { const adminExists = await this.findOneWithParam({ - role: UserRole.admin + where: { + role: UserRole.admin + } }); if(!adminExists) { user.role = UserRole.admin diff --git a/front/package.json b/front/package.json index 26d2a23..c4e772c 100644 --- a/front/package.json +++ b/front/package.json @@ -1,6 +1,6 @@ { "name": "fabrique-chatbot-front", - "version": "1.17.0", + "version": "1.17.1", "scripts": { "ng": "ng", "start": "ng serve",