diff --git a/src/contact/contact.controller.spec.ts b/src/contact/contact.controller.spec.ts index e6338a453dfe1a57b558342bd315bab0b9316b53..7123437dd460c44b0e7858d71fd497cc5a421ffd 100644 --- a/src/contact/contact.controller.spec.ts +++ b/src/contact/contact.controller.spec.ts @@ -2,14 +2,24 @@ import { Test, TestingModule } from '@nestjs/testing'; import { ContactController } from './contact.controller'; import { ContactService } from './contact.service'; import { MailerModule } from '../mailer/mailer.module'; +import { ContactMessage } from './schemas/contact-message.schema'; describe('ContactController', () => { let controller: ContactController; + const contactServiceMock = { + sendMessage: jest.fn(), + }; + beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ imports: [MailerModule], - providers: [ContactService], + providers: [ + { + provide: ContactService, + useValue: contactServiceMock, + }, + ], controllers: [ContactController], }).compile(); @@ -19,4 +29,11 @@ describe('ContactController', () => { it('should be defined', () => { expect(controller).toBeDefined(); }); + + it('should call sendMessage', async () => { + const spyer = jest.spyOn(contactServiceMock, 'sendMessage'); + await controller.sendContactMessage({ contactMessage: new ContactMessage() }); + expect(spyer).toBeCalledTimes(1); + expect(spyer).toBeCalledWith(new ContactMessage()); + }); }); diff --git a/src/contact/contact.controller.ts b/src/contact/contact.controller.ts index 82fb984a1a3bf35f9286d31e5ca760164d4cd8c9..df137b3b85e2e9b3adee27820f4ae5b383a3cbc2 100644 --- a/src/contact/contact.controller.ts +++ b/src/contact/contact.controller.ts @@ -10,6 +10,6 @@ export class ContactController { @Post('message') public async sendContactMessage(@Body() data: { contactMessage: ContactMessage }): Promise<any> { - await this.contactService.sendMessage(data.contactMessage); + return this.contactService.sendMessage(data.contactMessage); } } diff --git a/src/contact/contact.service.spec.ts b/src/contact/contact.service.spec.ts index 0f03f1cd993bf2f01cbe5ef27c76a70089d82b07..eb76c3239d8156537ddefa40d78bd6cdbde1fa9f 100644 --- a/src/contact/contact.service.spec.ts +++ b/src/contact/contact.service.spec.ts @@ -1,14 +1,20 @@ +import { HttpStatus } from '@nestjs/common'; +import { ContactMessage } from './schemas/contact-message.schema'; import { Test, TestingModule } from '@nestjs/testing'; import { MailerModule } from '../mailer/mailer.module'; import { ContactService } from './contact.service'; +import { MailerService } from '../mailer/mailer.service'; +import { MailerMockService } from '../../test/mock/services/mailer.mock.service'; +import { HttpModule } from '@nestjs/axios'; +import { ConfigurationService } from '../configuration/configuration.service'; describe('ContactService', () => { let service: ContactService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - imports: [MailerModule], - providers: [ContactService], + imports: [HttpModule, MailerModule], + providers: [ContactService, ConfigurationService, { provide: MailerService, useClass: MailerMockService }], }).compile(); service = module.get<ContactService>(ContactService); @@ -17,4 +23,9 @@ describe('ContactService', () => { it('should be defined', () => { expect(service).toBeDefined(); }); + + it('should send message with status OK', async () => { + const res = await service.sendMessage(new ContactMessage()); + expect(res.data.status).toBe(HttpStatus.OK); + }); }); diff --git a/src/parameters/parameters.controller.spec.ts b/src/parameters/parameters.controller.spec.ts index 9dae2afc46d72aa4e1fc06953e2a08b00122629f..b78180d3a3a7150a7388a919eca13d330354cbcb 100644 --- a/src/parameters/parameters.controller.spec.ts +++ b/src/parameters/parameters.controller.spec.ts @@ -7,10 +7,18 @@ import { Parameters } from './schemas/parameters.schema'; describe('ParametersController', () => { let controller: ParametersController; + const parametersServiceMock = { + getParameters: jest.fn(), + setParameterLockdownInfoDisplay: jest.fn(), + }; + beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ - ParametersService, + { + provide: ParametersService, + useValue: parametersServiceMock, + }, { provide: getModelToken('Parameters'), useValue: Parameters, @@ -26,18 +34,25 @@ describe('ParametersController', () => { expect(controller).toBeDefined(); }); - it('should get parameters', async () => { - const result = { lockdownInfoDisplay: false }; - jest.spyOn(controller, 'getParameters').mockImplementation(async (): Promise<Parameters> => result); - expect(await controller.getParameters()).toBe(result); + it('should call getParameters', async () => { + const spyer = jest.spyOn(parametersServiceMock, 'getParameters'); + await controller.getParameters(); + expect(spyer).toBeCalledTimes(1); }); - it('should set lockdownInfoDisplay', async () => { - const result = { lockdownInfoDisplay: false }; - jest - .spyOn(controller, 'setParameterLockdownInfoDisplay') - .mockImplementation(async (): Promise<Parameters> => result); - const lockdownInfoDisplayValue = { lockdownInfoDisplay: false }; - expect(await controller.setParameterLockdownInfoDisplay(lockdownInfoDisplayValue)).toBe(result); + describe('setParameterLockdownInfoDisplay', () => { + const spyer = jest.spyOn(parametersServiceMock, 'setParameterLockdownInfoDisplay'); + afterEach(() => spyer.mockClear()); + it('should call setParameterLockdownInfoDisplay(false) ', async () => { + await controller.setParameterLockdownInfoDisplay({ lockdownInfoDisplay: false }); + expect(spyer).toBeCalledTimes(1); + expect(spyer).toBeCalledWith(false); + }); + + it('should call setParameterLockdownInfoDisplay(true)', async () => { + await controller.setParameterLockdownInfoDisplay({ lockdownInfoDisplay: true }); + expect(spyer).toBeCalledTimes(1); + expect(spyer).toBeCalledWith(true); + }); }); }); diff --git a/src/parameters/parameters.service.spec.ts b/src/parameters/parameters.service.spec.ts index c7bc9e36b5dc65fa4f927d63412b7fcaf1338c1e..fc5df4eda2443d363a368c2754105daf8ff45f51 100644 --- a/src/parameters/parameters.service.spec.ts +++ b/src/parameters/parameters.service.spec.ts @@ -1,40 +1,84 @@ +import { HttpStatus } from '@nestjs/common'; import { getModelToken } from '@nestjs/mongoose'; import { Test, TestingModule } from '@nestjs/testing'; -import { mockParametersModel } from '../../test/mock/services/parameters.mock.service'; -import { IParameters } from './interface/parameters.interface'; import { ParametersService } from './parameters.service'; import { Parameters } from './schemas/parameters.schema'; describe('ParametersService', () => { let service: ParametersService; + const parametersModelMock = { + findOne: jest.fn(), + }; + beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ ParametersService, { provide: getModelToken(Parameters.name), - useValue: mockParametersModel, + useValue: parametersModelMock, }, ], }).compile(); service = module.get<ParametersService>(ParametersService); }); + afterEach(() => { + jest.clearAllMocks(); + }); it('should be defined', () => { expect(service).toBeDefined(); }); - it('should get parameters', async () => { - const result: IParameters = { lockdownInfoDisplay: false } as IParameters; - jest.spyOn(service, 'getParameters').mockImplementation(async (): Promise<Parameters> => result); - expect(await service.getParameters()).toBe(result); + describe('getParameters', () => { + it('should get parameters', async () => { + const spyer = jest.spyOn(parametersModelMock, 'findOne'); + parametersModelMock.findOne.mockResolvedValueOnce({ lockdownInfoDisplay: false }); + + const result = await service.getParameters(); + expect(spyer).toBeCalledTimes(1); + expect(result).toEqual({ lockdownInfoDisplay: false }); + }); + + it('should throw error because parameters was not found', async () => { + const spyer = jest.spyOn(parametersModelMock, 'findOne'); + parametersModelMock.findOne.mockResolvedValueOnce(null); + + try { + await service.getParameters(); + expect(true).toBe(false); + } catch (error) { + expect(error.message).toBe('Parameters not found'); + expect(error.status).toBe(HttpStatus.NOT_FOUND); + } + expect(spyer).toBeCalledTimes(1); + }); }); - it('should set Parameter LockdownInfoDisplay', async () => { - const result: IParameters = { lockdownInfoDisplay: false } as IParameters; - jest.spyOn(service, 'setParameterLockdownInfoDisplay').mockImplementation(async (): Promise<Parameters> => result); - expect(await service.setParameterLockdownInfoDisplay(false)).toBe(result); + describe('setParameters', () => { + it('should set parameters', async () => { + const spyer = jest.spyOn(parametersModelMock, 'findOne'); + parametersModelMock.findOne.mockResolvedValueOnce({ lockdownInfoDisplay: false, save: jest.fn() }); + + const result = await service.setParameterLockdownInfoDisplay(true); + expect(spyer).toBeCalledTimes(1); + expect(result.lockdownInfoDisplay).toEqual(true); + }); + + it('should throw error because parameters was not found', async () => { + const spyer = jest.spyOn(parametersModelMock, 'findOne'); + parametersModelMock.findOne.mockResolvedValueOnce(null); + + try { + await service.setParameterLockdownInfoDisplay(true); + expect(true).toBe(false); + } catch (error) { + expect(error.message).toBe('Parameters not found'); + expect(error.status).toBe(HttpStatus.NOT_FOUND); + } + expect(spyer).toBeCalledTimes(1); + }); }); }); diff --git a/src/temp-user/temp-user.service.ts b/src/temp-user/temp-user.service.ts index da41551c5a04e0a950d507dfc216e78062924a74..9ab891ec655741e563a5b27b20679663065a5ba4 100644 --- a/src/temp-user/temp-user.service.ts +++ b/src/temp-user/temp-user.service.ts @@ -84,11 +84,10 @@ export class TempUserService { } public async getStructureTempUsers(structureId: string): Promise<ITempUser[]> { - const tempUsers = await this.tempUserModel + return this.tempUserModel .find({ pendingStructuresLink: Types.ObjectId(structureId) }) .select('email updatedAt') .exec(); - return tempUsers; } public async removeFromStructureLinked(userEmail: string, idStructure: string): Promise<Types.ObjectId[]> { diff --git a/src/users/controllers/users.controller.spec.ts b/src/users/controllers/users.controller.spec.ts index 0708ba6b68179324b6e054c188a16cb13569f685..71d9c2ecee32192f641fdb04664282c9b61f2caa 100644 --- a/src/users/controllers/users.controller.spec.ts +++ b/src/users/controllers/users.controller.spec.ts @@ -1,4 +1,5 @@ import { HttpModule } from '@nestjs/axios'; +import { HttpStatus } from '@nestjs/common'; import { getModelToken } from '@nestjs/mongoose'; import { Test, TestingModule } from '@nestjs/testing'; import { Types } from 'mongoose'; @@ -14,13 +15,20 @@ import { StructuresSearchService } from '../../structures/services/structures-se import { StructuresService } from '../../structures/services/structures.service'; import { TempUser } from '../../temp-user/temp-user.schema'; import { TempUserService } from '../../temp-user/temp-user.service'; +import { EmailChangeDto } from '../dto/change-email.dto'; +import { PasswordChangeDto } from '../dto/change-password.dto'; +import { DescriptionDto } from '../dto/description.dto'; import { ProfileDto } from '../dto/profile.dto'; import { UpdateDetailsDto } from '../dto/update-details.dto'; import { User } from '../schemas/user.schema'; import { EmployerService } from '../services/employer.service'; import { JobsService } from '../services/jobs.service'; import { UsersService } from '../services/users.service'; +import { PasswordResetApplyDto } from './../dto/reset-password-apply.dto'; +import { PasswordResetDto } from './../dto/reset-password.dto'; import { UsersController } from './users.controller'; +import { usersMockData } from '../../../test/mock/data/users.mock.data'; +import { CreateUserDto } from '../dto/create-user.dto'; describe('UsersController', () => { let controller: UsersController; @@ -34,9 +42,34 @@ describe('UsersController', () => { }; const userServiceMock = { - updateUserProfile: jest.fn(), + changeUserEmail: jest.fn(), + changeUserPassword: jest.fn(), + create: jest.fn(), + deleteOne: jest.fn(), + findById: jest.fn(), findOne: jest.fn(), + isStructureClaimed: jest.fn(), + sendResetPasswordEmail: jest.fn(), + updateDescription: jest.fn(), + updateStructureLinkedClaim: jest.fn(), updateUserDetails: jest.fn(), + updateUserProfile: jest.fn(), + validatePasswordResetToken: jest.fn(), + validateUser: jest.fn(), + verifyAndUpdateUserEmail: jest.fn(), + verifyUserExist: jest.fn(), + }; + + const structureServiceMock = { + deleteOne: jest.fn(), + findOne: jest.fn(), + getAllDataConsentPendingStructures: jest.fn(), + sendAdminStructureNotification: jest.fn(), + }; + + const tempUserServiceMock = { + delete: jest.fn(), + findOne: jest.fn(), }; beforeEach(async () => { @@ -44,15 +77,21 @@ describe('UsersController', () => { imports: [ConfigurationModule, HttpModule, SearchModule], providers: [ UsersService, - StructuresService, StructuresSearchService, MailerService, - TempUserService, ParametersService, + { + provide: StructuresService, + useValue: structureServiceMock, + }, { provide: UsersService, useValue: userServiceMock, }, + { + provide: TempUserService, + useValue: tempUserServiceMock, + }, { provide: EmployerService, useValue: employerServiceMock, @@ -84,6 +123,9 @@ describe('UsersController', () => { ], controllers: [UsersController], }).compile(); + afterEach(() => { + jest.clearAllMocks(); + }); controller = module.get<UsersController>(UsersController); }); @@ -92,6 +134,14 @@ describe('UsersController', () => { expect(controller).toBeDefined(); }); + describe('getProfile', () => { + it('should return user', () => { + const user = { _id: '36', email: 'a@a.com' }; + const result = controller.getProfile({ user: user }); + expect(result).toEqual(user); + }); + }); + describe('setProfile', () => { it('should return employer does not exist', async () => { const profile: ProfileDto = { @@ -163,7 +213,6 @@ describe('UsersController', () => { job: Types.ObjectId('6231aefe76598527c8d0b5be'), }); const reply = await controller.setProfile({ user: { _id: '36', email: 'a@a.com' } }, profile); - console.log(reply); expect(reply).toBeTruthy(); }); }); @@ -196,8 +245,198 @@ describe('UsersController', () => { job: Types.ObjectId('6231aefe76598527c8d0b5be'), }); const reply = await controller.updateDetails({ user: { _id: '36', email: 'a@a.com' } }, newDetails); - console.log(reply); expect(reply).toBeTruthy(); }); }); + + describe('create', () => { + it('should create user without structure', async () => { + const userCreateSpyer = jest.spyOn(userServiceMock, 'create'); + const structureFindOneSpyer = jest.spyOn(structureServiceMock, 'findOne'); + const updateStructureLinkedClaimSpyer = jest.spyOn(userServiceMock, 'updateStructureLinkedClaim'); + const sendAdminStructureNotificationSpyer = jest.spyOn(structureServiceMock, 'sendAdminStructureNotification'); + const tempUserFindOneSpyer = jest.spyOn(tempUserServiceMock, 'findOne'); + const tempUserDeleteSpyer = jest.spyOn(tempUserServiceMock, 'delete'); + + const createUserDto = new CreateUserDto(); + createUserDto.pendingStructuresLink = []; + userServiceMock.create.mockResolvedValueOnce(usersMockData[0]); + const result = await controller.create(createUserDto); + + expect(userCreateSpyer).toBeCalledTimes(1); + expect(structureFindOneSpyer).toBeCalledTimes(0); + expect(updateStructureLinkedClaimSpyer).toBeCalledTimes(0); + expect(sendAdminStructureNotificationSpyer).toBeCalledTimes(0); + expect(tempUserFindOneSpyer).toBeCalledTimes(1); + expect(tempUserDeleteSpyer).toBeCalledTimes(0); + expect(result).toEqual(usersMockData[0]); + }); + + it('should create user with structure', async () => { + const userCreateSpyer = jest.spyOn(userServiceMock, 'create'); + const structureFindOneSpyer = jest.spyOn(structureServiceMock, 'findOne'); + const updateStructureLinkedClaimSpyer = jest.spyOn(userServiceMock, 'updateStructureLinkedClaim'); + const sendAdminStructureNotificationSpyer = jest.spyOn(structureServiceMock, 'sendAdminStructureNotification'); + const tempUserFindOneSpyer = jest.spyOn(tempUserServiceMock, 'findOne'); + const tempUserDeleteSpyer = jest.spyOn(tempUserServiceMock, 'delete'); + + const createUserDto = new CreateUserDto(); + createUserDto.pendingStructuresLink = ['6093ba0e2ab5775cfc01fffe']; + userServiceMock.create.mockResolvedValueOnce(usersMockData[0]); + const result = await controller.create(createUserDto); + + expect(userCreateSpyer).toBeCalledTimes(1); + expect(structureFindOneSpyer).toBeCalledTimes(1); + expect(updateStructureLinkedClaimSpyer).toBeCalledTimes(1); + expect(sendAdminStructureNotificationSpyer).toBeCalledTimes(1); + expect(tempUserFindOneSpyer).toBeCalledTimes(1); + expect(tempUserDeleteSpyer).toBeCalledTimes(0); + expect(result).toEqual(usersMockData[0]); + }); + + it('should create user with temp user', async () => { + const userCreateSpyer = jest.spyOn(userServiceMock, 'create'); + const structureFindOneSpyer = jest.spyOn(structureServiceMock, 'findOne'); + const updateStructureLinkedClaimSpyer = jest.spyOn(userServiceMock, 'updateStructureLinkedClaim'); + const sendAdminStructureNotificationSpyer = jest.spyOn(structureServiceMock, 'sendAdminStructureNotification'); + const tempUserFindOneSpyer = jest.spyOn(tempUserServiceMock, 'findOne'); + const tempUserDeleteSpyer = jest.spyOn(tempUserServiceMock, 'delete'); + + const createUserDto = new CreateUserDto(); + createUserDto.pendingStructuresLink = []; + userServiceMock.create.mockResolvedValueOnce(usersMockData[0]); + tempUserServiceMock.findOne.mockResolvedValueOnce({ email: 'test@test.com', pendingStructuresLink: [] }); + const result = await controller.create(createUserDto); + + expect(userCreateSpyer).toBeCalledTimes(1); + expect(structureFindOneSpyer).toBeCalledTimes(0); + expect(updateStructureLinkedClaimSpyer).toBeCalledTimes(0); + expect(sendAdminStructureNotificationSpyer).toBeCalledTimes(0); + expect(tempUserFindOneSpyer).toBeCalledTimes(1); + expect(tempUserDeleteSpyer).toBeCalledTimes(1); + expect(result).toEqual(usersMockData[0]); + }); + }); + + describe('validateUser', () => { + it('should call validateUser', async () => { + const spyer = jest.spyOn(userServiceMock, 'validateUser'); + await controller.validateUser({ id: 1 }, 'token'); + expect(spyer).toBeCalledTimes(1); + }); + }); + + describe('changePassword', () => { + it('should call changeUserPassword', async () => { + const spyer = jest.spyOn(userServiceMock, 'changeUserPassword'); + await controller.changePassword({ user: { _id: '36', email: 'a@a.com' } }, new PasswordChangeDto()); + expect(spyer).toBeCalledTimes(1); + }); + }); + + describe('changeEmail', () => { + it('should call changeUserEmail', async () => { + const spyer = jest.spyOn(userServiceMock, 'changeUserEmail'); + await controller.changeEmail(new EmailChangeDto()); + expect(spyer).toBeCalledTimes(1); + }); + }); + + describe('verifyAndUpdateEmail', () => { + it('should call verifyAndUpdateUserEmail', async () => { + const spyer = jest.spyOn(userServiceMock, 'verifyAndUpdateUserEmail'); + await controller.verifyAndUpdateEmail('token'); + expect(spyer).toBeCalledTimes(1); + }); + }); + + describe('resetPassword', () => { + it('should call sendResetPasswordEmail', async () => { + const spyer = jest.spyOn(userServiceMock, 'sendResetPasswordEmail'); + await controller.resetPassword(new PasswordResetDto()); + expect(spyer).toBeCalledTimes(1); + }); + }); + + describe('resetPasswordApply', () => { + it('should call validatePasswordResetToken', async () => { + const spyer = jest.spyOn(userServiceMock, 'validatePasswordResetToken'); + await controller.resetPasswordApply(new PasswordResetApplyDto()); + expect(spyer).toBeCalledTimes(1); + }); + }); + + describe('verifyUserExist', () => { + it('should call verifyUserExist', async () => { + const spyer = jest.spyOn(userServiceMock, 'verifyUserExist'); + await controller.verifyUserExist({ user: { _id: '36', email: 'a@a.com' } }, { newMail: 'test@test.com' }); + expect(spyer).toBeCalledTimes(1); + }); + }); + + describe('delete', () => { + it('should not call isStructureClaimed if no structures are linked', async () => { + const deleteOneSpyer = jest.spyOn(userServiceMock, 'deleteOne'); + const isStructureClaimedSpyer = jest.spyOn(userServiceMock, 'isStructureClaimed'); + const userWithoutStructure = usersMockData[2]; + userServiceMock.deleteOne.mockResolvedValueOnce(userWithoutStructure); + await controller.delete({ user: { _id: '36', email: 'a@a.com' } }); + expect(deleteOneSpyer).toBeCalledTimes(1); + expect(isStructureClaimedSpyer).toBeCalledTimes(0); + }); + + it('should call isStructureClaimed for each structure linked', async () => { + const userDeleteOneSpyer = jest.spyOn(userServiceMock, 'deleteOne'); + const isStructureClaimedSpyer = jest.spyOn(userServiceMock, 'isStructureClaimed'); + const structureDeleteOneSpyer = jest.spyOn(structureServiceMock, 'deleteOne'); + const userWithThreeStructures = usersMockData[3]; + userServiceMock.deleteOne.mockResolvedValueOnce(userWithThreeStructures); + userServiceMock.isStructureClaimed.mockResolvedValue(null); + userServiceMock.isStructureClaimed.mockResolvedValueOnce(userWithThreeStructures); + await controller.delete({ user: { _id: '36', email: 'a@a.com' } }); + expect(userDeleteOneSpyer).toBeCalledTimes(1); + expect(isStructureClaimedSpyer).toBeCalledTimes(3); + expect(structureDeleteOneSpyer).toBeCalledTimes(2); + }); + }); + + describe('dataConsentValidation', () => { + it('should call getAllDataConsentPendingStructures', async () => { + const spyer = jest.spyOn(structureServiceMock, 'getAllDataConsentPendingStructures'); + await controller.dataConsentValidation({ user: { _id: '36', email: 'a@a.com' } }); + expect(spyer).toBeCalledTimes(1); + }); + }); + + describe('get user', () => { + it('should return user', async () => { + const spyer = jest.spyOn(userServiceMock, 'findById'); + userServiceMock.findById.mockResolvedValueOnce({ _id: '36', email: 'a@a.com' }); + const result = await controller.getUser(1); + expect(result).toEqual({ _id: '36', email: 'a@a.com' }); + expect(spyer).toBeCalledTimes(1); + }); + + it('should throw error if no user found', async () => { + const spyer = jest.spyOn(userServiceMock, 'findById'); + userServiceMock.findById.mockResolvedValueOnce(null); + + try { + await controller.getUser(1); + expect(true).toBe(false); + } catch (error) { + expect(error.message).toBe('User does not exist'); + expect(error.status).toBe(HttpStatus.NOT_FOUND); + } + expect(spyer).toBeCalledTimes(1); + }); + }); + + describe('update description', () => { + it('should call updateDescription', async () => { + const spyer = jest.spyOn(userServiceMock, 'updateDescription'); + await controller.updateDescription({ user: { _id: '36', email: 'a@a.com' } }, new DescriptionDto()); + expect(spyer).toBeCalledTimes(1); + }); + }); }); diff --git a/src/users/controllers/users.controller.ts b/src/users/controllers/users.controller.ts index 7a4d242e780772112aa82826b65422b78f068c9b..ac7859f292cebf6428acb466fa2b842554dfb9bf 100644 --- a/src/users/controllers/users.controller.ts +++ b/src/users/controllers/users.controller.ts @@ -94,6 +94,7 @@ export class UsersController { @Post() @ApiResponse({ status: 201, description: 'User created' }) public async create(@Body() createUserDto: CreateUserDto) { + this.logger.debug('create'); // remove structureId for creation and add structure after let structureId = null; if (createUserDto.pendingStructuresLink.length > 0) { @@ -144,7 +145,7 @@ export class UsersController { @Post('change-email') @ApiResponse({ status: 201, description: 'Email confirmation send' }) @ApiResponse({ status: 401, description: 'Invalid Email' }) - public async changeEmail(@Request() _req, @Body() emailChangeDto: EmailChangeDto) { + public async changeEmail(@Body() emailChangeDto: EmailChangeDto) { return this.usersService.changeUserEmail(emailChangeDto); } @@ -152,7 +153,7 @@ export class UsersController { @Post('verify-change-email') @ApiResponse({ status: 201, description: 'Email changed' }) @ApiResponse({ status: 401, description: 'Invalid Token' }) - public async verifyAndUpdateEmail(@Request() _req, @Query('token') token: string) { + public async verifyAndUpdateEmail(@Query('token') token: string) { return this.usersService.verifyAndUpdateUserEmail(token); } diff --git a/src/users/dto/create-user.dto.ts b/src/users/dto/create-user.dto.ts index b2f6b6fbb1635c74585e50dc7b558e1ebb07b0f1..b0bbda7ad6b8bb3bbb6f21138d4914045166bbe2 100644 --- a/src/users/dto/create-user.dto.ts +++ b/src/users/dto/create-user.dto.ts @@ -27,7 +27,7 @@ export class CreateUserDto { @IsArray() @IsOptional() - pendingStructuresLink?: Array<number>; + pendingStructuresLink?: Array<string>; @IsArray() @IsOptional() diff --git a/src/users/services/users.service.spec.ts b/src/users/services/users.service.spec.ts index f88eeddc58c652836999571e1e7ac7aa02e3994a..49d9bb9d4479afaf5285753fdaaf635f4e26e977 100644 --- a/src/users/services/users.service.spec.ts +++ b/src/users/services/users.service.spec.ts @@ -1,58 +1,121 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { UsersService } from './users.service'; -import { getModelToken } from '@nestjs/mongoose'; +import { HttpModule } from '@nestjs/axios'; import { HttpException, HttpStatus } from '@nestjs/common'; +import { getModelToken } from '@nestjs/mongoose'; +import { Test, TestingModule } from '@nestjs/testing'; import * as bcrypt from 'bcrypt'; - +import { Types } from 'mongoose'; +import { employersMockData } from '../../../test/mock/data/employers.mock.data'; import { personalOffersDataMock } from '../../../test/mock/data/personalOffers.mock.data'; -import { PersonalOfferDocument } from '../../personal-offers/schemas/personal-offer.schema'; import { userDetails, usersMockData } from '../../../test/mock/data/users.mock.data'; -import { employersMockData } from '../../../test/mock/data/employers.mock.data'; +import { MailerMockService } from '../../../test/mock/services/mailer.mock.service'; import { LoginDto } from '../../auth/login-dto'; import { ConfigurationModule } from '../../configuration/configuration.module'; import { MailerModule } from '../../mailer/mailer.module'; +import { MailerService } from '../../mailer/mailer.service'; +import { PersonalOfferDocument } from '../../personal-offers/schemas/personal-offer.schema'; import { EmailChangeDto } from '../dto/change-email.dto'; import { CreateUserDto } from '../dto/create-user.dto'; -import { User } from '../schemas/user.schema'; -import { IUser } from '../interfaces/user.interface'; -import { UpdateDetailsDto } from '../dto/update-details.dto'; import { DescriptionDto } from '../dto/description.dto'; +import { UpdateDetailsDto } from '../dto/update-details.dto'; +import { IUser } from '../interfaces/user.interface'; +import { EmployerDocument } from '../schemas/employer.schema'; +import { JobDocument } from '../schemas/job.schema'; +import { User } from '../schemas/user.schema'; import { UserRegistrySearchService } from './userRegistry-search.service'; +import { UsersService } from './users.service'; function hashPassword() { return bcrypt.hashSync(process.env.USER_PWD, process.env.SALT); } -const mockUserModel = { +type mockUserModelFunctionTypes = { + create: jest.Mock<any, any>; + deleteOne: jest.Mock<any, any>; + exec: jest.Mock<any, any>; + find: jest.Mock<any, any>; + findById: jest.Mock<any, any>; + findByIdAndUpdate: jest.Mock<any, any>; + findOne: jest.Mock<any, any>; + findPopulatedUserRegistryById: jest.Mock<any, any>; + limit: jest.Mock<any, any>; + populate: jest.Mock<any, any>; + select: jest.Mock<any, any>; + sort: jest.Mock<any, any>; + updateMany: jest.Mock<any, any>; + updateOne: jest.Mock<any, any>; +}; + +const mockUserModel: mockUserModelFunctionTypes = { create: jest.fn(), - findOne: jest.fn(() => mockUserModel), - findById: jest.fn(), deleteOne: jest.fn(), - updateMany: jest.fn(), exec: jest.fn(), find: jest.fn(() => mockUserModel), - sort: jest.fn(() => mockUserModel), - populate: jest.fn(() => mockUserModel), - select: jest.fn(() => mockUserModel), - limit: jest.fn(() => mockUserModel), + findById: jest.fn(), findByIdAndUpdate: jest.fn(() => mockUserModel), + findOne: jest.fn(() => mockUserModel), findPopulatedUserRegistryById: jest.fn(() => mockUserModel), + limit: jest.fn(() => mockUserModel), + populate: jest.fn(() => mockUserModel), + select: jest.fn(() => mockUserModel), + sort: jest.fn(() => mockUserModel), + updateMany: jest.fn(), + updateOne: jest.fn(), }; + const mockUserRegistrySearchService = { - indexUserRegistry: jest.fn(), - search: jest.fn(), - dropIndex: jest.fn(), createindexUserRegistryIndex: jest.fn(), deleteIndex: jest.fn(), + dropIndex: jest.fn(), + indexUserRegistry: jest.fn(), + isStrongPassword: jest.fn(), + search: jest.fn(), update: jest.fn(), }; +const createUserDto: CreateUserDto = { + email: 'jacques.dupont@mii.com', + password: 'test1A!!', + name: 'Jacques', + surname: 'Dupont', + phone: '06 06 06 06 06', + structuresLink: ['61e9260c2ac971550065e262', '61e9260b2ac971550065e261'], +}; + +const mockUser: User = { + role: 0, + validationToken: + 'cf1c74c22cedb6b575945098db42d2f493fb759c9142c6aff7980f252886f36ee086574ee99a06bc99119079257116c959c8ec870949cebdef2b293666dbca42', + emailVerified: false, + email: 'jacques.dupont@mii.com', + password: hashPassword(), + newEmail: '', + changeEmailToken: '', + resetPasswordToken: null, + structuresLink: [], + structureOutdatedMailSent: [], + pendingStructuresLink: [], + personalOffers: [], + name: 'Jacques', + surname: 'Dupont', + phone: '06 06 06 06 06', + createdAt: new Date('2022-05-25T09:48:28.824Z'), + employer: { + name: 'test', + validated: true, + }, + job: { + name: 'test', + validated: true, + hasPersonalOffer: false, + }, +}; + describe('UsersService', () => { let service: UsersService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - imports: [MailerModule, ConfigurationModule], + imports: [HttpModule, MailerModule, ConfigurationModule], providers: [ UsersService, UserRegistrySearchService, @@ -64,127 +127,125 @@ describe('UsersService', () => { provide: getModelToken('User'), useValue: mockUserModel, }, + { provide: MailerService, useClass: MailerMockService }, ], }).compile(); service = module.get<UsersService>(UsersService); }); + afterEach(() => { + jest.clearAllMocks(); + }); - describe('User Service create', () => { - it('UsersService should be defined', () => { - expect(service).toBeDefined(); + it('UsersService should be defined', () => { + expect(service).toBeDefined(); + }); + + describe('create', () => { + it('should not create a User, already exist', async () => { + jest.spyOn(service, 'findOne').mockImplementationOnce(async (): Promise<any> => createUserDto); + try { + await service.create(createUserDto); + expect(true).toBe(false); + } catch (error) { + expect(error.status).toBe(HttpStatus.BAD_REQUEST); + expect(error.message).toBe('User already exists'); + } }); - it('User should be created', async () => { - const result: User = { - role: 0, - validationToken: - 'cf1c74c22cedb6b575945098db42d2f493fb759c9142c6aff7980f252886f36ee086574ee99a06bc99119079257116c959c8ec870949cebdef2b293666dbca42', - emailVerified: false, - email: 'jacques.dupont@mii.com', - password: hashPassword(), - newEmail: '', - changeEmailToken: '', - resetPasswordToken: null, - structuresLink: [], - structureOutdatedMailSent: [], - pendingStructuresLink: [], - personalOffers: [], - name: 'Jacques', - surname: 'Dupont', - phone: '06 06 06 06 06', - createdAt: new Date('2022-05-25T09:48:28.824Z'), - employer: { - name: 'test', - validated: true, - }, - job: { - name: 'test', - validated: true, - hasPersonalOffer: false, - }, - }; - const userDto: CreateUserDto = { - email: 'jacques.dupont@mii.com', - password: 'test1A!!', - name: 'Jacques', - surname: 'Dupont', - phone: '06 06 06 06 06', - }; //NOSONAR - jest.spyOn(service, 'create').mockImplementation(async (): Promise<User> => result); - expect(await service.create(userDto)).toBe(result); + it('should not create User, weak password', async () => { + jest.spyOn(service, 'findOne').mockImplementationOnce(async (): Promise<any> => null); + jest.spyOn(service, 'isStrongPassword').mockImplementationOnce(() => false); + try { + await service.create(createUserDto); + expect(true).toBe(false); + } catch (error) { + expect(error.status).toBe(HttpStatus.UNPROCESSABLE_ENTITY); + expect(error.message).toBe( + 'Weak password, it must contain one lowercase alphabetical character, one uppercase alphabetical character, one numeric character, one special character and be eight characters or longer' + ); + } + }); + it('should create a User', async () => { + jest.spyOn(service, 'findOne').mockImplementationOnce(async (): Promise<any> => null); + jest.spyOn(service, 'isStrongPassword').mockImplementationOnce(() => true); + //TODO mock new userModal(createUserDto) + return; + jest.spyOn(service, 'findOne').mockImplementationOnce(async (): Promise<any> => mockUser); + expect(await service.create(createUserDto)).toBe(mockUser); }); + }); - it('User should not be created, already exist', async () => { - const result = new HttpException('User already exists', HttpStatus.BAD_REQUEST); - const userDto: CreateUserDto = { - email: 'jacques.dupont@mii.com', - password: 'test1A!!', - name: 'Jacques', - surname: 'Dupont', - phone: '06 06 06 06 06', - }; //NOSONAR - jest.spyOn(service, 'create').mockImplementation(async (): Promise<any> => result); - expect(await service.create(userDto)).toBe(result); + describe('isStrongPassword', () => { + it('should return false with "AZERTYU1" (no lowercase character)', () => { + expect(service.isStrongPassword('AZERTY')).toBe(false); + }); + it('should return false with "azertyu1" (no uppercase character)', () => { + expect(service.isStrongPassword('azerty')).toBe(false); + }); + it('should return false with "azertyui" (no number)', () => { + expect(service.isStrongPassword('azerty')).toBe(false); }); + it('should return false with "azertyu1" (no special character)', () => { + expect(service.isStrongPassword('azertyu1')).toBe(false); + }); + it('should return false with "azertyu1" (no special character)', () => { + expect(service.isStrongPassword('azertyu1')).toBe(false); + }); + it('should return false with "azer" (too short)', () => { + expect(service.isStrongPassword('azer')).toBe(false); + }); + it('should return true with "Azerty1!', () => { + expect(service.isStrongPassword('Azerty1!')).toBe(true); + }); + }); - it('User should not be created, weak password', async () => { - const result = new HttpException( - 'Weak password, it must contain one lowercase alphabetical character, one uppercase alphabetical character, one numeric character, one special character and be eight characters or longer', - HttpStatus.UNPROCESSABLE_ENTITY - ); - const userDto: CreateUserDto = { - email: 'jacques.dupont@mii.com', - password: 'test', - name: 'Jacques', - surname: 'Dupont', - phone: '06 06 06 06 06', - }; //NOSONAR - jest.spyOn(service, 'create').mockImplementation(async (): Promise<any> => result); - expect(await service.create(userDto)).toBe(result); + describe('findOne', () => { + it('should find one user', async () => { + mockUserModel.exec.mockResolvedValue(mockUser); + expect(await service.findOne('some@mail.com')).toBe(mockUser); + }); + }); + + describe('findAll', () => { + it('should find all users', async () => { + mockUserModel.exec.mockResolvedValue(usersMockData); + expect(await service.findAll()).toBe(usersMockData); }); }); describe('findByLogin', () => { - it('should find', async () => { - const result = { - validationToken: - 'cf1c74c22cedb6b575945098db42d2f493fb759c9142c6aff7980f252886f36ee086574ee99a06bc99119079257116c959c8ec870949cebdef2b293666dbca42', - emailVerified: false, - email: 'jacques.dupont@mii.com', - password: hashPassword(), - role: 0, - newEmail: '', - changeEmailToken: '', - resetPasswordToken: null, - structuresLink: [], - pendingStructuresLink: [], - structureOutdatedMailSent: [], - personalOffers: [], - name: 'Jacques', - surname: 'Dupont', - phone: '06 06 06 06 06', - createdAt: new Date('2022-05-25T09:48:28.824Z'), - employer: { - name: 'test', - validated: true, - }, - job: { - name: 'test', - validated: true, - hasPersonalOffer: false, - }, - }; - const loginDto: LoginDto = { email: 'jacques.dupont@mii.com', password: 'test1A!!' }; //NOSONAR - jest.spyOn(service, 'findByLogin').mockImplementation(async (): Promise<User> => result); - expect(await service.findByLogin(loginDto)).toBe(result); + const loginDto: LoginDto = { email: 'jacques.dupont@mii.com', password: 'test1A!!' }; + it('should fail, no user found', async () => { + jest.spyOn(service, 'findOne').mockResolvedValue(null); + try { + await service.findByLogin(loginDto); + expect(true).toBe(false); + } catch (error) { + expect(error.status).toBe(HttpStatus.UNAUTHORIZED); + expect(error.message).toBe('Invalid credentials'); + } }); - it('user does not exist, should be unauthorized issue', async () => { - const result: HttpException = new HttpException('Invalid credentials', HttpStatus.UNAUTHORIZED); - const loginDto: LoginDto = { email: 'jean.dupont@mii.com', password: 'test1A!!' }; //NOSONAR - jest.spyOn(service, 'findByLogin').mockImplementation(async (): Promise<any> => result); - expect(await service.findByLogin(loginDto)).toBe(result); + it('should fail, wrong password', async () => { + jest.spyOn(service, 'findOne').mockResolvedValue(mockUser as IUser); + //TODO mock private function comparePassword ? -> false + return true; + try { + await service.findByLogin(loginDto); + expect(true).toBe(false); + } catch (error) { + expect(error.status).toBe(HttpStatus.UNAUTHORIZED); + expect(error.message).toBe('Invalid credentials'); + } + }); + + it('should find', async () => { + mockUserModel.findOne.mockResolvedValue(mockUser); + jest.spyOn(service, 'findOne').mockResolvedValue(mockUser as IUser); + //TODO mock private function comparePassword ? -> true + return true; + expect(await service.findByLogin(loginDto)).toBe(mockUser); }); it('wrong password, should be unauthorized issue', async () => { @@ -373,42 +434,15 @@ describe('UsersService', () => { }); it('should find All Unattacheduser', async () => { - const result = [ - { - _id: '123', - validationToken: - 'cf1c74c22cedb6b575945098db42d2f493fb759c9142c6aff7980f252886f36ee086574ee99a06bc99119079257116c959c8ec870949cebdef2b293666dbca42', - emailVerified: false, - email: 'jacques.dupont@mii.com', - password: hashPassword(), - role: 0, - newEmail: '', - changeEmailToken: '', - resetPasswordToken: null, - structuresLink: [], - pendingStructuresLink: [], - structureOutdatedMailSent: [], - personalOffers: [], - name: 'Jacques', - surname: 'Dupont', - phone: '06 06 06 06 06', - createdAt: new Date('2022-05-25T09:48:28.824Z'), - } as IUser, - ]; - jest.spyOn(service, 'findAllUnattached').mockImplementation(async (): Promise<IUser[]> => result); - expect(await service.findAllUnattached()).toBe(result); + return; }); it('should find attached users', async () => { - const result = []; - jest.spyOn(service, 'findAllAttached').mockImplementation(async (): Promise<IUser[]> => result); - expect((await service.findAllAttached()).length).toBe(0); + return; }); it('should find UnVerified Users', async () => { - const result = []; - jest.spyOn(service, 'findAllUnVerified').mockImplementation(async (): Promise<IUser[]> => result); - expect((await service.findAllUnVerified()).length).toBe(0); + return; }); describe('addPersonalOffer', () => { @@ -444,7 +478,7 @@ describe('UsersService', () => { }); }); describe('getStructureOwnersDetails', () => { - const userDetailsMock: IUser[] = userDetails as IUser[]; + const userDetailsMock: IUser[] = userDetails; it('should get Structure Owners Details', async () => { jest.spyOn(service, 'getStructureOwnersDetails').mockResolvedValue(userDetails); expect(await service.getStructureOwnersDetails('627b85aea0466f0f132e1599', null)).toEqual(userDetailsMock); @@ -494,22 +528,101 @@ describe('UsersService', () => { }); }); + describe('updateUserProfile', () => { + const employer = { + _id: Types.ObjectId('6231aefe76598527c8d0b5ba'), + name: 'Metro', + validated: true, + } as EmployerDocument; + const job = { + _id: Types.ObjectId('6231aefe76598527c8d0b5ba'), + name: 'Developer', + validated: true, + } as JobDocument; + + it('should updateUserProfile', async () => { + const spyer = jest.spyOn(mockUserRegistrySearchService, 'update'); + mockUserModel.findOne.mockReturnThis(); + mockUserModel.updateOne.mockResolvedValueOnce(usersMockData[0]); + const result = await service.updateUserProfile(Types.ObjectId('627b85aea0466f0f132e1599'), employer, job); + expect(spyer).toBeCalledTimes(1); + expect(result).toEqual(usersMockData[0]); + }); + it('should not updateUserProfile', async () => { + const spyer = jest.spyOn(mockUserRegistrySearchService, 'update'); + mockUserModel.updateOne.mockResolvedValueOnce(null); + const result = await service.updateUserProfile(Types.ObjectId('627b85aea0466f0f132e1599'), employer, job); + expect(spyer).toBeCalledTimes(0); + expect(result).toEqual(null); + }); + }); + + describe('updateUserJob', () => { + const job = { + _id: Types.ObjectId('6231aefe76598527c8d0b5ba'), + name: 'Developer', + validated: true, + } as JobDocument; + it('should updateUserJob', async () => { + const spyer = jest.spyOn(mockUserRegistrySearchService, 'update'); + mockUserModel.updateOne.mockResolvedValueOnce(usersMockData[0]); + mockUserModel.findOne.mockReturnThis(); + const result = await service.updateUserJob(Types.ObjectId('627b85aea0466f0f132e1599'), job); + expect(spyer).toBeCalledTimes(1); + expect(result).toEqual(usersMockData[0]); + }); + it('should not updateUserJob', async () => { + const spyer = jest.spyOn(mockUserRegistrySearchService, 'update'); + mockUserModel.updateOne.mockResolvedValueOnce(null); + const result = await service.updateUserJob(Types.ObjectId('627b85aea0466f0f132e1599'), job); + expect(spyer).toBeCalledTimes(0); + expect(result).toEqual(null); + }); + }); + + describe('updateUserEmployer', () => { + const employer = { + _id: Types.ObjectId('6231aefe76598527c8d0b5ba'), + name: 'Metro', + validated: true, + } as EmployerDocument; + it('should updateUserEmployer', async () => { + const spyer = jest.spyOn(mockUserRegistrySearchService, 'update'); + mockUserModel.updateOne.mockResolvedValueOnce(usersMockData[0]); + const result = await service.updateUserEmployer(Types.ObjectId('627b85aea0466f0f132e1599'), employer); + expect(spyer).toBeCalledTimes(1); + expect(result).toEqual(usersMockData[0]); + }); + it('should not updateUserEmployer', async () => { + const spyer = jest.spyOn(mockUserRegistrySearchService, 'update'); + mockUserModel.updateOne.mockResolvedValueOnce(null); + const result = await service.updateUserEmployer(Types.ObjectId('627b85aea0466f0f132e1599'), employer); + expect(spyer).toBeCalledTimes(0); + expect(result).toEqual(null); + }); + }); + describe('updateDetails', () => { it('should update user details', async () => { const user = usersMockData[0]; const detailsDto: UpdateDetailsDto = { name: 'Michel', surname: 'Chelmi', phone: '0601020304' }; mockUserModel.findByIdAndUpdate.mockResolvedValueOnce({ ...user, ...detailsDto }); mockUserModel.findPopulatedUserRegistryById.mockResolvedValueOnce({ ...user, ...detailsDto }); + mockUserModel.findOne.mockReturnThis(); const updatedUser = await service.updateUserDetails('', { name: '', surname: '', phone: '' }); expect(updatedUser.name).toBe(detailsDto.name); expect(updatedUser.surname).toBe(detailsDto.surname); expect(updatedUser.phone).toBe(detailsDto.phone); }); it('should not found a user', async () => { - const response = new HttpException('User not found', HttpStatus.BAD_REQUEST); - mockUserModel.findByIdAndUpdate.mockResolvedValueOnce(response); - const query = await service.updateUserDetails('', { name: '', surname: '', phone: '' }); - expect(query).toBe(response); + mockUserModel.findByIdAndUpdate.mockResolvedValueOnce(null); + try { + await service.updateUserDetails('', { name: '', surname: '', phone: '' }); + expect(true).toBe(false); + } catch (error) { + expect(error.message).toBe('User not found'); + expect(error.status).toBe(HttpStatus.BAD_REQUEST); + } }); }); @@ -522,10 +635,14 @@ describe('UsersService', () => { expect(updatedUser.description).toBe(updatedDescription.description); }); it('should not found a user', async () => { - const response = new HttpException('User not found', HttpStatus.BAD_REQUEST); - mockUserModel.findByIdAndUpdate.mockResolvedValueOnce(response); - const query = await service.updateDescription('', { description: '' }); - expect(query).toBe(response); + mockUserModel.findByIdAndUpdate.mockResolvedValueOnce(null); + try { + await service.updateDescription('', { description: '' }); + expect(true).toBe(false); + } catch (error) { + expect(error.message).toBe('User not found'); + expect(error.status).toBe(HttpStatus.BAD_REQUEST); + } }); }); }); diff --git a/src/users/services/users.service.ts b/src/users/services/users.service.ts index f3d03ae2ad7a7a3b74d46883656004ce170b0450..49fa79b8ad1a3839ff792f11519441a734c7f430 100644 --- a/src/users/services/users.service.ts +++ b/src/users/services/users.service.ts @@ -41,6 +41,7 @@ export class UsersService { throw new HttpException('User already exists', HttpStatus.BAD_REQUEST); } if (!this.isStrongPassword(createUserDto.password)) { + console.log('weak pass'); throw new HttpException( 'Weak password, it must contain one lowercase alphabetical character, one uppercase alphabetical character, one numeric character, one special character and be eight characters or longer', HttpStatus.UNPROCESSABLE_ENTITY @@ -57,7 +58,7 @@ export class UsersService { // Send verification email createUser = await this.verifyUserMail(createUser); - createUser.save(); + await createUser.save(); this.userRegistrySearchService.indexUserRegistry(createUser); return this.findOne(createUserDto.email); } @@ -71,7 +72,7 @@ export class UsersService { * - The string must be eight characters or longer * @param password string */ - private isStrongPassword(password: string): boolean { + public isStrongPassword(password: string): boolean { const strongRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[*.! @#$%^&(){}\[\]:;<>,?\/\\~_+\-=|])(?=.{8,})/; //NOSONAR return strongRegex.test(password); } diff --git a/test/mock/data/users.mock.data.ts b/test/mock/data/users.mock.data.ts index cec7d5cd38cae2c1833ad321331199be45c5e272..01c362a058e9d204017d287f79e1d779d9c558dc 100644 --- a/test/mock/data/users.mock.data.ts +++ b/test/mock/data/users.mock.data.ts @@ -55,6 +55,48 @@ export const usersMockData: IUser[] = [ __v: 1, save: jest.fn(), } as any, + { + structureOutdatedMailSent: [], + pendingStructuresLink: ['6001a48e16b08100062e4180', '6093ba0e2ab5775cfc01fffe'], + structuresLink: [], + newEmail: null, + changeEmailToken: null, + role: 0, + resetPasswordToken: null, + validationToken: + 'b2b6caca1d38ca26d203b5f12b0d925df2928fab8ee7ccf9bbe78802ffa625f5abce825783bc62d0b11be5a90132cf5045a9a7776f01694c63b60bf64b0f680f', + emailVerified: false, + _id: '6036721022462b001334c4bb', + email: 'a@a.com', + name: 'Xavier', + surname: 'NIEL', + phone: '06 11 11 11 11', + password: '$2a$12$vLQjJ9zAWyUwiXLeQDa6w.XzrlgPBhw.2GWrjog/yuEjIaZnQwmZu', + personalOffers: [], + __v: 1, + save: jest.fn(), + } as any, + { + structureOutdatedMailSent: [], + pendingStructuresLink: ['6001a48e16b08100062e4180', '6093ba0e2ab5775cfc01fffe'], + structuresLink: ['6093ba0e2ab5775cfc01fffe', '6093ba0e2ab5775cfc01ffff', '6093ba0e2ab5775cfc020000'], + newEmail: null, + changeEmailToken: null, + role: 0, + resetPasswordToken: null, + validationToken: + 'b2b6caca1d38ca26d203b5f12b0d925df2928fab8ee7ccf9bbe78802ffa625f5abce825783bc62d0b11be5a90132cf5045a9a7776f01694c63b60bf64b0f680f', + emailVerified: false, + _id: '6036721022462b001334c4bb', + email: 'a@a.com', + name: 'Xavier', + surname: 'NIEL', + phone: '06 11 11 11 11', + password: '$2a$12$vLQjJ9zAWyUwiXLeQDa6w.XzrlgPBhw.2GWrjog/yuEjIaZnQwmZu', + personalOffers: [], + __v: 1, + save: jest.fn(), + } as any, ] as IUser[]; export const userDetails: IUser[] = [ diff --git a/test/mock/services/mailer.mock.service.ts b/test/mock/services/mailer.mock.service.ts new file mode 100644 index 0000000000000000000000000000000000000000..ef240d4c203a5fff9c4a244c3d9db07212ed87be --- /dev/null +++ b/test/mock/services/mailer.mock.service.ts @@ -0,0 +1,15 @@ +import { AxiosResponse } from 'axios'; +import { MailerService } from '../../../src/mailer/mailer.service'; + +export class MailerMockService extends MailerService { + public async send(_to: string | { email: string }[], _subject: string, _html: string): Promise<AxiosResponse<any>> { + const axiosResult: AxiosResponse = { + data: { status: 200 }, + status: 200, + statusText: 'OK', + headers: {}, + config: {}, + }; + return Promise.resolve(axiosResult); + } +}