|
| 1 | +import { App, joinUrl } from '@solid/community-server'; |
| 2 | +import { setGlobalLoggerFactory, WinstonLoggerFactory } from 'global-logger-factory'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { getDefaultCssVariables, getPorts, instantiateFromConfig } from '../util/ServerUtil'; |
| 5 | +import { generateCredentials, umaFetch } from '../util/UmaUtil'; |
| 6 | + |
| 7 | +const [ cssPort, umaPort ] = getPorts('Collections'); |
| 8 | + |
| 9 | +describe('A server with collections', (): void => { |
| 10 | + const owner = `http://localhost:${cssPort}/alice/profile/card#me`; |
| 11 | + const user = `http://example.com/bob`; |
| 12 | + let umaApp: App; |
| 13 | + let cssApp: App; |
| 14 | + |
| 15 | + beforeAll(async(): Promise<void> => { |
| 16 | + setGlobalLoggerFactory(new WinstonLoggerFactory('off')); |
| 17 | + |
| 18 | + umaApp = await instantiateFromConfig( |
| 19 | + 'urn:uma:default:App', |
| 20 | + path.join(__dirname, '../../packages/uma/config/default.json'), |
| 21 | + { |
| 22 | + 'urn:uma:variables:port': umaPort, |
| 23 | + 'urn:uma:variables:baseUrl': `http://localhost:${umaPort}/uma`, |
| 24 | + 'urn:uma:variables:eyePath': 'eye', |
| 25 | + 'urn:uma:variables:backupFilePath': '', |
| 26 | + } |
| 27 | + ); |
| 28 | + |
| 29 | + cssApp = await instantiateFromConfig( |
| 30 | + 'urn:solid-server:default:App', |
| 31 | + path.join(__dirname, '../../packages/css/config/default.json'), |
| 32 | + { |
| 33 | + ...getDefaultCssVariables(cssPort), |
| 34 | + 'urn:solid-server:default:variable:seedConfig': path.join(__dirname, '../../packages/css/config/seed.json'), |
| 35 | + }, |
| 36 | + ); |
| 37 | + |
| 38 | + await Promise.all([ umaApp.start(), cssApp.start() ]); |
| 39 | + }); |
| 40 | + |
| 41 | + afterAll(async(): Promise<void> => { |
| 42 | + await Promise.all([ umaApp.stop(), cssApp.stop() ]); |
| 43 | + }); |
| 44 | + |
| 45 | + it('can register client credentials for the user/RS combination.', async(): Promise<void> => { |
| 46 | + await generateCredentials({ |
| 47 | + webId: owner, |
| 48 | + authorizationServer: `http://localhost:${umaPort}/uma`, |
| 49 | + resourceServer: `http://localhost:${cssPort}/`, |
| 50 | + email: 'alice@example.org', |
| 51 | + password: 'abc123' |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('can create a policy targeting an asset collection.', async(): Promise<void> => { |
| 56 | + // TODO: hardcoded collection identifier due to lack of collection API |
| 57 | + const policy = ` |
| 58 | + @prefix ex: <http://example.org/> . |
| 59 | + @prefix ldp: <http://www.w3.org/ns/ldp#>. |
| 60 | + @prefix odrl: <http://www.w3.org/ns/odrl/2/>. |
| 61 | + @prefix odrl_p: <https://w3id.org/force/odrl3proposal#>. |
| 62 | + |
| 63 | + ex:policy a odrl:Set ; |
| 64 | + odrl:uid ex:policy ; |
| 65 | + odrl:permission ex:permission . |
| 66 | + |
| 67 | + ex:permission a odrl:Permission ; |
| 68 | + odrl:assignee <${user}> ; |
| 69 | + odrl:assigner <${owner}> ; |
| 70 | + odrl:action odrl:read ; |
| 71 | + odrl:target <collection:http://localhost:${cssPort}/alice/:http://www.w3.org/ns/ldp#contains> .` |
| 72 | + |
| 73 | + const url = `http://localhost:${umaPort}/uma/policies`; |
| 74 | + let response = await fetch(url, { |
| 75 | + method: 'POST', |
| 76 | + headers: { authorization: `WebID ${encodeURIComponent(owner)}`, 'content-type': 'text/turtle' }, |
| 77 | + body: policy, |
| 78 | + }); |
| 79 | + expect(response.status).toBe(201); |
| 80 | + |
| 81 | + response = await fetch(joinUrl(url, encodeURIComponent('http://example.org/policy')), { |
| 82 | + headers: { authorization: `WebID ${encodeURIComponent(owner)}` }, |
| 83 | + }); |
| 84 | + console.log(await response.text()); |
| 85 | + }); |
| 86 | + |
| 87 | + it('can access a resource in the asset collection.', async(): Promise<void> => { |
| 88 | + const response = await umaFetch(`http://localhost:${cssPort}/alice/README`, {}, user); |
| 89 | + expect(response.status).toBe(200); |
| 90 | + }); |
| 91 | +}); |
0 commit comments