Skip to content

Commit

Permalink
rewrite test and fix coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
MajedAlaitwniCap committed Dec 30, 2024
1 parent 430da92 commit 2b3a25a
Showing 1 changed file with 57 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Account> => {
// 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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 2b3a25a

Please sign in to comment.