0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Refactored hardcoded v0.1 url unit tests to support multiple versions (#9945)

refs #9866

- Added test util method for api path based on version
- Updated all hardcoded v0.1 tests strings to use dynamic string from util method
- Updated hardcoded v0.1 tests using regex match to use string equal with new util method
This commit is contained in:
Rishabh Garg 2018-10-04 21:43:12 +05:30 committed by GitHub
parent 434a0435fd
commit f78d9d3914
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 264 additions and 232 deletions

View file

@ -1,7 +1,8 @@
var should = require('should'),
ghostSdk = require('../../../server/public/ghost-sdk'),
configUtils = require('../../utils/configUtils'),
urlService = require('../../../server/services/url');
const should = require('should');
const ghostSdk = require('../../../server/public/ghost-sdk');
const configUtils = require('../../utils/configUtils');
const urlService = require('../../../server/services/url');
const testUtils = require('../../utils');
describe('Ghost Ajax Helper', function () {
beforeEach(function () {
@ -23,82 +24,123 @@ describe('Ghost Ajax Helper', function () {
ghostSdk.url.api().should.equal('');
});
it('renders basic url correctly when no arguments are presented', function () {
ghostSdk.init({
clientId: '',
clientSecret: '',
url: urlService.utils.urlFor('api', {cors: true, version: 'deprecated', versionType: 'content'}, true)
['deprecated', 'active'].forEach((apiVersion) => {
describe(`for api version: ${apiVersion}`, function () {
it('renders basic url correctly when no arguments are presented', function () {
ghostSdk.init({
clientId: '',
clientSecret: '',
url: urlService.utils.urlFor('api', {cors: true, version: apiVersion, versionType: 'content'}, true)
});
ghostSdk.url.api().should.equal(`//testblog.com${testUtils.API.getApiPath({version: apiVersion})}`);
});
it('blog url is https', function () {
configUtils.set({
url: 'https://testblog.com/'
});
ghostSdk.init({
clientId: '',
clientSecret: '',
url: urlService.utils.urlFor('api', {cors: true, version: apiVersion, versionType: 'content'}, true)
});
ghostSdk.url.api().should.equal(`https://testblog.com${testUtils.API.getApiPath({version: apiVersion})}`);
});
it('admin url is https', function () {
configUtils.set({
url: 'http://testblog.com/',
admin: {
url: 'https://admin.testblog.com'
}
});
ghostSdk.init({
clientId: '',
clientSecret: '',
url: urlService.utils.urlFor('api', {cors: true, version: apiVersion, versionType: 'content'}, true)
});
ghostSdk.url.api().should.equal(`https://admin.testblog.com${testUtils.API.getApiPath({version: apiVersion})}`);
});
it('strips arguments of forward and trailing slashes correctly', function () {
ghostSdk.init({
clientId: '',
clientSecret: '',
url: urlService.utils.urlFor('api', {cors: true, version: apiVersion, versionType: 'content'}, true)
});
ghostSdk.url.api('a/', '/b', '/c/').should.equal(`//testblog.com${testUtils.API.getApiPath({version: apiVersion})}a/b/c/`);
});
it('appends client_id & client_secret to query string automatically', function () {
ghostSdk.init({
clientId: 'ghost-frontend',
clientSecret: 'notasecret',
url: urlService.utils.urlFor('api', {cors: true, version: apiVersion, versionType: 'content'}, true)
});
ghostSdk.url.api().should.equal(`//testblog.com${testUtils.API.getApiPath({version: apiVersion})}?client_id=ghost-frontend&client_secret=notasecret`);
});
it('generates query parameters correctly', function () {
ghostSdk.init({
clientId: 'ghost-frontend',
clientSecret: 'notasecret',
url: urlService.utils.urlFor('api', {cors: true, version: apiVersion, versionType: 'content'}, true)
});
var rendered = ghostSdk.url.api({a: 'string', b: 5, c: 'en coded'});
rendered.should.equal(`//testblog.com${testUtils.API.getApiPath({version: apiVersion, versionType: 'content'})}?a=string&b=5&c=en%20coded&client_id=ghost-frontend&client_secret=notasecret`);
});
it('generates complex query correctly', function () {
ghostSdk.init({
clientId: 'ghost-frontend',
clientSecret: 'notasecret',
url: urlService.utils.urlFor('api', {cors: true, version: apiVersion, versionType: 'content'}, true)
});
var rendered = ghostSdk.url.api('posts/', '/tags/', '/count', {include: 'tags,tests', page: 2});
rendered.should.equal(`//testblog.com${testUtils.API.getApiPath({version: apiVersion, versionType: 'content'})}posts/tags/count/?include=tags%2Ctests&page=2&client_id=ghost-frontend&client_secret=notasecret`);
});
it('works with an https config', function () {
configUtils.set({
url: 'https://testblog.com/'
});
ghostSdk.init({
clientId: 'ghost-frontend',
clientSecret: 'notasecret',
url: urlService.utils.urlFor('api', {version: apiVersion, versionType: 'content'}, true)
});
var rendered = ghostSdk.url.api('posts/', '/tags/', '/count', {include: 'tags,tests', page: 2});
rendered.should.equal(`https://testblog.com${testUtils.API.getApiPath({version: apiVersion, versionType: 'content'})}posts/tags/count/?include=tags%2Ctests&page=2&client_id=ghost-frontend&client_secret=notasecret`);
});
it('works with an https config and subdirectory', function () {
configUtils.set({
url: 'https://testblog.com/blog/'
});
ghostSdk.init({
clientId: 'ghost-frontend',
clientSecret: 'notasecret',
url: urlService.utils.urlFor('api', {version: apiVersion, versionType: 'content'}, true)
});
var rendered = ghostSdk.url.api('posts/', '/tags/', '/count', {include: 'tags,tests', page: 2});
rendered.should.equal(`https://testblog.com/blog${testUtils.API.getApiPath({version: apiVersion, versionType: 'content'})}posts/tags/count/?include=tags%2Ctests&page=2&client_id=ghost-frontend&client_secret=notasecret`);
});
});
ghostSdk.url.api().should.equal('//testblog.com/ghost/api/v0.1/');
});
it('blog url is https', function () {
configUtils.set({
url: 'https://testblog.com/'
});
ghostSdk.init({
clientId: '',
clientSecret: '',
url: urlService.utils.urlFor('api', {cors: true, version: 'deprecated', versionType: 'content'}, true)
});
ghostSdk.url.api().should.equal('https://testblog.com/ghost/api/v0.1/');
});
it('admin url is https', function () {
configUtils.set({
url: 'http://testblog.com/',
admin: {
url: 'https://admin.testblog.com'
}
});
ghostSdk.init({
clientId: '',
clientSecret: '',
url: urlService.utils.urlFor('api', {cors: true, version: 'deprecated', versionType: 'content'}, true)
});
ghostSdk.url.api().should.equal('https://admin.testblog.com/ghost/api/v0.1/');
});
it('strips arguments of forward and trailing slashes correctly', function () {
ghostSdk.init({
clientId: '',
clientSecret: '',
url: urlService.utils.urlFor('api', {cors: true, version: 'deprecated', versionType: 'content'}, true)
});
ghostSdk.url.api('a/', '/b', '/c/').should.equal('//testblog.com/ghost/api/v0.1/a/b/c/');
});
it('appends client_id & client_secret to query string automatically', function () {
ghostSdk.init({
clientId: 'ghost-frontend',
clientSecret: 'notasecret',
url: urlService.utils.urlFor('api', {cors: true, version: 'deprecated', versionType: 'content'}, true)
});
ghostSdk.url.api().should.equal('//testblog.com/ghost/api/v0.1/?client_id=ghost-frontend&client_secret=notasecret');
});
it('generates query parameters correctly', function () {
ghostSdk.init({
clientId: 'ghost-frontend',
clientSecret: 'notasecret',
url: urlService.utils.urlFor('api', {cors: true, version: 'deprecated', versionType: 'content'}, true)
});
var rendered = ghostSdk.url.api({a: 'string', b: 5, c: 'en coded'});
rendered.should.match(/\/\/testblog\.com\/ghost\/api\/v0\.1\/\?/);
rendered.should.match(/client_id=ghost-frontend/);
rendered.should.match(/client_secret=notasecret/);
rendered.should.match(/a/);
rendered.should.match(/b=5/);
rendered.should.match(/c=en\%20coded/);
});
it('handles null/undefined queryOptions correctly', function () {
@ -122,61 +164,6 @@ describe('Ghost Ajax Helper', function () {
rendered2.should.match(/client_secret=notasecret/);
});
it('generates complex query correctly', function () {
ghostSdk.init({
clientId: 'ghost-frontend',
clientSecret: 'notasecret',
url: urlService.utils.urlFor('api', {cors: true, version: 'deprecated', versionType: 'content'}, true)
});
var rendered = ghostSdk.url.api('posts/', '/tags/', '/count', {include: 'tags,tests', page: 2});
rendered.should.match(/\/\/testblog\.com\/ghost\/api\/v0\.1\/posts\/tags\/count\/\?/);
rendered.should.match(/client_id=ghost-frontend/);
rendered.should.match(/client_secret=notasecret/);
rendered.should.match(/include=tags%2Ctests/);
rendered.should.match(/page=2/);
});
it('works with an https config', function () {
configUtils.set({
url: 'https://testblog.com/'
});
ghostSdk.init({
clientId: 'ghost-frontend',
clientSecret: 'notasecret',
url: urlService.utils.urlFor('api', {version: 'deprecated', versionType: 'content'}, true)
});
var rendered = ghostSdk.url.api('posts/', '/tags/', '/count', {include: 'tags,tests', page: 2});
rendered.should.match(/https:\/\/testblog\.com\/ghost\/api\/v0\.1\/posts\/tags\/count\/\?/);
rendered.should.match(/client_id=ghost-frontend/);
rendered.should.match(/client_secret=notasecret/);
rendered.should.match(/include=tags%2Ctests/);
rendered.should.match(/page=2/);
});
it('works with an https config and subdirectory', function () {
configUtils.set({
url: 'https://testblog.com/blog/'
});
ghostSdk.init({
clientId: 'ghost-frontend',
clientSecret: 'notasecret',
url: urlService.utils.urlFor('api', {version: 'deprecated', versionType: 'content'}, true)
});
var rendered = ghostSdk.url.api('posts/', '/tags/', '/count', {include: 'tags,tests', page: 2});
rendered.should.match(/https:\/\/testblog\.com\/blog\/ghost\/api\/v0\.1\/posts\/tags\/count\/\?/);
rendered.should.match(/client_id=ghost-frontend/);
rendered.should.match(/client_secret=notasecret/);
rendered.should.match(/include=tags%2Ctests/);
rendered.should.match(/page=2/);
});
it('should be idempotent', function () {
configUtils.set({
url: 'https://testblog.com/blog/'

View file

@ -438,105 +438,133 @@ describe('Url', function () {
urlService.utils.urlFor('admin', true).should.equal('http://something.com/blog/ghost/');
});
it('api: should return admin url is set', function () {
configUtils.set({
url: 'http://my-ghost-blog.com',
admin: {
url: 'https://something.de'
}
['deprecated', 'active'].forEach((apiVersion) => {
describe(`for api version: ${apiVersion}`, function () {
it('api: should return admin url is set', function () {
configUtils.set({
url: 'http://my-ghost-blog.com',
admin: {
url: 'https://something.de'
}
});
urlService.utils
.urlFor('api', {version: apiVersion, versionType: 'content'}, true)
.should.eql(`https://something.de${testUtils.API.getApiPath({version: apiVersion, versionType: 'content'})}`);
});
it('api: url has subdir', function () {
configUtils.set({
url: 'http://my-ghost-blog.com/blog'
});
urlService.utils
.urlFor('api', {version: apiVersion, versionType: 'content'}, true)
.should.eql(`http://my-ghost-blog.com/blog${testUtils.API.getApiPath({version: apiVersion, versionType: 'content'})}`);
});
it('api: relative path is correct', function () {
urlService.utils
.urlFor('api', {version: apiVersion, versionType: 'content'})
.should.eql(testUtils.API.getApiPath({version: apiVersion, versionType: 'content'}));
});
it('api: relative path with subdir is correct', function () {
configUtils.set({
url: 'http://my-ghost-blog.com/blog'
});
urlService.utils
.urlFor('api', {version: apiVersion, versionType: 'content'})
.should.eql(`/blog${testUtils.API.getApiPath({version: apiVersion, versionType: 'content'})}`);
});
it('api: should return http if config.url is http', function () {
configUtils.set({
url: 'http://my-ghost-blog.com'
});
urlService.utils
.urlFor('api', {version: apiVersion, versionType: 'content'}, true)
.should.eql(`http://my-ghost-blog.com${testUtils.API.getApiPath({version: apiVersion, versionType: 'content'})}`);
});
it('api: should return https if config.url is https', function () {
configUtils.set({
url: 'https://my-ghost-blog.com'
});
urlService.utils
.urlFor('api', {version: apiVersion, versionType: 'content'}, true)
.should.eql(`https://my-ghost-blog.com${testUtils.API.getApiPath({version: apiVersion, versionType: 'content'})}`);
});
it('api: with cors, blog url is http: should return no protocol', function () {
configUtils.set({
url: 'http://my-ghost-blog.com'
});
urlService.utils
.urlFor('api', {cors: true, version: apiVersion, versionType: 'content'}, true)
.should.eql(`//my-ghost-blog.com${testUtils.API.getApiPath({version: apiVersion, versionType: 'content'})}`);
});
it('api: with cors, admin url is http: cors should return no protocol', function () {
configUtils.set({
url: 'http://my-ghost-blog.com',
admin: {
url: 'http://admin.ghost.example'
}
});
urlService.utils
.urlFor('api', {cors: true, version: apiVersion, versionType: 'content'}, true)
.should.eql(`//admin.ghost.example${testUtils.API.getApiPath({version: apiVersion, versionType: 'content'})}`);
});
it('api: with cors, admin url is https: should return with protocol', function () {
configUtils.set({
url: 'https://my-ghost-blog.com',
admin: {
url: 'https://admin.ghost.example'
}
});
urlService.utils
.urlFor('api', {cors: true, version: apiVersion, versionType: 'content'}, true)
.should.eql(`https://admin.ghost.example${testUtils.API.getApiPath({version: apiVersion, versionType: 'content'})}`);
});
it('api: with cors, blog url is https: should return with protocol', function () {
configUtils.set({
url: 'https://my-ghost-blog.com'
});
urlService.utils
.urlFor('api', {cors: true, version: apiVersion, versionType: 'content'}, true)
.should.eql(`https://my-ghost-blog.com${testUtils.API.getApiPath({version: apiVersion, versionType: 'content'})}`);
});
it('api: with stable version, blog url is https: should return stable content api path', function () {
configUtils.set({
url: 'https://my-ghost-blog.com'
});
urlService.utils
.urlFor('api', {cors: true, version: apiVersion, versionType: 'content'}, true)
.should.eql(`https://my-ghost-blog.com${testUtils.API.getApiPath({version: apiVersion, versionType: 'content'})}`);
});
it('api: with stable version and admin true, blog url is https: should return stable admin api path', function () {
configUtils.set({
url: 'https://my-ghost-blog.com'
});
urlService.utils
.urlFor('api', {cors: true, version: apiVersion, versionType: 'admin'}, true)
.should.eql(`https://my-ghost-blog.com${testUtils.API.getApiPath({version: apiVersion, versionType: 'admin'})}`);
});
});
urlService.utils.urlFor('api', {version: 'deprecated', versionType: 'content'}, true).should.eql('https://something.de/ghost/api/v0.1/');
});
it('api: url has subdir', function () {
configUtils.set({
url: 'http://my-ghost-blog.com/blog'
});
urlService.utils.urlFor('api', {version: 'deprecated', versionType: 'content'}, true).should.eql('http://my-ghost-blog.com/blog/ghost/api/v0.1/');
});
it('api: relative path is correct', function () {
urlService.utils.urlFor('api', {version: 'deprecated', versionType: 'content'}).should.eql('/ghost/api/v0.1/');
});
it('api: relative path with subdir is correct', function () {
configUtils.set({
url: 'http://my-ghost-blog.com/blog'
});
urlService.utils.urlFor('api', {version: 'deprecated', versionType: 'content'}).should.eql('/blog/ghost/api/v0.1/');
});
it('api: should return http if config.url is http', function () {
configUtils.set({
url: 'http://my-ghost-blog.com'
});
urlService.utils.urlFor('api', {version: 'deprecated', versionType: 'content'}, true).should.eql('http://my-ghost-blog.com/ghost/api/v0.1/');
});
it('api: should return https if config.url is https', function () {
configUtils.set({
url: 'https://my-ghost-blog.com'
});
urlService.utils.urlFor('api', {version: 'deprecated', versionType: 'content'}, true).should.eql('https://my-ghost-blog.com/ghost/api/v0.1/');
});
it('api: with cors, blog url is http: should return no protocol', function () {
configUtils.set({
url: 'http://my-ghost-blog.com'
});
urlService.utils.urlFor('api', {cors: true, version: 'deprecated', versionType: 'content'}, true).should.eql('//my-ghost-blog.com/ghost/api/v0.1/');
});
it('api: with cors, admin url is http: cors should return no protocol', function () {
configUtils.set({
url: 'http://my-ghost-blog.com',
admin: {
url: 'http://admin.ghost.example'
}
});
urlService.utils.urlFor('api', {cors: true, version: 'deprecated', versionType: 'content'}, true).should.eql('//admin.ghost.example/ghost/api/v0.1/');
});
it('api: with cors, admin url is https: should return with protocol', function () {
configUtils.set({
url: 'https://my-ghost-blog.com',
admin: {
url: 'https://admin.ghost.example'
}
});
urlService.utils.urlFor('api', {cors: true, version: 'deprecated', versionType: 'content'}, true).should.eql('https://admin.ghost.example/ghost/api/v0.1/');
});
it('api: with cors, blog url is https: should return with protocol', function () {
configUtils.set({
url: 'https://my-ghost-blog.com'
});
urlService.utils.urlFor('api', {cors: true, version: 'deprecated', versionType: 'content'}, true).should.eql('https://my-ghost-blog.com/ghost/api/v0.1/');
});
it('api: with stable version, blog url is https: should return stable content api path', function () {
configUtils.set({
url: 'https://my-ghost-blog.com'
});
urlService.utils.urlFor('api', {cors: true, version: "deprecated", versionType: 'content'}, true).should.eql('https://my-ghost-blog.com/ghost/api/v0.1/');
});
it('api: with stable version and admin true, blog url is https: should return stable admin api path', function () {
configUtils.set({
url: 'https://my-ghost-blog.com'
});
urlService.utils.urlFor('api', {cors: true, version: "deprecated", versionType: 'admin'}, true).should.eql('https://my-ghost-blog.com/ghost/api/v0.1/');
});
it('api: with active version, blog url is https: should return active content api path', function () {

View file

@ -61,6 +61,22 @@ function getApiQuery(route) {
return url.resolve(ApiRouteBase, route);
}
function getApiPath(options) {
const baseAPIPath = '/ghost/api/';
switch (options.version) {
case 'deprecated':
return `${baseAPIPath}v0.1/`;
case 'active':
if (options.versionType === 'admin') {
return `${baseAPIPath}v2/admin/`;
} else {
return `${baseAPIPath}v2/content/`;
}
default:
return `${baseAPIPath}v0.1/`;
}
}
function getURL() {
return protocol + host;
}
@ -109,6 +125,7 @@ function checkResponse(jsonResponse, objectType, additionalProperties, missingPr
module.exports = {
getApiQuery: getApiQuery,
getApiPath: getApiPath,
getSigninURL: getSigninURL,
getAdminURL: getAdminURL,
getURL: getURL,