From 81df7ca05b90936e7013f524a6c09c3246a06c41 Mon Sep 17 00:00:00 2001 From: Princi Vershwal Date: Wed, 10 Jul 2024 02:04:45 +0530 Subject: [PATCH] Added tests for fix - URLs sent in emails containing a % can not be updated (#20569) ref ENG-1372 ENG-447 --- .../test/LinkClickTrackingService.test.js | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/ghost/link-tracking/test/LinkClickTrackingService.test.js b/ghost/link-tracking/test/LinkClickTrackingService.test.js index 7cfd4f55c2..f356446797 100644 --- a/ghost/link-tracking/test/LinkClickTrackingService.test.js +++ b/ghost/link-tracking/test/LinkClickTrackingService.test.js @@ -5,6 +5,7 @@ const assert = require('assert/strict'); const ObjectID = require('bson-objectid').default; const PostLink = require('../lib/PostLink'); const {RedirectEvent} = require('@tryghost/link-redirects'); +const errors = require('@tryghost/errors'); describe('LinkClickTrackingService', function () { it('exists', function () { @@ -208,5 +209,141 @@ describe('LinkClickTrackingService', function () { unsuccessfulData: [] }); }); + + //test for #parseLinkFilter method + it('correctly decodes and parses the filter', async function () { + const urlUtilsStub = { + absoluteToTransformReady: sinon.stub().returnsArg(0), + isSiteUrl: sinon.stub().returns(true) + }; + const postLinkRepositoryStub = { + updateLinks: sinon.stub().resolves({ + successful: 0, + unsuccessful: 0, + errors: [], + unsuccessfulData: [] + }) + }; + const linkRedirectServiceStub = { + getFilteredIds: sinon.stub().resolves([]) + }; + + const service = new LinkClickTrackingService({ + urlUtils: urlUtilsStub, + postLinkRepository: postLinkRepositoryStub, + linkRedirectService: linkRedirectServiceStub + }); + + const options = { + filter: 'post_id:1+to:\'https://example.com/path\'' + }; + + const data = { + action: 'updateLink', + meta: { + link: {to: 'https://example.com/new-path'} + } + }; + + const result = await service.bulkEdit(data, options); + + should(postLinkRepositoryStub.updateLinks.calledOnce).be.true(); + should(result).eql({ + successful: 0, + unsuccessful: 0, + errors: [], + unsuccessfulData: [] + }); + + const [filterOptions] = linkRedirectServiceStub.getFilteredIds.firstCall.args; + should(filterOptions.filter).equal('post_id:\'1\'+to:\'https://example.com/path\''); + }); + + //test for #parseLinkFilter method + it('correctly decodes and parses the filter for encoded urls', async function () { + const urlUtilsStub = { + absoluteToTransformReady: sinon.stub().returnsArg(0), + isSiteUrl: sinon.stub().returns(true) + }; + const postLinkRepositoryStub = { + updateLinks: sinon.stub().resolves({ + successful: 0, + unsuccessful: 0, + errors: [], + unsuccessfulData: [] + }) + }; + const linkRedirectServiceStub = { + getFilteredIds: sinon.stub().resolves([]) + }; + + const service = new LinkClickTrackingService({ + urlUtils: urlUtilsStub, + postLinkRepository: postLinkRepositoryStub, + linkRedirectService: linkRedirectServiceStub + }); + + const options = { + filter: 'post_id:1+to:\'https://example.com/path%2Ftestpath\'' + }; + + const data = { + action: 'updateLink', + meta: { + link: {to: 'https://example.com/new-path'} + } + }; + + const result = await service.bulkEdit(data, options); + + should(postLinkRepositoryStub.updateLinks.calledOnce).be.true(); + should(result).eql({ + successful: 0, + unsuccessful: 0, + errors: [], + unsuccessfulData: [] + }); + + const [filterOptions] = linkRedirectServiceStub.getFilteredIds.firstCall.args; + should(filterOptions.filter).equal('post_id:\'1\'+to:\'https://example.com/path%2Ftestpath\''); + }); + + //test for #parseLinkFilter method + it('throws BadRequestError for invalid filter', async function () { + const urlUtilsStub = { + absoluteToTransformReady: sinon.stub().returnsArg(0), + isSiteUrl: sinon.stub().returns(true) + }; + const postLinkRepositoryStub = { + updateLinks: sinon.stub().resolves({ + successful: 0, + unsuccessful: 0, + errors: [], + unsuccessfulData: [] + }) + }; + const linkRedirectServiceStub = { + getFilteredIds: sinon.stub().resolves([]) + }; + + const service = new LinkClickTrackingService({ + urlUtils: urlUtilsStub, + postLinkRepository: postLinkRepositoryStub, + linkRedirectService: linkRedirectServiceStub + }); + + const options = { + filter: 'invalid_filter' + }; + + const data = { + action: 'updateLink', + meta: { + link: {to: 'https://example.com/new-path'} + } + }; + + await should(service.bulkEdit(data, options)).be.rejectedWith(errors.BadRequestError); + }); }); });