diff --git a/tests/handlers/records-query.spec.ts b/tests/handlers/records-query.spec.ts index 6584260f0..8eb094ad8 100644 --- a/tests/handlers/records-query.spec.ts +++ b/tests/handlers/records-query.spec.ts @@ -405,6 +405,194 @@ export function testRecordsQueryHandler(): void { ]); }); + it('should be able to query by recipient', async () => { + // scenario alice authors records for bob and carol into alice's DWN. + // bob and carol are able to filter for records for them. + const alice = await TestDataGenerator.generateDidKeyPersona(); + const bob = await TestDataGenerator.generateDidKeyPersona(); + const carol = await TestDataGenerator.generateDidKeyPersona(); + + const protocolDefinition = freeForAll; + + const protocolsConfig = await TestDataGenerator.generateProtocolsConfigure({ + author: alice, + protocolDefinition + }); + const protocolsConfigureReply = await dwn.processMessage(alice.did, protocolsConfig.message); + expect(protocolsConfigureReply.status.code).to.equal(202); + + const aliceToBob = await TestDataGenerator.generateRecordsWrite({ + author : alice, + recipient : bob.did, + protocol : protocolDefinition.protocol, + schema : protocolDefinition.types.post.schema, + dataFormat : protocolDefinition.types.post.dataFormats[0], + protocolPath : 'post' + }); + const aliceToBobReply = await dwn.processMessage(alice.did, aliceToBob.message, { dataStream: aliceToBob.dataStream }); + expect(aliceToBobReply.status.code).to.equal(202); + + const aliceToCarol = await TestDataGenerator.generateRecordsWrite({ + author : alice, + recipient : carol.did, + protocol : protocolDefinition.protocol, + schema : protocolDefinition.types.post.schema, + dataFormat : protocolDefinition.types.post.dataFormats[0], + protocolPath : 'post' + }); + const aliceToCarolReply = await dwn.processMessage(alice.did, aliceToCarol.message, { dataStream: aliceToCarol.dataStream }); + expect(aliceToCarolReply.status.code).to.equal(202); + + // alice queries with an empty filter, gets both + let recordsQuery = await TestDataGenerator.generateRecordsQuery({ + author : alice, + filter : { + protocol : protocolDefinition.protocol, + schema : protocolDefinition.types.post.schema, + dataFormat : protocolDefinition.types.post.dataFormats[0], + protocolPath : 'post' + } + }); + let queryReply = await dwn.processMessage(alice.did, recordsQuery.message); + expect(queryReply.status.code).to.equal(200); + expect(queryReply.entries?.length).to.equal(2); + + // filter for bob as recipient + recordsQuery = await TestDataGenerator.generateRecordsQuery({ + author : alice, + filter : { + recipient : bob.did, + protocol : protocolDefinition.protocol, + schema : protocolDefinition.types.post.schema, + dataFormat : protocolDefinition.types.post.dataFormats[0], + protocolPath : 'post' + } + }); + queryReply = await dwn.processMessage(alice.did, recordsQuery.message); + expect(queryReply.status.code).to.equal(200); + expect(queryReply.entries?.length).to.equal(1); + expect(queryReply.entries![0].recordId).to.equal(aliceToBob.message.recordId); + + // filter for carol as recipient + recordsQuery = await TestDataGenerator.generateRecordsQuery({ + author : alice, + filter : { + recipient : carol.did, + protocol : protocolDefinition.protocol, + schema : protocolDefinition.types.post.schema, + dataFormat : protocolDefinition.types.post.dataFormats[0], + protocolPath : 'post' + } + }); + queryReply = await dwn.processMessage(alice.did, recordsQuery.message); + expect(queryReply.status.code).to.equal(200); + expect(queryReply.entries?.length).to.equal(1); + expect(queryReply.entries![0].recordId).to.equal(aliceToCarol.message.recordId); + }); + + it('should be able to query by multiple recipients', async () => { + // scenario: alice, bob and carol author records for various recipients into alice's DWN. + // alice is able to filter based on multiple recipients + + const alice = await TestDataGenerator.generateDidKeyPersona(); + const bob = await TestDataGenerator.generateDidKeyPersona(); + const carol = await TestDataGenerator.generateDidKeyPersona(); + + const protocolDefinition = freeForAll; + + const protocolsConfig = await TestDataGenerator.generateProtocolsConfigure({ + author: alice, + protocolDefinition + }); + const protocolsConfigureReply = await dwn.processMessage(alice.did, protocolsConfig.message); + expect(protocolsConfigureReply.status.code).to.equal(202); + + const bobToAliceWrite = await TestDataGenerator.generateRecordsWrite({ + author : bob, + recipient : alice.did, + protocol : protocolDefinition.protocol, + schema : protocolDefinition.types.post.schema, + dataFormat : protocolDefinition.types.post.dataFormats[0], + protocolPath : 'post' + }); + const bobToAliceReply = await dwn.processMessage(alice.did, bobToAliceWrite.message, { dataStream: bobToAliceWrite.dataStream }); + expect(bobToAliceReply.status.code).to.equal(202); + + const aliceToBobWrite = await TestDataGenerator.generateRecordsWrite({ + author : alice, + recipient : bob.did, + protocol : protocolDefinition.protocol, + schema : protocolDefinition.types.post.schema, + dataFormat : protocolDefinition.types.post.dataFormats[0], + protocolPath : 'post' + }); + const aliceToBobReply = await dwn.processMessage(alice.did, aliceToBobWrite.message, { dataStream: aliceToBobWrite.dataStream }); + expect(aliceToBobReply.status.code).to.equal(202); + + const carolToBobWrite = await TestDataGenerator.generateRecordsWrite({ + author : carol, + recipient : bob.did, + protocol : protocolDefinition.protocol, + schema : protocolDefinition.types.post.schema, + dataFormat : protocolDefinition.types.post.dataFormats[0], + protocolPath : 'post' + }); + const carolToBobReply = await dwn.processMessage(alice.did, carolToBobWrite.message, { dataStream: carolToBobWrite.dataStream }); + expect(carolToBobReply.status.code).to.equal(202); + + const aliceToCarolWrite = await TestDataGenerator.generateRecordsWrite({ + author : alice, + recipient : carol.did, + protocol : protocolDefinition.protocol, + schema : protocolDefinition.types.post.schema, + dataFormat : protocolDefinition.types.post.dataFormats[0], + protocolPath : 'post' + }); + const aliceToCarolReply = await dwn.processMessage(alice.did, aliceToCarolWrite.message, { dataStream: aliceToCarolWrite.dataStream }); + expect(aliceToCarolReply.status.code).to.equal(202); + + // alice queries with an empty array, gets all + let recordsQuery = await TestDataGenerator.generateRecordsQuery({ + author : alice, + filter : { + protocol : protocolDefinition.protocol, + schema : protocolDefinition.types.post.schema, + dataFormat : protocolDefinition.types.post.dataFormats[0], + protocolPath : 'post', + recipient : [] + } + }); + let queryReply = await dwn.processMessage(alice.did, recordsQuery.message); + expect(queryReply.status.code).to.equal(200); + expect(queryReply.entries?.length).to.equal(4); + expect(queryReply.entries?.map(e => e.recordId)).to.have.members([ + bobToAliceWrite.message.recordId, + aliceToBobWrite.message.recordId, + carolToBobWrite.message.recordId, + aliceToCarolWrite.message.recordId + ]); + + // filter for alice and bob as authors + recordsQuery = await TestDataGenerator.generateRecordsQuery({ + author : alice, + filter : { + recipient : [alice.did, bob.did], + protocol : protocolDefinition.protocol, + schema : protocolDefinition.types.post.schema, + dataFormat : protocolDefinition.types.post.dataFormats[0], + protocolPath : 'post' + } + }); + queryReply = await dwn.processMessage(alice.did, recordsQuery.message); + expect(queryReply.status.code).to.equal(200); + expect(queryReply.entries?.length).to.equal(3); + expect(queryReply.entries?.map(e => e.recordId)).to.have.members([ + bobToAliceWrite.message.recordId, + aliceToBobWrite.message.recordId, + carolToBobWrite.message.recordId + ]); + }); + it('should be able to query for published records', async () => { const alice = await TestDataGenerator.generateDidKeyPersona(); const bob = await TestDataGenerator.generateDidKeyPersona(); @@ -1670,109 +1858,6 @@ export function testRecordsQueryHandler(): void { expect(replyToBobCarolUnpublishedQuery.entries?.length).to.equal(0); }); - it('should be able to query by multiple recipients', async () => { - // scenario: alice, bob and carol author records for various recipients into alice's DWN. - // alice is able to filter based on multiple recipients - - const alice = await TestDataGenerator.generateDidKeyPersona(); - const bob = await TestDataGenerator.generateDidKeyPersona(); - const carol = await TestDataGenerator.generateDidKeyPersona(); - - const protocolDefinition = freeForAll; - - const protocolsConfig = await TestDataGenerator.generateProtocolsConfigure({ - author: alice, - protocolDefinition - }); - const protocolsConfigureReply = await dwn.processMessage(alice.did, protocolsConfig.message); - expect(protocolsConfigureReply.status.code).to.equal(202); - - const bobToAliceWrite = await TestDataGenerator.generateRecordsWrite({ - author : bob, - recipient : alice.did, - protocol : protocolDefinition.protocol, - schema : protocolDefinition.types.post.schema, - dataFormat : protocolDefinition.types.post.dataFormats[0], - protocolPath : 'post' - }); - const bobToAliceReply = await dwn.processMessage(alice.did, bobToAliceWrite.message, { dataStream: bobToAliceWrite.dataStream }); - expect(bobToAliceReply.status.code).to.equal(202); - - const aliceToBobWrite = await TestDataGenerator.generateRecordsWrite({ - author : alice, - recipient : bob.did, - protocol : protocolDefinition.protocol, - schema : protocolDefinition.types.post.schema, - dataFormat : protocolDefinition.types.post.dataFormats[0], - protocolPath : 'post' - }); - const aliceToBobReply = await dwn.processMessage(alice.did, aliceToBobWrite.message, { dataStream: aliceToBobWrite.dataStream }); - expect(aliceToBobReply.status.code).to.equal(202); - - const carolToBobWrite = await TestDataGenerator.generateRecordsWrite({ - author : carol, - recipient : bob.did, - protocol : protocolDefinition.protocol, - schema : protocolDefinition.types.post.schema, - dataFormat : protocolDefinition.types.post.dataFormats[0], - protocolPath : 'post' - }); - const carolToBobReply = await dwn.processMessage(alice.did, carolToBobWrite.message, { dataStream: carolToBobWrite.dataStream }); - expect(carolToBobReply.status.code).to.equal(202); - - const aliceToCarolWrite = await TestDataGenerator.generateRecordsWrite({ - author : alice, - recipient : carol.did, - protocol : protocolDefinition.protocol, - schema : protocolDefinition.types.post.schema, - dataFormat : protocolDefinition.types.post.dataFormats[0], - protocolPath : 'post' - }); - const aliceToCarolReply = await dwn.processMessage(alice.did, aliceToCarolWrite.message, { dataStream: aliceToCarolWrite.dataStream }); - expect(aliceToCarolReply.status.code).to.equal(202); - - // alice queries with an empty array, gets all - let recordsQuery = await TestDataGenerator.generateRecordsQuery({ - author : alice, - filter : { - protocol : protocolDefinition.protocol, - schema : protocolDefinition.types.post.schema, - dataFormat : protocolDefinition.types.post.dataFormats[0], - protocolPath : 'post', - recipient : [] - } - }); - let queryReply = await dwn.processMessage(alice.did, recordsQuery.message); - expect(queryReply.status.code).to.equal(200); - expect(queryReply.entries?.length).to.equal(4); - expect(queryReply.entries?.map(e => e.recordId)).to.have.members([ - bobToAliceWrite.message.recordId, - aliceToBobWrite.message.recordId, - carolToBobWrite.message.recordId, - aliceToCarolWrite.message.recordId - ]); - - // filter for alice and bob as authors - recordsQuery = await TestDataGenerator.generateRecordsQuery({ - author : alice, - filter : { - recipient : [alice.did, bob.did], - protocol : protocolDefinition.protocol, - schema : protocolDefinition.types.post.schema, - dataFormat : protocolDefinition.types.post.dataFormats[0], - protocolPath : 'post' - } - }); - queryReply = await dwn.processMessage(alice.did, recordsQuery.message); - expect(queryReply.status.code).to.equal(200); - expect(queryReply.entries?.length).to.equal(3); - expect(queryReply.entries?.map(e => e.recordId)).to.have.members([ - bobToAliceWrite.message.recordId, - aliceToBobWrite.message.recordId, - carolToBobWrite.message.recordId - ]); - }); - it('should paginate correctly for fetchRecordsAsNonOwner()', async () => { const alice = await TestDataGenerator.generateDidKeyPersona(); const bob = await TestDataGenerator.generateDidKeyPersona(); diff --git a/tests/handlers/records-read.spec.ts b/tests/handlers/records-read.spec.ts index c384a74c7..7bddd5f98 100644 --- a/tests/handlers/records-read.spec.ts +++ b/tests/handlers/records-read.spec.ts @@ -690,7 +690,7 @@ export function testRecordsReadHandler(): void { signer : Jws.createSigner(bob), filter : { protocolPath : 'thread/participant', - recipient : [ bob.did ], + recipient : bob.did, contextId : threadRecord.message.contextId }, });