mirror of
https://github.com/directus/directus.git
synced 2026-01-25 07:48:09 -05:00
* Add "authenticate" filter hook that allows custom auth check * Start on test * Update Jest, restructure API tests, start implementing authenticate test * Move access token verify to util function * Ensure jest can show inline warnings on correct lines * Update is-directus-jwt to use jsonwebtoken decode + add tests * Remove unused package * Tweak and finish + test authenticate * Tweak test * Add authenticate filter to docs * Don't scan tests for codeql * No seriously, ignore tests
68 lines
1.7 KiB
TypeScript
68 lines
1.7 KiB
TypeScript
import { NextFunction, Request, Response } from 'express';
|
|
import extractToken from '../../src/middleware/extract-token';
|
|
import '../../src/types/express.d.ts';
|
|
|
|
let mockRequest: Partial<Request & { token?: string }>;
|
|
let mockResponse: Partial<Response>;
|
|
const nextFunction: NextFunction = jest.fn();
|
|
|
|
beforeEach(() => {
|
|
mockRequest = {};
|
|
mockResponse = {};
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
test('Token from query', () => {
|
|
mockRequest = {
|
|
query: {
|
|
access_token: 'test',
|
|
},
|
|
};
|
|
|
|
extractToken(mockRequest as Request, mockResponse as Response, nextFunction);
|
|
expect(mockRequest.token).toBe('test');
|
|
expect(nextFunction).toBeCalledTimes(1);
|
|
});
|
|
|
|
test('Token from Authorization header (capitalized)', () => {
|
|
mockRequest = {
|
|
headers: {
|
|
authorization: 'Bearer test',
|
|
},
|
|
};
|
|
|
|
extractToken(mockRequest as Request, mockResponse as Response, nextFunction);
|
|
expect(mockRequest.token).toBe('test');
|
|
expect(nextFunction).toBeCalledTimes(1);
|
|
});
|
|
|
|
test('Token from Authorization header (lowercase)', () => {
|
|
mockRequest = {
|
|
headers: {
|
|
authorization: 'bearer test',
|
|
},
|
|
};
|
|
|
|
extractToken(mockRequest as Request, mockResponse as Response, nextFunction);
|
|
expect(mockRequest.token).toBe('test');
|
|
expect(nextFunction).toBeCalledTimes(1);
|
|
});
|
|
|
|
test('Ignore the token if authorization header is too many parts', () => {
|
|
mockRequest = {
|
|
headers: {
|
|
authorization: 'bearer test what another one',
|
|
},
|
|
};
|
|
|
|
extractToken(mockRequest as Request, mockResponse as Response, nextFunction);
|
|
expect(mockRequest.token).toBeNull();
|
|
expect(nextFunction).toBeCalledTimes(1);
|
|
});
|
|
|
|
test('Null if no token passed', () => {
|
|
extractToken(mockRequest as Request, mockResponse as Response, nextFunction);
|
|
expect(mockRequest.token).toBeNull();
|
|
expect(nextFunction).toBeCalledTimes(1);
|
|
});
|