-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a7b7c9
commit 2c50c3b
Showing
3 changed files
with
21 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,24 +130,26 @@ describe('AuthService', () => { | |
it('should throw an UnauthorizedException for invalid credentials', async () => { | ||
const email = '[email protected]'; | ||
const password = 'wrongPassword'; | ||
const role = UserRoles.User; | ||
const user = new User(); | ||
user.email = email; | ||
user.password = 'hashedPassword'; | ||
|
||
jest.spyOn(userRepository, 'findOneBy').mockResolvedValueOnce(user); | ||
jest.spyOn(bcrypt, 'compare').mockResolvedValueOnce(false); | ||
|
||
await expect(service.signIn({ email, password })).rejects.toThrow( | ||
await expect(service.signIn({ email, password, role })).rejects.toThrow( | ||
UnauthorizedException, | ||
); | ||
|
||
expect(userRepository.findOneBy).toHaveBeenCalledWith({ email }); | ||
expect(userRepository.findOneBy).toHaveBeenCalledWith({ email, role }); | ||
expect(bcrypt.compare).toHaveBeenCalledWith(password, user.password); | ||
}); | ||
|
||
it('should return a signed token on successful login', async () => { | ||
const email = '[email protected]'; | ||
const password = 'validPassword'; | ||
const role = UserRoles.User; | ||
const user = new User(); | ||
user.id = '18ea976e-367b-4138-b68e-7aff3f7ae4de'; | ||
user.email = email; | ||
|
@@ -159,14 +161,14 @@ describe('AuthService', () => { | |
jest.spyOn(userRepository, 'findOneBy').mockResolvedValueOnce(user); | ||
jest.spyOn(bcrypt, 'compare').mockResolvedValueOnce(true); | ||
|
||
const response = await service.signIn({ email, password }); | ||
const response = await service.signIn({ email, password, role }); | ||
|
||
expect(response).toEqual({ | ||
accessToken: 'access-token', | ||
refreshToken: 'access-token', | ||
}); | ||
|
||
expect(userRepository.findOneBy).toHaveBeenCalledWith({ email }); | ||
expect(userRepository.findOneBy).toHaveBeenCalledWith({ email, role }); | ||
expect(bcrypt.compare).toHaveBeenCalledWith(password, user.password); | ||
expect(jwtService.signAsync).toHaveBeenCalledTimes(2); | ||
expect(jwtService.signAsync).toHaveBeenCalledWith(payload); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters