Ghost/ghost/core/test/e2e-api/admin/links.test.js
Simon Backx b6519e0f1f
Removed usage of unquoted ids in filter strings (#19070)
fixes GRO-34
fixes GRO-33

This is a revision of a previous commit, that broke the browser tests
because changes in the data generator (requiring bookshelf had side
effects).

This adds a new way to run all tests with enforced numeric ObjectIDs.
These numeric ids cause issues if they are used withing NQL filters. So
they surface tiny bugs in our codebase.

You can run tests using this option via:
NUMERIC_IDS=1 yarn test:e2e

Removed some defensive logic that could be explained by this discovered
issue.
2023-11-21 09:45:36 +01:00

243 lines
7.7 KiB
JavaScript

const {agentProvider, fixtureManager, matchers} = require('../../utils/e2e-framework');
const {anyContentVersion, anyObjectId, anyString, anyEtag, anyNumber} = matchers;
const sinon = require('sinon');
const matchLink = {
post_id: anyObjectId,
link: {
link_id: anyObjectId,
from: anyString,
to: anyString,
edited: false
},
count: {
clicks: anyNumber
}
};
describe('Links API', function () {
let agent;
let clock;
beforeEach(async function () {
agent = await agentProvider.getAdminAPIAgent();
await fixtureManager.init('posts', 'links');
await agent.loginAsOwner();
clock = sinon.useFakeTimers(new Date());
});
afterEach(async function () {
clock.restore();
});
it('Can browse all links', async function () {
await agent
.get('links')
.expectStatus(200)
.matchHeaderSnapshot({
'content-version': anyContentVersion,
etag: anyEtag
})
.matchBodySnapshot({
links: new Array(3).fill(matchLink)
});
});
it('Can bulk update multiple links with same site redirect', async function () {
const req = await agent.get('links');
const siteLink = req.body.links.find((link) => {
return link.link.to.includes('/email/');
});
const postId = siteLink.post_id;
const originalTo = siteLink.link.to;
const filter = `post_id:'${postId}'+to:'${originalTo}'`;
// Wait minimum 2 seconds
clock.tick(2 * 1000);
await agent
.put(`links/bulk/?filter=${encodeURIComponent(filter)}`)
.body({
bulk: {
action: 'updateLink',
meta: {
link: {
to: 'http://127.0.0.1:2369/blog/emails/test?example=1'
}
}
}
})
.expectStatus(200)
.matchBodySnapshot({
bulk: {
action: 'updateLink',
meta: {
stats: {
successful: 2,
unsuccessful: 0
},
errors: [],
unsuccessfulData: []
}
}
})
.matchHeaderSnapshot({
'content-version': anyContentVersion,
etag: anyEtag
});
await agent
.get('links')
.expectStatus(200)
.matchHeaderSnapshot({
'content-version': anyContentVersion,
etag: anyEtag
})
.matchBodySnapshot({
links: [
matchLink,
{
...matchLink,
link: {
...matchLink.link,
to: 'http://127.0.0.1:2369/blog/emails/test?example=1&ref=Test-newsletter&attribution_type=post&attribution_id=618ba1ffbe2896088840a6df',
edited: true
}
},
{
...matchLink,
link: {
...matchLink.link,
to: 'http://127.0.0.1:2369/blog/emails/test?example=1&ref=Test-newsletter&attribution_type=post&attribution_id=618ba1ffbe2896088840a6df',
edited: true
}
}
]
});
});
it('Can bulk update links with external redirect', async function () {
const req = await agent.get('links');
const siteLink = req.body.links.find((link) => {
return link.link.to.includes('subscripe');
});
const postId = siteLink.post_id;
const originalTo = siteLink.link.to;
const filter = `post_id:'${postId}'+to:'${originalTo}'`;
// Wait minimum 2 seconds
clock.tick(2 * 1000);
await agent
.put(`links/bulk/?filter=${encodeURIComponent(filter)}`)
.body({
bulk: {
action: 'updateLink',
meta: {
link: {
to: 'https://example.com/subscribe?ref=Test-newsletter'
}
}
}
})
.expectStatus(200)
.matchBodySnapshot({
bulk: {
action: 'updateLink',
meta: {
stats: {
successful: 1,
unsuccessful: 0
},
errors: [],
unsuccessfulData: []
}
}
})
.matchHeaderSnapshot({
'content-version': anyContentVersion,
etag: anyEtag
});
await agent
.get('links')
.expectStatus(200)
.matchHeaderSnapshot({
'content-version': anyContentVersion,
etag: anyEtag
})
.matchBodySnapshot({
links: [
{
...matchLink,
link: {
...matchLink.link,
to: 'https://example.com/subscribe?ref=Test-newsletter',
edited: true
}
},
matchLink,
matchLink
]
});
});
it('Can call bulk update link with 0 matches', async function () {
const req = await agent.get('links');
const siteLink = req.body.links.find((link) => {
return link.link.to.includes('subscripe');
});
const postId = siteLink.post_id;
const originalTo = 'https://empty.example.com';
const filter = `post_id:'${postId}'+to:'${originalTo}'`;
await agent
.put(`links/bulk/?filter=${encodeURIComponent(filter)}`)
.body({
bulk: {
action: 'updateLink',
meta: {
link: {
to: 'https://example.com/subscribe?ref=Test-newsletter'
}
}
}
})
.expectStatus(200)
.matchBodySnapshot({
bulk: {
action: 'updateLink',
meta: {
stats: {
successful: 0,
unsuccessful: 0
},
errors: [],
unsuccessfulData: []
}
}
})
.matchHeaderSnapshot({
'content-version': anyContentVersion,
etag: anyEtag
});
await agent
.get('links')
.expectStatus(200)
.matchHeaderSnapshot({
'content-version': anyContentVersion,
etag: anyEtag
})
.matchBodySnapshot({
links: [
{
...matchLink,
link: {
...matchLink.link,
to: 'https://example.com/subscripe?ref=Test-newsletter'
}
},
matchLink,
matchLink
]
});
});
});