Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored hardcoded v0.1 url tests to test all supported versions #9945

Merged
merged 2 commits into from Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
255 changes: 121 additions & 134 deletions 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 () {
Expand All @@ -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)
});

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)
['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/?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 () {
Expand All @@ -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/'
Expand Down