import { DomainEventPublisherModule } from '@common/domain-event-publisher/domainEventPublisher.module' ; import { FakeLoggerService } from '@common/logger/adapters/fake/FakeLogger.service' ; import { PinoLoggerService } from '@common/logger/adapters/real/pinoLogger.service' ; import { executeTask } from '@common/utils/executeTask' ; import { SignUp , SignUpHandler } from '@identity-and-access/application/commands/signUp.command' ; import { EmailAlreadyExistsException } from '@identity-and-access/domain/exceptions/emailAlreadyExists.exception' ; import { FakeUserRepository } from '@identity-and-access/infrastructure/adapters/secondaries/fake/fakeUser.repository' ; import { RealHashingService } from '@identity-and-access/infrastructure/adapters/secondaries/real/realHashing.service' ; import { RealRandomNumberGenerator } from '@identity-and-access/infrastructure/adapters/secondaries/real/realRandomNumberGenerator' ; import { RealUUIDGeneratorService } from '@identity-and-access/infrastructure/adapters/secondaries/real/realUUIDGenerator.service' ; import { UserRepository } from '@identity-and-access/infrastructure/ports/user.repository' ; import { UnprocessableEntityException } from '@nestjs/common' ; import { Test } from '@nestjs/testing' ; let userRepository ; describe ( '[Unit] Sign up with credentials' , ( ) => { let signUpHandler ; beforeEach ( async ( ) => { const moduleRef = await Test . createTestingModule ( { imports : [ DomainEventPublisherModule ] , providers : [ SignUpHandler , RealUUIDGeneratorService , RealRandomNumberGenerator , RealHashingService , { provide : UserRepository , useClass : FakeUserRepository } , { provide : PinoLoggerService , useClass : FakeLoggerService } , ] , } ) . compile ( ) ; userRepository = moduleRef . get < UserRepository > ( UserRepository ) as FakeUserRepository ; signUpHandler = moduleRef . get < SignUpHandler > ( SignUpHandler ) ; } ) ; it ( 'Should sign up a user if email and passwords are valid' , async ( ) => { const email = 'dummy1@gmail.com' ; const password = 'paSSw0rd!' ; const result = await signUpHandler . execute ( new SignUp ( email , password ) ) ; expect ( result ) . toEqual ( undefined ) ; const users = await executeTask ( userRepository . all ( ) ) ; expect ( users . length ) . toEqual ( 1 ) ; } ) ; it ( 'Should have set the user as unverified if he successfully signed up' , async ( ) => { const email = 'dummy1@gmail.com' ; const password = 'paSSw0rd!' ; const result = await signUpHandler . execute ( new SignUp ( email , password ) ) ; const users = await executeTask ( userRepository . all ( ) ) ; expect ( users [ 0 ] . contactInformation . isVerified ) . toEqual ( false ) ; } ) ; it ( 'Should not create a user if email is invalid' , async ( ) => { const email = 'abc123' ; const password = 'paSSw0rd!' ; const resultPromise = signUpHandler . execute ( new SignUp ( email , password ) ) ; await expect ( resultPromise ) . rejects . toBeInstanceOf ( UnprocessableEntityException ) ; const users = await executeTask ( userRepository . all ( ) ) ; expect ( users . length ) . toEqual ( 0 ) ; } ) ; it ( 'Should not create a user if password is invalid' , async ( ) => { const email = 'dummy1@gmail.com' ; const password = 'toosimple' ; const resultPromise = signUpHandler . execute ( new SignUp ( email , password ) ) ; await expect ( resultPromise ) . rejects . toBeInstanceOf ( UnprocessableEntityException ) ; const users = await executeTask ( userRepository . all ( ) ) ; expect ( users . length ) . toEqual ( 0 ) ; } ) ; it ( 'Should not create a user if email already exists' , async ( ) => { const email = 'dummy1@gmail.com' ; const password = 'paSSw0rd!' ; await signUpHandler . execute ( new SignUp ( email , password ) ) ; const resultPromise = signUpHandler . execute ( new SignUp ( email , password ) ) ; await expect ( resultPromise ) . rejects . toBeInstanceOf ( EmailAlreadyExistsException ) ; } ) ; } ) ;