2
1
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2023-12-13 21:00:40 +01:00

Moved shared test utility fn's doAuth and login to shared api utility

refs #9866

- try to keep API interactions for routing tests in a single place, because it gives a better overview
- tiny improvement
- there are lot's of other things we could do, but we want to limit the changes for now

Goal:

e2e/api/v0.1
  utils (local)
e2e/api/v2
  utils (local)
utils
  api (shared)
This commit is contained in:
kirrg001 2018-10-05 15:08:30 +02:00 committed by Katharina Irrgang
parent b855f2d8f0
commit 353f5d9181
3 changed files with 75 additions and 73 deletions

View file

@ -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);
}
};

View file

@ -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
};
};

View file

@ -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<AccessToken>}
*/
// 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: {