Skip to content

Commit

Permalink
enhance caching tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Aug 12, 2024
1 parent 175778e commit e6d45ed
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
4 changes: 1 addition & 3 deletions packages/api/src/dwn-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,7 @@ export class DwnApi {
return cachedGrant;
}

const permissionGrants = await this.permissions.queryGrants({
grantor: this.connectedDid,
});
const permissionGrants = await this.permissions.queryGrants({ checkRevoked: true, grantor: this.connectedDid });

const grantEntries = permissionGrants.map(grant => ({ message: grant.rawMessage, grant: grant.toJSON() }));

Expand Down
35 changes: 25 additions & 10 deletions 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 { DwnDateSort, DwnInterface, PlatformAgentTestHarness } from '@web5/agent';
import { AgentPermissionsApi, DwnDateSort, DwnInterface, PlatformAgentTestHarness } from '@web5/agent';

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

const processDwnRequestSpy = sinon.spy(testHarness.agent, 'processDwnRequest');
// find the grant for a request

// simulate a connect where bobDid can impersonate aliceDid
dwnBob['connectedDid'] = aliceDid.uri;
dwnBob['delegateDid'] = bobDid.uri;
Expand All @@ -1394,6 +1391,9 @@ describe('DwnApi', () => {
grants : [ deviceXGrant.rawMessage ]
});

const fetchGrantsSpy = sinon.spy(AgentPermissionsApi.prototype, 'fetchGrants');

// find the grant for a request
let grantForRequest = await dwnBob['connected'].findPermissionGrantForMessage({
messageParams: {
messageType : DwnInterface.RecordsWrite,
Expand All @@ -1404,9 +1404,9 @@ describe('DwnApi', () => {
// expect to have the grant
expect(grantForRequest).to.exist;
expect(grantForRequest.id).to.equal(deviceXGrant.id);
expect(processDwnRequestSpy.callCount).to.equal(2); // 1 for the request, and 1 for the revocation check
expect(fetchGrantsSpy.callCount).to.equal(1);

processDwnRequestSpy.resetHistory();
fetchGrantsSpy.resetHistory();

// attempt to find the grant again
grantForRequest = await dwnBob['connected'].findPermissionGrantForMessage({
Expand All @@ -1417,7 +1417,23 @@ describe('DwnApi', () => {
});
expect(grantForRequest).to.exist;
expect(grantForRequest.id).to.equal(deviceXGrant.id);
expect(processDwnRequestSpy.callCount).to.equal(0); // should not have been called again
expect(fetchGrantsSpy.callCount).to.equal(0);

// should call again if cached:false is passed
grantForRequest = await dwnBob['connected'].findPermissionGrantForMessage({
messageParams: {
messageType : DwnInterface.RecordsWrite,
protocol : 'http://example.com/protocol'
},
cached: false
});
expect(grantForRequest).to.exist;
expect(grantForRequest.id).to.equal(deviceXGrant.id);
expect(fetchGrantsSpy.callCount).to.equal(1);

// reset the spy
fetchGrantsSpy.resetHistory();
expect(fetchGrantsSpy.callCount).to.equal(0);

// call for a different grant
try {
Expand All @@ -1431,8 +1447,7 @@ describe('DwnApi', () => {
} catch(error:any) {
expect(error.message).to.equal('AgentDwnApi: No permissions found for RecordsRead: http://example.com/protocol');
}

expect(processDwnRequestSpy.callCount).to.equal(1); // should have been called once for the request
expect(fetchGrantsSpy.callCount).to.equal(1);

// call again to ensure grants which are not found are not cached
try {
Expand All @@ -1447,7 +1462,7 @@ describe('DwnApi', () => {
expect(error.message).to.equal('AgentDwnApi: No permissions found for RecordsRead: http://example.com/protocol');
}

expect(processDwnRequestSpy.callCount).to.equal(2); // should have been called again for the request
expect(fetchGrantsSpy.callCount).to.equal(2); // should have been called again
});

it('throws if no delegateDid is set', async () => {
Expand Down

0 comments on commit e6d45ed

Please sign in to comment.