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); + }); }); });