From b74879c64e578cd03a0459bc5a1ae2e9bd48aaec Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Fri, 5 Oct 2018 14:43:29 +0200 Subject: [PATCH] Refactored Ghost SDK unit test refs #9866 - the sdk does not use the url utility - it just respects what you pass in - there is no need to test this behaviour for multiple API versions --- core/test/unit/public/ghost_sdk_spec.js | 187 +++++++----------------- 1 file changed, 52 insertions(+), 135 deletions(-) diff --git a/core/test/unit/public/ghost_sdk_spec.js b/core/test/unit/public/ghost_sdk_spec.js index 7919c5fed6..2cd36643e2 100644 --- a/core/test/unit/public/ghost_sdk_spec.js +++ b/core/test/unit/public/ghost_sdk_spec.js @@ -1,20 +1,7 @@ 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 () { - configUtils.set({ - url: 'http://testblog.com/' - }); - }); - - afterEach(function () { - configUtils.restore(); - }); +describe('Ghost SDK Helper', function () { it('sets url empty if it is not set on init', function () { ghostSdk.init({ clientId: '', @@ -24,123 +11,57 @@ describe('Ghost Ajax Helper', function () { ghostSdk.url.api().should.equal(''); }); - ['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`); - }); + it('renders basic url correctly when no arguments are presented', function () { + ghostSdk.init({ + clientId: '', + clientSecret: '', + url: '/api-url/' }); + + ghostSdk.url.api().should.equal('/api-url/'); + }); + + it('strips arguments of forward and trailing slashes correctly', function () { + ghostSdk.init({ + clientId: '', + clientSecret: '', + url: '/api-url/' + }); + + ghostSdk.url.api('a/', '/b', '/c/').should.equal('/api-url/a/b/c/'); + }); + + it('appends client_id & client_secret to query string automatically', function () { + ghostSdk.init({ + clientId: 'ghost-frontend', + clientSecret: 'notasecret', + url: '/api-url/' + }); + + ghostSdk.url.api().should.equal('/api-url/?client_id=ghost-frontend&client_secret=notasecret'); + }); + + it('generates query parameters correctly', function () { + ghostSdk.init({ + clientId: 'ghost-frontend', + clientSecret: 'notasecret', + url: '/api-url/' + }); + + var rendered = ghostSdk.url.api({a: 'string', b: 5, c: 'en coded'}); + rendered.should.equal('/api-url/?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: '/api-url/' + }); + + var rendered = ghostSdk.url.api('posts/', '/tags/', '/count', {include: 'tags,tests', page: 2}); + + rendered.should.equal('/api-url/posts/tags/count/?include=tags%2Ctests&page=2&client_id=ghost-frontend&client_secret=notasecret'); }); it('handles null/undefined queryOptions correctly', function () { @@ -165,14 +86,10 @@ describe('Ghost Ajax Helper', function () { }); it('should be idempotent', function () { - configUtils.set({ - url: 'https://testblog.com/blog/' - }); - ghostSdk.init({ clientId: 'ghost-frontend', clientSecret: 'notasecret', - url: urlService.utils.urlFor('api', {cors: true, version: 'deprecated', versionType: 'content'}, true) + url: '/api-url/' }); var rendered = ghostSdk.url.api('posts', {limit: 3}),