From 8b9734ea315caa0b2a5383c3d889c1165359b83d Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Sun, 10 Apr 2016 19:22:00 +0100 Subject: [PATCH] Add new ghost-scheduler client refs #6301, #6399 - new scheduler client will be used by any web app that handles time and calls back to the scheduling API at the right time - new scheduler client will need to be confidential, rather than public, hence the 'web' type instead of 'ua' - adds validation to client type that it must have a type of 'ua', 'web', or 'native' --- .../005/02-add-ghost-scheduler-client.js | 16 +++++++ .../data/migration/fixtures/005/index.js | 4 +- .../data/migration/fixtures/fixtures.json | 6 +++ core/server/data/schema/schema.js | 2 +- core/test/integration/migration_spec.js | 3 +- core/test/unit/migration_fixture_spec.js | 47 +++++++++++++++++-- core/test/unit/migration_spec.js | 2 +- 7 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 core/server/data/migration/fixtures/005/02-add-ghost-scheduler-client.js diff --git a/core/server/data/migration/fixtures/005/02-add-ghost-scheduler-client.js b/core/server/data/migration/fixtures/005/02-add-ghost-scheduler-client.js new file mode 100644 index 0000000000..6e1d57be04 --- /dev/null +++ b/core/server/data/migration/fixtures/005/02-add-ghost-scheduler-client.js @@ -0,0 +1,16 @@ +// Create a new `ghost-scheduler` client for use in themes +var models = require('../../../../models'), + + schedulerClient = require('../utils').findModelFixtureEntry('Client', {slug: 'ghost-scheduler'}), + message = 'Add ghost-scheduler client fixture'; + +module.exports = function addGhostFrontendClient(options, logger) { + return models.Client.findOne({slug: schedulerClient.slug}).then(function (client) { + if (!client) { + logger.info(message); + return models.Client.add(schedulerClient, options); + } else { + logger.warn(message); + } + }); +}; diff --git a/core/server/data/migration/fixtures/005/index.js b/core/server/data/migration/fixtures/005/index.js index 291f252e04..8fccf79b8c 100644 --- a/core/server/data/migration/fixtures/005/index.js +++ b/core/server/data/migration/fixtures/005/index.js @@ -1,4 +1,6 @@ module.exports = [ // add jquery setting and privacy info - require('./01-update-ghost-client-secrets') + require('./01-update-ghost-client-secrets'), + // add ghost-scheduler client + require('./02-add-ghost-scheduler-client') ]; diff --git a/core/server/data/migration/fixtures/fixtures.json b/core/server/data/migration/fixtures/fixtures.json index bf99d57ee2..675a5920de 100644 --- a/core/server/data/migration/fixtures/fixtures.json +++ b/core/server/data/migration/fixtures/fixtures.json @@ -42,6 +42,12 @@ "name": "Ghost Frontend", "slug": "ghost-frontend", "status": "enabled" + }, + { + "name": "Ghost Scheduler", + "slug": "ghost-scheduler", + "status": "enabled", + "type": "web" } ] }, diff --git a/core/server/data/schema/schema.js b/core/server/data/schema/schema.js index 488e4e95d4..6bb36fc952 100644 --- a/core/server/data/schema/schema.js +++ b/core/server/data/schema/schema.js @@ -172,7 +172,7 @@ module.exports = { redirection_uri: {type: 'string', maxlength: 2000, nullable: true}, logo: {type: 'string', maxlength: 2000, nullable: true}, status: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'development'}, - type: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'ua'}, + type: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'ua', validations: {isIn: [['ua', 'web', 'native']]}}, description: {type: 'string', maxlength: 200, nullable: true}, created_at: {type: 'dateTime', nullable: false}, created_by: {type: 'integer', nullable: false}, diff --git a/core/test/integration/migration_spec.js b/core/test/integration/migration_spec.js index 9923c0537e..22ce1c0805 100644 --- a/core/test/integration/migration_spec.js +++ b/core/test/integration/migration_spec.js @@ -159,9 +159,10 @@ describe('Database Migration (special functions)', function () { // Clients should.exist(result.clients); - result.clients.length.should.eql(2); + result.clients.length.should.eql(3); result.clients.at(0).get('name').should.eql('Ghost Admin'); result.clients.at(1).get('name').should.eql('Ghost Frontend'); + result.clients.at(2).get('name').should.eql('Ghost Scheduler'); // User (Owner) should.exist(result.users); diff --git a/core/test/unit/migration_fixture_spec.js b/core/test/unit/migration_fixture_spec.js index a75c9e9fff..07b6d6961f 100644 --- a/core/test/unit/migration_fixture_spec.js +++ b/core/test/unit/migration_fixture_spec.js @@ -718,8 +718,9 @@ describe('Fixtures', function () { sequenceStub.firstCall.args[0][0].should.be.a.Function().with.property('name', 'runVersionTasks'); sequenceStub.secondCall.calledWith(sinon.match.array, sinon.match.object, loggerStub).should.be.true(); - sequenceStub.secondCall.args[0].should.be.an.Array().with.lengthOf(1); + sequenceStub.secondCall.args[0].should.be.an.Array().with.lengthOf(2); sequenceStub.secondCall.args[0][0].should.be.a.Function().with.property('name', 'updateGhostClientsSecrets'); + sequenceStub.secondCall.args[0][1].should.be.a.Function().with.property('name', 'addGhostFrontendClient'); // Reset sequenceReset(); @@ -730,7 +731,7 @@ describe('Fixtures', function () { describe('Tasks:', function () { it('should have tasks for 005', function () { should.exist(fixtures005); - fixtures005.should.be.an.Array().with.lengthOf(1); + fixtures005.should.be.an.Array().with.lengthOf(2); }); describe('01-update-ghost-client-secrets', function () { @@ -774,6 +775,44 @@ describe('Fixtures', function () { }).catch(done); }); }); + + describe('02-add-ghost-scheduler-client', function () { + var clientOneStub; + + beforeEach(function () { + clientOneStub = sandbox.stub(models.Client, 'findOne').returns(Promise.resolve({})); + }); + + it('tries to add client correctly', function (done) { + var clientAddStub = sandbox.stub(models.Client, 'add').returns(Promise.resolve()); + clientOneStub.returns(Promise.resolve()); + + fixtures005[1]({}, loggerStub).then(function () { + clientOneStub.calledOnce.should.be.true(); + clientOneStub.calledWith({slug: 'ghost-scheduler'}).should.be.true(); + clientAddStub.calledOnce.should.be.true(); + loggerStub.info.calledOnce.should.be.true(); + loggerStub.warn.called.should.be.false(); + sinon.assert.callOrder(clientOneStub, loggerStub.info, clientAddStub); + + done(); + }).catch(done); + }); + + it('does not try to add client if it already exists', function (done) { + var clientAddStub = sandbox.stub(models.Client, 'add').returns(Promise.resolve()); + + fixtures005[1]({}, loggerStub).then(function () { + clientOneStub.calledOnce.should.be.true(); + clientOneStub.calledWith({slug: 'ghost-scheduler'}).should.be.true(); + clientAddStub.called.should.be.false(); + loggerStub.info.called.should.be.false(); + loggerStub.warn.calledOnce.should.be.true(); + + done(); + }).catch(done); + }); + }); }); }); }); @@ -821,8 +860,8 @@ describe('Fixtures', function () { tagAddStub.calledOnce.should.be.true(); roleOneStub.callCount.should.be.aboveOrEqual(4); roleAddStub.callCount.should.eql(4); - clientOneStub.calledTwice.should.be.true(); - clientAddStub.calledTwice.should.be.true(); + clientOneStub.calledThrice.should.be.true(); + clientAddStub.calledThrice.should.be.true(); permOneStub.callCount.should.eql(30); permsAddStub.called.should.be.true(); diff --git a/core/test/unit/migration_spec.js b/core/test/unit/migration_spec.js index 4d0d663d9d..0b9e328bbf 100644 --- a/core/test/unit/migration_spec.js +++ b/core/test/unit/migration_spec.js @@ -33,7 +33,7 @@ describe('DB version integrity', function () { // Only these variables should need updating var currentDbVersion = '005', currentSchemaHash = 'be706cdbeb06103d90703ee733efc556', - currentFixturesHash = '77ebb081539f9e0c49f487faf7fd929e'; + currentFixturesHash = '21dd859601c8e1c12eaff9eccfbe966a'; // If this test is failing, then it is likely a change has been made that requires a DB version bump, // and the values above will need updating as confirmation