Skip to content

Commit

Permalink
increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Aug 12, 2024
1 parent e6d45ed commit 980fbf6
Showing 1 changed file with 222 additions and 1 deletion.
223 changes: 222 additions & 1 deletion packages/api/tests/dwn-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { BearerDid } from '@web5/dids';
import sinon from 'sinon';
import { expect } from 'chai';
import { Web5UserAgent } from '@web5/user-agent';
import { AgentPermissionsApi, DwnDateSort, DwnInterface, PlatformAgentTestHarness } from '@web5/agent';
import { AgentPermissionsApi, DwnDateSort, DwnInterface, getRecordAuthor, PlatformAgentTestHarness } from '@web5/agent';

import { DwnApi } from '../src/dwn-api.js';
import { testDwnUrl } from './utils/test-config.js';
Expand Down Expand Up @@ -1483,6 +1483,53 @@ describe('DwnApi', () => {
});

describe('permissions.grant', () => {
it('uses the connected DID to create a grant if no delegate DID is set', async () => {
// scenario: create a permission grant for bob, confirm that alice is the signer

// create a permission grant for bob
const deviceXGrant = await dwnAlice.permissions.request({
store : true,
scope : {
interface : DwnInterfaceName.Records,
method : DwnMethodName.Write,
protocol : 'http://example.com/protocol'
}
});

const author = getRecordAuthor(deviceXGrant.rawMessage);
expect(dwnAlice['connectedDid']).to.equal(aliceDid.uri); // connected DID should be alice
expect(author).to.equal(aliceDid.uri);
});

it('uses the delegate DID to create a grant if set', async () => {
// scenario: create a permission grant for aliceDeviceX, confirm that deviceX is the signer

// create an identity for deviceX
const aliceDeviceX = await testHarness.agent.identity.create({
store : true,
metadata : { name: 'Alice Device X' },
didMethod : 'jwk'
});

// set the delegate DID, this happens during a connect flow
dwnAlice['delegateDid'] = aliceDeviceX.did.uri;

// create a permission grant for deviceX
const deviceXGrant = await dwnAlice.permissions.request({
store : true,
scope : {
interface : DwnInterfaceName.Records,
method : DwnMethodName.Write,
protocol : 'http://example.com/protocol'
}
});

const author = getRecordAuthor(deviceXGrant.rawMessage);
expect(dwnAlice['connectedDid']).to.equal(aliceDid.uri); // connected DID should be alice
expect(dwnAlice['delegateDid']).to.equal(aliceDeviceX.did.uri); // delegate DID should be deviceX
expect(author).to.equal(aliceDeviceX.did.uri);
});

it('creates and stores a grant', async () => {
// scenario: create a grant for deviceX, confirm the grant exists

Expand Down Expand Up @@ -1582,6 +1629,53 @@ describe('DwnApi', () => {
});

describe('permissions.request', () => {
it('uses the connected DID to create a request if no delegate DID is set', async () => {
// scenario: create a permission request for bob, confirm the request exists

// create a permission request for bob
const deviceXRequest = await dwnAlice.permissions.request({
store : true,
scope : {
interface : DwnInterfaceName.Records,
method : DwnMethodName.Write,
protocol : 'http://example.com/protocol'
}
});

const author = getRecordAuthor(deviceXRequest.rawMessage);
expect(dwnAlice['connectedDid']).to.equal(aliceDid.uri); // connected DID should be alice
expect(author).to.equal(aliceDid.uri);
});

it('uses the delegate DID to create a request if set', async () => {
// scenario: create a permission request for aliceDeviceX, the signer

// create an identity for deviceX
const aliceDeviceX = await testHarness.agent.identity.create({
store : true,
metadata : { name: 'Alice Device X' },
didMethod : 'jwk'
});

// set the delegate DID
dwnAlice['delegateDid'] = aliceDeviceX.did.uri;

// create a permission request for deviceX
const deviceXRequest = await dwnAlice.permissions.request({
store : true,
scope : {
interface : DwnInterfaceName.Records,
method : DwnMethodName.Write,
protocol : 'http://example.com/protocol'
}
});

const author = getRecordAuthor(deviceXRequest.rawMessage);
expect(dwnAlice['connectedDid']).to.equal(aliceDid.uri); // connected DID should be alice
expect(dwnAlice['delegateDid']).to.equal(aliceDeviceX.did.uri); // delegate DID should be deviceX
expect(author).to.equal(aliceDeviceX.did.uri);
});

it('creates a permission request and stores it', async () => {
// scenario: create a permission request confirm the request exists

Expand Down Expand Up @@ -1662,6 +1756,51 @@ describe('DwnApi', () => {
});

describe('permissions.queryRequests', () => {
it('uses the connected DID to query for permission requests if no delegate DID is set', async () => {
// scenario: query for permission requests, confirm that alice is the author of the query

// spy on the fetch requests method to confirm the author
const fetchRequestsSpy = sinon.spy(AgentPermissionsApi.prototype, 'fetchRequests');

// Query for requests
const deviceXRequests = await dwnAlice.permissions.queryRequests();
expect(deviceXRequests.length).to.equal(0);

// confirm alice is the connected DID
expect(dwnAlice['connectedDid']).to.equal(aliceDid.uri);
expect(fetchRequestsSpy.callCount).to.equal(1);
expect(fetchRequestsSpy.args[0][0].author).to.equal(aliceDid.uri);
});

it('uses the delegate DID to query for permission requests if set', async () => {
// scenario: query for permission requests for aliceDeviceX, confirm that deviceX is the signer

// create an identity for deviceX
const aliceDeviceX = await testHarness.agent.identity.create({
store : true,
metadata : { name: 'Alice Device X' },
didMethod : 'jwk'
});

// spy on the fetch requests method to confirm the author
const fetchRequestsSpy = sinon.spy(AgentPermissionsApi.prototype, 'fetchRequests');

// set the delegate DID, this happens during a connect flow
dwnAlice['delegateDid'] = aliceDeviceX.did.uri;

// Query for requests
const deviceXRequests = await dwnAlice.permissions.queryRequests();
expect(deviceXRequests.length).to.equal(0);

// confirm alice is the connected DID, and aliceDeviceX is the delegate DID
expect(dwnAlice['connectedDid']).to.equal(aliceDid.uri);
expect(dwnAlice['delegateDid']).to.equal(aliceDeviceX.did.uri);

// confirm the author is aliceDeviceX
expect(fetchRequestsSpy.callCount).to.equal(1);
expect(fetchRequestsSpy.args[0][0].author).to.equal(aliceDeviceX.did.uri);
});

it('should query for permission requests from the local DWN', async () => {
// bob creates two different requests and stores it
const bobRequest = await dwnBob.permissions.request({
Expand Down Expand Up @@ -1738,6 +1877,51 @@ describe('DwnApi', () => {
});

describe('permissions.queryGrants', () => {
it('uses the connected DID to query for grants if no delegate DID is set', async () => {
// scenario: query for grants, confirm that alice is the author of the query

// spy on the fetch grants method to confirm the author
const fetchGrantsSpy = sinon.spy(AgentPermissionsApi.prototype, 'fetchGrants');

// Query for grants
const deviceXGrants = await dwnAlice.permissions.queryGrants();
expect(deviceXGrants.length).to.equal(0);

// confirm alice is the connected DID
expect(dwnAlice['connectedDid']).to.equal(aliceDid.uri);
expect(fetchGrantsSpy.callCount).to.equal(1);
expect(fetchGrantsSpy.args[0][0].author).to.equal(aliceDid.uri);
});

it('uses the delegate DID to query for grants if set', async () => {
// scenario: query for grants for aliceDeviceX, confirm that deviceX is the signer

// create an identity for deviceX
const aliceDeviceX = await testHarness.agent.identity.create({
store : true,
metadata : { name: 'Alice Device X' },
didMethod : 'jwk'
});

// spy on the fetch grants method to confirm the author
const fetchGrantsSpy = sinon.spy(AgentPermissionsApi.prototype, 'fetchGrants');

// set the delegate DID, this happens during a connect flow
dwnAlice['delegateDid'] = aliceDeviceX.did.uri;

// Query for grants
const deviceXGrants = await dwnAlice.permissions.queryGrants();
expect(deviceXGrants.length).to.equal(0);

// confirm alice is the connected DID, and aliceDeviceX is the delegate DID
expect(dwnAlice['connectedDid']).to.equal(aliceDid.uri);
expect(dwnAlice['delegateDid']).to.equal(aliceDeviceX.did.uri);

// confirm the author is aliceDeviceX
expect(fetchGrantsSpy.callCount).to.equal(1);
expect(fetchGrantsSpy.args[0][0].author).to.equal(aliceDeviceX.did.uri);
});

it('should query for permission grants from the local DWN', async () => {
// alice creates a grant for bob and stores it
const bobGrant = await dwnAlice.permissions.grant({
Expand Down Expand Up @@ -1936,5 +2120,42 @@ describe('DwnApi', () => {
expect(fetchedGrantsCarol.length).to.equal(1);
expect(fetchedGrantsCarol[0].id).to.equal(grantFromCarol.id);
});

it('should check revocation status if option is set', async () => {
// alice creates a grant for bob and stores it
const bobGrant = await dwnAlice.permissions.grant({
store : true,
grantedTo : bobDid.uri,
dateExpires : Time.createOffsetTimestamp({ seconds: 60 }),
scope : {
interface : DwnInterfaceName.Records,
method : DwnMethodName.Write,
protocol : 'http://example.com/protocol'
}
});

// query for the grants
let fetchedGrants = await dwnAlice.permissions.queryGrants({
checkRevoked: true
});

// expect to have the 1 grant created
expect(fetchedGrants.length).to.equal(1);
expect(fetchedGrants[0].id).to.equal(bobGrant.id);

// stub the isRevoked method to return true
sinon.stub(AgentPermissionsApi.prototype, 'isGrantRevoked').resolves(true);

// query for the grants
fetchedGrants = await dwnAlice.permissions.queryGrants({
checkRevoked: true
});
expect(fetchedGrants.length).to.equal(0);

// return without checking revoked status
fetchedGrants = await dwnAlice.permissions.queryGrants();
expect(fetchedGrants.length).to.equal(1);
expect(fetchedGrants[0].id).to.equal(bobGrant.id);
});
});
});

0 comments on commit 980fbf6

Please sign in to comment.