Skip to content

Commit af75550

Browse files
committed
Replace incomplete tests
1 parent 8880037 commit af75550

File tree

2 files changed

+130
-22
lines changed

2 files changed

+130
-22
lines changed

src/Typescript/src/client.test.ts

Lines changed: 95 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,6 @@ describe('AuthZenClient', () => {
5858

5959
expect(client.pdpUrl).toBe('https://example.com');
6060
});
61-
62-
// it('should set Authorization header when token provided', () => {
63-
// const client = new AuthZenClient({
64-
// pdpUrl: 'https://example.com',
65-
// token: 'test-token',
66-
// });
67-
68-
// expect(client).toBeInstanceOf(AuthZenClient);
69-
70-
// // Todo
71-
// });
72-
73-
// it('should merge custom headers', () => {
74-
// const client = new AuthZenClient({
75-
// pdpUrl: 'https://example.com',
76-
// headers: { 'Custom-Header': 'test-value' },
77-
// });
78-
79-
// expect(client).toBeInstanceOf(AuthZenClient);
80-
81-
// // Todo
82-
// });
8361
});
8462

8563
describe('evaluate', () => {
@@ -511,6 +489,54 @@ describe('AuthZenClient', () => {
511489
await expect(client.evaluate(invalidRequest)).rejects.toThrow(AuthZenValidationError);
512490
expect(mockFetch).not.toHaveBeenCalled();
513491
});
492+
493+
it('should include Authorization header with bearer token when provided', async () => {
494+
const mockDiscoveryResponse = {
495+
ok: true,
496+
status: 200,
497+
json: jest.fn().mockResolvedValue({
498+
policy_decision_point: 'https://example.com',
499+
access_evaluation_endpoint: 'https://example.com/custom/evaluate',
500+
}),
501+
headers: {
502+
get: jest.fn().mockReturnValue('application/json')
503+
},
504+
};
505+
506+
const mockEvaluateResponse = {
507+
ok: true,
508+
status: 200,
509+
json: jest.fn().mockResolvedValue({ decision: true }),
510+
headers: {
511+
get: jest.fn().mockReturnValue('application/json')
512+
},
513+
};
514+
515+
mockFetch
516+
.mockResolvedValueOnce(mockDiscoveryResponse)
517+
.mockResolvedValueOnce(mockEvaluateResponse);
518+
519+
const client = new AuthZenClient({
520+
pdpUrl: 'https://example.com',
521+
token: 'my-secret-token',
522+
});
523+
524+
await client.evaluate(validRequest);
525+
526+
// Second call: evaluate should include Authorization header
527+
expect(mockFetch).toHaveBeenNthCalledWith(
528+
2,
529+
'https://example.com/custom/evaluate',
530+
expect.objectContaining({
531+
method: 'POST',
532+
headers: expect.objectContaining({
533+
'Authorization': 'Bearer my-secret-token',
534+
}),
535+
body: JSON.stringify(validRequest),
536+
signal: expect.any(AbortSignal),
537+
})
538+
);
539+
});
514540
});
515541

516542
describe('evaluations', () => {
@@ -1045,6 +1071,53 @@ describe('AuthZenClient', () => {
10451071
await expect(client.evaluations(requestWithDefaults)).resolves.toBeDefined();
10461072
expect(mockFetch).toHaveBeenCalled();
10471073
});
1074+
1075+
it('should include Authorization header with bearer token when provided', async () => {
1076+
const mockDiscoveryResponse = {
1077+
ok: true,
1078+
status: 200,
1079+
json: jest.fn().mockResolvedValue({
1080+
policy_decision_point: 'https://example.com',
1081+
access_evaluations_endpoint: 'https://example.com/custom/evaluations',
1082+
}),
1083+
headers: {
1084+
get: jest.fn().mockReturnValue('application/json')
1085+
},
1086+
};
1087+
1088+
const mockEvaluationsResponse = {
1089+
ok: true,
1090+
status: 200,
1091+
json: jest.fn().mockResolvedValue({ evaluations: [{ decision: true }, { decision: false }] }),
1092+
headers: {
1093+
get: jest.fn().mockReturnValue('application/json')
1094+
},
1095+
};
1096+
1097+
mockFetch
1098+
.mockResolvedValueOnce(mockDiscoveryResponse)
1099+
.mockResolvedValueOnce(mockEvaluationsResponse);
1100+
1101+
const client = new AuthZenClient({
1102+
pdpUrl: 'https://example.com',
1103+
token: 'my-evaluations-token',
1104+
});
1105+
1106+
await client.evaluations(validRequest);
1107+
1108+
expect(mockFetch).toHaveBeenNthCalledWith(
1109+
2,
1110+
'https://example.com/custom/evaluations',
1111+
expect.objectContaining({
1112+
method: 'POST',
1113+
headers: expect.objectContaining({
1114+
'Authorization': 'Bearer my-evaluations-token',
1115+
}),
1116+
body: JSON.stringify(validRequest),
1117+
signal: expect.any(AbortSignal),
1118+
})
1119+
);
1120+
});
10481121
});
10491122

10501123
describe('default value handling', () => {

src/Typescript/src/discovery.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,41 @@ describe('AuthZenClient - Discovery', () => {
148148

149149
expect(result).toEqual(mockConfig);
150150
});
151+
152+
it('should include Authorization header with bearer token when provided', async () => {
153+
const mockDiscoveryResponse = {
154+
ok: true,
155+
status: 200,
156+
json: jest.fn().mockResolvedValue({
157+
policy_decision_point: 'https://example.com',
158+
access_evaluation_endpoint: 'https://example.com/custom/evaluate',
159+
access_evaluations_endpoint: 'https://example.com/custom/evaluations',
160+
}),
161+
headers: {
162+
get: jest.fn().mockReturnValue('application/json')
163+
},
164+
};
165+
166+
mockFetch.mockResolvedValueOnce(mockDiscoveryResponse);
167+
168+
const client = new AuthZenClient({
169+
pdpUrl: 'https://example.com',
170+
token: 'my-discovery-token',
171+
});
172+
173+
await client.discover();
174+
175+
expect(mockFetch).toHaveBeenCalledWith(
176+
'https://example.com/.well-known/authzen-configuration',
177+
expect.objectContaining({
178+
method: 'GET',
179+
headers: expect.objectContaining({
180+
'Authorization': 'Bearer my-discovery-token',
181+
}),
182+
signal: expect.any(AbortSignal),
183+
})
184+
);
185+
});
151186
});
152187

153188
describe('validation errors', () => {

0 commit comments

Comments
 (0)