|
4 | 4 | * The purpose of this file is to test the /policies endpoint. |
5 | 5 | */ |
6 | 6 |
|
| 7 | +import path from "path"; |
| 8 | +import { DirectoryUCRulesStorage } from "../packages/ucp/src/storage/DirectoryUCRulesStorage" |
| 9 | +import { PolicyRequestHandler } from "../packages/uma/src/routes/Policy" |
| 10 | + |
7 | 11 | const endpoint = 'http://localhost:4000/uma/policies' |
8 | 12 | const client1 = 'https://pod.woutslabbinck.com/profile/card#me'; |
9 | 13 | const client2 = 'https://pod.example.com/profile/card#me'; |
10 | 14 |
|
| 15 | +// Pathname for the test-policy directory, will probably be changed with the new test structure |
| 16 | +const policyStorePath = path.join(__dirname, '..', 'packages', 'uma', 'config', 'rules', 'test'); |
11 | 17 |
|
12 | | -async function main() { |
13 | | - console.log(`Primitive unit test to check policy access based on the client\n`); |
| 18 | +async function testGetAllPolicies() { |
| 19 | + console.log("\n\nTest GET all policies endpoint",); |
14 | 20 |
|
15 | | - let response = await fetch(endpoint, { headers: { 'Authorization': client1 } }) |
| 21 | + let response = await fetch(endpoint, { headers: { 'Authorization': client1 } }); |
16 | 22 |
|
17 | | - console.log("expecting all five policies and their relations: \n", await response.text()) |
| 23 | + console.log("expecting all five policies and their relations: \n", await response.text()); |
18 | 24 |
|
19 | | - response = await fetch(endpoint, { headers: { 'Authorization': client2 } }) |
| 25 | + response = await fetch(endpoint, { headers: { 'Authorization': client2 } }); |
20 | 26 |
|
21 | | - console.log("expecting zero policies: ", await response.text()) |
| 27 | + console.log("expecting zero policies: ", await response.text()); |
22 | 28 |
|
23 | 29 | response = await fetch(endpoint, {}); |
24 | 30 |
|
25 | | - console.log(`expecting 4xx error code (no authorization header provided): ${response.status}`) |
| 31 | + console.log(`expecting 4xx error code (no authorization header provided): ${response.status}`); |
| 32 | + |
| 33 | + // Manual test for specific test cases |
| 34 | + // This test uses the existing test.ttl policy directory as the store, could be any other store |
| 35 | + const store = new DirectoryUCRulesStorage(policyStorePath); |
| 36 | + const handler = new PolicyRequestHandler(store); |
| 37 | + |
| 38 | + let res = await handler.handle({ request: { url: new URL("http://localhost:4000/uma/policies"), method: 'GET', headers: { 'authorization': "https://pod.a.com/profile/card#me" } } }); |
| 39 | + console.log("expecting only policy 1: ", res.body); |
| 40 | +} |
| 41 | + |
| 42 | +async function main() { |
| 43 | + console.log(`Primitive unit test to check policy access based on the client\n`); |
| 44 | + |
| 45 | + await testGetAllPolicies(); |
| 46 | + |
26 | 47 | } |
27 | 48 | main() |
0 commit comments