Ghost/ghost/core/test/e2e-server/click-tracking.test.js

118 lines
4.3 KiB
JavaScript
Raw Normal View History

const assert = require('assert');
const fetch = require('node-fetch').default;
const {agentProvider, mockManager, fixtureManager} = require('../utils/e2e-framework');
const urlUtils = require('../../core/shared/url-utils');
Added a test suite for OPTIONS requests refs https://github.com/TryGhost/Toolbox/issues/461 - The codebase has ambiguous behavior with OPTIONS request. Adding tests covering edge cases for all possible variations of OPTIONS responses is the first step to solving cahceability of these requests. - The obvious question if you look into the changeset itself would also be: "WTF did you do with test suite naming? What are these changes in admin and click tracking suites? You having a bad day Naz?". The answer is "yes" (╯°□°)╯︵ ┻━┻ - On a serious note. I've introduced multiple hacks here that should be fixed: 1. Forced test suite execution order for options request - extreme blasphemy. This was last resort decision. I went deep into trying to fixup the server shutdown in the "admin" test suite, which cascaded into failing "click tracking" suite, which has shortcomings on it's own (see notes left in that suite) 2. Exposed "ghostServer" from the e2e-framework's "getAgentsWithFrontend" method. Exposing ghostServer to be able to shut it down (or do other manipulations) was one of the pitfalls we had in the previous test utils, which ended up plaguing the test codebase. Ideally the framework should only be exposing the agents and the rest would happen behind the scenes. - To fix the hacks above I've raised a cleanup issue (https://github.com/TryGhost/Toolbox/issues/471). I'm very sorry for this mess. The issue at hand has very little to do with fixing the e2e framework, so leaving things "as is".
2022-11-02 08:10:49 +03:00
// @NOTE: this test suite cannot be run in isolation - most likely because it needs
// to have full frontend part of Ghost initialized, not just the backend
describe('Click Tracking', function () {
let agent;
before(async function () {
agent = await agentProvider.getAdminAPIAgent();
await fixtureManager.init('newsletters', 'members:newsletters');
await agent.loginAsOwner();
});
beforeEach(function () {
mockManager.mockLabsDisabled('emailStability');
mockManager.mockMail();
});
afterEach(function () {
mockManager.restore();
});
it('Full test', async function () {
const {body: {posts: [draft]}} = await agent.post('/posts/', {
body: {
posts: [{
title: 'My Newsletter'
}]
}
});
const newsletterSlug = fixtureManager.get('newsletters', 0).slug;
const {body: {posts: [post]}} = await agent.put(
`/posts/${draft.id}/?newsletter=${newsletterSlug}`,
{
body: {
posts: [{
updated_at: draft.updated_at,
status: 'published'
}]
}
}
);
const {body: {links}} = await agent.get(
`/links/?filter=post_id:${post.id}`
);
/** @type {(url: string) => Promise<import('node-fetch').Response>} */
const fetchWithoutFollowingRedirect = url => fetch(url, {redirect: 'manual'});
const siteUrl = new URL(urlUtils.urlFor('home', true));
let internalRedirectHappened = false;
let externalRedirectHappened = false;
for (const link of links) {
const res = await fetchWithoutFollowingRedirect(link.link.from);
const redirectedToUrl = new URL(res.headers.get('location'));
// startsWith is a little dirty, but we need this because siteUrl
// can have a path when Ghost is hosted on a subdomain.
const isInternal = redirectedToUrl.href.startsWith(siteUrl.href);
if (isInternal) {
internalRedirectHappened = true;
assert(redirectedToUrl.searchParams.get('attribution_id'), 'attribution_id should be present on internal redirects');
assert(redirectedToUrl.searchParams.get('attribution_type'), 'attribution_type should be present on internal redirects');
} else {
externalRedirectHappened = true;
assert(!redirectedToUrl.searchParams.get('attribution_id'), 'attribution_id should not be present on internal redirects');
assert(!redirectedToUrl.searchParams.get('attribution_type'), 'attribution_type should not be present on internal redirects');
}
assert(redirectedToUrl.searchParams.get('ref'), 'ref should be present on all redirects');
}
assert(internalRedirectHappened);
assert(externalRedirectHappened);
const {body: {members}} = await agent.get(
`/members/`
);
const linkToClick = links[0];
const memberToClickLink = members[0];
const urlOfLinkToClick = new URL(linkToClick.link.from);
urlOfLinkToClick.searchParams.set('m', memberToClickLink.uuid);
const previousClickCount = linkToClick.count.clicks;
await fetchWithoutFollowingRedirect(urlOfLinkToClick.href);
const {body: {links: [clickedLink]}} = await agent.get(
`/links/?filter=post_id:${post.id}`
);
const clickCount = clickedLink.count.clicks;
const {body: {events: clickEvents}} = await agent.get(
`/members/events/?filter=data.member_id:${memberToClickLink.id}${encodeURIComponent('+')}type:click_event`
);
const clickEvent = clickEvents.find((/** @type any */ event) => {
return event.data.post.id === post.id && event.data.link.from === urlOfLinkToClick.pathname;
});
assert(clickEvent);
assert(previousClickCount + 1 === clickCount);
});
});