2024-01-24 04:38:57 -08:00
|
|
|
import Container from 'typedi';
|
2024-09-12 09:07:18 -07:00
|
|
|
|
|
|
|
import { ProjectRelationRepository } from '@/databases/repositories/project-relation.repository';
|
2024-08-27 08:24:20 -07:00
|
|
|
import { UserRepository } from '@/databases/repositories/user.repository';
|
2024-09-12 09:07:18 -07:00
|
|
|
|
2023-12-19 08:02:52 -08:00
|
|
|
import { createAdmin, createMember, createOwner } from './shared/db/users';
|
2024-05-17 01:53:15 -07:00
|
|
|
import { randomEmail } from './shared/random';
|
2024-09-12 09:07:18 -07:00
|
|
|
import * as testDb from './shared/test-db';
|
2023-12-19 08:02:52 -08:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
describe('UserRepository', () => {
|
|
|
|
let userRepository: UserRepository;
|
2023-12-19 08:02:52 -08:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
await testDb.init();
|
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
userRepository = Container.get(UserRepository);
|
2023-12-19 08:02:52 -08:00
|
|
|
|
|
|
|
await testDb.truncate(['User']);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await testDb.terminate();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('countUsersByRole()', () => {
|
|
|
|
test('should return the number of users in each role', async () => {
|
|
|
|
await Promise.all([
|
|
|
|
createOwner(),
|
|
|
|
createAdmin(),
|
|
|
|
createAdmin(),
|
|
|
|
createMember(),
|
|
|
|
createMember(),
|
|
|
|
createMember(),
|
|
|
|
]);
|
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
const usersByRole = await userRepository.countUsersByRole();
|
2023-12-19 08:02:52 -08:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
expect(usersByRole).toStrictEqual({
|
|
|
|
'global:admin': 2,
|
|
|
|
'global:member': 3,
|
|
|
|
'global:owner': 1,
|
|
|
|
});
|
2023-12-19 08:02:52 -08:00
|
|
|
});
|
|
|
|
});
|
2024-05-17 01:53:15 -07:00
|
|
|
|
|
|
|
describe('createUserWithProject()', () => {
|
|
|
|
test('should create personal project for a user', async () => {
|
|
|
|
const { user, project } = await userRepository.createUserWithProject({
|
|
|
|
email: randomEmail(),
|
|
|
|
role: 'global:member',
|
|
|
|
});
|
|
|
|
|
|
|
|
const projectRelation = await Container.get(ProjectRelationRepository).findOneOrFail({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
project: {
|
|
|
|
type: 'personal',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
relations: ['project'],
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(projectRelation.project.id).toBe(project.id);
|
|
|
|
});
|
|
|
|
});
|
2023-12-19 08:02:52 -08:00
|
|
|
});
|