Ghost/test/regression/mock-express-style/parent_app_vhosts.test.js

497 lines
16 KiB
JavaScript
Raw Normal View History

const should = require('should');
const sinon = require('sinon');
const _ = require('lodash');
const testUtils = require('../../utils');
2021-10-20 14:27:20 +03:00
const localUtils = require('./utils');
const adminUtils = require('../../utils/admin-utils');
const configUtils = require('../../utils/configUtils');
const urlUtils = require('../../utils/urlUtils');
const themeEngine = require('../../../core/frontend/services/theme-engine');
describe('Integration - Web - vhosts', function () {
let app;
2021-10-20 14:27:20 +03:00
before(localUtils.urlService.resetGenerators);
before(testUtils.teardownDb);
before(testUtils.setup('users:roles', 'posts'));
Improved dev tooling (#13118) This commit achieves a few things: - ☑️ No longer having to remember whether a command is yarn something or grunt something - ☑️ Simplification of tools hopefully making them easier to remember and use - ☑️ Complete removal of the need for grunt from our test tooling Several of the tools still use grunt under the hood, but the **entrypoint** should aways be `yarn xxx`. - `grunt main` -> `yarn main` - `grunt dev` -> `yarn dev` - `grunt build` -> `yarn build` - `grunt test:file-or-folder` -> `yarn test file-or-folder` - `grunt test-unit` -> `yarn test:unit` - `grunt test-acceptance` -> `yarn test:acceptance` - `grunt test-regression` -> `yarn test:regression` - `grunt validate` -> removed due to lack of use There is now also `yarn test:all` to run all 3 classes of tests This PR also reorders & restructures the Gruntfile extensively so that: - The remaining useful commands are all at the top of the file - Config and other blah happens after all the useful commands - All release-only config happens in the release task at the very end of the file --- DONE: * Removed all references to npm/bower * Removed all references to lint / deprecated command * Moved debug to yarn dev:debug * Removed all references to travis * Removed broken help task + useless comment * Removed unused knex-migrator and clean:test setup tasks * Added new test commands, removed grunt validate * Moved stubClientFiles to test utility and use in the few tests that need it * Used mocha in yarn directly except grunt test:x * Swapped grunt test for yarn test * extensive cleanup and reshuffling
2021-07-05 22:02:22 +03:00
before(adminUtils.stubClientFiles);
after(function () {
configUtils.restore();
urlUtils.restore();
sinon.restore();
});
describe('no separate admin', function () {
before(async function () {
2021-10-20 14:27:20 +03:00
localUtils.urlService.resetGenerators();
localUtils.defaultMocks(sinon, {amp: true});
localUtils.overrideGhostConfig(configUtils);
configUtils.set('url', 'http://example.com');
configUtils.set('admin:url', null);
sinon.stub(themeEngine.getActive(), 'engine').withArgs('ghost-api').returns('v2');
sinon.stub(themeEngine.getActive(), 'config').withArgs('posts_per_page').returns(2);
2021-10-20 14:27:20 +03:00
app = await localUtils.initGhost();
});
before(function () {
configUtils.set('url', 'http://example.com');
configUtils.set('admin:url', null);
urlUtils.stubUrlUtilsFromConfig();
});
after(function () {
configUtils.restore();
urlUtils.restore();
sinon.restore();
});
it('loads the front-end on configured url', function () {
const req = {
secure: false,
method: 'GET',
url: '/',
host: 'example.com'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(200);
});
});
it('loads the front-end on localhost', function () {
const req = {
secure: false,
method: 'GET',
url: '/',
host: 'localhost'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(200);
});
});
it('loads the admin', function () {
const req = {
secure: false,
method: 'GET',
url: '/ghost/',
host: 'example.com'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(200);
});
});
it('loads the admin on localhost', function () {
const req = {
secure: false,
method: 'GET',
url: '/ghost/',
host: 'localhost'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(200);
});
});
it('loads the api', function () {
const req = {
secure: false,
method: 'GET',
url: '/ghost/api/v2/admin/site/',
host: 'example.com'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(200);
});
});
it('loads the api on localhost', function () {
const req = {
secure: false,
method: 'GET',
url: '/ghost/api/v2/admin/site/',
host: 'localhost'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(200);
});
});
});
describe('separate admin host', function () {
before(async function () {
2021-10-20 14:27:20 +03:00
localUtils.urlService.resetGenerators();
localUtils.defaultMocks(sinon, {amp: true});
localUtils.overrideGhostConfig(configUtils);
configUtils.set('url', 'http://example.com');
configUtils.set('admin:url', 'https://admin.example.com');
sinon.stub(themeEngine.getActive(), 'engine').withArgs('ghost-api').returns('v2');
sinon.stub(themeEngine.getActive(), 'config').withArgs('posts_per_page').returns(2);
2021-10-20 14:27:20 +03:00
app = await localUtils.initGhost();
});
before(function () {
urlUtils.stubUrlUtilsFromConfig();
});
after(function () {
configUtils.restore();
urlUtils.restore();
sinon.restore();
});
it('loads the front-end on configured url', function () {
const req = {
secure: false,
method: 'GET',
url: '/',
host: 'example.com'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(200);
});
});
it('loads the front-end on localhost', function () {
const req = {
secure: false,
method: 'GET',
url: '/',
host: 'localhost'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(200);
});
});
it('redirects /ghost/ on configured url', function () {
const req = {
secure: false,
method: 'GET',
url: '/ghost/',
host: 'example.com'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(301);
response.headers.location.should.eql('https://admin.example.com/ghost/');
});
});
it('404s the api on configured url', function () {
const req = {
secure: false,
method: 'GET',
url: '/ghost/api/v2/admin/site/',
host: 'example.com'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(404);
});
});
it('404s the api on localhost', function () {
const req = {
secure: false,
method: 'GET',
url: '/ghost/api/v2/admin/site/',
host: 'localhost'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(404);
});
});
it('loads the admin on configured admin url', function () {
const req = {
secure: true,
method: 'GET',
url: '/ghost/',
host: 'admin.example.com'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(200);
});
});
it('loads the api on configured admin url', function () {
const req = {
secure: true,
method: 'GET',
url: '/ghost/api/v2/admin/site/',
host: 'admin.example.com'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(200);
});
});
it('redirects to the correct protocol on configured admin url', function () {
const req = {
secure: false,
method: 'GET',
url: '/ghost/',
host: 'admin.example.com'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(301);
response.headers.location.should.eql('https://admin.example.com/ghost/');
});
});
it('404s the front-end on configured admin url', function () {
const req = {
secure: false,
method: 'GET',
url: '/',
host: 'admin.example.com'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(404);
});
});
});
describe('separate admin host w/ admin redirects disabled', function () {
before(async function () {
2021-10-20 14:27:20 +03:00
localUtils.urlService.resetGenerators();
localUtils.defaultMocks(sinon, {amp: true});
localUtils.overrideGhostConfig(configUtils);
configUtils.set('url', 'http://example.com');
configUtils.set('admin:url', 'https://admin.example.com');
configUtils.set('admin:redirects', false);
sinon.stub(themeEngine.getActive(), 'engine').withArgs('ghost-api').returns('v2');
sinon.stub(themeEngine.getActive(), 'config').withArgs('posts_per_page').returns(2);
2021-10-20 14:27:20 +03:00
app = await localUtils.initGhost();
});
before(function () {
urlUtils.stubUrlUtilsFromConfig();
});
after(function () {
configUtils.restore();
urlUtils.restore();
sinon.restore();
});
it('does not redirect /ghost/ on configured url', function () {
const req = {
secure: false,
method: 'GET',
url: '/ghost/',
host: 'example.com'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(404);
});
});
});
describe('same host separate protocol', function () {
before(async function () {
2021-10-20 14:27:20 +03:00
localUtils.urlService.resetGenerators();
localUtils.defaultMocks(sinon, {amp: true});
localUtils.overrideGhostConfig(configUtils);
configUtils.set('url', 'http://example.com');
configUtils.set('admin:url', 'https://example.com');
sinon.stub(themeEngine.getActive(), 'engine').withArgs('ghost-api').returns('v2');
sinon.stub(themeEngine.getActive(), 'config').withArgs('posts_per_page').returns(2);
2021-10-20 14:27:20 +03:00
app = await localUtils.initGhost();
});
before(function () {
urlUtils.stubUrlUtilsFromConfig();
});
it('loads the front-end on configured url (http)', function () {
const req = {
secure: false,
method: 'GET',
url: '/',
host: 'example.com'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(200);
});
});
it('404s the front-end on configured url (https)', function () {
const req = {
secure: true,
method: 'GET',
url: '/',
host: 'example.com'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(200);
});
});
it('loads the front-end on localhost', function () {
const req = {
secure: false,
method: 'GET',
url: '/',
host: 'localhost'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(200);
});
});
it('redirects /ghost/ on configured url', function () {
const req = {
secure: false,
method: 'GET',
url: '/ghost/',
host: 'example.com'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(301);
response.headers.location.should.eql('https://example.com/ghost/');
});
});
it('redirects /ghost/ on localhost', function () {
const req = {
secure: false,
method: 'GET',
url: '/ghost/',
host: 'localhost'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(301);
response.headers.location.should.eql('https://example.com/ghost/');
});
});
it('redirects api to correct protocol on configured admin url', function () {
const req = {
secure: false,
method: 'GET',
url: '/ghost/api/v2/admin/site/',
host: 'example.com'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(301);
response.headers.location.should.eql('https://example.com/ghost/api/v2/admin/site/');
});
});
it('loads the admin on configured admin url', function () {
const req = {
secure: true,
method: 'GET',
url: '/ghost/',
host: 'example.com'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(200);
});
});
it('redirects the admin on localhost', function () {
const req = {
secure: false,
method: 'GET',
url: '/ghost/',
host: 'localhost'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(301);
response.headers.location.should.eql('https://example.com/ghost/');
});
});
it('loads the api on configured admin url', function () {
const req = {
secure: true,
method: 'GET',
url: '/ghost/api/v2/admin/site/',
host: 'example.com'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(200);
});
});
it('redirects the api on localhost', function () {
const req = {
secure: false,
method: 'GET',
url: '/ghost/api/v2/admin/site/',
host: 'localhost'
};
2021-10-20 14:27:20 +03:00
return localUtils.mockExpress.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(301);
response.headers.location.should.eql('https://example.com/ghost/api/v2/admin/site/');
});
});
});
});