Skip to content

Commit 2e469d3

Browse files
committed
MOL-815: update unit tests
1 parent 025d871 commit 2e469d3

File tree

3 files changed

+123
-17
lines changed

3 files changed

+123
-17
lines changed

processor/tests/commercetools/extension.commercetools.spec.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ import {
88
} from '../../src/commercetools/extensions.commercetools';
99
import { Extension } from '@commercetools/platform-sdk';
1010

11+
jest.mock('../../src/utils/config.utils', () => ({
12+
readConfiguration: jest.fn().mockReturnValue({
13+
commerceTools: {
14+
clientId: 'test-client-id',
15+
clientSecret: 'test-client-secret',
16+
},
17+
}),
18+
}));
19+
1120
jest.mock('../../src/client/create.client', () => ({
1221
createApiRoot: jest.fn(),
1322
}));
@@ -29,7 +38,7 @@ describe('Test extension.commercetools', () => {
2938
url: mockUrl,
3039
authentication: {
3140
type: 'AuthorizationHeader',
32-
headerValue: 'Bearer _token_',
41+
headerValue: 'Basic dGVzdC1jbGllbnQtaWQ6dGVzdC1jbGllbnQtc2VjcmV0',
3342
},
3443
},
3544
triggers: [
@@ -170,9 +179,7 @@ describe('Test extension.commercetools', () => {
170179
}),
171180
);
172181

173-
const accessToken = 'token123';
174-
175-
await createPaymentExtension(mockUrl, accessToken);
182+
await createPaymentExtension(mockUrl);
176183

177184
expect(getExtensions).toHaveBeenCalledTimes(1);
178185
expect(withKey).toHaveBeenCalledTimes(1);
@@ -192,7 +199,7 @@ describe('Test extension.commercetools', () => {
192199
url: mockUrl,
193200
authentication: {
194201
type: 'AuthorizationHeader',
195-
headerValue: `Bearer ${accessToken}`,
202+
headerValue: 'Basic dGVzdC1jbGllbnQtaWQ6dGVzdC1jbGllbnQtc2VjcmV0',
196203
},
197204
},
198205
triggers: [
@@ -215,9 +222,7 @@ describe('Test extension.commercetools', () => {
215222
}),
216223
);
217224

218-
const accessToken = 'token123';
219-
220-
await createPaymentExtension(mockUrl, accessToken);
225+
await createPaymentExtension(mockUrl);
221226

222227
expect(getExtensions).toHaveBeenCalledTimes(1);
223228
expect(createMock).toHaveBeenCalledTimes(1);
@@ -229,7 +234,7 @@ describe('Test extension.commercetools', () => {
229234
url: mockUrl,
230235
authentication: {
231236
type: 'AuthorizationHeader',
232-
headerValue: `Bearer ${accessToken}`,
237+
headerValue: 'Basic dGVzdC1jbGllbnQtaWQ6dGVzdC1jbGllbnQtc2VjcmV0',
233238
},
234239
},
235240
triggers: [
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
});

processor/tests/routes/processor.route.spec.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import {
88
createCustomPaymentInterfaceInteractionType,
99
createCustomTransactionType,
1010
} from '../../src/commercetools/customFields.commercetools';
11-
import { getAccessToken } from '../../src/commercetools/auth.commercetools';
12-
1311
jest.mock('../../src/commercetools/extensions.commercetools', () => ({
1412
deletePaymentExtension: jest.fn(),
1513
createPaymentExtension: jest.fn(),
@@ -21,10 +19,6 @@ jest.mock('../../src/commercetools/customFields.commercetools', () => ({
2119
createCustomTransactionType: jest.fn(),
2220
}));
2321

24-
jest.mock('../../src/commercetools/auth.commercetools', () => ({
25-
getAccessToken: jest.fn(),
26-
}));
27-
2822
describe('Test src/route/processor.route.ts', () => {
2923
let req: Partial<Request>;
3024
let res: Partial<Response>;
@@ -158,8 +152,6 @@ describe('Test src/route/processor.route.ts', () => {
158152
(createCustomPaymentInterfaceInteractionType as jest.Mock).mockReturnValueOnce(Promise.resolve());
159153
(createCustomTransactionType as jest.Mock).mockReturnValueOnce(Promise.resolve());
160154

161-
(getAccessToken as jest.Mock).mockReturnValueOnce(Promise.resolve());
162-
163155
req = {
164156
hostname: 'test.com',
165157
secure: true,

0 commit comments

Comments
 (0)