2021-12-07 21:40:46 +03:00
|
|
|
// Switch these lines once there are useful utils
|
|
|
|
// const testUtils = require('./utils');
|
|
|
|
require('./utils');
|
2022-04-08 07:40:37 +03:00
|
|
|
const should = require('should');
|
2021-12-14 15:23:17 +03:00
|
|
|
const {InternalServerError} = require('@tryghost/errors');
|
2022-04-08 06:24:03 +03:00
|
|
|
const {
|
|
|
|
prepareError,
|
|
|
|
handleJSONResponse,
|
|
|
|
handleHTMLResponse,
|
2022-04-08 07:40:37 +03:00
|
|
|
prepareStack,
|
2022-05-06 17:42:58 +03:00
|
|
|
resourceNotFound,
|
|
|
|
pageNotFound
|
2022-04-08 06:24:03 +03:00
|
|
|
} = require('../');
|
2021-12-07 21:40:46 +03:00
|
|
|
|
2021-12-14 15:23:17 +03:00
|
|
|
describe('Prepare Error', function () {
|
|
|
|
it('Correctly prepares a normal error', function (done) {
|
|
|
|
prepareError(new Error('test!'), {}, {
|
|
|
|
set: () => {}
|
|
|
|
}, (err) => {
|
|
|
|
err.statusCode.should.eql(500);
|
|
|
|
err.name.should.eql('InternalServerError');
|
|
|
|
err.stack.should.startWith('Error: test!');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2022-05-06 17:42:58 +03:00
|
|
|
|
|
|
|
it('Correctly prepares a 404 error', function (done) {
|
|
|
|
let error = {message: 'Oh dear', statusCode: 404};
|
|
|
|
|
|
|
|
prepareError(error, {}, {
|
|
|
|
set: () => {}
|
|
|
|
}, (err) => {
|
|
|
|
err.statusCode.should.eql(404);
|
|
|
|
err.name.should.eql('NotFoundError');
|
|
|
|
err.stack.should.startWith('NotFoundError: Resource could not be found');
|
|
|
|
err.hideStack.should.eql(true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Correctly prepares an error array', function (done) {
|
|
|
|
prepareError([new Error('test!')], {}, {
|
|
|
|
set: () => {}
|
|
|
|
}, (err) => {
|
|
|
|
err.statusCode.should.eql(500);
|
|
|
|
err.name.should.eql('InternalServerError');
|
|
|
|
err.stack.should.startWith('Error: test!');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Correctly prepares a handlebars errpr', function (done) {
|
|
|
|
let error = new Error('obscure handlebars message!');
|
|
|
|
error.stack += '\nnode_modules/handlebars/something';
|
|
|
|
|
|
|
|
prepareError(error, {}, {
|
|
|
|
set: () => {}
|
|
|
|
}, (err) => {
|
|
|
|
err.statusCode.should.eql(400);
|
|
|
|
err.name.should.eql('IncorrectUsageError');
|
|
|
|
err.stack.should.startWith('Error: obscure handlebars message!');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2021-12-14 15:23:17 +03:00
|
|
|
});
|
|
|
|
|
2022-03-21 12:33:54 +03:00
|
|
|
describe('Prepare Stack', function () {
|
|
|
|
it('Correctly prepares the stack for an error', function (done) {
|
|
|
|
prepareStack(new Error('test!'), {}, {}, (err) => {
|
|
|
|
// Includes "Stack Trace" text prepending human readable trace
|
|
|
|
err.stack.should.startWith('Error: test!\nStack Trace:');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-14 15:23:17 +03:00
|
|
|
describe('Error renderers', function () {
|
|
|
|
it('Renders JSON', function (done) {
|
|
|
|
const errorRenderer = handleJSONResponse({
|
|
|
|
errorHandler: () => {}
|
2022-03-21 12:33:54 +03:00
|
|
|
})[3];
|
2021-12-14 15:23:17 +03:00
|
|
|
|
|
|
|
errorRenderer(new Error('test!'), {}, {
|
|
|
|
json: (data) => {
|
|
|
|
data.errors.length.should.eql(1);
|
|
|
|
data.errors[0].message.should.eql('test!');
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
}, () => {});
|
|
|
|
});
|
|
|
|
|
2022-03-24 13:06:55 +03:00
|
|
|
it('Handles unknown errors when preparing user message', function (done) {
|
2022-05-06 17:42:58 +03:00
|
|
|
const errorRenderer = handleJSONResponse({
|
2022-03-24 13:06:55 +03:00
|
|
|
errorHandler: () => {}
|
|
|
|
})[3];
|
|
|
|
|
|
|
|
errorRenderer(new RangeError('test!'), {
|
|
|
|
frameOptions: {
|
|
|
|
docName: 'oembed',
|
|
|
|
method: 'read'
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
json: (data) => {
|
|
|
|
data.errors.length.should.eql(1);
|
|
|
|
data.errors[0].message.should.eql('Unknown error - RangeError, cannot read oembed.');
|
|
|
|
data.errors[0].context.should.eql('test!');
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
}, () => {});
|
|
|
|
});
|
|
|
|
|
2021-12-14 15:23:17 +03:00
|
|
|
it('Uses templates when required', function (done) {
|
2022-05-06 17:42:58 +03:00
|
|
|
const errorRenderer = handleJSONResponse({
|
2021-12-14 15:23:17 +03:00
|
|
|
errorHandler: () => {}
|
2022-03-21 12:33:54 +03:00
|
|
|
})[3];
|
2021-12-14 15:23:17 +03:00
|
|
|
|
|
|
|
errorRenderer(new InternalServerError({
|
|
|
|
message: 'test!'
|
|
|
|
}), {
|
|
|
|
frameOptions: {
|
|
|
|
docName: 'blog',
|
|
|
|
method: 'browse'
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
json: (data) => {
|
|
|
|
data.errors.length.should.eql(1);
|
|
|
|
data.errors[0].message.should.eql('Internal server error, cannot list blog.');
|
2022-05-06 17:42:58 +03:00
|
|
|
data.errors[0].context.should.eql('test!');
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
}, () => {});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Uses defined message + context when available', function (done) {
|
|
|
|
const errorRenderer = handleJSONResponse({
|
|
|
|
errorHandler: () => {}
|
|
|
|
})[3];
|
|
|
|
|
|
|
|
errorRenderer(new InternalServerError({
|
|
|
|
message: 'test!',
|
|
|
|
context: 'Image was too large.'
|
|
|
|
}), {
|
|
|
|
frameOptions: {
|
|
|
|
docName: 'images',
|
|
|
|
method: 'upload'
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
json: (data) => {
|
|
|
|
data.errors.length.should.eql(1);
|
|
|
|
data.errors[0].message.should.eql('Internal server error, cannot upload image.');
|
|
|
|
data.errors[0].context.should.eql('test! Image was too large.');
|
2021-12-14 15:23:17 +03:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
}, () => {});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Exports the HTML renderer', function () {
|
|
|
|
const renderer = handleHTMLResponse({
|
|
|
|
errorHandler: () => {}
|
|
|
|
});
|
|
|
|
|
2022-03-21 12:33:54 +03:00
|
|
|
renderer.length.should.eql(3);
|
2021-12-07 21:40:46 +03:00
|
|
|
});
|
|
|
|
});
|
2022-04-08 07:40:37 +03:00
|
|
|
|
|
|
|
describe('Resource Not Found', function () {
|
|
|
|
it('Returns 404 Not Found Error for a generic case', function (done) {
|
|
|
|
resourceNotFound({}, {}, (error) => {
|
|
|
|
should.equal(error.statusCode, 404);
|
|
|
|
should.equal(error.message, 'Resource not found');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Returns 406 Request Not Acceptable Error for when requested version is behind current version', function (done) {
|
|
|
|
const req = {
|
|
|
|
headers: {
|
|
|
|
'accept-version': 'v3.9'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const res = {
|
|
|
|
locals: {
|
|
|
|
safeVersion: '4.3'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
resourceNotFound(req, res, (error) => {
|
|
|
|
should.equal(error.statusCode, 406);
|
2022-05-04 15:04:40 +03:00
|
|
|
should.equal(error.message, 'Request could not be served, the endpoint was not found.');
|
|
|
|
should.equal(error.context, 'Provided client accept-version v3.9 is behind current Ghost version v4.3.');
|
|
|
|
should.equal(error.help, 'Try upgrading your Ghost API client.');
|
2022-04-08 07:40:37 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-04-08 07:52:41 +03:00
|
|
|
it('Returns 406 Request Not Acceptable Error for when requested version is ahead current version', function (done) {
|
|
|
|
const req = {
|
|
|
|
headers: {
|
|
|
|
'accept-version': 'v4.8'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const res = {
|
|
|
|
locals: {
|
|
|
|
safeVersion: '4.3'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
resourceNotFound(req, res, (error) => {
|
|
|
|
should.equal(error.statusCode, 406);
|
2022-05-04 15:04:40 +03:00
|
|
|
should.equal(error.message, 'Request could not be served, the endpoint was not found.');
|
|
|
|
should.equal(error.context, 'Provided client accept-version v4.8 is ahead of current Ghost version v4.3.');
|
|
|
|
should.equal(error.help, 'Try upgrading your Ghost install.');
|
2022-04-08 07:52:41 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-04-08 07:40:37 +03:00
|
|
|
it('Returns 404 Not Found Error for when requested version is the same as current version', function (done) {
|
|
|
|
const req = {
|
|
|
|
headers: {
|
|
|
|
'accept-version': 'v4.3'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const res = {
|
|
|
|
locals: {
|
|
|
|
safeVersion: '4.3'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
resourceNotFound(req, res, (error) => {
|
|
|
|
should.equal(error.statusCode, 404);
|
|
|
|
should.equal(error.message, 'Resource not found');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2022-05-06 17:42:58 +03:00
|
|
|
|
|
|
|
describe('pageNotFound', function () {
|
|
|
|
it('returns 404 with special message when message not set', function (done) {
|
|
|
|
pageNotFound({}, {}, (error) => {
|
|
|
|
should.equal(error.statusCode, 404);
|
|
|
|
should.equal(error.message, 'Page not found');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns 404 with special message even if message is set', function (done) {
|
|
|
|
pageNotFound({message: 'uh oh'}, {}, (error) => {
|
|
|
|
should.equal(error.statusCode, 404);
|
|
|
|
should.equal(error.message, 'Page not found');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-04-08 07:40:37 +03:00
|
|
|
});
|