Skip to content

Commit

Permalink
Add tests for user service and repo.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkreuzkam-cap committed Dec 18, 2024
1 parent 216dfb0 commit c734169
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
20 changes: 20 additions & 0 deletions apps/server/src/modules/user/service/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});
29 changes: 29 additions & 0 deletions apps/server/src/shared/repo/user/user-do.repo.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
});
});
});
18 changes: 9 additions & 9 deletions apps/server/src/shared/repo/user/user-do.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class UserDORepo extends BaseDORepo<UserDO, User> {
return User;
}

async find(query: UserQuery, options?: IFindOptions<UserDO>) {
public async find(query: UserQuery, options?: IFindOptions<UserDO>) {
const pagination: Pagination = options?.pagination || {};
const order: QueryOrderMap<User> = this.createQueryOrderMap(options?.order || {});
const scope: Scope<User> = new UserScope()
Expand Down Expand Up @@ -49,7 +49,7 @@ export class UserDORepo extends BaseDORepo<UserDO, User> {
return page;
}

async findById(id: EntityId, populate = false): Promise<UserDO> {
public async findById(id: EntityId, populate = false): Promise<UserDO> {
const userEntity: User = await this._em.findOneOrFail(this.entityName, id as FilterQuery<User>);

if (populate) {
Expand All @@ -60,7 +60,7 @@ export class UserDORepo extends BaseDORepo<UserDO, User> {
return this.mapEntityToDO(userEntity);
}

async findByIds(ids: string[], populate = false): Promise<UserDO[]> {
public async findByIds(ids: string[], populate = false): Promise<UserDO[]> {
const users = await this._em.find(User, { id: { $in: ids } });

if (populate) {
Expand All @@ -83,7 +83,7 @@ export class UserDORepo extends BaseDORepo<UserDO, User> {
return userDOs;
}

async findByIdOrNull(id: EntityId, populate = false): Promise<UserDO | null> {
public async findByIdOrNull(id: EntityId, populate = false): Promise<UserDO | null> {
const user: User | null = await this._em.findOne(this.entityName, id as FilterQuery<User>);

if (!user) {
Expand All @@ -100,15 +100,15 @@ export class UserDORepo extends BaseDORepo<UserDO, User> {
return domainObject;
}

async findByExternalIdOrFail(externalId: string, systemId: string): Promise<UserDO> {
public async findByExternalIdOrFail(externalId: string, systemId: string): Promise<UserDO> {
const userDo: UserDO | null = await this.findByExternalId(externalId, systemId);
if (userDo) {
return userDo;
}
throw new EntityNotFoundError('User');
}

async findByExternalId(externalId: string, systemId: string): Promise<UserDO | null> {
public async findByExternalId(externalId: string, systemId: string): Promise<UserDO | null> {
const userEntitys: User[] = await this._em.find(User, { externalId }, { populate: ['school.systems'] });

if (userEntitys.length > 1) {
Expand All @@ -123,7 +123,7 @@ export class UserDORepo extends BaseDORepo<UserDO, User> {
return userDo;
}

async findByEmail(email: string): Promise<UserDO[]> {
public async findByEmail(email: string): Promise<UserDO[]> {
// find mail case-insensitive by regex
const userEntitys: User[] = await this._em.find(User, {
email: new RegExp(`^${email.replace(/\W/g, '\\$&')}$`, 'i'),
Expand All @@ -136,7 +136,7 @@ export class UserDORepo extends BaseDORepo<UserDO, User> {
return userDos;
}

mapEntityToDO(entity: User): UserDO {
public mapEntityToDO(entity: User): UserDO {
const user: UserDO = new UserDO({
id: entity.id,
createdAt: entity.createdAt,
Expand Down Expand Up @@ -184,7 +184,7 @@ export class UserDORepo extends BaseDORepo<UserDO, User> {
return user;
}

mapDOToEntityProperties(entityDO: UserDO): EntityData<User> {
public mapDOToEntityProperties(entityDO: UserDO): EntityData<User> {
return {
email: entityDO.email,
firstName: entityDO.firstName,
Expand Down

0 comments on commit c734169

Please sign in to comment.