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 5700db0758..278002c24a 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 @@ -18,7 +18,6 @@ import { Account } from '../account'; import { AccountEntity } from '../entity/account.entity'; import { AccountServiceDb } from './account-db.service'; -// TODO: fix tests describe('AccountDbService', () => { let module: TestingModule; let accountService: AccountServiceDb; @@ -312,28 +311,16 @@ describe('AccountDbService', () => { }); }); - // describe('when user does not exist', () => { - // const setup = () => { - // const mockTeacherUser = userFactory.buildWithId(); - // const mockTeacherAccount = accountDoFactory.build({ - // userId: mockTeacherUser.id, - // password: defaultPassword, - // }); - - // accountRepo.findByUserIdOrFail.mockImplementation((userId: EntityId | ObjectId): Promise => { - // if (mockTeacherUser.id === userId) { - // return Promise.resolve(mockTeacherAccount); - // } - // throw new EntityNotFoundError(AccountEntity.name); - // }); - // return {}; - // }; - - // it('should throw EntityNotFoundError', async () => { - // setup(); - // await expect(accountService.findByUserIdOrFail('nonExistentId')).rejects.toThrow(EntityNotFoundError); - // }); - // }); + describe('when user does not exist', () => { + const setup = () => { + accountRepo.findByUserIdOrFail.mockRejectedValue(new EntityNotFoundError(AccountEntity.name)); + }; + + it('should throw EntityNotFoundError', async () => { + setup(); + await expect(accountService.findByUserIdOrFail('nonExistentId')).rejects.toBeInstanceOf(EntityNotFoundError); + }); + }); }); describe('save', () => { @@ -715,42 +702,44 @@ describe('AccountDbService', () => { }); }); - // describe('updateLastLogin', () => { - // const setup = () => { - // const mockTeacherAccount = accountDoFactory.build(); - // const theNewDate = new Date(); + describe('updateLastLogin', () => { + const setup = () => { + const mockTeacherAccount = accountDoFactory.build(); + const theNewDate = new Date(); - // accountRepo.findById.mockResolvedValue(mockTeacherAccount); + accountRepo.findById.mockResolvedValue(mockTeacherAccount); + accountRepo.save.mockResolvedValue(mockTeacherAccount); - // return { mockTeacherAccount, theNewDate }; - // }; + return { mockTeacherAccount, theNewDate }; + }; - // it('should update last tried failed login', async () => { - // const { mockTeacherAccount, theNewDate } = setup(); + it('should update last tried failed login', async () => { + const { mockTeacherAccount, theNewDate } = setup(); - // const ret = await accountService.updateLastLogin(mockTeacherAccount.id, theNewDate); + const ret = await accountService.updateLastLogin(mockTeacherAccount.id, theNewDate); - // expect(ret.lastLogin).toEqual(theNewDate); - // }); - // }); + expect(ret.lastLogin).toEqual(theNewDate); + }); + }); - // describe('updateLastTriedFailedLogin', () => { - // const setup = () => { - // const mockTeacherAccount = accountDoFactory.build(); - // const theNewDate = new Date(); + describe('updateLastTriedFailedLogin', () => { + const setup = () => { + const mockTeacherAccount = accountDoFactory.build(); + const theNewDate = new Date(); - // accountRepo.findById.mockResolvedValue(mockTeacherAccount); + accountRepo.findById.mockResolvedValue(mockTeacherAccount); + accountRepo.save.mockResolvedValue(mockTeacherAccount); - // return { mockTeacherAccount, theNewDate }; - // }; + return { mockTeacherAccount, theNewDate }; + }; - // it('should update last tried failed login', async () => { - // const { mockTeacherAccount, theNewDate } = setup(); - // const ret = await accountService.updateLastTriedFailedLogin(mockTeacherAccount.id, theNewDate); + it('should update last tried failed login', async () => { + const { mockTeacherAccount, theNewDate } = setup(); + const ret = await accountService.updateLastTriedFailedLogin(mockTeacherAccount.id, theNewDate); - // expect(ret.lasttriedFailedLogin).toEqual(theNewDate); - // }); - // }); + expect(ret.lasttriedFailedLogin).toEqual(theNewDate); + }); + }); describe('validatePassword', () => { describe('when accepted Password', () => { @@ -802,31 +791,29 @@ describe('AccountDbService', () => { }); }); - // describe('updatePassword', () => { - // describe('when update Password', () => { - // const setup = () => { - // const mockTeacherAccount = accountDoFactory.build(); - // const newPassword = 'newPassword'; - - // accountRepo.findById.mockResolvedValue(mockTeacherAccount); + describe('updatePassword', () => { + describe('when update Password', () => { + const setup = () => { + const mockTeacherAccount = accountDoFactory.build(); + const newPassword = 'newPassword'; - // return { mockTeacherAccount, newPassword }; - // }; + accountRepo.findById.mockResolvedValue(mockTeacherAccount); + accountRepo.save.mockResolvedValue(mockTeacherAccount); + return { mockTeacherAccount, newPassword }; + }; - // it('should update password', async () => { - // const { mockTeacherAccount, newPassword } = setup(); + it('should update password', async () => { + const { mockTeacherAccount, newPassword } = setup(); - // const ret = await accountService.updatePassword(mockTeacherAccount.id, newPassword); + const ret = await accountService.updatePassword(mockTeacherAccount.id, newPassword); - // expect(ret).toBeDefined(); - // if (ret.password) { - // await expect(bcrypt.compare(newPassword, ret.password)).resolves.toBe(true); - // } else { - // fail('return password is undefined'); - // } - // }); - // }); - // }); + expect(ret).toBeDefined(); + if (ret.password) { + await expect(bcrypt.compare(newPassword, ret.password)).resolves.toBe(true); + } + }); + }); + }); describe('delete', () => { describe('when delete an existing account', () => {