Skip to content

Commit

Permalink
feat: more webhook int test (#1037)
Browse files Browse the repository at this point in the history
  • Loading branch information
bruuuuuuuce authored Nov 14, 2023
1 parent ff573ae commit fe2bb8c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
22 changes: 20 additions & 2 deletions packages/common-integration-tests/src/common-int-test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ export function testIndexName(): string {

export function testWebhook(cache?: string): Webhook {
const cacheName = cache ?? testCacheName();
const webhookName = `webhook-${cacheName}`;
return {
id: {
cacheName,
webhookName: `webhook-${cacheName}`,
webhookName,
},
destination: new PostUrlWebhookDestination('https://www.fake.url.com'),
destination: new PostUrlWebhookDestination(
`https://synthetics-webhooks.synthetics.preprod.a.momentohq.com/v1/webhooks/${webhookName}`
),
topicName: `topic-${v4()}`,
};
}
Expand Down Expand Up @@ -393,3 +396,18 @@ export function expectWithMessage(expected: () => void, message: string) {
throw new Error(message);
}
}

type WebhookRequestDetails = {
invocationCount: number;
requestId: string;
};
export async function getWebhookRequestDetails(
webhookDestination: string
): Promise<WebhookRequestDetails> {
const resp = await fetch(webhookDestination);

if (!resp.ok) {
throw new Error(`failed to get webhook details: ${await resp.text()}`);
}
return (await resp.json()) as WebhookRequestDetails;
}
38 changes: 32 additions & 6 deletions packages/common-integration-tests/src/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import {
PostUrlWebhookDestination,
ListWebhooks,
GetWebhookSecret,
TopicPublish,
} from '@gomomento/sdk-core';
import {
getWebhookRequestDetails,
ItBehavesLikeItValidatesCacheName,
ItBehavesLikeItValidatesTopicName,
testWebhook,
Expand All @@ -15,6 +17,7 @@ import {
ICacheClient,
ITopicClient,
} from '@gomomento/sdk-core/dist/src/internal/clients';
import {delay} from './auth-client';

export function runWebhookTests(
topicClient: ITopicClient,
Expand Down Expand Up @@ -77,11 +80,11 @@ export function runWebhookTests(
wh.id.cacheName === webhook.id.cacheName
);
expect(webhookWeAreLookingFor).toBeTruthy();
} else if (resp instanceof ListWebhooks.Error) {
throw new Error(`list webhooks request failed: ${resp.message()}`);
} else {
throw new Error(
`list webhooks request failed: ${(
resp as ListWebhooks.Error
).message()}`
`unknown error occured when making a 'listWebhooks' request: ${resp.toString()}`
);
}
});
Expand All @@ -97,14 +100,37 @@ export function runWebhookTests(
expect(resp.secret()).toBeTruthy();
expect(resp.webhookName()).toEqual(webhook.id.webhookName);
expect(resp.cacheName()).toEqual(webhook.id.cacheName);
} else if (resp instanceof GetWebhookSecret.Error) {
throw new Error(`getWebhookSecret request failed: ${resp.message()}`);
} else {
throw new Error(
`getWebhookSecret request failed: ${(
resp as GetWebhookSecret.Error
).message()}`
`unknown error occured when making a 'getWebhookSecret' request: ${resp.toString()}`
);
}
});
});
it('should create a new webhook, publish a message to a topic, and verify that the webhook was called', async () => {
const webhook = testWebhook(integrationTestCacheName);
await WithWebhook(topicClient, webhook, async () => {
const publishResp = await topicClient.publish(
webhook.id.cacheName,
webhook.topicName,
'a message'
);
if (publishResp instanceof TopicPublish.Error) {
throw new Error(
`failed to publish to topic: ${webhook.topicName} in cache: ${
webhook.id.cacheName
} webhook: ${
webhook.id.webhookName
} error: ${publishResp.toString()}`
);
}
// wait 5 seconds for webhook to get called. Can increase this if needed
await delay(5 * 1000);
const detes = await getWebhookRequestDetails(webhook.destination.url());
expect(detes.invocationCount).toBe(1);
});
});
});
}

0 comments on commit fe2bb8c

Please sign in to comment.