From 80f7e0b19e6cb002731c825bba8208dcb0dca075 Mon Sep 17 00:00:00 2001 From: Naz Gargol Date: Thu, 26 Sep 2019 15:40:24 +0200 Subject: [PATCH] Added default_content_visibility flag for content gating (#11163) no issue - Added 'labs' flag settings test - Added test for default_content_visibility flag - Default post's visibility takes into account values set in default_content_visibility setting --- core/server/models/post.js | 9 ++- .../acceptance/old/admin/settings_spec.js | 7 ++ .../regression/models/model_posts_spec.js | 76 +++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/core/server/models/post.js b/core/server/models/post.js index 690c82f7b2..d226930867 100644 --- a/core/server/models/post.js +++ b/core/server/models/post.js @@ -8,6 +8,7 @@ const common = require('../lib/common'); const htmlToText = require('html-to-text'); const ghostBookshelf = require('./base'); const config = require('../config'); +const settingsCache = require('../services/settings/cache'); const converters = require('../lib/mobiledoc/converters'); const relations = require('./relations'); const MOBILEDOC_REVISIONS_COUNT = 10; @@ -40,12 +41,18 @@ Post = ghostBookshelf.Model.extend({ * 2. model events e.g. "post.published" are using the inserted resource, not the fetched resource */ defaults: function defaults() { + let visibility = 'public'; + + if (settingsCache.get('labs') && (settingsCache.get('labs').members === true) && settingsCache.get('labs').default_content_visibility) { + visibility = settingsCache.get('labs').default_content_visibility; + } + return { uuid: uuid.v4(), status: 'draft', featured: false, page: false, - visibility: 'public' + visibility: visibility }; }, diff --git a/core/test/acceptance/old/admin/settings_spec.js b/core/test/acceptance/old/admin/settings_spec.js index 4d89a75303..493262f0e3 100644 --- a/core/test/acceptance/old/admin/settings_spec.js +++ b/core/test/acceptance/old/admin/settings_spec.js @@ -150,6 +150,10 @@ describe('Settings API', function () { { key: 'twitter_description', value: 'twitter description' + }, + { + key: 'labs', + value: '{"subscribers":false,"members":true,"default_content_visibility":"paid"}' } ] }; @@ -214,6 +218,9 @@ describe('Settings API', function () { putBody.settings[12].key.should.eql('twitter_description'); should.equal(putBody.settings[12].value, 'twitter description'); + putBody.settings[13].key.should.eql('labs'); + should.equal(putBody.settings[13].value, '{"subscribers":false,"members":true,"default_content_visibility":"paid"}'); + localUtils.API.checkResponse(putBody, 'settings'); done(); }); diff --git a/core/test/regression/models/model_posts_spec.js b/core/test/regression/models/model_posts_spec.js index 72e68b0436..c444e8ca94 100644 --- a/core/test/regression/models/model_posts_spec.js +++ b/core/test/regression/models/model_posts_spec.js @@ -8,6 +8,7 @@ var should = require('should'), urlService = require('../../../frontend/services/url'), ghostBookshelf = require('../../../server/models/base'), models = require('../../../server/models'), + settingsCache = require('../../../server/services/settings/cache'), common = require('../../../server/lib/common'), configUtils = require('../../utils/configUtils'), DataGenerator = testUtils.DataGenerator, @@ -747,6 +748,81 @@ describe('Post Model', function () { (!!createdPost.get('page')).should.equal(false); should.equal(createdPost.get('locale'), null); + should.equal(createdPost.get('visibility'), 'public'); + + // testing for nulls + (createdPost.get('feature_image') === null).should.equal(true); + (createdPost.get('meta_title') === null).should.equal(true); + (createdPost.get('meta_description') === null).should.equal(true); + + createdPost.get('created_at').should.be.above(new Date(0).getTime()); + createdPost.get('created_by').should.equal(testUtils.DataGenerator.Content.users[0].id); + createdPost.get('author_id').should.equal(testUtils.DataGenerator.Content.users[0].id); + createdPost.has('author').should.equal(false); + createdPost.get('created_by').should.equal(createdPost.get('author_id')); + createdPost.get('updated_at').should.be.above(new Date(0).getTime()); + createdPost.get('updated_by').should.equal(testUtils.DataGenerator.Content.users[0].id); + should.equal(createdPost.get('published_at'), null); + should.equal(createdPost.get('published_by'), null); + + createdPostUpdatedDate = createdPost.get('updated_at'); + + Object.keys(eventsTriggered).length.should.eql(2); + should.exist(eventsTriggered['post.added']); + should.exist(eventsTriggered['user.attached']); + + // Set the status to published to check that `published_at` is set. + return createdPost.save({status: 'published'}, context); + }).then(function (publishedPost) { + publishedPost.get('published_at').should.be.instanceOf(Date); + publishedPost.get('published_by').should.equal(testUtils.DataGenerator.Content.users[0].id); + publishedPost.get('updated_at').should.be.instanceOf(Date); + publishedPost.get('updated_by').should.equal(testUtils.DataGenerator.Content.users[0].id); + publishedPost.get('updated_at').should.not.equal(createdPostUpdatedDate); + + Object.keys(eventsTriggered).length.should.eql(4); + should.exist(eventsTriggered['post.published']); + should.exist(eventsTriggered['post.edited']); + + done(); + }).catch(done); + }); + + it('can add, default visibility is taken from settings cache', function (done) { + var originalSettingsCacheGetFn = settingsCache.get; + sinon.stub(settingsCache, 'get').callsFake(function (key, options) { + if (key === 'labs') { + return { + members: true, + default_content_visibility: 'paid' + }; + } + + return originalSettingsCacheGetFn(key, options); + }); + + var createdPostUpdatedDate, + newPost = testUtils.DataGenerator.forModel.posts[2], + newPostDB = testUtils.DataGenerator.Content.posts[2]; + + models.Post.add(newPost, _.merge({withRelated: ['author']}, context)).then(function (createdPost) { + return models.Post.findOne({id: createdPost.id, status: 'all'}); + }).then(function (createdPost) { + should.exist(createdPost); + createdPost.has('uuid').should.equal(true); + createdPost.get('status').should.equal('draft'); + createdPost.get('title').should.equal(newPost.title, 'title is correct'); + createdPost.get('mobiledoc').should.equal(newPost.mobiledoc, 'mobiledoc is correct'); + createdPost.has('html').should.equal(true); + createdPost.get('html').should.equal(newPostDB.html); + createdPost.has('plaintext').should.equal(true); + createdPost.get('plaintext').should.match(/^testing/); + createdPost.get('slug').should.equal(newPostDB.slug + '-2'); + (!!createdPost.get('featured')).should.equal(false); + (!!createdPost.get('page')).should.equal(false); + + should.equal(createdPost.get('locale'), null); + should.equal(createdPost.get('visibility'), 'paid'); // testing for nulls (createdPost.get('feature_image') === null).should.equal(true);