Skip to content

Commit

Permalink
chore: add ns/* tests (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Oct 12, 2022
1 parent fcfb2a9 commit 93a8ae4
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 27 deletions.
108 changes: 101 additions & 7 deletions packages/access/test/capabilities/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '../helpers/fixtures.js'

describe('store capabilities', function () {
const proof = delegate({
const any = delegate({
issuer: account,
audience: alice,
capabilities: [
Expand All @@ -23,15 +23,109 @@ 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,
with: account.did(),
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(), {
Expand Down Expand Up @@ -62,7 +156,7 @@ describe('store capabilities', function () {
caveats: {
size: 1024,
},
proofs: [await proof],
proofs: [await any],
})
.delegate()

Expand Down Expand Up @@ -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,
Expand All @@ -154,7 +248,7 @@ describe('store capabilities', function () {
size,
},
],
proofs: [await proof],
proofs: [await any],
})

const result = await access(add, {
Expand All @@ -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,
Expand Down
49 changes: 29 additions & 20 deletions packages/access/test/capabilities/upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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(), {
Expand All @@ -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({
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -123,7 +132,7 @@ describe('upload capabilities', function () {
shards: [parseLink('bafkqaaa')],
},
],
proofs: [await proof],
proofs: [await any],
})

const result = await access(add, {
Expand All @@ -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(), {
Expand Down Expand Up @@ -196,7 +205,7 @@ describe('upload capabilities', function () {
root: parseLink('bafkqaaa'),
},
],
proofs: [await proof],
proofs: [await any],
})

const result = await access(add, {
Expand All @@ -221,7 +230,7 @@ describe('upload capabilities', function () {
audience: bob,
with: account.did(),
caveats: {},
proofs: [await proof],
proofs: [await any],
})
.delegate()

Expand Down Expand Up @@ -263,7 +272,7 @@ describe('upload capabilities', function () {
caveats: {
root: parseLink('bafkqaaa'),
},
proofs: [await proof],
proofs: [await any],
})
.delegate()

Expand Down Expand Up @@ -305,7 +314,7 @@ describe('upload capabilities', function () {
caveats: {
shards: [shard.cid],
},
proofs: [await proof],
proofs: [await any],
})
.delegate()

Expand Down Expand Up @@ -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(), {
Expand All @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -445,7 +454,7 @@ describe('upload capabilities', function () {
root: parseLink('bafkqaaa'),
},
],
proofs: [await proof],
proofs: [await any],
})

const result = await access(list, {
Expand All @@ -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'),
},
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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'),
},
Expand Down Expand Up @@ -592,7 +601,7 @@ describe('upload capabilities', function () {
root: parseLink('bafkqaaa'),
},
],
proofs: [await proof],
proofs: [await any],
})

const result = await access(remove, {
Expand All @@ -618,7 +627,7 @@ describe('upload capabilities', function () {
caveats: {
root: parseLink('bafkqaaa'),
},
proofs: [await proof],
proofs: [await any],
})
.delegate()

Expand Down

0 comments on commit 93a8ae4

Please sign in to comment.