Skip to content

Commit

Permalink
First attempt at payment-required test
Browse files Browse the repository at this point in the history
  • Loading branch information
michielbdejong committed Mar 3, 2021
1 parent 84ee0d3 commit f753ec2
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 36 deletions.
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions run-against-nss.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ echo Automated way to get an OIDC issuer cookie for Alice:
export CURL_RESULT_ALICE=`curl -ki $OIDC_ISSUER_ALICE/login/password -d"username=$USERNAME_ALICE&password=$PASSWORD_ALICE" | grep Set-Cookie`
export COOKIE_ALICE=`expr "$CURL_RESULT_ALICE" : '^Set-Cookie:\ \(.*\).'`

# echo Automated way to get an OIDC issuer cookie for Bob:
# export CURL_RESULT_BOB=`curl -ki $OIDC_ISSUER_BOB/login/password -d"username=$USERNAME_BOB&password=$PASSWORD_BOB" | grep Set-Cookie`
# export COOKIE_BOB=`expr "$CURL_RESULT_BOB" : '^Set-Cookie:\ \(.*\).'`
echo Automated way to get an OIDC issuer cookie for Bob:
export CURL_RESULT_BOB=`curl -ki $OIDC_ISSUER_BOB/login/password -d"username=$USERNAME_BOB&password=$PASSWORD_BOB" | grep Set-Cookie`
export COOKIE_BOB=`expr "$CURL_RESULT_BOB" : '^Set-Cookie:\ \(.*\).'`

echo Running with these values for Alice:
echo Cookie: $COOKIE_ALICE
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function generateTestFolder(who: string) {
console.warn(`Adding slash to the end of ${who}'s storage root ->"${storageRoot}"+"/"`);
storageRoot += '/';
}
const testFolder = `web-access-control-tests-${new Date().getTime()}`;
const testFolder = `monetization-tests-${new Date().getTime()}`;
return {
testFolder,
testFolderUrl: `${storageRoot}${testFolder}/`
Expand Down
77 changes: 77 additions & 0 deletions test/surface/payment-required.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { SolidLogic } from 'solid-logic';
import { generateTestFolder, getSolidLogicInstance, WEBID_ALICE, WEBID_BOB } from '../helpers/env';
import { responseCodeGroup } from '../helpers/util';

function makeBody(accessToModes: string, defaultModes: string, target: string) {
let str = [
'@prefix acl: <http://www.w3.org/ns/auth/acl#>.',
'',
`<#alice> a acl:Authorization;\n acl:agent <${WEBID_ALICE}>;`,
` acl:accessTo <${target}>;`,
` acl:default <${target}>;`,
' acl:mode acl:Read, acl:Write, acl:Control.',
''
].join('\n')
if (accessToModes) {
str += [
'<#bobAccessTo> a acl:Authorization;',
' acl:agentClass acl:PayingAgent;',
` acl:accessTo <${target}>;`,
` acl:mode ${accessToModes}.`,
''
].join('\n')
}
if (defaultModes) {
str += [
'<#bobDefault> a acl:Authorization;',
' acl:agentClass acl:PayingAgent;',
` acl:default <${target}>;`,
` acl:mode ${defaultModes}.`,
''
].join('\n')
}
return str
}

describe('Read-Paying', () => {
let solidLogicAlice: SolidLogic;
let solidLogicBob: SolidLogic;
beforeAll(async () => {
solidLogicAlice = await getSolidLogicInstance('ALICE')
solidLogicBob = await getSolidLogicInstance('BOB')
});

const { testFolderUrl } = generateTestFolder('ALICE');
beforeEach(async () => {
// FIXME: NSS ACL cache,
// wait for ACL cache to clear:
await new Promise(resolve => setTimeout(resolve, 20));
});

afterEach(() => {
// return solidLogicAlice.recursiveDelete(testFolderUrl);
});
it('Gives a 402 with accessTo Read access on non-container resource', async () => {
const resourceUrl = `${testFolderUrl}1/test.txt`;
// This will do mkdir-p:
const creationResult = await solidLogicAlice.fetch(resourceUrl, {
method: 'PUT',
body: '<#hello> <#linked> <#world> .',
headers: {
'Content-Type': 'text/turtle',
'If-None-Match': '*'
}
});
const aclDocUrl = await solidLogicAlice.findAclDocUrl(resourceUrl);
await solidLogicAlice.fetch(aclDocUrl, {
method: 'PUT',
body: makeBody('acl:Read', null, resourceUrl),
headers: {
'Content-Type': 'text/turtle',
// 'If-None-Match': '*' - work around a bug in some servers that don't support If-None-Match on ACL doc URLs
}
});
const result = await solidLogicBob.fetch(resourceUrl)
expect(result.status).toEqual("402");
});
});
42 changes: 20 additions & 22 deletions test/surface/vanity-payment-pointer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,32 @@ import { getSolidLogicInstance, WEBID_ALICE } from '../helpers/env';
import { responseCodeGroup } from '../helpers/util';
import fetch from 'node-fetch';

async function setPaymentPointer(solidLogic, paymentPointer) {
const body = '@prefix acl: <http://www.w3.org/ns/auth/acl#>.\n' +
'@prefix pp: <http://paymentpointers.org/ns#>.\n' +
`<#me> pp:PaymentPointer "${paymentPointer}";\n` +
' acl:trustedApp [\n'+
' acl:mode acl:Append, acl:Control, acl:Read, acl:Write;\n'+
' acl:origin <https://tester>\n'+
' ].';
const result = await solidLogic.fetch(solidLogic.me, {
method: 'PUT',
body,
headers: {
'Content-Type': 'text/turtle'
}
});
expect(responseCodeGroup(result.status)).toEqual('2xx');
}

describe('Vanity Payment Pointer', () => {
let solidLogicAlice: SolidLogic;
beforeAll(async () => {
solidLogicAlice = await getSolidLogicInstance('ALICE')
});

describe('Alice has a pp:PaymentPointer triple in her profile, no slash', () => {
beforeAll(async () => {
const result = await solidLogicAlice.fetch(solidLogicAlice.me, {
method: 'PUT',
body: '<#me> <http://paymentpointers.org/ns#PaymentPointer> "$example.com" .',
headers: {
'Content-Type': 'text/turtle'
}
});
expect(responseCodeGroup(result.status)).toEqual('2xx');
});

beforeAll(() => setPaymentPointer(solidLogicAlice, '$example.com'));
it('makes /.well-known/pay redirects to it', async () => {
const podRoot = new URL(solidLogicAlice.me).origin;
const result = await fetch(`${podRoot}/.well-known/pay`, { redirect: 'manual' });
Expand All @@ -29,17 +37,7 @@ describe('Vanity Payment Pointer', () => {
});
});
describe('Alice has a pp:PaymentPointer triple in her profile, with slash', () => {
beforeAll(async () => {
const result = await solidLogicAlice.fetch(solidLogicAlice.me, {
method: 'PUT',
body: '<#me> <http://paymentpointers.org/ns#PaymentPointer> "$example.com/foo/bar" .',
headers: {
'Content-Type': 'text/turtle'
}
});
expect(responseCodeGroup(result.status)).toEqual('2xx');
});

beforeAll(() => setPaymentPointer(solidLogicAlice, '$example.com/foo/bar'));
it('makes /.well-known/pay redirects to it', async () => {
const podRoot = new URL(solidLogicAlice.me).origin;
const result = await fetch(`${podRoot}/.well-known/pay`, { redirect: 'manual' });
Expand Down

0 comments on commit f753ec2

Please sign in to comment.