From f78d9d39146190a8236368a394a024947065fb9a Mon Sep 17 00:00:00 2001 From: Rishabh Garg Date: Thu, 4 Oct 2018 21:43:12 +0530 Subject: [PATCH] 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 --- core/test/unit/public/ghost_sdk_spec.js | 255 ++++++++++------------ core/test/unit/services/url/utils_spec.js | 224 ++++++++++--------- core/test/utils/api.js | 17 ++ 3 files changed, 264 insertions(+), 232 deletions(-) diff --git a/core/test/unit/public/ghost_sdk_spec.js b/core/test/unit/public/ghost_sdk_spec.js index 0be02a74c4..7919c5fed6 100644 --- a/core/test/unit/public/ghost_sdk_spec.js +++ b/core/test/unit/public/ghost_sdk_spec.js @@ -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/' diff --git a/core/test/unit/services/url/utils_spec.js b/core/test/unit/services/url/utils_spec.js index 908f6f824b..022dae01b8 100644 --- a/core/test/unit/services/url/utils_spec.js +++ b/core/test/unit/services/url/utils_spec.js @@ -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 () { diff --git a/core/test/utils/api.js b/core/test/utils/api.js index fbb7c2d39a..4e5ac0ffeb 100644 --- a/core/test/utils/api.js +++ b/core/test/utils/api.js @@ -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,