From 9f3e9f1b8c48f681692137bb83b218c561a5f346 Mon Sep 17 00:00:00 2001 From: Maximilian Kreuzkam Date: Thu, 19 Dec 2024 10:24:11 +0100 Subject: [PATCH] Add tests for accounts. --- .../services/account-db.service.spec.ts | 51 +++++++++++++++++++ .../domain/services/account.service.spec.ts | 24 +++++++++ .../account.repo.integration.spec.ts | 20 ++++++++ .../account/repo/micro-orm/account.repo.ts | 6 +-- 4 files changed, 98 insertions(+), 3 deletions(-) diff --git a/apps/server/src/modules/account/domain/services/account-db.service.spec.ts b/apps/server/src/modules/account/domain/services/account-db.service.spec.ts index 9ab458a4411..9334a0ca937 100644 --- a/apps/server/src/modules/account/domain/services/account-db.service.spec.ts +++ b/apps/server/src/modules/account/domain/services/account-db.service.spec.ts @@ -691,6 +691,57 @@ describe('AccountDbService', () => { }); }); + describe('saveAll', () => { + describe('when given account that does not exist', () => { + const setup = () => { + const account = accountDoFactory.build({ + id: undefined, + }); + const savedAccount = accountDoFactory.build({ + ...account, + id: new ObjectId().toHexString(), + }); + + accountRepo.saveAll.mockResolvedValueOnce([savedAccount]); + + return { account, savedAccount }; + }; + + it('should save it', async () => { + const { account, savedAccount } = setup(); + + const result = await accountService.saveAll([account]); + + expect(result.length).toBe(1); + expect(result[0]).toStrictEqual(savedAccount); + expect(accountRepo.saveAll).toHaveBeenCalledTimes(1); + }); + }); + + describe('when given account that exist', () => { + const setup = () => { + const account = accountDoFactory.build(); + const foundAccount = accountDoFactory.build(); + const updateSpy = jest.spyOn(foundAccount, 'update'); + + accountRepo.findById.mockResolvedValueOnce(foundAccount); + accountRepo.saveAll.mockResolvedValueOnce([foundAccount]); + + return { account, foundAccount, updateSpy }; + }; + + it('should update it', async () => { + const { account, foundAccount, updateSpy } = setup(); + + const result = await accountService.saveAll([account]); + + expect(updateSpy).toHaveBeenCalledTimes(1); + expect(result.length).toBe(1); + expect(result[0].id).toBe(foundAccount.id); + }); + }); + }); + describe('updateUsername', () => { describe('when updating username', () => { const setup = () => { diff --git a/apps/server/src/modules/account/domain/services/account.service.spec.ts b/apps/server/src/modules/account/domain/services/account.service.spec.ts index 595dd31ee5c..63d56f28f00 100644 --- a/apps/server/src/modules/account/domain/services/account.service.spec.ts +++ b/apps/server/src/modules/account/domain/services/account.service.spec.ts @@ -380,6 +380,30 @@ describe('AccountService', () => { }); }); + describe('saveAll', () => { + describe('when saving accounts', () => { + const setup = () => { + const accounts = accountDoFactory.buildList(1); + + configService.get.mockReturnValue(true); + accountServiceDb.saveAll.mockResolvedValueOnce(accounts); + accountServiceIdm.saveAll.mockResolvedValueOnce(accounts); + + return { accounts, sut: newAccountService() }; + }; + + it('should delegate to db and idm service', async () => { + const { accounts, sut } = setup(); + + const result = await sut.saveAll(accounts); + + expect(result).toBeDefined(); + expect(accountServiceDb.saveAll).toHaveBeenCalledTimes(1); + expect(accountServiceIdm.saveAll).toHaveBeenCalledTimes(1); + }); + }); + }); + describe('saveWithValidation', () => { describe('When calling with an empty username', () => { it('should throw an ValidationError', async () => { diff --git a/apps/server/src/modules/account/repo/micro-orm/account.repo.integration.spec.ts b/apps/server/src/modules/account/repo/micro-orm/account.repo.integration.spec.ts index 76aff5bad1a..a20ee96bf16 100644 --- a/apps/server/src/modules/account/repo/micro-orm/account.repo.integration.spec.ts +++ b/apps/server/src/modules/account/repo/micro-orm/account.repo.integration.spec.ts @@ -68,6 +68,26 @@ describe('account repo', () => { }); }); + describe('saveAll', () => { + describe('When multiple accounts are given', () => { + const setup = () => { + const accounts = accountDoFactory.buildList(3); + const accountIds = accounts.map((account) => account.id); + + return { accounts, accountIds }; + }; + + it('should save all accounts', async () => { + const { accounts, accountIds } = setup(); + + await repo.saveAll(accounts); + + const foundAccounts = await em.find(AccountEntity, { id: { $in: accountIds } }); + expect(foundAccounts.length).toBe(accounts.length); + }); + }); + }); + describe('findById', () => { describe('When the account exists', () => { const setup = async () => { diff --git a/apps/server/src/modules/account/repo/micro-orm/account.repo.ts b/apps/server/src/modules/account/repo/micro-orm/account.repo.ts index d7179c5fe6b..54641b91530 100644 --- a/apps/server/src/modules/account/repo/micro-orm/account.repo.ts +++ b/apps/server/src/modules/account/repo/micro-orm/account.repo.ts @@ -13,7 +13,7 @@ import { AccountScope } from './scope/account-scope'; export class AccountRepo { constructor(private readonly em: EntityManager) {} - get entityName() { + get entityName(): typeof AccountEntity { return AccountEntity; } @@ -93,7 +93,7 @@ export class AccountRepo { return AccountEntityToDoMapper.mapToDo(entity); } - getObjectReference>( + public getObjectReference>( entityName: EntityName, id: Primary | Primary[] ): Entity { @@ -186,7 +186,7 @@ export class AccountRepo { return AccountEntityToDoMapper.mapEntitiesToDos(result); } - async findByUserIdsAndSystemId(userIds: string[], systemId: string): Promise { + public async findByUserIdsAndSystemId(userIds: string[], systemId: string): Promise { const scope = new AccountScope(); const userIdScope = new AccountScope();