mirror of
https://gitlab.com/mobicoop/v3/service/auth.git
synced 2026-01-02 20:52:41 +00:00
integration tests
This commit is contained in:
@@ -17,9 +17,7 @@ export class DeleteAuthUseCase {
|
||||
await this._usernameRepository.deleteMany({
|
||||
uuid: command.deleteAuthRequest.uuid,
|
||||
});
|
||||
return await this._authRepository.delete({
|
||||
uuid: command.deleteAuthRequest.uuid,
|
||||
});
|
||||
return await this._authRepository.delete(command.deleteAuthRequest.uuid);
|
||||
} catch (error) {
|
||||
this._loggingMessager.publish(
|
||||
'auth.delete.crit',
|
||||
|
||||
@@ -21,7 +21,7 @@ export class DeleteUsernameUseCase {
|
||||
uuid: usernameFound.uuid,
|
||||
});
|
||||
if (usernames.total > 1) {
|
||||
return await this._usernameRepository.delete({ username });
|
||||
return await this._usernameRepository.deleteMany({ username });
|
||||
}
|
||||
throw new UnauthorizedException();
|
||||
} catch (error) {
|
||||
|
||||
153
src/modules/auth/tests/integration/auth.repository.spec.ts
Normal file
153
src/modules/auth/tests/integration/auth.repository.spec.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
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/DatabaseException';
|
||||
import { AuthRepository } from '../../adapters/secondaries/auth.repository';
|
||||
import { v4 } from 'uuid';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { Auth } from '../../domain/entities/auth';
|
||||
|
||||
describe('AuthRepository', () => {
|
||||
let prismaService: PrismaService;
|
||||
let authRepository: AuthRepository;
|
||||
|
||||
const createAuths = 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: [AuthRepository, PrismaService],
|
||||
}).compile();
|
||||
|
||||
prismaService = module.get<PrismaService>(PrismaService);
|
||||
authRepository = module.get<AuthRepository>(AuthRepository);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await prismaService.$disconnect();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await prismaService.auth.deleteMany();
|
||||
});
|
||||
|
||||
describe('findAll', () => {
|
||||
it('should return an empty data array', async () => {
|
||||
const res = await authRepository.findAll();
|
||||
expect(res).toEqual({
|
||||
data: [],
|
||||
total: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a data array with 8 auths', async () => {
|
||||
await createAuths(8);
|
||||
const auths = await authRepository.findAll();
|
||||
expect(auths.data.length).toBe(8);
|
||||
expect(auths.total).toBe(8);
|
||||
});
|
||||
|
||||
it('should return a data array limited to 10 auths', async () => {
|
||||
await createAuths(20);
|
||||
const auths = await authRepository.findAll();
|
||||
expect(auths.data.length).toBe(10);
|
||||
expect(auths.total).toBe(20);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findOneByUuid', () => {
|
||||
it('should return an auth', async () => {
|
||||
const authToFind = await prismaService.auth.create({
|
||||
data: {
|
||||
uuid: v4(),
|
||||
password: bcrypt.hashSync(`password`, 10),
|
||||
},
|
||||
});
|
||||
|
||||
const auth = await authRepository.findOneByUuid(authToFind.uuid);
|
||||
expect(auth.uuid).toBe(authToFind.uuid);
|
||||
});
|
||||
|
||||
it('should return null', async () => {
|
||||
const auth = await authRepository.findOneByUuid(
|
||||
'544572be-11fb-4244-8235-587221fc9104',
|
||||
);
|
||||
expect(auth).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('create', () => {
|
||||
it('should create an auth', async () => {
|
||||
const beforeCount = await prismaService.auth.count();
|
||||
|
||||
const authToCreate: Auth = new Auth();
|
||||
authToCreate.uuid = v4();
|
||||
authToCreate.password = bcrypt.hashSync(`password`, 10);
|
||||
const auth = await authRepository.create(authToCreate);
|
||||
|
||||
const afterCount = await prismaService.auth.count();
|
||||
|
||||
expect(afterCount - beforeCount).toBe(1);
|
||||
expect(auth.uuid).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('update', () => {
|
||||
it('should update auth password', async () => {
|
||||
const authToUpdate = await prismaService.auth.create({
|
||||
data: {
|
||||
uuid: v4(),
|
||||
password: bcrypt.hashSync(`password`, 10),
|
||||
},
|
||||
});
|
||||
|
||||
const toUpdate: Auth = new Auth();
|
||||
toUpdate.password = bcrypt.hashSync(`newPassword`, 10);
|
||||
const updatedAuth = await authRepository.update(
|
||||
authToUpdate.uuid,
|
||||
toUpdate,
|
||||
);
|
||||
|
||||
expect(updatedAuth.uuid).toBe(authToUpdate.uuid);
|
||||
});
|
||||
|
||||
it('should throw DatabaseException', async () => {
|
||||
const toUpdate: Auth = new Auth();
|
||||
toUpdate.password = bcrypt.hashSync(`newPassword`, 10);
|
||||
|
||||
await expect(
|
||||
authRepository.update('544572be-11fb-4244-8235-587221fc9104', toUpdate),
|
||||
).rejects.toBeInstanceOf(DatabaseException);
|
||||
});
|
||||
});
|
||||
|
||||
describe('delete', () => {
|
||||
it('should delete an auth', async () => {
|
||||
const authToRemove = await prismaService.auth.create({
|
||||
data: {
|
||||
uuid: v4(),
|
||||
password: bcrypt.hashSync(`password`, 10),
|
||||
},
|
||||
});
|
||||
await authRepository.delete(authToRemove.uuid);
|
||||
|
||||
const count = await prismaService.auth.count();
|
||||
expect(count).toBe(0);
|
||||
});
|
||||
|
||||
it('should throw DatabaseException', async () => {
|
||||
await expect(
|
||||
authRepository.delete('544572be-11fb-4244-8235-587221fc9104'),
|
||||
).rejects.toBeInstanceOf(DatabaseException);
|
||||
});
|
||||
});
|
||||
});
|
||||
282
src/modules/auth/tests/integration/username.repository.spec.ts
Normal file
282
src/modules/auth/tests/integration/username.repository.spec.ts
Normal file
@@ -0,0 +1,282 @@
|
||||
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/DatabaseException';
|
||||
import { v4 } from 'uuid';
|
||||
import { Type } from '../../domain/dtos/type.enum';
|
||||
import { UsernameRepository } from '../../adapters/secondaries/username.repository';
|
||||
import { Username } from '../../domain/entities/username';
|
||||
|
||||
describe('UsernameRepository', () => {
|
||||
let prismaService: PrismaService;
|
||||
let usernameRepository: UsernameRepository;
|
||||
|
||||
const createUsernames = async (nbToCreate = 10) => {
|
||||
for (let i = 0; i < nbToCreate; i++) {
|
||||
await prismaService.username.create({
|
||||
data: {
|
||||
uuid: v4(),
|
||||
username: `john.doe.${i}@email.com`,
|
||||
type: Type.EMAIL,
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
imports: [DatabaseModule],
|
||||
providers: [UsernameRepository, PrismaService],
|
||||
}).compile();
|
||||
|
||||
prismaService = module.get<PrismaService>(PrismaService);
|
||||
usernameRepository = module.get<UsernameRepository>(UsernameRepository);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await prismaService.$disconnect();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await prismaService.username.deleteMany();
|
||||
});
|
||||
|
||||
describe('findAll', () => {
|
||||
it('should return an empty data array', async () => {
|
||||
const res = await usernameRepository.findAll();
|
||||
expect(res).toEqual({
|
||||
data: [],
|
||||
total: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a data array with 8 usernames', async () => {
|
||||
await createUsernames(8);
|
||||
const usernames = await usernameRepository.findAll();
|
||||
expect(usernames.data.length).toBe(8);
|
||||
expect(usernames.total).toBe(8);
|
||||
});
|
||||
|
||||
it('should return a data array limited to 10 usernames', async () => {
|
||||
await createUsernames(20);
|
||||
const usernames = await usernameRepository.findAll();
|
||||
expect(usernames.data.length).toBe(10);
|
||||
expect(usernames.total).toBe(20);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findOne', () => {
|
||||
it('should return a username with uuid and email', async () => {
|
||||
const usernameToFind = await prismaService.username.create({
|
||||
data: {
|
||||
uuid: v4(),
|
||||
username: 'john.doe@email.com',
|
||||
type: Type.EMAIL,
|
||||
},
|
||||
});
|
||||
|
||||
const username = await usernameRepository.findOne({
|
||||
username: 'john.doe@email.com',
|
||||
type: Type.EMAIL,
|
||||
});
|
||||
expect(username.uuid).toBe(usernameToFind.uuid);
|
||||
});
|
||||
|
||||
it('should return null', async () => {
|
||||
const username = await usernameRepository.findOne({
|
||||
username: 'jane.doe@email.com',
|
||||
type: Type.EMAIL,
|
||||
});
|
||||
expect(username).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('create', () => {
|
||||
it('should create a username with an email', async () => {
|
||||
const beforeCount = await prismaService.username.count();
|
||||
|
||||
const usernameToCreate: Username = new Username();
|
||||
usernameToCreate.uuid = v4();
|
||||
usernameToCreate.username = 'john.doe@email.com';
|
||||
usernameToCreate.type = Type.EMAIL;
|
||||
const username = await usernameRepository.create(usernameToCreate);
|
||||
|
||||
const afterCount = await prismaService.username.count();
|
||||
|
||||
expect(afterCount - beforeCount).toBe(1);
|
||||
expect(username.uuid).toBeDefined();
|
||||
});
|
||||
it('should create a username with a phone number', async () => {
|
||||
const beforeCount = await prismaService.username.count();
|
||||
|
||||
const usernameToCreate: Username = new Username();
|
||||
usernameToCreate.uuid = v4();
|
||||
usernameToCreate.username = '+33611223344';
|
||||
usernameToCreate.type = Type.PHONE;
|
||||
const username = await usernameRepository.create(usernameToCreate);
|
||||
|
||||
const afterCount = await prismaService.username.count();
|
||||
|
||||
expect(afterCount - beforeCount).toBe(1);
|
||||
expect(username.uuid).toBeDefined();
|
||||
});
|
||||
it('should create a username with an email for an existing uuid', async () => {
|
||||
const beforeCount = await prismaService.username.count();
|
||||
|
||||
const uuid = v4();
|
||||
|
||||
const firstUsernameToCreate: Username = new Username();
|
||||
firstUsernameToCreate.uuid = uuid;
|
||||
firstUsernameToCreate.username = '+33611223344';
|
||||
firstUsernameToCreate.type = Type.PHONE;
|
||||
const firstUsername = await usernameRepository.create(
|
||||
firstUsernameToCreate,
|
||||
);
|
||||
|
||||
const secondUsernameToCreate: Username = new Username();
|
||||
secondUsernameToCreate.uuid = uuid;
|
||||
secondUsernameToCreate.username = 'john.doe@email.com';
|
||||
secondUsernameToCreate.type = Type.EMAIL;
|
||||
const secondUsername = await usernameRepository.create(
|
||||
secondUsernameToCreate,
|
||||
);
|
||||
|
||||
const afterCount = await prismaService.username.count();
|
||||
|
||||
expect(afterCount - beforeCount).toBe(2);
|
||||
expect(firstUsername.uuid).toEqual(secondUsername.uuid);
|
||||
});
|
||||
it('should throw DatabaseException if username already exists for a given type', async () => {
|
||||
const uuid = v4();
|
||||
|
||||
const firstUsernameToCreate: Username = new Username();
|
||||
firstUsernameToCreate.uuid = uuid;
|
||||
firstUsernameToCreate.username = 'john.doe@email.com';
|
||||
firstUsernameToCreate.type = Type.EMAIL;
|
||||
await usernameRepository.create(firstUsernameToCreate);
|
||||
|
||||
const secondUsernameToCreate: Username = new Username();
|
||||
secondUsernameToCreate.uuid = uuid;
|
||||
secondUsernameToCreate.username = 'jane.doe@email.com';
|
||||
secondUsernameToCreate.type = Type.EMAIL;
|
||||
|
||||
await expect(
|
||||
usernameRepository.create(secondUsernameToCreate),
|
||||
).rejects.toBeInstanceOf(DatabaseException);
|
||||
});
|
||||
});
|
||||
|
||||
describe('update', () => {
|
||||
it('should update username email', async () => {
|
||||
const usernameToUpdate = await prismaService.username.create({
|
||||
data: {
|
||||
uuid: v4(),
|
||||
username: `john.doe@email.com`,
|
||||
type: Type.EMAIL,
|
||||
},
|
||||
});
|
||||
|
||||
const toUpdate: Username = new Username();
|
||||
toUpdate.username = 'jane.doe@email.com';
|
||||
const updatedUsername = await usernameRepository.updateWhere(
|
||||
{
|
||||
uuid_type: {
|
||||
uuid: usernameToUpdate.uuid,
|
||||
type: usernameToUpdate.type,
|
||||
},
|
||||
},
|
||||
{
|
||||
username: toUpdate.username,
|
||||
},
|
||||
);
|
||||
|
||||
expect(updatedUsername.uuid).toBe(usernameToUpdate.uuid);
|
||||
expect(updatedUsername.username).toBe('jane.doe@email.com');
|
||||
});
|
||||
|
||||
it('should update username phone', async () => {
|
||||
const usernameToUpdate = await prismaService.username.create({
|
||||
data: {
|
||||
uuid: v4(),
|
||||
username: `+33611223344`,
|
||||
type: Type.PHONE,
|
||||
},
|
||||
});
|
||||
|
||||
const toUpdate: Username = new Username();
|
||||
toUpdate.username = '+33622334455';
|
||||
const updatedUsername = await usernameRepository.updateWhere(
|
||||
{
|
||||
uuid_type: {
|
||||
uuid: usernameToUpdate.uuid,
|
||||
type: usernameToUpdate.type,
|
||||
},
|
||||
},
|
||||
{
|
||||
username: toUpdate.username,
|
||||
},
|
||||
);
|
||||
|
||||
expect(updatedUsername.uuid).toBe(usernameToUpdate.uuid);
|
||||
expect(updatedUsername.username).toBe('+33622334455');
|
||||
});
|
||||
it('should throw DatabaseException if email not found', async () => {
|
||||
const toUpdate: Username = new Username();
|
||||
toUpdate.username = 'jane.doe@email.com';
|
||||
|
||||
await expect(
|
||||
usernameRepository.updateWhere(
|
||||
{
|
||||
uuid_type: {
|
||||
uuid: '544572be-11fb-4244-8235-587221fc9104',
|
||||
type: Type.EMAIL,
|
||||
},
|
||||
},
|
||||
{
|
||||
username: toUpdate.username,
|
||||
},
|
||||
),
|
||||
).rejects.toBeInstanceOf(DatabaseException);
|
||||
});
|
||||
it('should throw DatabaseException if phone not found', async () => {
|
||||
const toUpdate: Username = new Username();
|
||||
toUpdate.username = '+33611223344';
|
||||
|
||||
await expect(
|
||||
usernameRepository.updateWhere(
|
||||
{
|
||||
uuid_type: {
|
||||
uuid: '544572be-11fb-4244-8235-587221fc9104',
|
||||
type: Type.PHONE,
|
||||
},
|
||||
},
|
||||
{
|
||||
username: toUpdate.username,
|
||||
},
|
||||
),
|
||||
).rejects.toBeInstanceOf(DatabaseException);
|
||||
});
|
||||
});
|
||||
|
||||
describe('delete', () => {
|
||||
it('should delete a username', async () => {
|
||||
const usernameToRemove = await prismaService.username.create({
|
||||
data: {
|
||||
uuid: v4(),
|
||||
username: `+33611223344`,
|
||||
type: Type.PHONE,
|
||||
},
|
||||
});
|
||||
await usernameRepository.deleteMany({ uuid: usernameToRemove.uuid });
|
||||
|
||||
const count = await prismaService.username.count();
|
||||
expect(count).toBe(0);
|
||||
});
|
||||
|
||||
it('should throw DatabaseException', async () => {
|
||||
await expect(
|
||||
usernameRepository.delete('544572be-11fb-4244-8235-587221fc9104'),
|
||||
).rejects.toBeInstanceOf(DatabaseException);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -63,7 +63,7 @@ const mockUsernameRepository = {
|
||||
}
|
||||
return Promise.resolve(usernamesPhone);
|
||||
}),
|
||||
delete: jest.fn().mockResolvedValue(undefined),
|
||||
deleteMany: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
|
||||
const mockMessager = {
|
||||
|
||||
@@ -131,7 +131,6 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||
|
||||
return updatedEntity;
|
||||
} catch (e) {
|
||||
console.log('error', e);
|
||||
if (e instanceof PrismaClientKnownRequestError) {
|
||||
throw new DatabaseException(
|
||||
PrismaClientKnownRequestError.name,
|
||||
@@ -144,10 +143,10 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||
}
|
||||
}
|
||||
|
||||
async delete(where: any): Promise<void> {
|
||||
async delete(uuid: string): Promise<void> {
|
||||
try {
|
||||
const entity = await this._prisma[this._model].delete({
|
||||
where: where,
|
||||
where: { uuid },
|
||||
});
|
||||
|
||||
return entity;
|
||||
|
||||
@@ -231,7 +231,7 @@ describe('PrismaRepository', () => {
|
||||
const savedUuid = fakeEntities[0].uuid;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const res = await fakeRepository.delete({ uuid: savedUuid });
|
||||
const res = await fakeRepository.delete(savedUuid);
|
||||
|
||||
const deletedEntity = fakeEntities.find(
|
||||
(entity) => entity.uuid === savedUuid,
|
||||
|
||||
Reference in New Issue
Block a user