From c734169f288bb054dc2f6b036b2bab354eac0de6 Mon Sep 17 00:00:00 2001 From: Maximilian Kreuzkam Date: Wed, 18 Dec 2024 15:30:51 +0100 Subject: [PATCH] Add tests for user service and repo. --- .../modules/user/service/user.service.spec.ts | 20 +++++++++++++ .../user/user-do.repo.integration.spec.ts | 29 +++++++++++++++++++ .../src/shared/repo/user/user-do.repo.ts | 18 ++++++------ 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/apps/server/src/modules/user/service/user.service.spec.ts b/apps/server/src/modules/user/service/user.service.spec.ts index 48e0706635..ec71f6a848 100644 --- a/apps/server/src/modules/user/service/user.service.spec.ts +++ b/apps/server/src/modules/user/service/user.service.spec.ts @@ -1150,4 +1150,24 @@ describe('UserService', () => { }); }); }); + + describe('findByTspUids', () => { + describe('when looking for users with tspUids', () => { + const setup = () => { + const user = userDoFactory.build(); + userDORepo.findByTspUids.mockResolvedValueOnce([user]); + + return { user }; + }; + + it('should delegate to the userRepo', async () => { + const { user } = setup(); + + const result = await service.findByTspUids(['tspUid']); + + expect(result).toStrictEqual([user]); + expect(userDORepo.findByTspUids).toHaveBeenCalledTimes(1); + }); + }); + }); }); diff --git a/apps/server/src/shared/repo/user/user-do.repo.integration.spec.ts b/apps/server/src/shared/repo/user/user-do.repo.integration.spec.ts index 4828117220..fc3beb920b 100644 --- a/apps/server/src/shared/repo/user/user-do.repo.integration.spec.ts +++ b/apps/server/src/shared/repo/user/user-do.repo.integration.spec.ts @@ -9,6 +9,7 @@ import { Test, TestingModule } from '@nestjs/testing'; import { EntityNotFoundError } from '@shared/common'; import { RoleReference } from '@shared/domain/domainobject'; import { Page } from '@shared/domain/domainobject/page'; +import { UserSourceOptions } from '@shared/domain/domainobject/user-source-options.do'; import { UserDO } from '@shared/domain/domainobject/user.do'; import { Role, SchoolEntity, User } from '@shared/domain/entity'; import { IFindOptions, LanguageType, RoleName, SortOrder } from '@shared/domain/interface'; @@ -845,4 +846,32 @@ describe('UserRepo', () => { }); }); }); + + describe('findByTspUids', () => { + describe('when users are found', () => { + const setup = async () => { + const tspUid = new ObjectId().toHexString(); + + const user: User = userFactory.buildWithId({ + sourceOptions: new UserSourceOptions({ + tspUid, + }), + }); + + await em.persistAndFlush([user]); + em.clear(); + + return { tspUid, user }; + }; + + it('should return mapped users', async () => { + const { tspUid, user } = await setup(); + + const result = await repo.findByTspUids([tspUid]); + + expect(result.length).toBe(1); + expect(result[0].id).toBe(user.id); + }); + }); + }); }); diff --git a/apps/server/src/shared/repo/user/user-do.repo.ts b/apps/server/src/shared/repo/user/user-do.repo.ts index ac71d3f3bf..c06c9c5b96 100644 --- a/apps/server/src/shared/repo/user/user-do.repo.ts +++ b/apps/server/src/shared/repo/user/user-do.repo.ts @@ -19,7 +19,7 @@ export class UserDORepo extends BaseDORepo { return User; } - async find(query: UserQuery, options?: IFindOptions) { + public async find(query: UserQuery, options?: IFindOptions) { const pagination: Pagination = options?.pagination || {}; const order: QueryOrderMap = this.createQueryOrderMap(options?.order || {}); const scope: Scope = new UserScope() @@ -49,7 +49,7 @@ export class UserDORepo extends BaseDORepo { return page; } - async findById(id: EntityId, populate = false): Promise { + public async findById(id: EntityId, populate = false): Promise { const userEntity: User = await this._em.findOneOrFail(this.entityName, id as FilterQuery); if (populate) { @@ -60,7 +60,7 @@ export class UserDORepo extends BaseDORepo { return this.mapEntityToDO(userEntity); } - async findByIds(ids: string[], populate = false): Promise { + public async findByIds(ids: string[], populate = false): Promise { const users = await this._em.find(User, { id: { $in: ids } }); if (populate) { @@ -83,7 +83,7 @@ export class UserDORepo extends BaseDORepo { return userDOs; } - async findByIdOrNull(id: EntityId, populate = false): Promise { + public async findByIdOrNull(id: EntityId, populate = false): Promise { const user: User | null = await this._em.findOne(this.entityName, id as FilterQuery); if (!user) { @@ -100,7 +100,7 @@ export class UserDORepo extends BaseDORepo { return domainObject; } - async findByExternalIdOrFail(externalId: string, systemId: string): Promise { + public async findByExternalIdOrFail(externalId: string, systemId: string): Promise { const userDo: UserDO | null = await this.findByExternalId(externalId, systemId); if (userDo) { return userDo; @@ -108,7 +108,7 @@ export class UserDORepo extends BaseDORepo { throw new EntityNotFoundError('User'); } - async findByExternalId(externalId: string, systemId: string): Promise { + public async findByExternalId(externalId: string, systemId: string): Promise { const userEntitys: User[] = await this._em.find(User, { externalId }, { populate: ['school.systems'] }); if (userEntitys.length > 1) { @@ -123,7 +123,7 @@ export class UserDORepo extends BaseDORepo { return userDo; } - async findByEmail(email: string): Promise { + public async findByEmail(email: string): Promise { // find mail case-insensitive by regex const userEntitys: User[] = await this._em.find(User, { email: new RegExp(`^${email.replace(/\W/g, '\\$&')}$`, 'i'), @@ -136,7 +136,7 @@ export class UserDORepo extends BaseDORepo { return userDos; } - mapEntityToDO(entity: User): UserDO { + public mapEntityToDO(entity: User): UserDO { const user: UserDO = new UserDO({ id: entity.id, createdAt: entity.createdAt, @@ -184,7 +184,7 @@ export class UserDORepo extends BaseDORepo { return user; } - mapDOToEntityProperties(entityDO: UserDO): EntityData { + public mapDOToEntityProperties(entityDO: UserDO): EntityData { return { email: entityDO.email, firstName: entityDO.firstName,