|
| 1 | +import { describe, it, jest, beforeEach, expect, afterEach } from '@jest/globals'; |
| 2 | +import { Request, Response, NextFunction } from 'express'; |
| 3 | +import { basicAuthMiddleware } from '../../src/middleware/basicAuth.middleware'; |
| 4 | + |
| 5 | +jest.mock('../../src/utils/config.utils', () => ({ |
| 6 | + readConfiguration: jest.fn().mockReturnValue({ |
| 7 | + commerceTools: { |
| 8 | + clientId: 'test-client-id', |
| 9 | + clientSecret: 'test-client-secret', |
| 10 | + }, |
| 11 | + }), |
| 12 | +})); |
| 13 | + |
| 14 | +describe('basicAuthMiddleware', () => { |
| 15 | + let req: Partial<Request>; |
| 16 | + let res: Partial<Response>; |
| 17 | + let next: NextFunction; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + req = { |
| 21 | + headers: {}, |
| 22 | + }; |
| 23 | + res = { |
| 24 | + // @ts-expect-error: ignore type error |
| 25 | + status: jest.fn().mockReturnThis(), |
| 26 | + // @ts-expect-error: ignore type error |
| 27 | + json: jest.fn(), |
| 28 | + }; |
| 29 | + next = jest.fn(); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + jest.clearAllMocks(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should call next() when valid Basic Auth credentials are provided', () => { |
| 37 | + const validCredentials = Buffer.from('test-client-id:test-client-secret').toString('base64'); |
| 38 | + req.headers = { |
| 39 | + authorization: `Basic ${validCredentials}`, |
| 40 | + }; |
| 41 | + |
| 42 | + basicAuthMiddleware(req as Request, res as Response, next); |
| 43 | + |
| 44 | + expect(next).toHaveBeenCalledTimes(1); |
| 45 | + expect(res.status).not.toHaveBeenCalled(); |
| 46 | + expect(res.json).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should return 401 when Authorization header is missing', () => { |
| 50 | + req.headers = {}; |
| 51 | + |
| 52 | + basicAuthMiddleware(req as Request, res as Response, next); |
| 53 | + |
| 54 | + expect(res.status).toHaveBeenCalledWith(401); |
| 55 | + expect(res.json).toHaveBeenCalledWith({ error: 'Missing or invalid Authorization header' }); |
| 56 | + expect(next).not.toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should return 401 when Authorization header does not start with "Basic "', () => { |
| 60 | + req.headers = { |
| 61 | + authorization: 'Bearer token123', |
| 62 | + }; |
| 63 | + |
| 64 | + basicAuthMiddleware(req as Request, res as Response, next); |
| 65 | + |
| 66 | + expect(res.status).toHaveBeenCalledWith(401); |
| 67 | + expect(res.json).toHaveBeenCalledWith({ error: 'Missing or invalid Authorization header' }); |
| 68 | + expect(next).not.toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should return 403 when credentials do not match', () => { |
| 72 | + const invalidCredentials = Buffer.from('wrong-id:wrong-secret').toString('base64'); |
| 73 | + req.headers = { |
| 74 | + authorization: `Basic ${invalidCredentials}`, |
| 75 | + }; |
| 76 | + |
| 77 | + basicAuthMiddleware(req as Request, res as Response, next); |
| 78 | + |
| 79 | + expect(res.status).toHaveBeenCalledWith(403); |
| 80 | + expect(res.json).toHaveBeenCalledWith({ error: 'Invalid credentials' }); |
| 81 | + expect(next).not.toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should return 403 when client ID is correct but secret is wrong', () => { |
| 85 | + const invalidCredentials = Buffer.from('test-client-id:wrong-secret').toString('base64'); |
| 86 | + req.headers = { |
| 87 | + authorization: `Basic ${invalidCredentials}`, |
| 88 | + }; |
| 89 | + |
| 90 | + basicAuthMiddleware(req as Request, res as Response, next); |
| 91 | + |
| 92 | + expect(res.status).toHaveBeenCalledWith(403); |
| 93 | + expect(res.json).toHaveBeenCalledWith({ error: 'Invalid credentials' }); |
| 94 | + expect(next).not.toHaveBeenCalled(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should return 403 when client secret is correct but ID is wrong', () => { |
| 98 | + const invalidCredentials = Buffer.from('wrong-id:test-client-secret').toString('base64'); |
| 99 | + req.headers = { |
| 100 | + authorization: `Basic ${invalidCredentials}`, |
| 101 | + }; |
| 102 | + |
| 103 | + basicAuthMiddleware(req as Request, res as Response, next); |
| 104 | + |
| 105 | + expect(res.status).toHaveBeenCalledWith(403); |
| 106 | + expect(res.json).toHaveBeenCalledWith({ error: 'Invalid credentials' }); |
| 107 | + expect(next).not.toHaveBeenCalled(); |
| 108 | + }); |
| 109 | +}); |
0 commit comments