mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05: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:
parent
b855f2d8f0
commit
353f5d9181
3 changed files with 75 additions and 73 deletions
|
@ -13,6 +13,6 @@ module.exports = {
|
||||||
doAuth() {
|
doAuth() {
|
||||||
const args = Array.prototype.slice.call(arguments);
|
const args = Array.prototype.slice.call(arguments);
|
||||||
args.unshift(`${API_URL}authentication/token/`);
|
args.unshift(`${API_URL}authentication/token/`);
|
||||||
return testUtils.doAuth.apply(null, args);
|
return testUtils.API.doAuth.apply(null, args);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
var _ = require('lodash'),
|
var _ = require('lodash'),
|
||||||
url = require('url'),
|
url = require('url'),
|
||||||
moment = require('moment'),
|
moment = require('moment'),
|
||||||
|
DataGenerator = require('./fixtures/data-generator'),
|
||||||
config = require('../../server/config'),
|
config = require('../../server/config'),
|
||||||
|
common = require('../../server/lib/common'),
|
||||||
|
sequence = require('../../server/lib/promise/sequence'),
|
||||||
schema = require('../../server/data/schema').tables,
|
schema = require('../../server/data/schema').tables,
|
||||||
host = config.get('server').host,
|
host = config.get('server').host,
|
||||||
port = config.get('server').port,
|
port = config.get('server').port,
|
||||||
|
@ -102,11 +105,73 @@ function checkResponse(jsonResponse, objectType, additionalProperties, missingPr
|
||||||
checkResponseValue(jsonResponse, checkProperties);
|
checkResponseValue(jsonResponse, checkProperties);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
/**
|
||||||
|
* 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,
|
getSigninURL: getSigninURL,
|
||||||
getAdminURL: getAdminURL,
|
getAdminURL: getAdminURL,
|
||||||
|
doAuth: doAuth(options),
|
||||||
|
login: login,
|
||||||
getURL: getURL,
|
getURL: getURL,
|
||||||
checkResponse: checkResponse,
|
checkResponse: checkResponse,
|
||||||
checkResponseValue: checkResponseValue,
|
checkResponseValue: checkResponseValue,
|
||||||
isISO8601: isISO8601
|
isISO8601: isISO8601
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
|
@ -30,7 +30,7 @@ var Promise = require('bluebird'),
|
||||||
DataGenerator = require('./fixtures/data-generator'),
|
DataGenerator = require('./fixtures/data-generator'),
|
||||||
configUtils = require('./configUtils'),
|
configUtils = require('./configUtils'),
|
||||||
filterData = require('./fixtures/filter-param'),
|
filterData = require('./fixtures/filter-param'),
|
||||||
APIAssertions = require('./api'),
|
APIUtils = require('./api'),
|
||||||
mocks = require('./mocks'),
|
mocks = require('./mocks'),
|
||||||
config = require('../../server/config'),
|
config = require('../../server/config'),
|
||||||
knexMigrator = new KnexMigrator(),
|
knexMigrator = new KnexMigrator(),
|
||||||
|
@ -45,12 +45,9 @@ var Promise = require('bluebird'),
|
||||||
teardown,
|
teardown,
|
||||||
setup,
|
setup,
|
||||||
truncate,
|
truncate,
|
||||||
doAuth,
|
|
||||||
createUser,
|
createUser,
|
||||||
createPost,
|
createPost,
|
||||||
login,
|
|
||||||
startGhost,
|
startGhost,
|
||||||
configureGhost,
|
|
||||||
|
|
||||||
initFixtures,
|
initFixtures,
|
||||||
initData,
|
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) {
|
createUser = function createUser(options) {
|
||||||
var user = options.user,
|
var user = options.user,
|
||||||
role = options.role;
|
role = options.role;
|
||||||
|
@ -786,33 +752,6 @@ createPost = function createPost(options) {
|
||||||
return models.Post.add(post, module.exports.context.internal);
|
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.
|
* Has to run in a transaction for MySQL, otherwise the foreign key check does not work.
|
||||||
* Sqlite3 has no truncate command.
|
* Sqlite3 has no truncate command.
|
||||||
|
@ -1104,10 +1043,8 @@ module.exports = {
|
||||||
teardown: teardown,
|
teardown: teardown,
|
||||||
truncate: truncate,
|
truncate: truncate,
|
||||||
setup: setup,
|
setup: setup,
|
||||||
doAuth: doAuth,
|
|
||||||
createUser: createUser,
|
createUser: createUser,
|
||||||
createPost: createPost,
|
createPost: createPost,
|
||||||
login: login,
|
|
||||||
|
|
||||||
mockNotExistingModule: mockNotExistingModule,
|
mockNotExistingModule: mockNotExistingModule,
|
||||||
unmockNotExistingModule: unmockNotExistingModule,
|
unmockNotExistingModule: unmockNotExistingModule,
|
||||||
|
@ -1150,7 +1087,7 @@ module.exports = {
|
||||||
|
|
||||||
DataGenerator: DataGenerator,
|
DataGenerator: DataGenerator,
|
||||||
filterData: filterData,
|
filterData: filterData,
|
||||||
API: APIAssertions,
|
API: APIUtils({getFixtureOps: getFixtureOps}),
|
||||||
|
|
||||||
// Helpers to make it easier to write tests which are easy to read
|
// Helpers to make it easier to write tests which are easy to read
|
||||||
context: {
|
context: {
|
||||||
|
|
Loading…
Add table
Reference in a new issue