From e6d45ed303ec18066965237901580d7215a5fbd2 Mon Sep 17 00:00:00 2001 From: Liran Cohen Date: Mon, 12 Aug 2024 12:44:18 -0400 Subject: [PATCH] enhance caching tests --- packages/api/src/dwn-api.ts | 4 +--- packages/api/tests/dwn-api.spec.ts | 35 +++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/api/src/dwn-api.ts b/packages/api/src/dwn-api.ts index e58186712..411416cc2 100644 --- a/packages/api/src/dwn-api.ts +++ b/packages/api/src/dwn-api.ts @@ -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() })); diff --git a/packages/api/tests/dwn-api.spec.ts b/packages/api/tests/dwn-api.spec.ts index 5f733a6f7..d3602880d 100644 --- a/packages/api/tests/dwn-api.spec.ts +++ b/packages/api/tests/dwn-api.spec.ts @@ -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'; @@ -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; @@ -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, @@ -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({ @@ -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 { @@ -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 { @@ -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 () => {