2022-05-30 10:53:43 +03:00
|
|
|
const {agentProvider, mockManager, fixtureManager, matchers} = require('../utils/e2e-framework');
|
2022-06-03 06:26:53 +03:00
|
|
|
const {anyObjectId, anyISODateTime, anyUuid, stringMatching} = matchers;
|
2022-05-30 10:53:43 +03:00
|
|
|
|
|
|
|
const tierSnapshot = {
|
|
|
|
id: anyObjectId,
|
|
|
|
created_at: anyISODateTime,
|
|
|
|
updated_at: anyISODateTime
|
|
|
|
};
|
|
|
|
|
2022-07-13 13:58:23 +03:00
|
|
|
const buildAuthorSnapshot = (roles = false) => {
|
|
|
|
const authorSnapshot = {
|
|
|
|
last_seen: anyISODateTime,
|
|
|
|
created_at: anyISODateTime,
|
|
|
|
updated_at: anyISODateTime
|
|
|
|
};
|
|
|
|
|
|
|
|
// NOTE: this is such a bad hack! for the reasons I did not investigate the "add" event does not include
|
|
|
|
// the roles but the "published" does! massive inconsistency and needs to be fixed one day
|
|
|
|
if (roles) {
|
|
|
|
authorSnapshot.roles = new Array(1).fill({
|
|
|
|
id: anyObjectId,
|
|
|
|
created_at: anyISODateTime,
|
|
|
|
updated_at: anyISODateTime
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return authorSnapshot;
|
2022-06-03 12:35:26 +03:00
|
|
|
};
|
|
|
|
|
2022-07-13 13:58:23 +03:00
|
|
|
const buildPostSnapshotWithTiers = ({published, tiersCount, roles = false}) => {
|
2022-05-30 10:53:43 +03:00
|
|
|
return {
|
|
|
|
id: anyObjectId,
|
|
|
|
uuid: anyUuid,
|
|
|
|
comment_id: anyObjectId,
|
2022-06-03 09:56:55 +03:00
|
|
|
published_at: published ? anyISODateTime : null,
|
2022-05-30 10:53:43 +03:00
|
|
|
created_at: anyISODateTime,
|
|
|
|
updated_at: anyISODateTime,
|
2022-06-03 06:26:53 +03:00
|
|
|
// @TODO: hack here! it's due to https://github.com/TryGhost/Toolbox/issues/341
|
|
|
|
// this matcher should be removed once the issue is solved
|
|
|
|
url: stringMatching(/http:\/\/127.0.0.1:2369\/\w+\//),
|
2022-06-03 12:35:26 +03:00
|
|
|
tiers: new Array(tiersCount).fill(tierSnapshot),
|
2022-07-13 13:58:23 +03:00
|
|
|
primary_author: buildAuthorSnapshot(roles),
|
|
|
|
authors: new Array(1).fill(buildAuthorSnapshot(roles))
|
2022-05-30 10:53:43 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const buildPreviousPostSnapshotWithTiers = ({tiersCount}) => {
|
|
|
|
return {
|
|
|
|
updated_at: anyISODateTime,
|
|
|
|
tiers: new Array(tiersCount).fill(tierSnapshot)
|
|
|
|
};
|
|
|
|
};
|
2022-05-26 06:11:41 +03:00
|
|
|
|
|
|
|
describe('post.* events', function () {
|
|
|
|
let adminAPIAgent;
|
|
|
|
let webhookMockReceiver;
|
|
|
|
|
|
|
|
before(async function () {
|
|
|
|
adminAPIAgent = await agentProvider.getAdminAPIAgent();
|
|
|
|
await fixtureManager.init('integrations');
|
|
|
|
await adminAPIAgent.loginAsOwner();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
webhookMockReceiver = mockManager.mockWebhookRequests();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
mockManager.restore();
|
|
|
|
});
|
|
|
|
|
2022-07-13 13:58:23 +03:00
|
|
|
it('post.published event is triggered', async function () {
|
2022-05-30 10:53:43 +03:00
|
|
|
const webhookURL = 'https://test-webhook-receiver.com/post-published/';
|
|
|
|
await webhookMockReceiver.mock(webhookURL);
|
2022-05-26 06:11:41 +03:00
|
|
|
await fixtureManager.insertWebhook({
|
|
|
|
event: 'post.published',
|
2022-05-30 10:53:43 +03:00
|
|
|
url: webhookURL
|
2022-05-26 06:11:41 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const res = await adminAPIAgent
|
|
|
|
.post('posts/')
|
|
|
|
.body({
|
|
|
|
posts: [{
|
|
|
|
title: 'webhookz',
|
|
|
|
status: 'draft'
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.expectStatus(201);
|
|
|
|
|
|
|
|
const id = res.body.posts[0].id;
|
|
|
|
const updatedPost = res.body.posts[0];
|
|
|
|
updatedPost.status = 'published';
|
|
|
|
|
|
|
|
await adminAPIAgent
|
|
|
|
.put('posts/' + id)
|
|
|
|
.body({
|
|
|
|
posts: [updatedPost]
|
|
|
|
})
|
|
|
|
.expectStatus(200);
|
|
|
|
|
|
|
|
await webhookMockReceiver
|
2022-05-30 10:53:43 +03:00
|
|
|
// TODO: implement header matching feature next!
|
|
|
|
// .matchHeaderSnapshot();
|
|
|
|
.matchBodySnapshot({
|
|
|
|
post: {
|
2022-06-03 09:56:55 +03:00
|
|
|
current: buildPostSnapshotWithTiers({
|
|
|
|
published: true,
|
2022-07-13 13:58:23 +03:00
|
|
|
tiersCount: 2,
|
|
|
|
roles: true
|
2022-06-03 09:56:55 +03:00
|
|
|
}),
|
2022-07-13 13:58:23 +03:00
|
|
|
previous: buildPreviousPostSnapshotWithTiers({
|
|
|
|
tiersCount: 2
|
|
|
|
})
|
2022-05-30 10:53:43 +03:00
|
|
|
}
|
|
|
|
});
|
2022-05-26 06:11:41 +03:00
|
|
|
});
|
2022-06-03 09:56:55 +03:00
|
|
|
|
2022-07-13 13:58:23 +03:00
|
|
|
it('post.added event is triggered', async function () {
|
2022-06-03 09:56:55 +03:00
|
|
|
const webhookURL = 'https://test-webhook-receiver.com/post-added/';
|
|
|
|
await webhookMockReceiver.mock(webhookURL);
|
|
|
|
await fixtureManager.insertWebhook({
|
|
|
|
event: 'post.added',
|
|
|
|
url: webhookURL
|
|
|
|
});
|
|
|
|
|
|
|
|
await adminAPIAgent
|
|
|
|
.post('posts/')
|
|
|
|
.body({
|
|
|
|
posts: [{
|
|
|
|
title: 'testing post.added webhook',
|
|
|
|
status: 'draft'
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.expectStatus(201);
|
|
|
|
|
|
|
|
await webhookMockReceiver
|
|
|
|
// TODO: implement header matching feature next!
|
|
|
|
// .matchHeaderSnapshot();
|
|
|
|
.matchBodySnapshot({
|
|
|
|
post: {
|
|
|
|
current: buildPostSnapshotWithTiers({
|
|
|
|
published: false,
|
|
|
|
tiersCount: 2
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2022-05-26 06:11:41 +03:00
|
|
|
});
|