diff --git a/core/test/functional/routes/api/utils.js b/core/test/functional/routes/api/utils.js index d83b61d30c..0cfb32b927 100644 --- a/core/test/functional/routes/api/utils.js +++ b/core/test/functional/routes/api/utils.js @@ -13,6 +13,6 @@ module.exports = { doAuth() { const args = Array.prototype.slice.call(arguments); args.unshift(`${API_URL}authentication/token/`); - return testUtils.doAuth.apply(null, args); + return testUtils.API.doAuth.apply(null, args); } }; diff --git a/core/test/utils/api.js b/core/test/utils/api.js index 7e60065643..d798abbe7e 100644 --- a/core/test/utils/api.js +++ b/core/test/utils/api.js @@ -1,7 +1,10 @@ var _ = require('lodash'), url = require('url'), moment = require('moment'), + DataGenerator = require('./fixtures/data-generator'), config = require('../../server/config'), + common = require('../../server/lib/common'), + sequence = require('../../server/lib/promise/sequence'), schema = require('../../server/data/schema').tables, host = config.get('server').host, port = config.get('server').port, @@ -102,11 +105,73 @@ function checkResponse(jsonResponse, objectType, additionalProperties, missingPr checkResponseValue(jsonResponse, checkProperties); } -module.exports = { - getSigninURL: getSigninURL, - getAdminURL: getAdminURL, - getURL: getURL, - checkResponse: checkResponse, - checkResponseValue: checkResponseValue, - isISO8601: isISO8601 +/** + * This function manages the work of ensuring we have an overridden owner user, and grabbing an access token + * + * @TODO make this do the DB init as well + */ +const doAuth = (apiOptions) => { + return function doAuthInner() { + let API_URL = arguments[0]; + let request = arguments[1]; + let options = arguments; + let fixtureOps; + + // Remove API_URL & request from this list + delete options[0]; + delete options[1]; + + // No DB setup, but override the owner + options = _.merge({'owner:post': true}, _.transform(options, function (result, val) { + if (val) { + result[val] = true; + } + })); + + fixtureOps = apiOptions.getFixtureOps(options); + + return sequence(fixtureOps).then(function () { + return login(request, API_URL); + }); + }; +}; + +const login = (request, API_URL) => { + // CASE: by default we use the owner to login + if (!request.user) { + request.user = DataGenerator.Content.users[0]; + } + + return new Promise(function (resolve, reject) { + request.post(API_URL) + .set('Origin', config.get('url')) + .send({ + grant_type: 'password', + username: request.user.email, + password: 'Sl1m3rson99', + client_id: 'ghost-admin', + client_secret: 'not_available' + }).then(function then(res) { + if (res.statusCode !== 200) { + return reject(new common.errors.GhostError({ + message: res.body.errors[0].message + })); + } + + resolve(res.body.access_token); + }, reject); + }); +}; + +module.exports = (options = {}) => { + return { + getSigninURL: getSigninURL, + getAdminURL: getAdminURL, + doAuth: doAuth(options), + login: login, + getURL: getURL, + checkResponse: checkResponse, + checkResponseValue: checkResponseValue, + isISO8601: isISO8601 + }; }; diff --git a/core/test/utils/index.js b/core/test/utils/index.js index 073e347cb0..52ca21d744 100644 --- a/core/test/utils/index.js +++ b/core/test/utils/index.js @@ -30,7 +30,7 @@ var Promise = require('bluebird'), DataGenerator = require('./fixtures/data-generator'), configUtils = require('./configUtils'), filterData = require('./fixtures/filter-param'), - APIAssertions = require('./api'), + APIUtils = require('./api'), mocks = require('./mocks'), config = require('../../server/config'), knexMigrator = new KnexMigrator(), @@ -45,12 +45,9 @@ var Promise = require('bluebird'), teardown, setup, truncate, - doAuth, createUser, createPost, - login, startGhost, - configureGhost, initFixtures, initData, @@ -728,37 +725,6 @@ setup = function setup() { }; }; -// ## Shared functions for Routing Tests - -/** - * This function manages the work of ensuring we have an overridden owner user, and grabbing an access token - * @returns {deferred.promise} - */ -// TODO make this do the DB init as well -doAuth = function doAuth() { - let API_URL = arguments[0]; - let request = arguments[1]; - let options = arguments; - let fixtureOps; - - // Remove API_URL & request from this list - delete options[0]; - delete options[1]; - - // No DB setup, but override the owner - options = _.merge({'owner:post': true}, _.transform(options, function (result, val) { - if (val) { - result[val] = true; - } - })); - - fixtureOps = getFixtureOps(options); - - return sequence(fixtureOps).then(function () { - return login(request, API_URL); - }); -}; - createUser = function createUser(options) { var user = options.user, role = options.role; @@ -786,33 +752,6 @@ createPost = function createPost(options) { return models.Post.add(post, module.exports.context.internal); }; -login = function login(request, API_URL) { - // CASE: by default we use the owner to login - if (!request.user) { - request.user = DataGenerator.Content.users[0]; - } - - return new Promise(function (resolve, reject) { - request.post(API_URL) - .set('Origin', config.get('url')) - .send({ - grant_type: 'password', - username: request.user.email, - password: 'Sl1m3rson99', - client_id: 'ghost-admin', - client_secret: 'not_available' - }).then(function then(res) { - if (res.statusCode !== 200) { - return reject(new common.errors.GhostError({ - message: res.body.errors[0].message - })); - } - - resolve(res.body.access_token); - }, reject); - }); -}; - /** * Has to run in a transaction for MySQL, otherwise the foreign key check does not work. * Sqlite3 has no truncate command. @@ -1104,10 +1043,8 @@ module.exports = { teardown: teardown, truncate: truncate, setup: setup, - doAuth: doAuth, createUser: createUser, createPost: createPost, - login: login, mockNotExistingModule: mockNotExistingModule, unmockNotExistingModule: unmockNotExistingModule, @@ -1150,7 +1087,7 @@ module.exports = { DataGenerator: DataGenerator, filterData: filterData, - API: APIAssertions, + API: APIUtils({getFixtureOps: getFixtureOps}), // Helpers to make it easier to write tests which are easy to read context: {