Added tests for fix - URLs sent in emails containing a % can not be updated (#20569)

ref ENG-1372 ENG-447
This commit is contained in:
Princi Vershwal 2024-07-10 02:04:45 +05:30 committed by GitHub
parent c6e001280c
commit 81df7ca05b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

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