Fixed email preview using wrong newsletter (#14756)

refs https://github.com/TryGhost/Team/issues/1603

When previewing a scheduled/published post via Post editor menu > E-mail newsletter > Preview in browser. The e-mail template from the default newsletter was used instead of the newsletter that was selected when scheduling the post.
This commit is contained in:
Simon Backx 2022-05-11 17:52:24 +02:00 committed by GitHub
parent 23c88d250e
commit 837e11b4d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 2 deletions

View File

@ -8,7 +8,10 @@ class EmailPreview {
* @returns {Promise<Object>}
*/
async generateEmailContent(post, memberSegment) {
const newsletter = await models.Newsletter.getDefaultNewsletter();
let newsletter = await post.related('newsletter').fetch();
if (!newsletter) {
newsletter = await models.Newsletter.getDefaultNewsletter();
}
let emailContent = await postEmailSerializer.serialize(post, newsletter, {
isBrowserPreview: true

View File

@ -12,7 +12,7 @@ describe('Email Preview API', function () {
before(async function () {
await localUtils.startGhost();
request = supertest.agent(config.get('url'));
await localUtils.doAuth(request, 'users:extra', 'posts');
await localUtils.doAuth(request, 'users:extra', 'newsletters', 'posts');
});
describe('Read', function () {
@ -129,6 +129,41 @@ describe('Email Preview API', function () {
preview.html.should.match(/&#39;/);
preview.html.should.not.match(/&apos;/);
});
it('uses the posts newsletter', async function () {
const defaultNewsletter = await models.Newsletter.getDefaultNewsletter();
defaultNewsletter.id.should.not.eql(testUtils.DataGenerator.Content.newsletters[0].id, 'Should use a non-default newsletter for this test');
const post = testUtils.DataGenerator.forKnex.createPost({
id: ObjectId().toHexString(),
title: 'Post with email-only card',
slug: 'email-only-card',
mobiledoc: '{"version":"0.3.1","atoms":[],"cards":[],"markups":[["a",["href","https://ghost.org"]]],"sections":[[1,"p",[[0,[],0,"Testing "],[0,[0],1,"links"],[0,[],0," in email excerpt and apostrophes \'"]]]]}',
html: '<p>This is the actual post content...</p>',
plaintext: 'This is the actual post content...',
status: 'scheduled',
uuid: 'd52c42ae-2755-455c-80ec-70b2ec55c904',
newsletter_id: testUtils.DataGenerator.Content.newsletters[0].id
});
await models.Post.add(post, {context: {internal: true}});
const res = await request
.get(localUtils.API.getApiQuery(`email_previews/posts/${post.id}/`))
.set('Origin', config.get('url'))
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(200);
should.not.exist(res.headers['x-cache-invalidate']);
const jsonResponse = res.body;
should.exist(jsonResponse);
should.exist(jsonResponse.email_previews);
const [preview] = jsonResponse.email_previews;
preview.html.should.containEql(testUtils.DataGenerator.Content.newsletters[0].name);
});
});
describe('As Owner', function () {