Skip to content

Commit

Permalink
Refactor setupUser test utility
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Mar 3, 2024
1 parent 39982af commit 9ff4b8d
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions api/test/utils/userAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,29 @@ export type TestUser = { jwtToken: string; user: User; password: string };
export async function setupTestUser(
applicationManager: TestApplication,
roleName: ROLES = ROLES.ADMIN,
extraData: Partial<User> = { password: 'Password123!' },
password: string = 'Password123',
extraData: Partial<Omit<User, 'password'>> = {},
): Promise<TestUser> {
const salt = await genSalt();
const role = new Role();
role.name = roleName;
const entityManager = applicationManager.get<EntityManager>(EntityManager);
const userRepository = entityManager.getRepository(User);

const { password, ...restOfExtraData } = extraData;

await setUpRolesAndPermissions(entityManager);

let existingUser = await userRepository.findOne({
where: { email: E2E_CONFIG.users.signUp.email },
where: { email: extraData.email ?? E2E_CONFIG.users.signUp.email },
});
if (!existingUser) {
existingUser = await userRepository.save({
...E2E_CONFIG.users.signUp,
salt,
password: await hash(password!, salt),
password: await hash(password, salt),
isActive: true,
isDeleted: false,
roles: [role],
...restOfExtraData,
...extraData,
});
}

Expand Down

0 comments on commit 9ff4b8d

Please sign in to comment.