2020-04-25 23:06:33 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
2021-10-06 13:12:21 +03:00
|
|
|
const uncapitalise = require('../../../../../../core/server/web/shared/middlewares/uncapitalise');
|
2015-06-10 22:18:31 +03:00
|
|
|
|
2016-10-10 22:14:32 +03:00
|
|
|
// NOTE: all urls will have had trailing slashes added before uncapitalise is called
|
|
|
|
|
2015-06-10 22:18:31 +03:00
|
|
|
describe('Middleware: uncapitalise', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
let res;
|
|
|
|
let req;
|
|
|
|
let next;
|
2015-06-10 22:18:31 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2016-10-10 22:14:32 +03:00
|
|
|
res = {
|
2019-01-21 19:53:44 +03:00
|
|
|
redirect: sinon.spy(),
|
|
|
|
set: sinon.spy()
|
2016-10-10 22:14:32 +03:00
|
|
|
};
|
|
|
|
req = {};
|
2019-01-21 19:53:44 +03:00
|
|
|
next = sinon.spy();
|
2015-06-10 22:18:31 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2015-06-10 22:18:31 +03:00
|
|
|
});
|
|
|
|
|
2016-10-10 22:14:32 +03:00
|
|
|
describe('Signup or reset request', function () {
|
|
|
|
it('[signup] does nothing if there are no capitals in req.path', function (done) {
|
|
|
|
req.path = '/ghost/signup/';
|
|
|
|
uncapitalise(req, res, next);
|
|
|
|
|
|
|
|
next.calledOnce.should.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('[signup] does nothing if there are no capitals in the baseUrl', function (done) {
|
|
|
|
req.baseUrl = '/ghost/signup/';
|
|
|
|
req.path = '';
|
|
|
|
uncapitalise(req, res, next);
|
|
|
|
|
|
|
|
next.calledOnce.should.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('[signup] does nothing if there are no capitals except in a token', function (done) {
|
|
|
|
req.baseUrl = '/blog';
|
|
|
|
req.path = '/ghost/signup/XEB123';
|
|
|
|
|
2015-06-10 22:18:31 +03:00
|
|
|
uncapitalise(req, res, next);
|
|
|
|
|
2016-03-14 19:52:22 +03:00
|
|
|
next.calledOnce.should.be.true();
|
2015-06-10 22:18:31 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2016-10-10 22:14:32 +03:00
|
|
|
it('[reset] does nothing if there are no capitals except in a token', function (done) {
|
|
|
|
req.baseUrl = '/blog';
|
|
|
|
req.path = '/ghost/reset/NCR3NjY4NzI1ODI1OHzlcmlzZHNAZ51haWwuY29tfEpWeGxRWHUzZ3Y0cEpQRkNYYzQvbUZyc2xFSVozU3lIZHZWeFJLRml6cY54';
|
|
|
|
uncapitalise(req, res, next);
|
|
|
|
|
|
|
|
next.calledOnce.should.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('[signup] redirects if there are capitals in req.path', function (done) {
|
|
|
|
req.path = '/ghost/SignUP/';
|
|
|
|
req.url = req.path;
|
|
|
|
|
|
|
|
uncapitalise(req, res, next);
|
|
|
|
|
|
|
|
next.called.should.be.false();
|
|
|
|
res.redirect.calledOnce.should.be.true();
|
|
|
|
res.redirect.calledWith(301, '/ghost/signup/').should.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('[signup] redirects if there are capitals in req.baseUrl', function (done) {
|
|
|
|
req.baseUrl = '/ghost/SignUP/';
|
|
|
|
req.path = '';
|
|
|
|
req.url = req.path;
|
|
|
|
req.originalUrl = req.baseUrl + req.path;
|
|
|
|
|
|
|
|
uncapitalise(req, res, next);
|
|
|
|
|
|
|
|
next.called.should.be.false();
|
|
|
|
res.redirect.calledOnce.should.be.true();
|
|
|
|
res.redirect.calledWith(301, '/ghost/signup/').should.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('[signup] redirects correctly if there are capitals in req.path and req.baseUrl', function (done) {
|
|
|
|
req.baseUrl = '/Blog';
|
|
|
|
req.path = '/ghosT/signUp/';
|
|
|
|
req.url = req.path;
|
|
|
|
req.originalUrl = req.baseUrl + req.path;
|
|
|
|
|
|
|
|
uncapitalise(req, res, next);
|
|
|
|
|
|
|
|
next.called.should.be.false();
|
|
|
|
res.redirect.calledOnce.should.be.true();
|
|
|
|
res.redirect.calledWith(301, '/blog/ghost/signup/').should.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('[signup] redirects correctly with capitals in req.path if there is a token', function (done) {
|
|
|
|
req.path = '/ghosT/sigNup/XEB123';
|
|
|
|
req.url = req.path;
|
|
|
|
|
|
|
|
uncapitalise(req, res, next);
|
|
|
|
|
|
|
|
next.called.should.be.false();
|
|
|
|
res.redirect.calledOnce.should.be.true();
|
|
|
|
res.redirect.calledWith(301, '/ghost/signup/XEB123').should.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('[reset] redirects correctly with capitals in req.path & req.baseUrl if there is a token', function (done) {
|
|
|
|
req.baseUrl = '/Blog';
|
|
|
|
req.path = '/Ghost/Reset/NCR3NjY4NzI1ODI1OHzlcmlzZHNAZ51haWwuY29tfEpWeGxRWHUzZ3Y0cEpQRkNYYzQvbUZyc2xFSVozU3lIZHZWeFJLRml6cY54';
|
|
|
|
req.url = req.path;
|
|
|
|
req.originalUrl = req.baseUrl + req.path;
|
2015-06-10 22:18:31 +03:00
|
|
|
|
|
|
|
uncapitalise(req, res, next);
|
|
|
|
|
2016-03-14 19:52:22 +03:00
|
|
|
next.called.should.be.false();
|
|
|
|
res.redirect.calledOnce.should.be.true();
|
2016-10-10 22:14:32 +03:00
|
|
|
res.redirect.calledWith(301, '/blog/ghost/reset/NCR3NjY4NzI1ODI1OHzlcmlzZHNAZ51haWwuY29tfEpWeGxRWHUzZ3Y0cEpQRkNYYzQvbUZyc2xFSVozU3lIZHZWeFJLRml6cY54').should.be.true();
|
2015-06-10 22:18:31 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('An API request', function () {
|
2019-09-03 10:03:31 +03:00
|
|
|
['v0.1', 'v2', 'v3', 'canary', 'v10'].forEach((apiVersion) => {
|
2018-09-20 23:27:38 +03:00
|
|
|
describe(`for ${apiVersion}`, function () {
|
|
|
|
it('does nothing if there are no capitals', function (done) {
|
|
|
|
req.path = `/ghost/api/${apiVersion}/endpoint/`;
|
|
|
|
uncapitalise(req, res, next);
|
|
|
|
|
|
|
|
next.calledOnce.should.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('version identifier is uppercase', function (done) {
|
|
|
|
req.path = `/ghost/api/${apiVersion.toUpperCase()}/endpoint/`;
|
|
|
|
req.url = req.path;
|
|
|
|
|
|
|
|
uncapitalise(req, res, next);
|
|
|
|
|
|
|
|
next.called.should.be.false();
|
|
|
|
res.redirect.calledOnce.should.be.true();
|
|
|
|
res.redirect.calledWith(301, `/ghost/api/${apiVersion}/endpoint/`).should.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('redirects to the lower case slug if there are capitals', function (done) {
|
|
|
|
req.path = `/ghost/api/${apiVersion}/ASDfJ/`;
|
|
|
|
req.url = req.path;
|
|
|
|
|
|
|
|
uncapitalise(req, res, next);
|
|
|
|
|
|
|
|
next.called.should.be.false();
|
|
|
|
res.redirect.calledOnce.should.be.true();
|
|
|
|
res.redirect.calledWith(301, `/ghost/api/${apiVersion}/asdfj/`).should.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('redirects to the lower case slug if there are capitals in req.baseUrl', function (done) {
|
|
|
|
req.baseUrl = '/Blog';
|
|
|
|
req.path = `/ghost/api/${apiVersion}/ASDfJ/`;
|
|
|
|
req.url = req.path;
|
|
|
|
req.originalUrl = req.baseUrl + req.path;
|
|
|
|
|
|
|
|
uncapitalise(req, res, next);
|
|
|
|
|
|
|
|
next.called.should.be.false();
|
|
|
|
res.redirect.calledOnce.should.be.true();
|
|
|
|
res.redirect.calledWith(301, `/blog/ghost/api/${apiVersion}/asdfj/`).should.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not convert any capitals after the endpoint', function (done) {
|
2020-04-25 23:06:33 +03:00
|
|
|
const query = '?filter=mAgic';
|
2018-09-20 23:27:38 +03:00
|
|
|
req.path = `/Ghost/API/${apiVersion}/settings/is_private/`;
|
2020-04-25 23:06:33 +03:00
|
|
|
req.url = `${req.path}${query}`;
|
2018-09-20 23:27:38 +03:00
|
|
|
|
|
|
|
uncapitalise(req, res, next);
|
|
|
|
|
|
|
|
next.called.should.be.false();
|
|
|
|
res.redirect.calledOnce.should.be.true();
|
2020-04-25 23:06:33 +03:00
|
|
|
res.redirect.calledWith(301, `/ghost/api/${apiVersion}/settings/is_private/${query}`).should.be.true();
|
2018-09-20 23:27:38 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not convert any capitals after the endpoint with baseUrl', function (done) {
|
2020-04-25 23:06:33 +03:00
|
|
|
const query = '?filter=mAgic';
|
2018-09-20 23:27:38 +03:00
|
|
|
req.baseUrl = '/Blog';
|
|
|
|
req.path = `/ghost/api/${apiVersion}/mail/test@example.COM/`;
|
2020-04-25 23:06:33 +03:00
|
|
|
req.url = `${req.path}${query}`;
|
|
|
|
req.originalUrl = `${req.baseUrl}${req.path}${query}`;
|
2018-09-20 23:27:38 +03:00
|
|
|
|
|
|
|
uncapitalise(req, res, next);
|
|
|
|
|
|
|
|
next.called.should.be.false();
|
|
|
|
res.redirect.calledOnce.should.be.true();
|
2020-04-25 23:06:33 +03:00
|
|
|
res.redirect.calledWith(301, `/blog/ghost/api/${apiVersion}/mail/test@example.COM/${query}`).should.be.true();
|
2018-09-20 23:27:38 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-06-10 22:18:31 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Any other request', function () {
|
2016-10-10 22:14:32 +03:00
|
|
|
it('does nothing if there are no capitals', function (done) {
|
2015-06-10 22:18:31 +03:00
|
|
|
req.path = '/this-is-my-blog-post';
|
|
|
|
uncapitalise(req, res, next);
|
|
|
|
|
2016-03-14 19:52:22 +03:00
|
|
|
next.calledOnce.should.be.true();
|
2015-06-10 22:18:31 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2016-10-10 22:14:32 +03:00
|
|
|
it('redirects to the lower case slug if there are capitals', function (done) {
|
2015-06-10 22:18:31 +03:00
|
|
|
req.path = '/THis-iS-my-BLOg-poSt';
|
2016-10-10 22:14:32 +03:00
|
|
|
req.url = req.path;
|
2015-06-10 22:18:31 +03:00
|
|
|
|
|
|
|
uncapitalise(req, res, next);
|
|
|
|
|
2016-03-14 19:52:22 +03:00
|
|
|
next.called.should.be.false();
|
|
|
|
res.redirect.calledOnce.should.be.true();
|
2016-10-10 22:14:32 +03:00
|
|
|
res.redirect.calledWith(301, '/this-is-my-blog-post').should.be.true();
|
2015-06-10 22:18:31 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|