diff --git a/core/server/services/mega/email-preview.js b/core/server/services/mega/email-preview.js index 15620f96be..e3e2dd2e03 100644 --- a/core/server/services/mega/email-preview.js +++ b/core/server/services/mega/email-preview.js @@ -8,7 +8,10 @@ class EmailPreview { * @returns {Promise} */ 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 diff --git a/test/e2e-api/admin/email-previews.test.js b/test/e2e-api/admin/email-previews.test.js index 3500dd1484..ea556b36d7 100644 --- a/test/e2e-api/admin/email-previews.test.js +++ b/test/e2e-api/admin/email-previews.test.js @@ -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(/'/); preview.html.should.not.match(/'/); }); + + 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: '

This is the actual post content...

', + 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 () {