226 lines
10 KiB
TypeScript
226 lines
10 KiB
TypeScript
import "module-alias/register";
|
|
import "reflect-metadata";
|
|
import User from "le-coffre-resources/dist/SuperAdmin";
|
|
import UsersService from "@Services/super-admin/UsersService/UsersService";
|
|
import { PrismaClient } from "@prisma/client";
|
|
import { user, userContact, userContact_, user_ } from "@Test/config/MockedData";
|
|
import UsersRepository from "@Repositories/UsersRepository";
|
|
import Container from "typedi";
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
const UsersServiceTest = new UsersService(Container.get(UsersRepository));
|
|
|
|
afterAll(async () => {
|
|
/*
|
|
* Clean database after all tests execution.
|
|
* Due to cascade deletion, if addresses are deleted, all items following tables are dropped: contacts, users, offices
|
|
*/
|
|
const deleteAddresses = prisma.addresses.deleteMany();
|
|
await prisma.$transaction([deleteAddresses]);
|
|
await prisma.$disconnect();
|
|
});
|
|
|
|
describe("test create function", () => {
|
|
it("should create a new user", async () => {
|
|
const userCreated = await UsersServiceTest.create(user);
|
|
|
|
expect(userCreated?.idNot).toEqual(user.idNot);
|
|
|
|
// verify if user contact is created in db
|
|
const contactCreated = await prisma.contacts.findUnique({ where: { uid: userCreated.contact_uid } });
|
|
expect(contactCreated?.first_name).toEqual(user.contact.first_name);
|
|
expect(contactCreated?.last_name).toEqual(user.contact.last_name);
|
|
expect(contactCreated?.cell_phone_number).toEqual(user.contact.cell_phone_number);
|
|
expect(contactCreated?.phone_number).toEqual(user.contact.phone_number);
|
|
expect(contactCreated?.civility).toEqual(user.contact.civility);
|
|
expect(contactCreated?.email).toEqual(user.contact.email);
|
|
|
|
// verify if user address is created in db
|
|
const addressForContactCreated = await prisma.addresses.findUnique({ where: { uid: contactCreated?.address_uid } });
|
|
expect(addressForContactCreated?.address).toEqual(user.contact.address?.address);
|
|
expect(addressForContactCreated?.zip_code).toEqual(user.contact.address?.zip_code);
|
|
expect(addressForContactCreated?.city).toEqual(user.contact.address?.city);
|
|
|
|
// verify if user office is created in db
|
|
const officeCreated = await prisma.offices.findUnique({ where: { uid: userCreated.office_uid } });
|
|
expect(officeCreated?.idNot).toEqual(user.office_membership.idNot);
|
|
expect(officeCreated?.name).toEqual(user.office_membership.name);
|
|
expect(officeCreated?.crpcen).toEqual(user.office_membership.crpcen);
|
|
expect(officeCreated?.office_status).toEqual("DESACTIVATED");
|
|
|
|
// verify if user office's address is created in db
|
|
const addressForOfficeCreated = await prisma.addresses.findUnique({ where: { uid: officeCreated?.address_uid } });
|
|
expect(addressForOfficeCreated?.address).toEqual(user.office_membership.address.address);
|
|
expect(addressForOfficeCreated?.zip_code).toEqual(user.office_membership.address.zip_code);
|
|
expect(addressForOfficeCreated?.city).toEqual(user.office_membership.address.city);
|
|
});
|
|
|
|
it("should not create an user already created", async () => {
|
|
// try to create the same user
|
|
async function duplicateUser() {
|
|
await UsersServiceTest.create(user);
|
|
}
|
|
await expect(duplicateUser).rejects.toThrow();
|
|
});
|
|
|
|
it("should not create an new user with an email already created", async () => {
|
|
let newUser: User = JSON.parse(JSON.stringify(user_));
|
|
newUser.contact.email = userContact.email;
|
|
|
|
// try to create a new user with already used email
|
|
async function createUserWithDuplicateEmail() {
|
|
await UsersServiceTest.create(newUser);
|
|
}
|
|
await expect(createUserWithDuplicateEmail).rejects.toThrow();
|
|
});
|
|
|
|
it("should not create an user with an phone number already created", async () => {
|
|
let newUser: User = JSON.parse(JSON.stringify(user_));
|
|
newUser.contact.cell_phone_number = userContact.cell_phone_number;
|
|
|
|
// try to create a new user with already used cellphone number
|
|
async function duplicateUser() {
|
|
await UsersServiceTest.create(newUser);
|
|
}
|
|
await expect(duplicateUser).rejects.toThrow();
|
|
});
|
|
|
|
it("should create an new user if unique attributes differ from existing users", async () => {
|
|
let newUser: User = JSON.parse(JSON.stringify(user));
|
|
newUser.idNot = user_.idNot;
|
|
newUser.contact.email = userContact_.email;
|
|
newUser.contact.cell_phone_number = userContact_.cell_phone_number;
|
|
|
|
const userCreated = await UsersServiceTest.create(newUser);
|
|
|
|
expect(userCreated?.idNot).toEqual(user_.idNot);
|
|
|
|
// verify if user_ contact is created in db
|
|
const contactCreated = await prisma.contacts.findUnique({ where: { uid: userCreated.contact_uid } });
|
|
expect(contactCreated?.first_name).toEqual(user.contact.first_name);
|
|
expect(contactCreated?.last_name).toEqual(user.contact.last_name);
|
|
expect(contactCreated?.cell_phone_number).toEqual(user_.contact.cell_phone_number);
|
|
expect(contactCreated?.phone_number).toEqual(user.contact.phone_number);
|
|
expect(contactCreated?.civility).toEqual(user.contact.civility);
|
|
expect(contactCreated?.email).toEqual(user_.contact.email);
|
|
|
|
// verify if user_ address is created in db
|
|
const addressForContactCreated = await prisma.addresses.findUnique({ where: { uid: contactCreated?.address_uid } });
|
|
expect(addressForContactCreated?.address).toEqual(user.contact.address?.address);
|
|
expect(addressForContactCreated?.zip_code).toEqual(user.contact.address?.zip_code);
|
|
expect(addressForContactCreated?.city).toEqual(user.contact.address?.city);
|
|
|
|
// verify if user joined the existing office
|
|
const officeJoined = await prisma.offices.findUnique({ where: { uid: userCreated.office_uid } });
|
|
expect(officeJoined?.idNot).toEqual(user.office_membership.idNot);
|
|
expect(officeJoined?.name).toEqual(user.office_membership.name);
|
|
expect(officeJoined?.crpcen).toEqual(user.office_membership.crpcen);
|
|
expect(officeJoined?.office_status).toEqual("DESACTIVATED");
|
|
});
|
|
});
|
|
|
|
describe("test update function", () => {
|
|
it("should update an user's data", async () => {
|
|
const userCreated = await prisma.users.findFirstOrThrow({ where: { idNot: user_.idNot } });
|
|
|
|
const officeJoined = await prisma.offices.findUnique({ where: { uid: userCreated.office_uid } });
|
|
expect(officeJoined?.idNot).toEqual(user.office_membership.idNot);
|
|
expect(officeJoined?.name).toEqual(user.office_membership.name);
|
|
expect(officeJoined?.crpcen).toEqual(user.office_membership.crpcen);
|
|
expect(officeJoined?.office_status).toEqual("DESACTIVATED");
|
|
|
|
// update the last user created with his own new office and own contact
|
|
const updatedUser = await UsersServiceTest.update(userCreated.uid, user_);
|
|
|
|
expect(updatedUser?.idNot).toEqual(user_.idNot);
|
|
|
|
// verify if user_ contact is created in db
|
|
const existingContact = await prisma.contacts.findUnique({ where: { uid: updatedUser.contact_uid } });
|
|
expect(existingContact?.first_name).toEqual(user_.contact.first_name);
|
|
expect(existingContact?.last_name).toEqual(user_.contact.last_name);
|
|
expect(existingContact?.cell_phone_number).toEqual(user_.contact.cell_phone_number);
|
|
expect(existingContact?.phone_number).toEqual(user_.contact.phone_number);
|
|
expect(existingContact?.civility).toEqual(user_.contact.civility);
|
|
expect(existingContact?.email).toEqual(user_.contact.email);
|
|
|
|
// verify if user_ address is created in db
|
|
const addressForExistingContact = await prisma.addresses.findUnique({ where: { uid: existingContact?.address_uid } });
|
|
expect(addressForExistingContact?.address).toEqual(user_.contact.address?.address);
|
|
expect(addressForExistingContact?.zip_code).toEqual(user_.contact.address?.zip_code);
|
|
expect(addressForExistingContact?.city).toEqual(user_.contact.address?.city);
|
|
|
|
// verify if user_ joined the new office
|
|
const officeCreated = await prisma.offices.findUnique({ where: { uid: updatedUser.office_uid } });
|
|
expect(officeCreated?.idNot).toEqual(user_.office_membership.idNot);
|
|
expect(officeCreated?.name).toEqual(user_.office_membership.name);
|
|
expect(officeCreated?.crpcen).toEqual(user_.office_membership.crpcen);
|
|
expect(officeCreated?.office_status).toEqual("DESACTIVATED");
|
|
|
|
// verify is user_ office's address is created in db
|
|
const addressForOfficeCreated = await prisma.addresses.findUnique({ where: { uid: officeCreated?.address_uid } });
|
|
expect(addressForOfficeCreated?.address).toEqual(user_.office_membership.address.address);
|
|
expect(addressForOfficeCreated?.zip_code).toEqual(user_.office_membership.address.zip_code);
|
|
expect(addressForOfficeCreated?.city).toEqual(user_.office_membership.address.city);
|
|
});
|
|
|
|
it("should not update an user with an email already used", async () => {
|
|
const userUid = (await prisma.users.findFirstOrThrow({ where: { idNot: user_.idNot } })).uid;
|
|
let updatedUser: User = JSON.parse(JSON.stringify(user_));
|
|
updatedUser.contact.email = userContact.email;
|
|
|
|
// try to create a new user with already used email
|
|
async function updateUserWithDuplicateEmail() {
|
|
await UsersServiceTest.update(userUid, updatedUser);
|
|
}
|
|
await expect(updateUserWithDuplicateEmail).rejects.toThrow();
|
|
});
|
|
|
|
it("should not update an user with an phone number already used", async () => {
|
|
const userUid = (await prisma.users.findFirstOrThrow({ where: { idNot: user_.idNot } })).uid;
|
|
let updatedUser: User = JSON.parse(JSON.stringify(user_));
|
|
updatedUser.contact.cell_phone_number = userContact.cell_phone_number;
|
|
|
|
// try to create a new user with already used email
|
|
async function updateUserWithDuplicateEmail() {
|
|
await UsersServiceTest.update(userUid, updatedUser);
|
|
}
|
|
await expect(updateUserWithDuplicateEmail).rejects.toThrow();
|
|
});
|
|
});
|
|
|
|
describe("test get function", () => {
|
|
it("should return an array of Users", async () => {
|
|
const req = {};
|
|
const users = await UsersServiceTest.get(req);
|
|
|
|
// verify result typing
|
|
expect(users).toBeInstanceOf(Array<User>);
|
|
expect(users.length).toEqual(2);
|
|
|
|
// verify result content
|
|
const usersCreated = await prisma.users.findMany();
|
|
expect(users).toContainEqual(usersCreated[0]);
|
|
expect(users).toContainEqual(usersCreated[1]);
|
|
});
|
|
|
|
it("should return an array of Users per offices", async () => {
|
|
const officesCreated = await prisma.offices.findMany();
|
|
const reqForFirstOffice = { where: { office_uid: officesCreated[0]?.uid } };
|
|
const usersForFirstOffice = await UsersServiceTest.get(reqForFirstOffice);
|
|
|
|
expect(usersForFirstOffice.length).toEqual(1);
|
|
|
|
// verify result content
|
|
expect(usersForFirstOffice[0]?.idNot).toEqual(user.idNot);
|
|
|
|
const reqForSecondOffice = { where: { office_uid: officesCreated[1]?.uid } };
|
|
const usersForSecondOffice = await UsersServiceTest.get(reqForSecondOffice);
|
|
|
|
expect(usersForSecondOffice.length).toEqual(1);
|
|
|
|
// verify result content
|
|
expect(usersForSecondOffice[0]?.idNot).toEqual(user_.idNot);
|
|
});
|
|
});
|