From 4cc4781b3e8ca5347f29d4c9bfa449739264bc28 Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Fri, 3 Jun 2016 11:51:06 +0100 Subject: [PATCH] Add version header to API requests no issue - modifies the version info included in `env.APP.version` to only include the `major.minor` version numbers - update base adapter to include `X-Ghost-Version` header - update `ajax` service to include `X-Ghost-Version` header --- ghost/admin/app/adapters/base.js | 5 ++ ghost/admin/app/services/ajax.js | 12 ++--- ghost/admin/config/environment.js | 9 +++- ghost/admin/package.json | 2 +- .../tests/integration/services/ajax-test.js | 24 ++++++++-- .../tests/integration/services/store-test.js | 46 +++++++++++++++++++ 6 files changed, 84 insertions(+), 14 deletions(-) create mode 100644 ghost/admin/tests/integration/services/store-test.js diff --git a/ghost/admin/app/adapters/base.js b/ghost/admin/app/adapters/base.js index c5d42c0ce9..7c8395eec7 100644 --- a/ghost/admin/app/adapters/base.js +++ b/ghost/admin/app/adapters/base.js @@ -2,6 +2,7 @@ import Ember from 'ember'; import RESTAdapter from 'ember-data/adapters/rest'; import ghostPaths from 'ghost/utils/ghost-paths'; import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin'; +import config from 'ghost/config/environment'; const { inject: {service} @@ -15,6 +16,10 @@ export default RESTAdapter.extend(DataAdapterMixin, { session: service(), + headers: { + 'X-Ghost-Version': config.APP.version + }, + shouldBackgroundReloadRecord() { return false; }, diff --git a/ghost/admin/app/services/ajax.js b/ghost/admin/app/services/ajax.js index c8558bf14f..9db200abe2 100644 --- a/ghost/admin/app/services/ajax.js +++ b/ghost/admin/app/services/ajax.js @@ -1,6 +1,7 @@ import Ember from 'ember'; import AjaxService from 'ember-ajax/services/ajax'; import {AjaxError} from 'ember-ajax/errors'; +import config from 'ghost/config/environment'; const {inject, computed} = Ember; @@ -24,18 +25,17 @@ export default AjaxService.extend({ headers: computed('session.isAuthenticated', function () { let session = this.get('session'); + let headers = {}; + + headers['X-Ghost-Version'] = config.APP.version; if (session.get('isAuthenticated')) { - let headers = {}; - session.authorize('authorizer:oauth2', (headerName, headerValue) => { headers[headerName] = headerValue; }); - - return headers; - } else { - return []; } + + return headers; }), handleResponse(status, headers, payload) { diff --git a/ghost/admin/config/environment.js b/ghost/admin/config/environment.js index 3889a71ff3..091cca129f 100644 --- a/ghost/admin/config/environment.js +++ b/ghost/admin/config/environment.js @@ -15,8 +15,13 @@ module.exports = function (environment) { }, APP: { - // Here you can pass flags/options to your application instance - // when it is created + // Here you can pass flags/options to your application instance + // when it is created + + // override the default version string which contains git info from + // https://github.com/cibernox/git-repo-version. Only include the + // `major.minor` version numbers + version: require('../package.json').version.replace(/\.\d+$/, '') }, 'ember-simple-auth': { diff --git a/ghost/admin/package.json b/ghost/admin/package.json index 68f78fdb58..0bdc04cc2b 100644 --- a/ghost/admin/package.json +++ b/ghost/admin/package.json @@ -1,6 +1,6 @@ { "name": "ghost", - "version": "0.0.0", + "version": "0.8.0", "description": "Small description for ghost goes here", "private": true, "directories": { diff --git a/ghost/admin/tests/integration/services/ajax-test.js b/ghost/admin/tests/integration/services/ajax-test.js index 23e45488df..7dc4480062 100644 --- a/ghost/admin/tests/integration/services/ajax-test.js +++ b/ghost/admin/tests/integration/services/ajax-test.js @@ -6,8 +6,9 @@ import { import Pretender from 'pretender'; import {AjaxError, UnauthorizedError} from 'ember-ajax/errors'; import {RequestEntityTooLargeError, UnsupportedMediaTypeError} from 'ghost/services/ajax'; +import config from 'ghost/config/environment'; -function stubAjaxEndpoint(server, response = {}, code = 500) { +function stubAjaxEndpoint(server, response = {}, code = 200) { server.get('/test/', function () { return [ code, @@ -34,9 +35,22 @@ describeModule( server.shutdown(); }); + it('adds Ghost version header to requests', function (done) { + let {version} = config.APP; + let ajax = this.subject(); + + stubAjaxEndpoint(server, {}); + + ajax.request('/test/').then(() => { + let [request] = server.handledRequests; + expect(request.requestHeaders['X-Ghost-Version']).to.equal(version); + done(); + }); + }); + it('correctly parses single message response text', function (done) { let error = {message: 'Test Error'}; - stubAjaxEndpoint(server, error); + stubAjaxEndpoint(server, error, 500); let ajax = this.subject(); @@ -50,7 +64,7 @@ describeModule( it('correctly parses single error response text', function (done) { let error = {error: 'Test Error'}; - stubAjaxEndpoint(server, error); + stubAjaxEndpoint(server, error, 500); let ajax = this.subject(); @@ -64,7 +78,7 @@ describeModule( it('correctly parses multiple error messages', function (done) { let error = {errors: ['First Error', 'Second Error']}; - stubAjaxEndpoint(server, error); + stubAjaxEndpoint(server, error, 500); let ajax = this.subject(); @@ -77,7 +91,7 @@ describeModule( }); it('returns default error object for non built-in error', function (done) { - stubAjaxEndpoint(server, {}); + stubAjaxEndpoint(server, {}, 500); let ajax = this.subject(); diff --git a/ghost/admin/tests/integration/services/store-test.js b/ghost/admin/tests/integration/services/store-test.js new file mode 100644 index 0000000000..f4dcdc2b4b --- /dev/null +++ b/ghost/admin/tests/integration/services/store-test.js @@ -0,0 +1,46 @@ +import { expect } from 'chai'; +import { + describeModule, + it +} from 'ember-mocha'; +import Pretender from 'pretender'; +import config from 'ghost/config/environment'; + +describeModule( + 'service:store', + 'Integration: Service: store', + { + integration: true + }, + function () { + let server; + + beforeEach(function () { + server = new Pretender(); + }); + + afterEach(function () { + server.shutdown(); + }); + + it('adds Ghost version header to requests', function (done) { + let {version} = config.APP; + let store = this.subject(); + + server.get('/ghost/api/v0.1/posts/1/', function () { + return [ + 404, + {'Content-Type': 'application/json'}, + JSON.stringify({}) + ]; + }); + + store.find('post', 1).catch(() => { + let [request] = server.handledRequests; + console.log(request); + expect(request.requestHeaders['X-Ghost-Version']).to.equal(version); + done(); + }); + }); + } +);