From 7552873072401a679c95cabb8b60df4837e6e0ae Mon Sep 17 00:00:00 2001 From: naz Date: Thu, 6 Apr 2023 22:42:11 +0200 Subject: [PATCH] Added email content snapshots to API versioning tests (#16577) refs https://github.com/TryGhost/Team/issues/2691 refs https://github.com/TryGhost/Ghost/commit/939f25a987d26b385978609bac29d27ba8881326 - Resurrected refed commit that was adding tests for versioning API, this time it's using dynamic replacements to match dynamic content of the email using matchHTMLSnapshot / matchPlaintextSnapshot with dynamic content replacements. --- .../shared/__snapshots__/version.test.js.snap | 425 ++++++++++++++++++ .../core/test/e2e-api/shared/version.test.js | 47 +- ghost/core/test/utils/e2e-framework.js | 3 + 3 files changed, 459 insertions(+), 16 deletions(-) diff --git a/ghost/core/test/e2e-api/shared/__snapshots__/version.test.js.snap b/ghost/core/test/e2e-api/shared/__snapshots__/version.test.js.snap index 92c62f919e..7bfb78e7f5 100644 --- a/ghost/core/test/e2e-api/shared/__snapshots__/version.test.js.snap +++ b/ghost/core/test/e2e-api/shared/__snapshots__/version.test.js.snap @@ -356,6 +356,178 @@ Object { } `; +exports[`API Versioning Admin API responds with error and sends email ONCE when requested version is BEHIND and CANNOT respond multiple times 3: [html 1] 1`] = ` +" + + + + +Integration error + + + + + + + + + + +
  + +
+ + + + + + + + + + + + + +
+ + + + + + + + + + +
+

Uh-oh!

+
+

+ Your Zapier integration is no longer working as expected. This integration must be updated by its developer to work with your version of Ghost. +

+ +

+ To help you get things fixed as quickly as possible, Ghost has automatically generated some helpful information about the error that you can share with the creator of the Zapier integration below: +

+ +

+ Integration expected Ghost version:  v4.50 +
+ Current Ghost version:  v4.50 +
+ Failed request URL:  /ghost/api/admin/removed_endpoint/ +

+
+
+
+

This email was sent from http://127.0.0.1:2369/ to jbloggs@example.com

+
+
+ + +
+
 
+ + +" +`; + exports[`API Versioning Admin API responds with error and sends email ONCE when requested version is BEHIND and CANNOT respond multiple times 4: [headers] 1`] = ` Object { "access-control-allow-origin": "http://127.0.0.1:2369", @@ -369,6 +541,62 @@ Object { } `; +exports[`API Versioning Admin API responds with error and sends email ONCE when requested version is BEHIND and CANNOT respond multiple times 4: [text 1] 1`] = ` +" [https://static.ghost.org/v4.50.0/images/ghost-orb-1.png] Uh-oh! + +Your Zapier integration is no longer working as expected. This integration must +be updated by its developer to work with your version of Ghost. + +To help you get things fixed as quickly as possible, Ghost has automatically +generated some helpful information about the error that you can share with the +creator of the Zapier integration below: + + Integration expected Ghost version:v4.50 +Current Ghost version:v4.50 +Failed request URL:/ghost/api/admin/removed_endpoint/ + +This email was sent from http://127.0.0.1:2369/ [http://127.0.0.1:2369/] to +jbloggs@example.com [jbloggs@example.com]" +`; + +exports[`API Versioning Admin API responds with error and sends email ONCE when requested version is BEHIND and CANNOT respond multiple times 5: [metadata 1] 1`] = ` +Object { + "subject": "Attention required: Your Zapier integration has failed", + "to": "jbloggs@example.com", +} +`; + +exports[`API Versioning Admin API responds with error and sends email ONCE when requested version is BEHIND and CANNOT respond multiple times 6: [body] 1`] = ` +Object { + "errors": Array [ + Object { + "code": "UPDATE_CLIENT", + "context": StringMatching /Provided client accept-version v3\\.5 is behind current Ghost version v\\\\d\\+\\\\\\.\\\\d\\+/, + "details": null, + "ghostErrorCode": null, + "help": "Try upgrading your Ghost API client.", + "id": StringMatching /\\[a-f0-9\\]\\{8\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{12\\}/, + "message": "Request could not be served, the endpoint was not found.", + "property": null, + "type": "RequestNotAcceptableError", + }, + ], +} +`; + +exports[`API Versioning Admin API responds with error and sends email ONCE when requested version is BEHIND and CANNOT respond multiple times 7: [headers] 1`] = ` +Object { + "access-control-allow-origin": "http://127.0.0.1:2369", + "cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0", + "content-length": StringMatching /\\\\d\\+/, + "content-type": "application/json; charset=utf-8", + "content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/, + "etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/, + "vary": "Accept-Version, Origin, Accept-Encoding", + "x-powered-by": "Express", +} +`; + exports[`API Versioning Admin API responds with error requested version is AHEAD and CANNOT respond 1: [body] 1`] = ` Object { "errors": Array [ @@ -431,6 +659,203 @@ Object { } `; +exports[`API Versioning Admin API responds with error when requested version is BEHIND and CANNOT respond 3: [html 1] 1`] = ` +" + + + + +Integration error + + + + + + + + + + +
  + +
+ + + + + + + + + + + + + +
+ + + + + + + + + + +
+

Uh-oh!

+
+

+ Your Zapier integration is no longer working as expected. This integration must be updated by its developer to work with your version of Ghost. +

+ +

+ To help you get things fixed as quickly as possible, Ghost has automatically generated some helpful information about the error that you can share with the creator of the Zapier integration below: +

+ +

+ Integration expected Ghost version:  v4.50 +
+ Current Ghost version:  v4.50 +
+ Failed request URL:  /ghost/api/admin/removed_endpoint/ +

+
+
+
+

This email was sent from http://127.0.0.1:2369/ to jbloggs@example.com

+
+
+ + +
+
 
+ + +" +`; + +exports[`API Versioning Admin API responds with error when requested version is BEHIND and CANNOT respond 4: [text 1] 1`] = ` +" [https://static.ghost.org/v4.50.0/images/ghost-orb-1.png] Uh-oh! + +Your Zapier integration is no longer working as expected. This integration must +be updated by its developer to work with your version of Ghost. + +To help you get things fixed as quickly as possible, Ghost has automatically +generated some helpful information about the error that you can share with the +creator of the Zapier integration below: + + Integration expected Ghost version:v4.50 +Current Ghost version:v4.50 +Failed request URL:/ghost/api/admin/removed_endpoint/ + +This email was sent from http://127.0.0.1:2369/ [http://127.0.0.1:2369/] to +jbloggs@example.com [jbloggs@example.com]" +`; + +exports[`API Versioning Admin API responds with error when requested version is BEHIND and CANNOT respond 5: [metadata 1] 1`] = ` +Object { + "subject": "Attention required: Your Zapier integration has failed", + "to": "jbloggs@example.com", +} +`; + exports[`API Versioning Content API Does an internal rewrite with accept version set when version is included in the URL 1: [body] 1`] = ` Object { "meta": Object { diff --git a/ghost/core/test/e2e-api/shared/version.test.js b/ghost/core/test/e2e-api/shared/version.test.js index ee7ce6851c..add373cfcb 100644 --- a/ghost/core/test/e2e-api/shared/version.test.js +++ b/ghost/core/test/e2e-api/shared/version.test.js @@ -1,5 +1,6 @@ -const {agentProvider, fixtureManager, matchers, mockManager} = require('../../utils/e2e-framework'); +const {agentProvider, fixtureManager, matchers, regexes, mockManager} = require('../../utils/e2e-framework'); const {anyErrorId, stringMatching, anyObjectId, anyLocationFor, anyISODateTime, anyEtag, anyString, anyContentLength, anyContentVersion} = matchers; +const {anyMajorMinorVersion} = regexes; const settingsMatcher = { settings: { @@ -10,6 +11,7 @@ const settingsMatcher = { describe('API Versioning', function () { describe('Admin API', function () { let agentAdminAPI; + let emailMockReceiver; before(async function () { agentAdminAPI = await agentProvider.getAdminAPIAgent(); @@ -18,7 +20,7 @@ describe('API Versioning', function () { }); beforeEach(function () { - mockManager.mockMail(); + emailMockReceiver = mockManager.mockMail(); }); afterEach(function () { @@ -130,11 +132,17 @@ describe('API Versioning', function () { }] }); - mockManager.assert.sentEmailCount(1); - mockManager.assert.sentEmail({ - subject: 'Attention required: Your Zapier integration has failed', - to: 'jbloggs@example.com' - }); + emailMockReceiver + .assertSentEmailCount(1) + .matchMetadataSnapshot() + .matchHTMLSnapshot([{ + pattern: anyMajorMinorVersion, + replacement: 'v4.50' + }]) + .matchPlaintextSnapshot([{ + pattern: anyMajorMinorVersion, + replacement: 'v4.50' + }]); }); it('responds with error and sends email ONCE when requested version is BEHIND and CANNOT respond multiple times', async function () { @@ -155,11 +163,17 @@ describe('API Versioning', function () { }] }); - mockManager.assert.sentEmailCount(1); - mockManager.assert.sentEmail({ - subject: 'Attention required: Your Zapier integration has failed', - to: 'jbloggs@example.com' - }); + emailMockReceiver + .assertSentEmailCount(1) + .matchMetadataSnapshot() + .matchHTMLSnapshot([{ + pattern: anyMajorMinorVersion, + replacement: 'v4.50' + }]) + .matchPlaintextSnapshot([{ + pattern: anyMajorMinorVersion, + replacement: 'v4.50' + }]); await agentAdminAPI .get('removed_endpoint') @@ -178,7 +192,7 @@ describe('API Versioning', function () { }] }); - mockManager.assert.sentEmailCount(1); + emailMockReceiver.assertSentEmailCount(1); }); it('responds with 404 error when the resource cannot be found', async function () { @@ -197,7 +211,7 @@ describe('API Versioning', function () { }] }); - mockManager.assert.sentEmailCount(0); + emailMockReceiver.assertSentEmailCount(0); }); it('Does an internal rewrite for canary URLs with accept version set', async function () { @@ -271,9 +285,10 @@ describe('API Versioning', function () { describe('Content API', function () { let agentContentAPI; + let emailMockReceiver; beforeEach(function () { - mockManager.mockMail(); + emailMockReceiver = mockManager.mockMail(); }); afterEach(function () { @@ -341,7 +356,7 @@ describe('API Versioning', function () { }] }); - mockManager.assert.sentEmailCount(0); + emailMockReceiver.assertSentEmailCount(0); }); }); }); diff --git a/ghost/core/test/utils/e2e-framework.js b/ghost/core/test/utils/e2e-framework.js index 7711f2b5c4..3cac000ef6 100644 --- a/ghost/core/test/utils/e2e-framework.js +++ b/ghost/core/test/utils/e2e-framework.js @@ -451,6 +451,9 @@ module.exports = { return path.join(__dirname, 'fixtures', fixturePath); } }, + regexes: { + anyMajorMinorVersion: /v\d+\.\d+/gi + }, matchers: { anyBoolean: any(Boolean), anyString: any(String),