Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions api/src/__tests__/auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { Request, Response, NextFunction } from 'express';

Check warning on line 1 in api/src/__tests__/auth.test.ts

View workflow job for this annotation

GitHub Actions / API — Lint, Test, Build (20)

'Request' is defined but never used
import jwt from 'jsonwebtoken';
import { authenticateToken, AuthRequest } from '../middleware/auth';
import { UnauthorizedError } from '../utils/errors';

// Mock the config module
jest.mock('../config', () => ({
config: {
auth: {
jwtSecret: 'test-secret-key-for-testing',
jwtExpiresIn: '1h'
}
}
}));

describe('Auth Middleware', () => {
let mockRequest: Partial<AuthRequest>;
let mockResponse: Partial<Response>;
let mockNext: NextFunction;

beforeEach(() => {
mockRequest = {
headers: {},
};
mockResponse = {};
mockNext = jest.fn();
});

describe('authenticateToken', () => {
it('should pass through with valid token', () => {
const validToken = jwt.sign({ address: '0x1234567890123456789012345678901234567890' }, 'test-secret-key-for-testing');
mockRequest.headers = {
authorization: `Bearer ${validToken}`,
};

authenticateToken(mockRequest as AuthRequest, mockResponse as Response, mockNext);

expect(mockNext).toHaveBeenCalledWith();
expect(mockRequest.user).toEqual({ address: '0x1234567890123456789012345678901234567890' });
});

it('should return 401 when token is missing', () => {
mockRequest.headers = {};

expect(() => {
authenticateToken(mockRequest as AuthRequest, mockResponse as Response, mockNext);
}).toThrow(UnauthorizedError);

expect(mockNext).not.toHaveBeenCalled();
});

it('should return 401 when token is expired', () => {
const expiredToken = jwt.sign(
{ address: '0x1234567890123456789012345678901234567890' },
'test-secret-key-for-testing',
{ expiresIn: '-1s' }
);
mockRequest.headers = {
authorization: `Bearer ${expiredToken}`,
};

expect(() => {
authenticateToken(mockRequest as AuthRequest, mockResponse as Response, mockNext);
}).toThrow(UnauthorizedError);

expect(mockNext).not.toHaveBeenCalled();
});

it('should return 401 when token is invalid', () => {
const invalidToken = 'invalid-token-string';
mockRequest.headers = {
authorization: `Bearer ${invalidToken}`,
};

expect(() => {
authenticateToken(mockRequest as AuthRequest, mockResponse as Response, mockNext);
}).toThrow(UnauthorizedError);

expect(mockNext).not.toHaveBeenCalled();
});

it('should return 401 when authorization header is malformed', () => {
mockRequest.headers = {
authorization: 'InvalidFormat token',
};

expect(() => {
authenticateToken(mockRequest as AuthRequest, mockResponse as Response, mockNext);
}).toThrow(UnauthorizedError);

expect(mockNext).not.toHaveBeenCalled();
});

it('should return 401 when authorization header has no token', () => {
mockRequest.headers = {
authorization: 'Bearer',
};

expect(() => {
authenticateToken(mockRequest as AuthRequest, mockResponse as Response, mockNext);
}).toThrow(UnauthorizedError);

expect(mockNext).not.toHaveBeenCalled();
});

it('should return 401 when authorization header has only Bearer without space', () => {
mockRequest.headers = {
authorization: 'Bearertoken',
};

expect(() => {
authenticateToken(mockRequest as AuthRequest, mockResponse as Response, mockNext);
}).toThrow(UnauthorizedError);

expect(mockNext).not.toHaveBeenCalled();
});
});
});
Loading