Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Log translated webhook topic when not found
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomarg committed Dec 14, 2022
1 parent 532978b commit eb676b2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## Unreleased

- [Patch] Validate content of host parameter using sanitizeShop regex [#634](https://github.com/Shopify/shopify-api-js/pull/634)
- [Patch] Use the GraphQL format of webhook topics in the error message [#626](https://github.com/Shopify/shopify-api-js/pull/626)

## [6.0.1] - 2022-12-08

Expand Down
31 changes: 21 additions & 10 deletions lib/webhooks/__tests__/process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ import {shopify} from '../../__tests__/test-helper';
import {HTTP_HANDLER} from './handlers';
import {getTestExpressApp, headers, hmac} from './utils';

interface TestResponseInterface {
errorThrown: boolean;
error?: Error;
message?: string;
}

describe('shopify.webhooks.process', () => {
const rawBody = '{"foo": "bar"}';

const app = getTestExpressApp();
app.post('/webhooks', async (req, res) => {
let errorThrown = false;
const data: TestResponseInterface = {
errorThrown: false,
};
let statusCode = StatusCode.Ok;
try {
await shopify.webhooks.process({
Expand All @@ -21,11 +29,13 @@ describe('shopify.webhooks.process', () => {
rawResponse: res,
});
} catch (error) {
errorThrown = true;
data.errorThrown = true;
data.error = error;
data.message = error.message;
expect(error).toBeInstanceOf(InvalidWebhookError);
statusCode = error.response.statusCode;
}
res.status(statusCode).json({errorThrown});
res.status(statusCode).json({data});
});

let blockingWebhookHandlerCalled: boolean;
Expand Down Expand Up @@ -64,7 +74,7 @@ describe('shopify.webhooks.process', () => {
.send(rawBody)
.expect(StatusCode.Ok);

expect(response.body.errorThrown).toBeFalsy();
expect(response.body.data.errorThrown).toBeFalsy();
expect(blockingWebhookHandlerCalled).toBeTruthy();
});

Expand All @@ -83,7 +93,7 @@ describe('shopify.webhooks.process', () => {
.send(rawBody)
.expect(StatusCode.Ok);

expect(response.body.errorThrown).toBeFalsy();
expect(response.body.data.errorThrown).toBeFalsy();
expect(blockingWebhookHandlerCalled).toBeTruthy();
});

Expand All @@ -97,7 +107,8 @@ describe('shopify.webhooks.process', () => {
.send(rawBody)
.expect(StatusCode.NotFound);

expect(response.body.errorThrown).toBeTruthy();
expect(response.body.data.errorThrown).toBeTruthy();
expect(response.body.data.message).toContain('PRODUCTS_CREATE');
expect(blockingWebhookHandlerCalled).toBeFalsy();
});

Expand All @@ -111,7 +122,7 @@ describe('shopify.webhooks.process', () => {
.send(rawBody)
.expect(StatusCode.Unauthorized);

expect(response.body.errorThrown).toBeTruthy();
expect(response.body.data.errorThrown).toBeTruthy();
expect(blockingWebhookHandlerCalled).toBeFalsy();
});

Expand All @@ -124,7 +135,7 @@ describe('shopify.webhooks.process', () => {
.set(headers())
.expect(StatusCode.BadRequest);

expect(response.body.errorThrown).toBeTruthy();
expect(response.body.data.errorThrown).toBeTruthy();
expect(blockingWebhookHandlerCalled).toBeFalsy();
});

Expand All @@ -147,7 +158,7 @@ describe('shopify.webhooks.process', () => {
.send(rawBody)
.expect(StatusCode.BadRequest);

expect(response.body.errorThrown).toBeTruthy();
expect(response.body.data.errorThrown).toBeTruthy();
expect(blockingWebhookHandlerCalled).toBeFalsy();
}
});
Expand All @@ -169,6 +180,6 @@ describe('shopify.webhooks.process', () => {
.send(rawBody)
.expect(StatusCode.InternalServerError);

expect(response.body.errorThrown).toBeTruthy();
expect(response.body.data.errorThrown).toBeTruthy();
});
});
2 changes: 1 addition & 1 deletion lib/webhooks/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function headers({
apiVersion = '2023-01',
domain = 'shop1.myshopify.io',
hmac = 'fake',
topic = 'PRODUCTS_CREATE',
topic = 'products/create',
webhookId = '123456789',
lowercase = false,
}: {
Expand Down
2 changes: 1 addition & 1 deletion lib/webhooks/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function process(
log.debug('No HTTP handlers found', loggingContext);

response.statusCode = StatusCode.NotFound;
errorMessage = `No HTTP webhooks registered for topic ${topic}`;
errorMessage = `No HTTP webhooks registered for topic ${graphqlTopic}`;
}
} else {
log.debug('Webhook validation failed', loggingContext);
Expand Down

0 comments on commit eb676b2

Please sign in to comment.