2016-01-18 18:37:14 +03:00
|
|
|
import Pretender from 'pretender';
|
2017-05-29 21:50:03 +03:00
|
|
|
import config from 'ghost-admin/config/environment';
|
|
|
|
import {describe, it} from 'mocha';
|
|
|
|
import {expect} from 'chai';
|
2016-05-22 11:20:02 +03:00
|
|
|
import {
|
|
|
|
isAjaxError,
|
|
|
|
isUnauthorizedError
|
|
|
|
} from 'ember-ajax/errors';
|
|
|
|
import {
|
2018-10-05 21:46:33 +03:00
|
|
|
isMaintenanceError,
|
2016-05-22 11:20:02 +03:00
|
|
|
isRequestEntityTooLargeError,
|
2017-05-29 21:50:03 +03:00
|
|
|
isUnsupportedMediaTypeError,
|
|
|
|
isVersionMismatchError
|
2016-05-22 11:20:02 +03:00
|
|
|
} from 'ghost-admin/services/ajax';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {setupTest} from 'ember-mocha';
|
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)
|
|
|
|
];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
describe('Integration: Service: ajax', function () {
|
2019-05-13 15:43:53 +03:00
|
|
|
setupTest();
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
let server;
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
beforeEach(function () {
|
|
|
|
server = new Pretender();
|
|
|
|
});
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
afterEach(function () {
|
|
|
|
server.shutdown();
|
|
|
|
});
|
2016-06-03 13:51:06 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('adds Ghost version header to requests', function (done) {
|
|
|
|
let {version} = config.APP;
|
2019-05-13 15:43:53 +03:00
|
|
|
let ajax = this.owner.lookup('service:ajax');
|
2016-06-03 13:51:06 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
stubAjaxEndpoint(server, {});
|
|
|
|
|
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
let [request] = server.handledRequests;
|
|
|
|
expect(request.requestHeaders['X-Ghost-Version']).to.equal(version);
|
|
|
|
done();
|
2016-06-03 13:51:06 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-06-03 13:51:06 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('correctly parses single message response text', function (done) {
|
|
|
|
let error = {message: 'Test Error'};
|
|
|
|
stubAjaxEndpoint(server, error, 500);
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let ajax = this.owner.lookup('service:ajax');
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
expect(false).to.be.true();
|
|
|
|
}).catch((error) => {
|
2017-11-04 01:59:39 +03:00
|
|
|
expect(error.payload.errors.length).to.equal(1);
|
|
|
|
expect(error.payload.errors[0].message).to.equal('Test Error');
|
2016-11-24 01:50:57 +03:00
|
|
|
done();
|
2016-01-18 18:37:14 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('correctly parses single error response text', function (done) {
|
|
|
|
let error = {error: 'Test Error'};
|
|
|
|
stubAjaxEndpoint(server, error, 500);
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let ajax = this.owner.lookup('service:ajax');
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
expect(false).to.be.true();
|
|
|
|
}).catch((error) => {
|
2017-11-04 01:59:39 +03:00
|
|
|
expect(error.payload.errors.length).to.equal(1);
|
|
|
|
expect(error.payload.errors[0].message).to.equal('Test Error');
|
2016-11-24 01:50:57 +03:00
|
|
|
done();
|
2016-01-18 18:37:14 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('correctly parses multiple error messages', function (done) {
|
|
|
|
let error = {errors: ['First Error', 'Second Error']};
|
|
|
|
stubAjaxEndpoint(server, error, 500);
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let ajax = this.owner.lookup('service:ajax');
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
expect(false).to.be.true();
|
|
|
|
}).catch((error) => {
|
2017-11-04 01:59:39 +03:00
|
|
|
expect(error.payload.errors.length).to.equal(2);
|
|
|
|
expect(error.payload.errors[0].message).to.equal('First Error');
|
|
|
|
expect(error.payload.errors[1].message).to.equal('Second Error');
|
2016-11-24 01:50:57 +03:00
|
|
|
done();
|
2016-01-18 18:37:14 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('returns default error object for non built-in error', function (done) {
|
|
|
|
stubAjaxEndpoint(server, {}, 500);
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let ajax = this.owner.lookup('service:ajax');
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
expect(false).to.be.true;
|
|
|
|
}).catch((error) => {
|
|
|
|
expect(isAjaxError(error)).to.be.true;
|
|
|
|
done();
|
2016-04-04 13:44:54 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-04-04 13:44:54 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('handles error checking for built-in errors', function (done) {
|
|
|
|
stubAjaxEndpoint(server, '', 401);
|
2016-04-04 13:44:54 +03:00
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let ajax = this.owner.lookup('service:ajax');
|
2016-04-04 13:44:54 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
expect(false).to.be.true;
|
|
|
|
}).catch((error) => {
|
|
|
|
expect(isUnauthorizedError(error)).to.be.true;
|
|
|
|
done();
|
2016-01-18 18:37:14 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-02-26 16:25:47 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('handles error checking for VersionMismatchError', function (done) {
|
|
|
|
server.get('/test/', function () {
|
|
|
|
return [
|
|
|
|
400,
|
|
|
|
{'Content-Type': 'application/json'},
|
|
|
|
JSON.stringify({
|
|
|
|
errors: [{
|
2019-03-25 14:29:14 +03:00
|
|
|
type: 'VersionMismatchError',
|
2016-11-24 01:50:57 +03:00
|
|
|
statusCode: 400
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
];
|
|
|
|
});
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let ajax = this.owner.lookup('service:ajax');
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
expect(false).to.be.true;
|
|
|
|
}).catch((error) => {
|
|
|
|
expect(isVersionMismatchError(error)).to.be.true;
|
|
|
|
done();
|
2016-06-30 17:45:02 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('handles error checking for RequestEntityTooLargeError on 413 errors', function (done) {
|
|
|
|
stubAjaxEndpoint(server, {}, 413);
|
2016-02-26 16:25:47 +03:00
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let ajax = this.owner.lookup('service:ajax');
|
2016-02-26 16:25:47 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
expect(false).to.be.true;
|
|
|
|
}).catch((error) => {
|
|
|
|
expect(isRequestEntityTooLargeError(error)).to.be.true;
|
|
|
|
done();
|
2016-02-26 16:25:47 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-02-26 16:25:47 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('handles error checking for UnsupportedMediaTypeError on 415 errors', function (done) {
|
|
|
|
stubAjaxEndpoint(server, {}, 415);
|
2016-02-26 16:25:47 +03:00
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let ajax = this.owner.lookup('service:ajax');
|
2016-02-26 16:25:47 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
expect(false).to.be.true;
|
|
|
|
}).catch((error) => {
|
|
|
|
expect(isUnsupportedMediaTypeError(error)).to.be.true;
|
|
|
|
done();
|
2016-02-26 16:25:47 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-10-17 16:32:22 +03:00
|
|
|
|
2018-10-05 21:46:33 +03:00
|
|
|
it('handles error checking for MaintenanceError on 503 errors', function (done) {
|
|
|
|
stubAjaxEndpoint(server, {}, 503);
|
2016-10-17 16:32:22 +03:00
|
|
|
|
2019-05-13 15:43:53 +03:00
|
|
|
let ajax = this.owner.lookup('service:ajax');
|
2016-10-17 16:32:22 +03:00
|
|
|
|
2018-10-05 21:46:33 +03:00
|
|
|
ajax.request('/test/').then(() => {
|
|
|
|
expect(false).to.be.true;
|
|
|
|
}).catch((error) => {
|
|
|
|
expect(isMaintenanceError(error)).to.be.true;
|
|
|
|
done();
|
2016-10-17 16:32:22 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
|
|
|
});
|