jwt-auth.test.js
1.36 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
43
/**
* Unit Tests — middleware/jwt-auth.js
*/
import { describe, it, expect } from 'vitest';
const jwtAuth = require('../../middleware/jwt-auth');
describe('jwt-auth', () => {
describe('exports', () => {
it('should export authentication middleware functions', () => {
expect(typeof jwtAuth.requireAuth).toBe('function');
expect(typeof jwtAuth.requireAdmin).toBe('function');
expect(typeof jwtAuth.optionalAuth).toBe('function');
expect(typeof jwtAuth.extractUser).toBe('function');
});
it('should export cookie helpers', () => {
expect(typeof jwtAuth.signAndSetCookie).toBe('function');
expect(typeof jwtAuth.clearCookie).toBe('function');
});
it('should export token extraction', () => {
expect(typeof jwtAuth.extractUserFromToken).toBe('function');
});
it('should export constants', () => {
expect(typeof jwtAuth.JWT_SECRET).toBe('string');
expect(typeof jwtAuth.COOKIE_NAME).toBe('string');
});
});
describe('extractUserFromToken', () => {
it('should return null for invalid token', () => {
const user = jwtAuth.extractUserFromToken('invalid-token');
expect(user).toBeNull();
});
it('should return null for empty token', () => {
expect(jwtAuth.extractUserFromToken('')).toBeNull();
expect(jwtAuth.extractUserFromToken(null)).toBeNull();
});
});
});