Skip to content

Commit

Permalink
add coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Jan 20, 2024
1 parent c4d0357 commit 20fbcb4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/utils/cid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class Cid {

const payloadBytes = codec.encode(payload);
const payloadHash = await hasher.digest(payloadBytes);

const cid = await CID.createV1(codec.code, payloadHash);
return cid.toString();
}
Expand Down
79 changes: 76 additions & 3 deletions tests/scenarios/subscriptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -940,19 +940,25 @@ export function testSubscriptionScenarios(): void {
filter : { schema: 'http://schema1' }
});

const threadSubscriptionReply = await dwn.processMessage(alice.did, anonymousSubscription.message, {
const anonymousSubscriptionReply = await dwn.processMessage(alice.did, anonymousSubscription.message, {
subscriptionHandler
});
expect(threadSubscriptionReply.status.code).to.equal(200);
expect(threadSubscriptionReply.subscription).to.exist;
expect(anonymousSubscriptionReply.status.code).to.equal(200);
expect(anonymousSubscriptionReply.subscription).to.exist;

const write1 = await TestDataGenerator.generateRecordsWrite({ author: alice, schema: 'http://schema1', published: true });
const write1Reply = await dwn.processMessage(alice.did, write1.message, { dataStream: write1.dataStream });
expect(write1Reply.status.code).to.equal(202);

const write2 = await TestDataGenerator.generateRecordsWrite({ author: alice, schema: 'http://schema1', published: true });
const write2Reply = await dwn.processMessage(alice.did, write2.message, { dataStream: write2.dataStream });
expect(write2Reply.status.code).to.equal(202);

// will not be emitted as it is not explicitly published
const writeNotPublished = await TestDataGenerator.generateRecordsWrite({ author: alice, schema: 'http://schema1' });
const writeNotPublishedReply = await dwn.processMessage(alice.did, writeNotPublished.message, { dataStream: writeNotPublished.dataStream });
expect(writeNotPublishedReply.status.code).to.equal(202);

// await for handler to receive and process the message
await Time.minimalSleep();

Expand All @@ -963,6 +969,73 @@ export function testSubscriptionScenarios(): void {
]);
});

it('allows authorized subscriptions to records intended for a recipient', async () => {
const alice = await DidKeyResolver.generate();
const bob = await DidKeyResolver.generate();
const carol = await DidKeyResolver.generate();

// bob subscribes to any messages he's authorized to see
const bobMessages:string[] = [];
const bobSubscribeHandler = async (message:GenericMessage):Promise<void> => {
bobMessages.push(await Message.getCid(message));
};

const bobSubscribe = await TestDataGenerator.generateRecordsSubscribe({
author : bob,
filter : { schema: 'http://schema1' }
});

const bobSubscribeReply = await dwn.processMessage(alice.did, bobSubscribe.message, {
subscriptionHandler: bobSubscribeHandler
});
expect(bobSubscribeReply.status.code).to.equal(200);
expect(bobSubscribeReply.subscription).to.exist;

// carol subscribes to any messages she's the recipient of.
const carolMessages:string[] = [];
const carolSubscribeHandler = async (message:GenericMessage):Promise<void> => {
carolMessages.push(await Message.getCid(message));
};

const carolSubscribe = await TestDataGenerator.generateRecordsSubscribe({
author : carol,
filter : { schema: 'http://schema1', recipient: carol.did }
});

const carolSubscribeReply = await dwn.processMessage(alice.did, carolSubscribe.message, {
subscriptionHandler: carolSubscribeHandler
});
expect(carolSubscribeReply.status.code).to.equal(200);
expect(carolSubscribeReply.subscription).to.exist;

const write1 = await TestDataGenerator.generateRecordsWrite({ author: alice, schema: 'http://schema1', recipient: bob.did });
const write1Reply = await dwn.processMessage(alice.did, write1.message, { dataStream: write1.dataStream });
expect(write1Reply.status.code).to.equal(202);

const write2 = await TestDataGenerator.generateRecordsWrite({ author: alice, schema: 'http://schema1', recipient: bob.did });
const write2Reply = await dwn.processMessage(alice.did, write2.message, { dataStream: write2.dataStream });
expect(write2Reply.status.code).to.equal(202);

// will not be emitted as it is not intended for bob
const writeForCarol = await TestDataGenerator.generateRecordsWrite({ author: alice, schema: 'http://schema1', recipient: carol.did });
const writeForCarolReply = await dwn.processMessage(alice.did, writeForCarol.message, { dataStream: writeForCarol.dataStream });
expect(writeForCarolReply.status.code).to.equal(202);

// await for handler to receive and process the message
await Time.minimalSleep();

expect(bobMessages.length).to.equal(2);
expect(bobMessages).to.have.members([
await Message.getCid(write1.message),
await Message.getCid(write2.message),
]);

expect(carolMessages.length).to.equal(1);
expect(carolMessages).to.have.members([
await Message.getCid(writeForCarol.message),
]);
});

it('filters by protocol & parentId across multiple protocolPaths', async () => {
// scenario: subscribe to multiple protocolPaths for a given protocol and parentId
// alice installs a protocol and creates a thread
Expand Down

0 comments on commit 20fbcb4

Please sign in to comment.