2016-01-18 18:37:14 +03:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import {
|
|
|
|
describeModule,
|
|
|
|
it
|
|
|
|
} from 'ember-mocha';
|
|
|
|
import Pretender from 'pretender';
|
2016-05-22 11:20:02 +03:00
|
|
|
import {
|
|
|
|
isAjaxError,
|
|
|
|
isUnauthorizedError
|
|
|
|
} from 'ember-ajax/errors';
|
|
|
|
import {
|
2016-06-30 17:45:02 +03:00
|
|
|
isVersionMismatchError,
|
2016-05-22 11:20:02 +03:00
|
|
|
isRequestEntityTooLargeError,
|
|
|
|
isUnsupportedMediaTypeError
|
|
|
|
} from 'ghost-admin/services/ajax';
|
2016-05-24 15:06:59 +03:00
|
|
|
import config from 'ghost-admin/config/environment';
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2016-06-03 13:51:06 +03:00
|
|
|
function stubAjaxEndpoint(server, response = {}, code = 200) {
|
2016-01-18 18:37:14 +03:00
|
|
|
server.get('/test/', function () {
|
|
|
|
return [
|
2016-02-26 16:25:47 +03:00
|
|
|
code,
|
2016-01-18 18:37:14 +03:00
|
|
|
{'Content-Type': 'application/json'},
|
|
|
|
JSON.stringify(response)
|
|
|
|
];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describeModule(
|
|
|
|
'service:ajax',
|
|
|
|
'Integration: Service: ajax',
|
|
|
|
{
|
|
|
|
integration: true
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
let server;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
server = new Pretender();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
server.shutdown();
|
|
|
|
});
|
|
|
|
|
2016-06-03 13:51:06 +03:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-01-18 18:37:14 +03:00
|
|
|
it('correctly parses single message response text', function (done) {
|
|
|
|
let error = {message: 'Test Error'};
|
2016-06-03 13:51:06 +03:00
|
|
|
stubAjaxEndpoint(server, error, 500);
|
2016-01-18 18:37:14 +03:00
|
|
|
|
|
|
|
let ajax = this.subject();
|
|
|
|
|
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
expect(false).to.be.true();
|
|
|
|
}).catch((error) => {
|
2016-09-26 16:07:18 +03:00
|
|
|
expect(error.errors.length).to.equal(1);
|
|
|
|
expect(error.errors[0].message).to.equal('Test Error');
|
2016-01-18 18:37:14 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('correctly parses single error response text', function (done) {
|
|
|
|
let error = {error: 'Test Error'};
|
2016-06-03 13:51:06 +03:00
|
|
|
stubAjaxEndpoint(server, error, 500);
|
2016-01-18 18:37:14 +03:00
|
|
|
|
|
|
|
let ajax = this.subject();
|
|
|
|
|
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
expect(false).to.be.true();
|
|
|
|
}).catch((error) => {
|
2016-09-26 16:07:18 +03:00
|
|
|
expect(error.errors.length).to.equal(1);
|
|
|
|
expect(error.errors[0].message).to.equal('Test Error');
|
2016-01-18 18:37:14 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('correctly parses multiple error messages', function (done) {
|
|
|
|
let error = {errors: ['First Error', 'Second Error']};
|
2016-06-03 13:51:06 +03:00
|
|
|
stubAjaxEndpoint(server, error, 500);
|
2016-01-18 18:37:14 +03:00
|
|
|
|
|
|
|
let ajax = this.subject();
|
|
|
|
|
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
expect(false).to.be.true();
|
|
|
|
}).catch((error) => {
|
2016-09-26 16:07:18 +03:00
|
|
|
expect(error.errors.length).to.equal(2);
|
|
|
|
expect(error.errors[0].message).to.equal('First Error');
|
|
|
|
expect(error.errors[1].message).to.equal('Second Error');
|
2016-01-18 18:37:14 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-04 13:44:54 +03:00
|
|
|
it('returns default error object for non built-in error', function (done) {
|
2016-06-03 13:51:06 +03:00
|
|
|
stubAjaxEndpoint(server, {}, 500);
|
2016-01-18 18:37:14 +03:00
|
|
|
|
|
|
|
let ajax = this.subject();
|
|
|
|
|
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
expect(false).to.be.true;
|
|
|
|
}).catch((error) => {
|
2016-05-22 11:20:02 +03:00
|
|
|
expect(isAjaxError(error)).to.be.true;
|
2016-04-04 13:44:54 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-22 11:20:02 +03:00
|
|
|
it('handles error checking for built-in errors', function (done) {
|
2016-02-26 16:25:47 +03:00
|
|
|
stubAjaxEndpoint(server, '', 401);
|
2016-04-04 13:44:54 +03:00
|
|
|
|
|
|
|
let ajax = this.subject();
|
|
|
|
|
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
expect(false).to.be.true;
|
|
|
|
}).catch((error) => {
|
2016-05-22 11:20:02 +03:00
|
|
|
expect(isUnauthorizedError(error)).to.be.true;
|
2016-01-18 18:37:14 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-02-26 16:25:47 +03:00
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
it('handles error checking for VersionMismatchError', function (done) {
|
|
|
|
server.get('/test/', function () {
|
|
|
|
return [
|
|
|
|
400,
|
|
|
|
{'Content-Type': 'application/json'},
|
|
|
|
JSON.stringify({
|
|
|
|
errors: [{
|
|
|
|
errorType: 'VersionMismatchError',
|
|
|
|
statusCode: 400
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
];
|
|
|
|
});
|
|
|
|
|
|
|
|
let ajax = this.subject();
|
|
|
|
|
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
expect(false).to.be.true;
|
|
|
|
}).catch((error) => {
|
|
|
|
expect(isVersionMismatchError(error)).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-22 11:20:02 +03:00
|
|
|
it('handles error checking for RequestEntityTooLargeError on 413 errors', function (done) {
|
2016-02-26 16:25:47 +03:00
|
|
|
stubAjaxEndpoint(server, {}, 413);
|
|
|
|
|
|
|
|
let ajax = this.subject();
|
|
|
|
|
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
expect(false).to.be.true;
|
|
|
|
}).catch((error) => {
|
2016-05-22 11:20:02 +03:00
|
|
|
expect(isRequestEntityTooLargeError(error)).to.be.true;
|
2016-02-26 16:25:47 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-22 11:20:02 +03:00
|
|
|
it('handles error checking for UnsupportedMediaTypeError on 415 errors', function (done) {
|
2016-02-26 16:25:47 +03:00
|
|
|
stubAjaxEndpoint(server, {}, 415);
|
|
|
|
|
|
|
|
let ajax = this.subject();
|
|
|
|
|
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
expect(false).to.be.true;
|
|
|
|
}).catch((error) => {
|
2016-05-22 11:20:02 +03:00
|
|
|
expect(isUnsupportedMediaTypeError(error)).to.be.true;
|
2016-02-26 16:25:47 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-01-18 18:37:14 +03:00
|
|
|
}
|
|
|
|
);
|