Skip to content

Commit

Permalink
update test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Aug 15, 2024
1 parent 8efedc8 commit 02e1266
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 104 deletions.
291 changes: 188 additions & 103 deletions tests/handlers/records-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/handlers/records-read.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
});
Expand Down

0 comments on commit 02e1266

Please sign in to comment.