diff --git a/core/server/models/post.js b/core/server/models/post.js index 68f9e5e374..f8f3cf61d1 100644 --- a/core/server/models/post.js +++ b/core/server/models/post.js @@ -261,7 +261,7 @@ Post = ghostBookshelf.Model.extend({ }); }, - onSaving: function onSaving(model, attr, options) { + onSaving: async function onSaving(model, attr, options) { options = options || {}; const self = this; @@ -407,7 +407,7 @@ Post = ghostBookshelf.Model.extend({ // If we're force re-rendering we want to make sure that all image cards // have original dimensions stored in the payload for use by card renderers if (options.force_rerender) { - this.set('mobiledoc', mobiledocLib.populateImageDimensions(this.get('mobiledoc'))); + this.set('mobiledoc', await mobiledocLib.populateImageSizes(this.get('mobiledoc'))); } // CASE: mobiledoc has changed, generate html diff --git a/test/api-acceptance/admin/posts_spec.js b/test/api-acceptance/admin/posts_spec.js index d62c87cc05..5a31e30c0a 100644 --- a/test/api-acceptance/admin/posts_spec.js +++ b/test/api-acceptance/admin/posts_spec.js @@ -1,4 +1,6 @@ const should = require('should'); +const nock = require('nock'); +const path = require('path'); const supertest = require('supertest'); const _ = require('lodash'); const ObjectId = require('bson-objectid'); @@ -29,6 +31,10 @@ describe('Posts API', function () { }); }); + afterEach(function () { + nock.cleanAll(); + }); + it('Can retrieve all posts', function (done) { request.get(localUtils.API.getApiQuery('posts/')) .set('Origin', config.get('url')) @@ -325,6 +331,53 @@ describe('Posts API', function () { }); }); + it('Can update and force re-render', function () { + const unsplashMock = nock('https://images.unsplash.com/') + .get('/favicon_too_large') + .query(true) + .replyWithFile(200, path.join(__dirname, '../../utils/fixtures/images/ghost-logo.png'), { + 'Content-Type': 'image/png' + }); + + const mobiledoc = JSON.parse(testUtils.DataGenerator.Content.posts[3].mobiledoc); + mobiledoc.cards.push(['image', {src: 'https://images.unsplash.com/favicon_too_large?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=2000&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ'}]); + mobiledoc.sections.push([10, mobiledoc.cards.length - 1]); + + const post = { + mobiledoc: JSON.stringify(mobiledoc) + }; + + return request + .get(localUtils.API.getApiQuery(`posts/${testUtils.DataGenerator.Content.posts[3].id}/`)) + .set('Origin', config.get('url')) + .expect(200) + .then((res) => { + post.updated_at = res.body.posts[0].updated_at; + + return request + .put(localUtils.API.getApiQuery('posts/' + testUtils.DataGenerator.Content.posts[3].id + '/?force_rerender=true&formats=mobiledoc,html')) + .set('Origin', config.get('url')) + .send({posts: [post]}) + .expect('Content-Type', /json/) + .expect('Cache-Control', testUtils.cacheRules.private); + // .expect(200); + }) + .then((res) => { + res.headers['x-cache-invalidate'].should.eql('/p/' + res.body.posts[0].uuid + '/'); + + unsplashMock.isDone().should.be.true(); + + // mobiledoc is updated with image sizes + const resMobiledoc = JSON.parse(res.body.posts[0].mobiledoc); + const cardPayload = resMobiledoc.cards[mobiledoc.cards.length - 1][1]; + cardPayload.width.should.eql(800); + cardPayload.height.should.eql(257); + + // html is re-rendered to include srcset + res.body.posts[0].html.should.match(/srcset="https:\/\/images\.unsplash\.com\/favicon_too_large\?ixlib=rb-1\.2\.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=600&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ 600w, https:\/\/images\.unsplash\.com\/favicon_too_large\?ixlib=rb-1\.2\.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=800&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ 800w"/); + }); + }); + it('Can unpublish a post', function () { const post = { status: 'draft'