-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGetRequestUser.spec.ts
More file actions
42 lines (34 loc) · 1.07 KB
/
GetRequestUser.spec.ts
File metadata and controls
42 lines (34 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import type { UserDocument } from '@nbw/database';
import { ExecutionContext, HttpException, HttpStatus } from '@nestjs/common';
import { GetRequestToken, validateUser } from './GetRequestUser';
describe('GetRequestToken', () => {
it('should be a defined decorator', () => {
const mockExecutionContext = {
switchToHttp: jest.fn().mockReturnThis(),
} as unknown as ExecutionContext;
const result = GetRequestToken(null, mockExecutionContext);
expect(typeof result).toBe('function');
});
});
describe('validateUser', () => {
it('should return the user if the user exists', () => {
const mockUser = {
_id: 'test-id',
username: 'testuser',
} as unknown as UserDocument;
const result = validateUser(mockUser);
expect(result).toEqual(mockUser);
});
it('should throw an error if the user does not exist', () => {
expect(() => validateUser(null)).toThrow(
new HttpException(
{
error: {
user: 'User not found',
},
},
HttpStatus.UNAUTHORIZED,
),
);
});
});