Skip to content

Commit

Permalink
Merge branch 'main' into refactor/organize-integration-test-by-service
Browse files Browse the repository at this point in the history
  • Loading branch information
malandis committed Aug 15, 2024
2 parents ac6c4fb + 5557837 commit 1538e80
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 86 deletions.
81 changes: 45 additions & 36 deletions examples/nodejs/cache/doc-example-files/doc-examples-js-apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ import {
CreateCacheResponse,
CredentialProvider,
DeleteCacheResponse,
DeleteWebhook,
DeleteWebhookResponse,
DisposableTokenScopes,
ExpiresIn,
FlushCacheResponse,
GenerateApiKey,
GenerateApiKeyResponse,
GenerateDisposableTokenResponse,
GetWebhookSecret,
GetWebhookSecretResponse,
ILeaderboard,
ItemType,
LeaderboardConfigurations,
Expand All @@ -88,12 +88,12 @@ import {
LeaderboardRemoveElementsResponse,
LeaderboardUpsertResponse,
ListCachesResponse,
ListWebhooks,
ListWebhooksResponse,
PreviewLeaderboardClient,
PutWebhook,
PutWebhookResponse,
ReadConcern,
RefreshApiKeyResponse,
RotateWebhookSecret,
RotateWebhookSecretResponse,
TokenScopes,
TopicClient,
TopicConfigurations,
Expand Down Expand Up @@ -1483,24 +1483,27 @@ async function example_API_TopicSubscribe(topicClient: TopicClient, cacheName: s

async function example_API_ListWebhooks(topicClient: TopicClient, cacheName: string) {
const result = await topicClient.listWebhooks(cacheName);
if (result instanceof ListWebhooks.Success) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
console.log(`listed webhooks: ${result.getWebhooks()}`);
} else if (result instanceof ListWebhooks.Error) {
throw new Error(
`An error occurred while attempting to list webhooks for cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
switch (result.type) {
case ListWebhooksResponse.Success:
console.log(`Listed webhooks for cache '${cacheName}': ${result.getWebhooks()}`);
break;
case ListWebhooksResponse.Error:
throw new Error(
`An error occurred while attempting to list webhooks for cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
}

async function example_API_DeleteWebhook(topicClient: TopicClient, cacheName: string) {
const result = await topicClient.deleteWebhook(cacheName, 'examples webhook');
if (result instanceof DeleteWebhook.Success) {
console.log('successfully deleted webhook');
} else if (result instanceof DeleteWebhook.Error) {
throw new Error(
`An error occurred while attempting to delete webhook 'a webhook' inside of cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
switch (result.type) {
case DeleteWebhookResponse.Success:
console.log('Successfully deleted webhook');
break;
case DeleteWebhookResponse.Error:
throw new Error(
`An error occurred while attempting to delete webhook 'examples webhook' inside of cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
}

Expand All @@ -1509,34 +1512,40 @@ async function example_API_PutWebhook(topicClient: TopicClient, cacheName: strin
topicName: 'a topic',
destination: 'https://www.thisisawebhookurl.com/v1/webhook',
});
if (result instanceof PutWebhook.Success) {
console.log('successfully created webhook');
} else if (result instanceof PutWebhook.Error) {
throw new Error(
`An error occurred while attempting to create a webhook 'examples webhook' inside of cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
switch (result.type) {
case PutWebhookResponse.Success:
console.log('Successfully put webhook');
break;
case PutWebhookResponse.Error:
throw new Error(
`An error occurred while attempting to put the webhook 'examples webhook' inside of cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
}

async function example_API_RotateWebhookSecret(topicClient: TopicClient, cacheName: string) {
const result = await topicClient.rotateWebhookSecret(cacheName, 'examples webhook');
if (result instanceof RotateWebhookSecret.Success) {
console.log('successfully rotated the webhook secret');
} else if (result instanceof RotateWebhookSecret.Error) {
throw new Error(
`An error occurred while attempting to rotate the secret for the webhook 'examples webhook' inside of cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
switch (result.type) {
case RotateWebhookSecretResponse.Success:
console.log('Successfully rotated the webhook secret');
break;
case RotateWebhookSecretResponse.Error:
throw new Error(
`An error occurred while attempting to rotate the secret for the webhook 'examples webhook' inside of cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
}

async function example_API_GetWebhookSecret(topicClient: TopicClient, cacheName: string) {
const result = await topicClient.getWebhookSecret(cacheName, 'examples webhook');
if (result instanceof GetWebhookSecret.Success) {
console.log('successfully retrieved the webhook secret');
} else if (result instanceof GetWebhookSecret.Error) {
throw new Error(
`An error occurred while attempting to fetch the secret for the webhook 'examples webhook' inside of cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
switch (result.type) {
case GetWebhookSecretResponse.Success:
console.log(`Successfully retrieved the webhook secret: ${result.secret()}`);
break;
case GetWebhookSecretResponse.Error:
throw new Error(
`An error occurred while attempting to get the secret for the webhook 'examples webhook' inside of cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
}

Expand Down
52 changes: 30 additions & 22 deletions examples/nodejs/cache/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 37 additions & 28 deletions packages/common-integration-tests/src/topics/webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
PostUrlWebhookDestination,
ListWebhooks,
GetWebhookSecret,
TopicPublish,
RotateWebhookSecret,
ListWebhooksResponse,
GetWebhookSecretResponse,
TopicPublishResponse,
RotateWebhookSecretResponse,
} from '@gomomento/sdk-core';
import {
getWebhookRequestDetails,
Expand Down Expand Up @@ -73,17 +73,20 @@ export function runWebhookTests(
const webhook = testWebhook(integrationTestCacheName);
await WithWebhook(topicClient, webhook, async () => {
const resp = await topicClient.listWebhooks(integrationTestCacheName);
if (resp instanceof ListWebhooks.Success) {
const webhookWeAreLookingFor = resp
.getWebhooks()
.find(
wh =>
wh.id.webhookName === webhook.id.webhookName &&
wh.id.cacheName === webhook.id.cacheName
);
expect(webhookWeAreLookingFor).toBeTruthy();
} else if (resp instanceof ListWebhooks.Error) {
throw new Error(`list webhooks request failed: ${resp.message()}`);
switch (resp.type) {
case ListWebhooksResponse.Success: {
const webhookWeAreLookingFor = resp
.getWebhooks()
.find(
wh =>
wh.id.webhookName === webhook.id.webhookName &&
wh.id.cacheName === webhook.id.cacheName
);
expect(webhookWeAreLookingFor).toBeTruthy();
break;
}
case ListWebhooksResponse.Error:
throw new Error(`list webhooks request failed: ${resp.message()}`);
}
});
});
Expand All @@ -94,12 +97,16 @@ export function runWebhookTests(
webhook.id.cacheName,
webhook.id.webhookName
);
if (resp instanceof GetWebhookSecret.Success) {
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()}`);
switch (resp.type) {
case GetWebhookSecretResponse.Success:
expect(resp.secret()).toBeTruthy();
expect(resp.webhookName()).toEqual(webhook.id.webhookName);
expect(resp.cacheName()).toEqual(webhook.id.cacheName);
break;
case GetWebhookSecretResponse.Error:
throw new Error(
`getWebhookSecret request failed: ${resp.message()}`
);
}
});
});
Expand All @@ -111,7 +118,7 @@ export function runWebhookTests(
webhook.topicName,
'a message'
);
if (publishResp instanceof TopicPublish.Error) {
if (publishResp.type === TopicPublishResponse.Error) {
throw new Error(
`failed to publish to topic: ${webhook.topicName} in cache: ${
webhook.id.cacheName
Expand All @@ -122,8 +129,10 @@ export function runWebhookTests(
}
// 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);
const details = await getWebhookRequestDetails(
webhook.destination.url()
);
expect(details.invocationCount).toBe(1);
});
});
itOnlyInCi('should rotate a webhook secret', async () => {
Expand All @@ -133,19 +142,19 @@ export function runWebhookTests(
webhook.id.cacheName,
webhook.id.webhookName
);
if (!(getSecretResp instanceof GetWebhookSecret.Success)) {
if (!(getSecretResp.type === GetWebhookSecretResponse.Success)) {
throw new Error(
`unknown error occured when making a 'getWebhookSecret' request: ${getSecretResp.toString()}`
`unknown error occurred when making a 'getWebhookSecret' request: ${getSecretResp.toString()}`
);
}
expect(getSecretResp.secret()).toBeTruthy();
const rotateResp = await topicClient.rotateWebhookSecret(
webhook.id.cacheName,
webhook.id.webhookName
);
if (!(rotateResp instanceof RotateWebhookSecret.Success)) {
if (!(rotateResp.type === RotateWebhookSecretResponse.Success)) {
throw new Error(
`unknown error occured when making a 'rotateWebhookSecret' request: ${rotateResp.toString()}`
`unknown error occurred when making a 'rotateWebhookSecret' request: ${rotateResp.toString()}`
);
}
expect(rotateResp.secret()).toBeTruthy();
Expand Down

0 comments on commit 1538e80

Please sign in to comment.