From 93a8ae4bd3a72e3902e7913d9cca62c2c9c4e0cd Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Wed, 12 Oct 2022 06:49:39 -0700 Subject: [PATCH] chore: add ns/* tests (#92) --- .../access/test/capabilities/store.test.js | 108 ++++++++++++++++-- .../access/test/capabilities/upload.test.js | 49 ++++---- 2 files changed, 130 insertions(+), 27 deletions(-) diff --git a/packages/access/test/capabilities/store.test.js b/packages/access/test/capabilities/store.test.js index 9b46c3fce..0a6d7266d 100644 --- a/packages/access/test/capabilities/store.test.js +++ b/packages/access/test/capabilities/store.test.js @@ -12,7 +12,7 @@ import { } from '../helpers/fixtures.js' describe('store capabilities', function () { - const proof = delegate({ + const any = delegate({ issuer: account, audience: alice, capabilities: [ @@ -23,7 +23,48 @@ describe('store capabilities', function () { ], }) - it('should be able to derive from *', async () => { + const store = delegate({ + issuer: account, + audience: alice, + capabilities: [ + { + can: 'store/*', + with: account.did(), + }, + ], + }) + + it('store/add can be derived from *', async () => { + const add = Store.add.invoke({ + issuer: alice, + audience: w3, + with: account.did(), + caveats: { + link: parseLink('bafkqaaa'), + }, + proofs: [await any], + }) + + const result = await access(await add.delegate(), { + capability: Store.add, + principal: Principal, + canIssue: (claim, issuer) => { + return claim.with === issuer + }, + }) + + if (result.error) { + assert.fail(result.message) + } + + assert.deepEqual(result.audience.did(), w3.did()) + assert.equal(result.capability.can, 'store/add') + assert.deepEqual(result.capability.caveats, { + link: parseLink('bafkqaaa'), + }) + }) + + it('store/add can be derived from store/*', async () => { const add = Store.add.invoke({ issuer: alice, audience: w3, @@ -31,7 +72,60 @@ describe('store capabilities', function () { caveats: { link: parseLink('bafkqaaa'), }, - proofs: [await proof], + proofs: [await store], + }) + + const result = await access(await add.delegate(), { + capability: Store.add, + principal: Principal, + canIssue: (claim, issuer) => { + return claim.with === issuer + }, + }) + + if (result.error) { + assert.fail(result.message) + } + + assert.deepEqual(result.audience.did(), w3.did()) + assert.equal(result.capability.can, 'store/add') + assert.deepEqual(result.capability.caveats, { + link: parseLink('bafkqaaa'), + }) + }) + + it('store/add can be derived from store/* derived from *', async () => { + const any = await delegate({ + issuer: account, + audience: alice, + capabilities: [ + { + can: '*', + with: account.did(), + }, + ], + }) + + const store = await delegate({ + issuer: alice, + audience: bob, + capabilities: [ + { + can: 'store/*', + with: account.did(), + }, + ], + proofs: [any], + }) + + const add = Store.add.invoke({ + issuer: bob, + audience: w3, + with: account.did(), + caveats: { + link: parseLink('bafkqaaa'), + }, + proofs: [await store], }) const result = await access(await add.delegate(), { @@ -62,7 +156,7 @@ describe('store capabilities', function () { caveats: { size: 1024, }, - proofs: [await proof], + proofs: [await any], }) .delegate() @@ -127,7 +221,7 @@ describe('store capabilities', function () { for (const size of fixtures) { const json = JSON.stringify(size) it(`store/add size must be an int not ${json}`, async () => { - const proofs = [await proof] + const proofs = [await any] assert.throws(() => { Store.add.invoke({ issuer: alice, @@ -154,7 +248,7 @@ describe('store capabilities', function () { size, }, ], - proofs: [await proof], + proofs: [await any], }) const result = await access(add, { @@ -171,7 +265,7 @@ describe('store capabilities', function () { } it('store/add size must be an int', async () => { - const proofs = [await proof] + const proofs = [await any] assert.throws(() => { Store.add.invoke({ issuer: alice, diff --git a/packages/access/test/capabilities/upload.test.js b/packages/access/test/capabilities/upload.test.js index a973b42c6..95818761d 100644 --- a/packages/access/test/capabilities/upload.test.js +++ b/packages/access/test/capabilities/upload.test.js @@ -14,7 +14,7 @@ import { describe('upload capabilities', function () { // delegation from account to agent - const proof = delegate({ + const any = delegate({ issuer: account, audience: alice, capabilities: [ @@ -26,14 +26,23 @@ describe('upload capabilities', function () { }) it('upload/add can be derived from upload/* derived from *', async () => { + const upload = await Upload.upload + .invoke({ + issuer: alice, + audience: bob, + with: account.did(), + proofs: [await any], + }) + .delegate() + const add = Upload.add.invoke({ - issuer: alice, + issuer: bob, audience: w3, with: account.did(), caveats: { root: parseLink('bafkqaaa'), }, - proofs: [await proof], + proofs: [upload], }) const result = await access(await add.delegate(), { @@ -60,7 +69,7 @@ describe('upload capabilities', function () { issuer: alice, audience: bob, with: account.did(), - proofs: [await proof], + proofs: [await any], }) const add = Upload.add.invoke({ @@ -93,7 +102,7 @@ describe('upload capabilities', function () { }) it('creating upload/add throws if shards is contains non CAR cid', async () => { - const proofs = [await proof] + const proofs = [await any] assert.throws(() => { Upload.add.invoke({ issuer: alice, @@ -123,7 +132,7 @@ describe('upload capabilities', function () { shards: [parseLink('bafkqaaa')], }, ], - proofs: [await proof], + proofs: [await any], }) const result = await access(add, { @@ -148,7 +157,7 @@ describe('upload capabilities', function () { root: parseLink('bafkqaaa'), shards: [shard.cid], }, - proofs: [await proof], + proofs: [await any], }) const result = await access(await add.delegate(), { @@ -196,7 +205,7 @@ describe('upload capabilities', function () { root: parseLink('bafkqaaa'), }, ], - proofs: [await proof], + proofs: [await any], }) const result = await access(add, { @@ -221,7 +230,7 @@ describe('upload capabilities', function () { audience: bob, with: account.did(), caveats: {}, - proofs: [await proof], + proofs: [await any], }) .delegate() @@ -263,7 +272,7 @@ describe('upload capabilities', function () { caveats: { root: parseLink('bafkqaaa'), }, - proofs: [await proof], + proofs: [await any], }) .delegate() @@ -305,7 +314,7 @@ describe('upload capabilities', function () { caveats: { shards: [shard.cid], }, - proofs: [await proof], + proofs: [await any], }) .delegate() @@ -339,7 +348,7 @@ describe('upload capabilities', function () { issuer: alice, audience: w3, with: account.did(), - proofs: [await proof], + proofs: [await any], }) const result = await access(await list.delegate(), { @@ -364,7 +373,7 @@ describe('upload capabilities', function () { issuer: alice, audience: bob, with: account.did(), - proofs: [await proof], + proofs: [await any], }) const list = Upload.list.invoke({ @@ -396,7 +405,7 @@ describe('upload capabilities', function () { issuer: alice, audience: bob, with: account.did(), - proofs: [await proof], + proofs: [await any], }) const list = Upload.list.invoke({ @@ -445,7 +454,7 @@ describe('upload capabilities', function () { root: parseLink('bafkqaaa'), }, ], - proofs: [await proof], + proofs: [await any], }) const result = await access(list, { @@ -467,7 +476,7 @@ describe('upload capabilities', function () { issuer: alice, audience: w3, with: account.did(), - proofs: [await proof], + proofs: [await any], caveats: { root: parseLink('bafkqaaa'), }, @@ -497,7 +506,7 @@ describe('upload capabilities', function () { issuer: alice, audience: bob, with: account.did(), - proofs: [await proof], + proofs: [await any], }) const remove = Upload.remove.invoke({ @@ -534,7 +543,7 @@ describe('upload capabilities', function () { issuer: alice, audience: bob, with: account.did(), - proofs: [await proof], + proofs: [await any], caveats: { root: parseLink('bafkqaaa'), }, @@ -592,7 +601,7 @@ describe('upload capabilities', function () { root: parseLink('bafkqaaa'), }, ], - proofs: [await proof], + proofs: [await any], }) const result = await access(remove, { @@ -618,7 +627,7 @@ describe('upload capabilities', function () { caveats: { root: parseLink('bafkqaaa'), }, - proofs: [await proof], + proofs: [await any], }) .delegate()