Skip to content

Commit

Permalink
Fix/validate access token user not found (#8484)
Browse files Browse the repository at this point in the history
# Description
Closes #7244 

See details about implementation:
#7244 (comment)
and
#7244 (comment)

# Changes
- return a `USER_NOT_FOUND` error instead of `INVALID_INPUT` error
- tweak unit tests to correctly test `AuthExceptionCode`, as it wasn't
properly tested; it was actually a _false positive_. This is because
[`toThrow`](https://jestjs.io/docs/expect#tothrowerror) from jest only
checks the `message`, and not any other method / attributes from the
`Error`. It's a know behaviour and not considered a bug, see
jestjs/jest#13232 (comment)
  • Loading branch information
nicolasrouanne authored Nov 13, 2024
1 parent 898006f commit cde96cf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,13 @@ describe('JwtAuthStrategy', () => {
);

await expect(strategy.validate(payload as JwtPayload)).rejects.toThrow(
new AuthException('User not found', AuthExceptionCode.INVALID_INPUT),
new AuthException('User not found', expect.any(String)),
);
try {
await strategy.validate(payload as JwtPayload);
} catch (e) {
expect(e.code).toBe(AuthExceptionCode.USER_NOT_FOUND);
}
});

it('should be truthy if type is ACCESS, no jti, and user exist', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class JwtAuthStrategy extends PassportStrategy(Strategy, 'jwt') {
if (!user) {
throw new AuthException(
'User not found',
AuthExceptionCode.INVALID_INPUT,
AuthExceptionCode.USER_NOT_FOUND,
);
}

Expand Down

0 comments on commit cde96cf

Please sign in to comment.