Skip to content

Commit

Permalink
fix: tests for webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
eleanorreem committed May 16, 2024
1 parent 476f2fa commit 4aad887
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/webhooks/webhooks.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ import { WebhooksService } from './webhooks.service';
const webhookSecret = process.env.STORYBLOK_WEBHOOK_SECRET;

const getWebhookSignature = (body) => {
return createHmac('sha1', webhookSecret).update(JSON.stringify(body)).digest('hex');
return createHmac('sha1', webhookSecret).update(""+body).digest('hex');
};
const createRequestObject = (body) => {
return {
rawBody: "" + body,
setEncoding: ()=>{},
encoding: "utf8"
}}

// Difficult to mock classes as well as node modules.
// This seemed the best approach
Expand Down Expand Up @@ -228,7 +234,7 @@ describe('WebhooksService', () => {
text: '',
};

return expect(service.updateStory(body, getWebhookSignature(body))).rejects.toThrow(
return expect(service.updateStory(createRequestObject(body), body, getWebhookSignature(body))).rejects.toThrow(
'STORYBLOK STORY NOT FOUND',
);
});
Expand All @@ -241,6 +247,7 @@ describe('WebhooksService', () => {
};

const deletedStory = (await service.updateStory(
createRequestObject(body),
body,
getWebhookSignature(body),
)) as SessionEntity;
Expand All @@ -256,6 +263,7 @@ describe('WebhooksService', () => {
};

const unpublished = (await service.updateStory(
createRequestObject(body),
body,
getWebhookSignature(body),
)) as SessionEntity;
Expand Down Expand Up @@ -306,7 +314,7 @@ describe('WebhooksService', () => {
text: '',
};

const session = (await service.updateStory(body, getWebhookSignature(body))) as SessionEntity;
const session = (await service.updateStory(createRequestObject(body), body, getWebhookSignature(body))) as SessionEntity;

expect(courseFindOneSpy).toHaveBeenCalledWith({
storyblokUuid: 'anotherCourseUuId',
Expand Down Expand Up @@ -349,7 +357,7 @@ describe('WebhooksService', () => {
text: '',
};

const session = (await service.updateStory(body, getWebhookSignature(body))) as SessionEntity;
const session = (await service.updateStory(createRequestObject(body), body, getWebhookSignature(body))) as SessionEntity;

expect(session).toEqual(mockSession);
expect(courseFindOneSpy).toHaveBeenCalledWith({
Expand Down Expand Up @@ -408,7 +416,7 @@ describe('WebhooksService', () => {
text: '',
};

const session = (await service.updateStory(body, getWebhookSignature(body))) as SessionEntity;
const session = (await service.updateStory(createRequestObject(body), body, getWebhookSignature(body))) as SessionEntity;

expect(session).toEqual(mockSession);
expect(sessionSaveRepoSpy).toHaveBeenCalledWith({
Expand Down Expand Up @@ -442,7 +450,7 @@ describe('WebhooksService', () => {
text: '',
};

const course = (await service.updateStory(body, getWebhookSignature(body))) as CourseEntity;
const course = (await service.updateStory(createRequestObject(body), body, getWebhookSignature(body))) as CourseEntity;

expect(course).toEqual(mockCourse);
expect(courseFindOneRepoSpy).toHaveBeenCalledWith({
Expand Down

0 comments on commit 4aad887

Please sign in to comment.