2022-03-28 17:24:51 +02:00
|
|
|
const {agentProvider, mockManager, fixtureManager, matchers} = require('../../utils/e2e-framework');
|
2022-04-25 15:14:22 +01:00
|
|
|
const {anyEtag, anyObjectId, anyString, anyISODateTime, anyLocationFor} = matchers;
|
2022-04-21 09:59:44 +02:00
|
|
|
const testUtils = require('../../utils');
|
2022-03-28 17:24:51 +02:00
|
|
|
|
|
|
|
const newsletterSnapshot = {
|
2022-04-22 14:39:27 +02:00
|
|
|
id: anyObjectId,
|
|
|
|
created_at: anyISODateTime,
|
|
|
|
updated_at: anyISODateTime
|
2022-03-28 17:24:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let agent;
|
|
|
|
|
|
|
|
describe('Newsletters API', function () {
|
2022-04-22 13:20:44 +01:00
|
|
|
let mailMocks;
|
|
|
|
|
2022-03-28 17:24:51 +02:00
|
|
|
before(async function () {
|
|
|
|
agent = await agentProvider.getAdminAPIAgent();
|
2022-04-25 15:14:22 +01:00
|
|
|
await fixtureManager.init('newsletters', 'members:newsletters');
|
2022-03-28 17:24:51 +02:00
|
|
|
await agent.loginAsOwner();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2022-04-22 13:20:44 +01:00
|
|
|
mailMocks = mockManager.mockMail();
|
2022-03-28 17:24:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
mockManager.restore();
|
|
|
|
});
|
|
|
|
|
2022-04-25 15:14:22 +01:00
|
|
|
it('Can browse newsletters', async function () {
|
|
|
|
await agent.get('newsletters/')
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
newsletters: new Array(4).fill(newsletterSnapshot)
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can read a newsletter', async function () {
|
|
|
|
await agent
|
|
|
|
.get(`newsletters/${testUtils.DataGenerator.Content.newsletters[0].id}/`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
newsletters: [newsletterSnapshot]
|
|
|
|
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can include members & posts counts when browsing newsletters', async function () {
|
|
|
|
await agent
|
|
|
|
.get(`newsletters/?include=count.members,count.posts`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
newsletters: new Array(4).fill(newsletterSnapshot)
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can include members & posts counts when reading a newsletter', async function () {
|
|
|
|
await agent
|
|
|
|
.get(`newsletters/${testUtils.DataGenerator.Content.newsletters[0].id}/?include=count.members,count.posts`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
newsletters: new Array(1).fill(newsletterSnapshot)
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-03-31 10:52:04 +02:00
|
|
|
it('Can add a newsletter', async function () {
|
|
|
|
const newsletter = {
|
|
|
|
name: 'My test newsletter',
|
|
|
|
sender_name: 'Test',
|
2022-04-22 13:20:44 +01:00
|
|
|
sender_email: null,
|
2022-04-08 19:06:30 +05:30
|
|
|
sender_reply_to: 'newsletter',
|
2022-03-31 10:52:04 +02:00
|
|
|
status: 'active',
|
|
|
|
subscribe_on_signup: true,
|
2022-04-08 19:06:30 +05:30
|
|
|
title_font_category: 'serif',
|
|
|
|
body_font_category: 'serif',
|
|
|
|
show_header_icon: true,
|
|
|
|
show_header_title: true,
|
|
|
|
show_badge: true,
|
2022-03-31 10:52:04 +02:00
|
|
|
sort_order: 0
|
|
|
|
};
|
2022-03-28 17:24:51 +02:00
|
|
|
|
2022-03-31 10:52:04 +02:00
|
|
|
await agent
|
|
|
|
.post(`newsletters/`)
|
|
|
|
.body({newsletters: [newsletter]})
|
|
|
|
.expectStatus(201)
|
|
|
|
.matchBodySnapshot({
|
2022-04-12 19:44:21 +05:30
|
|
|
newsletters: new Array(1).fill(newsletterSnapshot)
|
2022-03-31 10:52:04 +02:00
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag,
|
2022-04-25 15:14:22 +01:00
|
|
|
location: anyLocationFor('newsletters')
|
2022-03-31 10:52:04 +02:00
|
|
|
});
|
|
|
|
});
|
2022-03-28 17:24:51 +02:00
|
|
|
|
2022-04-22 13:20:44 +01:00
|
|
|
it('Can add a newsletter - with custom sender_email', async function () {
|
|
|
|
const newsletter = {
|
|
|
|
name: 'My test newsletter with custom sender_email',
|
|
|
|
sender_name: 'Test',
|
|
|
|
sender_email: 'test@example.com',
|
|
|
|
sender_reply_to: 'newsletter',
|
|
|
|
status: 'active',
|
|
|
|
subscribe_on_signup: true,
|
|
|
|
title_font_category: 'serif',
|
|
|
|
body_font_category: 'serif',
|
|
|
|
show_header_icon: true,
|
|
|
|
show_header_title: true,
|
|
|
|
show_badge: true,
|
|
|
|
sort_order: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
await agent
|
|
|
|
.post(`newsletters/`)
|
|
|
|
.body({newsletters: [newsletter]})
|
|
|
|
.expectStatus(201)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
newsletters: new Array(1).fill(newsletterSnapshot),
|
|
|
|
meta: {
|
|
|
|
sent_email_verification: ['sender_email']
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag,
|
2022-04-25 15:14:22 +01:00
|
|
|
location: anyLocationFor('newsletters')
|
2022-04-22 13:20:44 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
mockManager.assert.sentEmail({
|
|
|
|
subject: 'Verify email address',
|
|
|
|
to: 'test@example.com'
|
|
|
|
});
|
2022-03-31 10:52:04 +02:00
|
|
|
});
|
2022-03-28 17:24:51 +02:00
|
|
|
|
2022-03-31 10:52:04 +02:00
|
|
|
it('Can edit newsletters', async function () {
|
2022-04-25 15:14:22 +01:00
|
|
|
const id = fixtureManager.get('newsletters', 0).id;
|
2022-03-31 10:52:04 +02:00
|
|
|
await agent.put(`newsletters/${id}`)
|
|
|
|
.body({
|
|
|
|
newsletters: [{
|
|
|
|
name: 'Updated newsletter name'
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
newsletters: [newsletterSnapshot]
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
});
|
|
|
|
});
|
2022-04-22 13:20:44 +01:00
|
|
|
|
2022-04-25 15:14:22 +01:00
|
|
|
it('Can edit a newsletters and update the sender_email when already set', async function () {
|
|
|
|
const id = fixtureManager.get('newsletters', 0).id;
|
2022-04-22 13:20:44 +01:00
|
|
|
|
|
|
|
await agent.put(`newsletters/${id}`)
|
|
|
|
.body({
|
|
|
|
newsletters: [{
|
|
|
|
name: 'Updated newsletter name',
|
|
|
|
sender_email: 'updated@example.com'
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
newsletters: [newsletterSnapshot],
|
|
|
|
meta: {
|
|
|
|
sent_email_verification: ['sender_email']
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
});
|
|
|
|
|
|
|
|
mockManager.assert.sentEmail({
|
|
|
|
subject: 'Verify email address',
|
|
|
|
to: 'updated@example.com'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can verify property updates', async function () {
|
|
|
|
const cheerio = require('cheerio');
|
|
|
|
|
2022-04-25 15:14:22 +01:00
|
|
|
const id = fixtureManager.get('newsletters', 0).id;
|
2022-04-22 13:20:44 +01:00
|
|
|
|
|
|
|
await agent.put(`newsletters/${id}`)
|
|
|
|
.body({
|
|
|
|
newsletters: [{
|
|
|
|
name: 'Updated newsletter name',
|
|
|
|
sender_email: 'verify@example.com'
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.expectStatus(200);
|
|
|
|
|
|
|
|
const mailHtml = mailMocks.getCall(0).args[0].html;
|
|
|
|
const $mailHtml = cheerio.load(mailHtml);
|
|
|
|
|
|
|
|
const verifyUrl = new URL($mailHtml('[data-test-verify-link]').attr('href'));
|
|
|
|
// convert Admin URL hash to native URL for easier token param extraction
|
|
|
|
const token = (new URL(verifyUrl.hash.replace('#', ''), 'http://example.com')).searchParams.get('verifyEmail');
|
|
|
|
|
|
|
|
await agent.put(`newsletters/verifications`)
|
|
|
|
.body({
|
|
|
|
token
|
|
|
|
})
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
newsletters: [newsletterSnapshot]
|
|
|
|
});
|
|
|
|
});
|
2022-03-28 17:24:51 +02:00
|
|
|
});
|