Skip to content

Commit

Permalink
Merge pull request #43 from xmtp/nm/add-more-unit-tests
Browse files Browse the repository at this point in the history
Add unit test
  • Loading branch information
neekolas authored Apr 17, 2024
2 parents 6c5e296 + e364476 commit 657b1f1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
Binary file modified integration/bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"@connectrpc/connect": "^1.4.0",
"@connectrpc/connect-web": "^1.4.0",
"@xmtp/xmtp-js": "11.4.0-beta.1",
"viem": "^2.7.16"
"@xmtp/xmtp-js": "11.5.0",
"viem": "^2.7.15"
}
}
62 changes: 60 additions & 2 deletions integration/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { serve } from "bun";
import { expect, test, beforeEach, afterAll, describe } from "bun:test";
import { createNotificationClient, randomClient } from ".";
import { buildUserInviteTopic } from "@xmtp/xmtp-js";
import { buildUserInviteTopic, fromNanoString } from "@xmtp/xmtp-js";
import type { NotificationResponse } from "./types";
import { fetcher } from "@xmtp/proto";
const { b64Decode } = fetcher;

const PORT = 7777;

Expand Down Expand Up @@ -45,7 +47,6 @@ describe("notifications", () => {
},
},
});
console.log("Installation registered");
const alixInviteTopic = buildUserInviteTopic(alix.address);
await alixNotificationClient.subscribeWithMetadata({
installationId: alix.address,
Expand All @@ -68,4 +69,61 @@ describe("notifications", () => {
expect(notification.installation.delivery_mechanism.token).toEqual("token");
expect(notification.message_context.message_type).toEqual("v2-invite");
});

test("hmac keys", async () => {
const alix = await randomClient();
const bo = await randomClient();
const alixNotificationClient = createNotificationClient();
await alixNotificationClient.registerInstallation({
installationId: alix.address,
deliveryMechanism: {
deliveryMechanismType: {
value: "token",
case: "apnsDeviceToken",
},
},
});
const conversation = await alix.conversations.newConversation(bo.address);
const hmacKeys = await alix.keystore.getV2ConversationHmacKeys({
topics: [conversation.topic],
});
const matchingKeys = hmacKeys.hmacKeys[conversation.topic].values.map(
(v) => ({
thirtyDayPeriodsSinceEpoch: v.thirtyDayPeriodsSinceEpoch,
key: v.hmacKey,
})
);
await alixNotificationClient.subscribeWithMetadata({
installationId: alix.address,
subscriptions: [
{
topic: conversation.topic,
isSilent: false,
hmacKeys: matchingKeys,
},
],
});

const notificationPromise = waitForNextRequest(10000);
await conversation.send("This should never be delivered");
const boConversation = await bo.conversations.newConversation(alix.address);
const boMessage = await boConversation.send("This should be delivered");
expect(boConversation.topic).toEqual(conversation.topic);

const notification = await notificationPromise;

expect(notification.idempotency_key).toBeString();
expect(notification.message.content_topic).toEqual(conversation.topic);
expect(notification.message.message).toBeString();
expect(notification.subscription.is_silent).toBeFalse();
expect(notification.installation.delivery_mechanism.token).toEqual("token");

const decryptedMessage = await boConversation.decodeMessage({
timestampNs: notification.message.timestamp_ns.toString(),
message: b64Decode(notification.message.message),
contentTopic: notification.message.content_topic,
});

expect(decryptedMessage.content).toEqual("This should be delivered");
});
});
4 changes: 2 additions & 2 deletions pkg/xmtp/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ func (l *Listener) processEnvelope(env *v1.Envelope) error {
func (l *Listener) shouldDeliver(messageContext interfaces.MessageContext, subscription interfaces.Subscription) bool {
if subscription.HmacKey != nil && len(subscription.HmacKey.Key) > 0 {
isSender := messageContext.IsSender(subscription.HmacKey.Key)
if (isSender) {
return !isSender
if isSender {
return false
}
}
if messageContext.ShouldPush != nil {
Expand Down

0 comments on commit 657b1f1

Please sign in to comment.