From 3c7271ab7660e923a53a8be763421b5c569ebffc Mon Sep 17 00:00:00 2001 From: Naz Date: Mon, 30 May 2022 15:53:43 +0800 Subject: [PATCH] Fixed post.published event test suite refs https://github.com/TryGhost/Toolbox/issues/320 - Updated the test suite to use updated webhookMockReceiver.mock method and corrected previously incorrect snapshot --- .../__snapshots__/posts.test.js.snap | 38 ++++++++--------- test/e2e-webhooks/posts.test.js | 42 +++++++++++++++++-- 2 files changed, 57 insertions(+), 23 deletions(-) diff --git a/test/e2e-webhooks/__snapshots__/posts.test.js.snap b/test/e2e-webhooks/__snapshots__/posts.test.js.snap index 856461fcb7..17ed416ed4 100644 --- a/test/e2e-webhooks/__snapshots__/posts.test.js.snap +++ b/test/e2e-webhooks/__snapshots__/posts.test.js.snap @@ -7,8 +7,8 @@ Object { "canonical_url": null, "codeinjection_foot": null, "codeinjection_head": null, - "comment_id": "62905373e751ff5d4a98db0f", - "created_at": "2022-05-27T04:28:35.000Z", + "comment_id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, "custom_excerpt": null, "custom_template": null, "email_only": false, @@ -21,7 +21,7 @@ Object { "featured": false, "frontmatter": null, "html": null, - "id": "62905373e751ff5d4a98db0f", + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, "meta_description": null, "meta_title": null, "mobiledoc": "{\\"version\\":\\"0.3.1\\",\\"ghostVersion\\":\\"4.0\\",\\"markups\\":[],\\"atoms\\":[],\\"cards\\":[],\\"sections\\":[[1,\\"p\\",[[0,[],0,\\"\\"]]]]}", @@ -30,35 +30,35 @@ Object { "og_title": null, "plaintext": null, "primary_tag": null, - "published_at": "2022-05-27T04:28:40.000Z", + "published_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, "slug": "webhookz", "status": "published", "tags": Array [], "tiers": Array [ Object { "active": true, - "created_at": "2022-05-27T04:28:30.000Z", + "created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, "description": null, - "id": "6290536ee751ff5d4a98d92a", + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, "monthly_price_id": null, "name": "Default Product", "slug": "default-product", "type": "paid", - "updated_at": "2022-05-27T04:28:30.000Z", + "updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, "visibility": "public", "welcome_page_url": null, "yearly_price_id": null, }, Object { "active": true, - "created_at": "2022-05-27T04:28:30.000Z", + "created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, "description": null, - "id": "6290536ee751ff5d4a98d92b", + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, "monthly_price_id": null, "name": "Free", "slug": "free", "type": "free", - "updated_at": "2022-05-27T04:28:30.000Z", + "updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, "visibility": "public", "welcome_page_url": null, "yearly_price_id": null, @@ -68,9 +68,9 @@ Object { "twitter_description": null, "twitter_image": null, "twitter_title": null, - "updated_at": "2022-05-27T04:28:40.000Z", + "updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, "url": "http://127.0.0.1:2369/404/", - "uuid": "e3e0900e-2d00-463f-b83f-c8fa44bea3ae", + "uuid": StringMatching /\\[a-f0-9\\]\\{8\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{12\\}/, "visibility": "public", }, "previous": Object { @@ -79,34 +79,34 @@ Object { "tiers": Array [ Object { "active": true, - "created_at": "2022-05-27T04:28:30.000Z", + "created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, "description": null, - "id": "6290536ee751ff5d4a98d92a", + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, "monthly_price_id": null, "name": "Default Product", "slug": "default-product", "type": "paid", - "updated_at": "2022-05-27T04:28:30.000Z", + "updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, "visibility": "public", "welcome_page_url": null, "yearly_price_id": null, }, Object { "active": true, - "created_at": "2022-05-27T04:28:30.000Z", + "created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, "description": null, - "id": "6290536ee751ff5d4a98d92b", + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, "monthly_price_id": null, "name": "Free", "slug": "free", "type": "free", - "updated_at": "2022-05-27T04:28:30.000Z", + "updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, "visibility": "public", "welcome_page_url": null, "yearly_price_id": null, }, ], - "updated_at": "2022-05-27T04:28:35.000Z", + "updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, }, }, } diff --git a/test/e2e-webhooks/posts.test.js b/test/e2e-webhooks/posts.test.js index 6cd0a6aa67..74fc8e73ef 100644 --- a/test/e2e-webhooks/posts.test.js +++ b/test/e2e-webhooks/posts.test.js @@ -1,4 +1,30 @@ -const {agentProvider, mockManager, fixtureManager} = require('../utils/e2e-framework'); +const {agentProvider, mockManager, fixtureManager, matchers} = require('../utils/e2e-framework'); +const {anyObjectId, anyISODateTime, anyUuid} = matchers; + +const tierSnapshot = { + id: anyObjectId, + created_at: anyISODateTime, + updated_at: anyISODateTime +}; + +const buildPostSnapshotWithTiers = ({tiersCount}) => { + return { + id: anyObjectId, + uuid: anyUuid, + comment_id: anyObjectId, + published_at: anyISODateTime, + created_at: anyISODateTime, + updated_at: anyISODateTime, + tiers: new Array(tiersCount).fill(tierSnapshot) + }; +}; + +const buildPreviousPostSnapshotWithTiers = ({tiersCount}) => { + return { + updated_at: anyISODateTime, + tiers: new Array(tiersCount).fill(tierSnapshot) + }; +}; describe('post.* events', function () { let adminAPIAgent; @@ -19,10 +45,11 @@ describe('post.* events', function () { }); it('post.published even is triggered', async function () { - await webhookMockReceiver.mock('post.published'); + const webhookURL = 'https://test-webhook-receiver.com/post-published/'; + await webhookMockReceiver.mock(webhookURL); await fixtureManager.insertWebhook({ event: 'post.published', - url: 'https://test-webhook-receiver.com/webhook' + url: webhookURL }); const res = await adminAPIAgent @@ -47,6 +74,13 @@ describe('post.* events', function () { .expectStatus(200); await webhookMockReceiver - .matchBodySnapshot(); + // TODO: implement header matching feature next! + // .matchHeaderSnapshot(); + .matchBodySnapshot({ + post: { + current: buildPostSnapshotWithTiers({tiersCount: 2}), + previous: buildPreviousPostSnapshotWithTiers({tiersCount: 2}) + } + }); }); });