From e9f709d902124b03441fd7a3981a55d15070f857 Mon Sep 17 00:00:00 2001 From: Liran Cohen Date: Sun, 1 Sep 2024 21:35:50 -0400 Subject: [PATCH] clear cache in tests, add check to process initial write within function --- .../agent/tests/sync-engine-level.spec.ts | 1 + packages/api/src/record.ts | 89 +++++++++---------- packages/api/tests/dwn-api.spec.ts | 1 + packages/api/tests/permission-grant.spec.ts | 1 + packages/api/tests/permission-request.spec.ts | 1 + packages/api/tests/protocol.spec.ts | 1 + packages/api/tests/record.spec.ts | 5 +- 7 files changed, 53 insertions(+), 46 deletions(-) diff --git a/packages/agent/tests/sync-engine-level.spec.ts b/packages/agent/tests/sync-engine-level.spec.ts index 85003f6ec..5037f6a30 100644 --- a/packages/agent/tests/sync-engine-level.spec.ts +++ b/packages/agent/tests/sync-engine-level.spec.ts @@ -159,6 +159,7 @@ describe('SyncEngineLevel', () => { await testHarness.dwnEventLog.clear(); await testHarness.dwnMessageStore.clear(); await testHarness.dwnResumableTaskStore.clear(); + await testHarness.agent.permissions.clear(); testHarness.dwnStores.clear(); }); diff --git a/packages/api/src/record.ts b/packages/api/src/record.ts index 3159a9dab..4405a5ed3 100644 --- a/packages/api/src/record.ts +++ b/packages/api/src/record.ts @@ -811,10 +811,7 @@ export class Record implements RecordModel { this._initialWrite = { ...this.rawMessage as DwnMessage[DwnInterface.RecordsWrite] }; } - // if there is an initial write and we haven't already processed it, we first process it and marked it as such. - if ((signAsOwner && !this._initialWriteSigned) || (store && !this._initialWriteStored)) { - await this.processInitialWrite({ store, signAsOwner }); - } + await this.processInitialWriteIfNeeded({ store, signAsOwner }); // prepare delete options let deleteOptions: ProcessDwnRequest = { @@ -878,49 +875,54 @@ export class Record implements RecordModel { return { status }; } - private async processInitialWrite({ store, signAsOwner }:{ store: boolean, signAsOwner: boolean }): Promise { - const signAsOwnerValue = signAsOwner && this._delegateDid === undefined; - const signAsOwnerDelegate = signAsOwner && this._delegateDid !== undefined; - - const initialWriteRequest: ProcessDwnRequest = { - messageType : DwnInterface.RecordsWrite, - rawMessage : this.initialWrite, - author : this._connectedDid, - target : this._connectedDid, - signAsOwner : signAsOwnerValue, - signAsOwnerDelegate, - store, - }; - - if (this._delegateDid) { - const { message: delegatedGrant } = await this._permissionsApi.getPermission({ - connectedDid : this._connectedDid, - delegateDid : this._delegateDid, - protocol : this.protocol, - delegate : true, - cached : true, - messageType : initialWriteRequest.messageType - }); + /** + * Process the initial write, if it hasn't already been processed, with the options set for storing and/or signing as the owner. + */ + private async processInitialWriteIfNeeded({ store, signAsOwner }:{ store: boolean, signAsOwner: boolean }): Promise { + if (this.initialWrite && ((signAsOwner && !this._initialWriteSigned) || (store && !this._initialWriteStored))) { + const signAsOwnerValue = signAsOwner && this._delegateDid === undefined; + const signAsOwnerDelegate = signAsOwner && this._delegateDid !== undefined; - initialWriteRequest.messageParams = { - ...initialWriteRequest.messageParams, - delegatedGrant + const initialWriteRequest: ProcessDwnRequest = { + messageType : DwnInterface.RecordsWrite, + rawMessage : this.initialWrite, + author : this._connectedDid, + target : this._connectedDid, + signAsOwner : signAsOwnerValue, + signAsOwnerDelegate, + store, }; - initialWriteRequest.granteeDid = this._delegateDid; - } + if (this._delegateDid) { + const { message: delegatedGrant } = await this._permissionsApi.getPermission({ + connectedDid : this._connectedDid, + delegateDid : this._delegateDid, + protocol : this.protocol, + delegate : true, + cached : true, + messageType : initialWriteRequest.messageType + }); + + initialWriteRequest.messageParams = { + ...initialWriteRequest.messageParams, + delegatedGrant + }; + + initialWriteRequest.granteeDid = this._delegateDid; + } - // Process the prepared initial write, with the options set for storing and/or signing as the owner. - const agentResponse = await this._agent.processDwnRequest(initialWriteRequest); + // Process the prepared initial write, with the options set for storing and/or signing as the owner. + const agentResponse = await this._agent.processDwnRequest(initialWriteRequest); - const { message, reply: { status } } = agentResponse; - const responseMessage = message; + const { message, reply: { status } } = agentResponse; + const responseMessage = message; - if (200 <= status.code && status.code <= 299) { - if (store) this._initialWriteStored = true; - if (signAsOwner) { - this._initialWriteSigned = true; - this.initialWrite.authorization = responseMessage.authorization; + if (200 <= status.code && status.code <= 299) { + if (store) this._initialWriteStored = true; + if (signAsOwner) { + this._initialWriteSigned = true; + this.initialWrite.authorization = responseMessage.authorization; + } } } } @@ -933,10 +935,7 @@ export class Record implements RecordModel { const signAsOwnerValue = signAsOwner && this._delegateDid === undefined; const signAsOwnerDelegate = signAsOwner && this._delegateDid !== undefined; - // if there is an initial write and we haven't already processed it, we first process it and marked it as such. - if (this.initialWrite && ((signAsOwner && !this._initialWriteSigned) || (store && !this._initialWriteStored))) { - await this.processInitialWrite({ store, signAsOwner }); - } + await this.processInitialWriteIfNeeded({ store, signAsOwner }); let requestOptions: ProcessDwnRequest; // Now that we've processed a potential initial write, we can process the current record state. diff --git a/packages/api/tests/dwn-api.spec.ts b/packages/api/tests/dwn-api.spec.ts index 5f7791004..08663e149 100644 --- a/packages/api/tests/dwn-api.spec.ts +++ b/packages/api/tests/dwn-api.spec.ts @@ -105,6 +105,7 @@ describe('DwnApi', () => { await delegateHarness.dwnEventLog.clear(); await delegateHarness.dwnMessageStore.clear(); await delegateHarness.dwnResumableTaskStore.clear(); + await testHarness.agent.permissions.clear(); delegateHarness.dwnStores.clear(); // avoid seeing the security warning of no password during connect diff --git a/packages/api/tests/permission-grant.spec.ts b/packages/api/tests/permission-grant.spec.ts index 8bc25ced1..cb070723b 100644 --- a/packages/api/tests/permission-grant.spec.ts +++ b/packages/api/tests/permission-grant.spec.ts @@ -56,6 +56,7 @@ describe('PermissionGrant', () => { await testHarness.dwnEventLog.clear(); await testHarness.dwnMessageStore.clear(); await testHarness.dwnResumableTaskStore.clear(); + await testHarness.agent.permissions.clear(); testHarness.dwnStores.clear(); // create a random protocol URI for each run diff --git a/packages/api/tests/permission-request.spec.ts b/packages/api/tests/permission-request.spec.ts index d7e125b0f..8cadb4c7e 100644 --- a/packages/api/tests/permission-request.spec.ts +++ b/packages/api/tests/permission-request.spec.ts @@ -52,6 +52,7 @@ describe('PermissionRequest', () => { await testHarness.dwnEventLog.clear(); await testHarness.dwnMessageStore.clear(); await testHarness.dwnResumableTaskStore.clear(); + await testHarness.agent.permissions.clear(); testHarness.dwnStores.clear(); // create a random protocol URI for each run diff --git a/packages/api/tests/protocol.spec.ts b/packages/api/tests/protocol.spec.ts index d9e8c4e80..541a77fc2 100644 --- a/packages/api/tests/protocol.spec.ts +++ b/packages/api/tests/protocol.spec.ts @@ -47,6 +47,7 @@ describe('Protocol', () => { await testHarness.dwnEventLog.clear(); await testHarness.dwnMessageStore.clear(); await testHarness.dwnResumableTaskStore.clear(); + await testHarness.agent.permissions.clear(); testHarness.dwnStores.clear(); }); diff --git a/packages/api/tests/record.spec.ts b/packages/api/tests/record.spec.ts index 97580466e..871324aff 100644 --- a/packages/api/tests/record.spec.ts +++ b/packages/api/tests/record.spec.ts @@ -75,6 +75,7 @@ describe('Record', () => { await testHarness.dwnEventLog.clear(); await testHarness.dwnMessageStore.clear(); await testHarness.dwnResumableTaskStore.clear(); + await testHarness.agent.permissions.clear(); testHarness.dwnStores.clear(); protocolDefinition = { @@ -93,7 +94,7 @@ describe('Record', () => { const { status: bobProtocolStatus, protocol: bobProtocol } = await dwnBob.protocols.configure({ message: { definition: protocolDefinition } }); expect(bobProtocolStatus.code).to.equal(202); expect(bobProtocol).to.exist; - const { status: bobProtocolSendStatus } = await bobProtocol!.send(bobDid.uri); + const { status: bobProtocolSendStatus } = await bobProtocol.send(bobDid.uri); expect(bobProtocolSendStatus.code).to.equal(202); }); @@ -130,6 +131,7 @@ describe('Record', () => { await delegateHarness.dwnEventLog.clear(); await delegateHarness.dwnMessageStore.clear(); await delegateHarness.dwnResumableTaskStore.clear(); + await testHarness.agent.permissions.clear(); delegateHarness.dwnStores.clear(); // avoid seeing the security warning of no password during connect @@ -1567,6 +1569,7 @@ describe('Record', () => { await testHarnessCarol.dwnEventLog.clear(); await testHarnessCarol.dwnMessageStore.clear(); await testHarnessCarol.dwnResumableTaskStore.clear(); + await testHarness.agent.permissions.clear(); testHarnessCarol.dwnStores.clear(); const { status: carolProtocolStatus, protocol: carolProtocol } = await dwnCarol.protocols.configure({ message: { definition: protocolDefinition } });