import { TestingModule, Test } from '@nestjs/testing'; import { DatabaseModule } from '../../../database/database.module'; import { PrismaService } from '../../../database/src/adapters/secondaries/prisma-service'; import { DatabaseException } from '../../../database/src/exceptions/database.exception'; import { AuthenticationRepository } from '../../adapters/secondaries/authentication.repository'; import { v4 } from 'uuid'; import * as bcrypt from 'bcrypt'; import { Authentication } from '../../domain/entities/authentication'; describe('AuthenticationRepository', () => { let prismaService: PrismaService; let authenticationRepository: AuthenticationRepository; const createAuthentications = async (nbToCreate = 10) => { for (let i = 0; i < nbToCreate; i++) { await prismaService.auth.create({ data: { uuid: v4(), password: bcrypt.hashSync(`password-${i}`, 10), }, }); } }; beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ imports: [DatabaseModule], providers: [AuthenticationRepository, PrismaService], }).compile(); prismaService = module.get(PrismaService); authenticationRepository = module.get( AuthenticationRepository, ); }); afterAll(async () => { await prismaService.$disconnect(); }); beforeEach(async () => { await prismaService.auth.deleteMany(); }); describe('findAll', () => { it('should return an empty data array', async () => { const res = await authenticationRepository.findAll(); expect(res).toEqual({ data: [], total: 0, }); }); it('should return a data array with 8 auths', async () => { await createAuthentications(8); const auths = await authenticationRepository.findAll(); expect(auths.data.length).toBe(8); expect(auths.total).toBe(8); }); it('should return a data array limited to 10 authentications', async () => { await createAuthentications(20); const auths = await authenticationRepository.findAll(); expect(auths.data.length).toBe(10); expect(auths.total).toBe(20); }); }); describe('findOneByUuid', () => { it('should return an authentication', async () => { const authToFind = await prismaService.auth.create({ data: { uuid: v4(), password: bcrypt.hashSync(`password`, 10), }, }); const auth = await authenticationRepository.findOneByUuid( authToFind.uuid, ); expect(auth.uuid).toBe(authToFind.uuid); }); it('should return null', async () => { const auth = await authenticationRepository.findOneByUuid( '544572be-11fb-4244-8235-587221fc9104', ); expect(auth).toBeNull(); }); }); describe('create', () => { it('should create an authentication', async () => { const beforeCount = await prismaService.auth.count(); const authenticationToCreate: Authentication = new Authentication(); authenticationToCreate.uuid = v4(); authenticationToCreate.password = bcrypt.hashSync(`password`, 10); const authentication = await authenticationRepository.create( authenticationToCreate, ); const afterCount = await prismaService.auth.count(); expect(afterCount - beforeCount).toBe(1); expect(authentication.uuid).toBeDefined(); }); }); describe('update', () => { it('should update authentication password', async () => { const authenticationToUpdate = await prismaService.auth.create({ data: { uuid: v4(), password: bcrypt.hashSync(`password`, 10), }, }); const toUpdate: Authentication = new Authentication(); toUpdate.password = bcrypt.hashSync(`newPassword`, 10); const updatedAuthentication = await authenticationRepository.update( authenticationToUpdate.uuid, toUpdate, ); expect(updatedAuthentication.uuid).toBe(authenticationToUpdate.uuid); }); it('should throw DatabaseException', async () => { const toUpdate: Authentication = new Authentication(); toUpdate.password = bcrypt.hashSync(`newPassword`, 10); await expect( authenticationRepository.update( '544572be-11fb-4244-8235-587221fc9104', toUpdate, ), ).rejects.toBeInstanceOf(DatabaseException); }); }); describe('delete', () => { it('should delete an authentication', async () => { const authenticationToRemove = await prismaService.auth.create({ data: { uuid: v4(), password: bcrypt.hashSync(`password`, 10), }, }); await authenticationRepository.delete(authenticationToRemove.uuid); const count = await prismaService.auth.count(); expect(count).toBe(0); }); it('should throw DatabaseException', async () => { await expect( authenticationRepository.delete('544572be-11fb-4244-8235-587221fc9104'), ).rejects.toBeInstanceOf(DatabaseException); }); }); });