mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 10:21:36 +03:00
8f8ca481a6
no issue There are a couple of issues with resetting the Ghost instance between E2E test files: These issues came to the surface because of new tests written in https://github.com/TryGhost/Ghost/pull/16117 **1. configUtils.restore does not work correctly** `config.reset()` is a callback based method. On top of that, it doesn't really work reliably (https://github.com/indexzero/nconf/issues/93) What kinda happens, is that you first call `config.reset` but immediately after you correcty reset the config using the `config.set` calls afterwards. But since `config.reset` is async, that reset will happen after all those sets, and the end result is that it isn't reset correctly. This mainly caused issues in the new updated images tests, which were updating the config `imageOptimization.contentImageSizes`, which is a deeply nested config value. Maybe some references to objects are reused in nconf that cause this issue? Wrapping `config.reset()` in a promise does fix the issue. **2. Adapters cache not reset between tests** At the start of each test, we set `paths:contentPath` to a nice new temporary directory. But if a previous test already requests a localStorage adapter, that adapter would have been created and in the constructor `paths:contentPath` would have been passed. That same instance will be reused in the next test run. So it won't read the new config again. To fix this, we need to reset the adapter instances between E2E tests. How was this visible? Test uploads were stored in the actual git repository, and not in a temporary directory. When writing the new image upload tests, this also resulted in unreliable test runs because some image names were already taken (from previous test runs). **3. Old 2E2 test Ghost server not stopped** Sometimes we still need access to the frontend test server using `getAgentsWithFrontend`. But that does start a new Ghost server which is actually listening for HTTP traffic. This could result in a fatal error in tests because the port is already in use. The issue is that old E2E tests also start a HTTP server, but they don't stop the server. When you used the old `startGhost` util, it would check if a server was already running and stop it first. The new `getAgentsWithFrontend` now also has the same functionality to fix that issue.
129 lines
4.6 KiB
JavaScript
129 lines
4.6 KiB
JavaScript
// # Frontend Route tests
|
|
// As it stands, these tests depend on the database, and as such are integration tests.
|
|
// Mocking out the models to not touch the DB would turn these into unit tests, and should probably be done in future,
|
|
// But then again testing real code, rather than mock code, might be more useful...
|
|
|
|
const should = require('should');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
const supertest = require('supertest');
|
|
const testUtils = require('../utils');
|
|
const configUtils = require('../utils/configUtils');
|
|
const urlUtils = require('../utils/urlUtils');
|
|
const adminUtils = require('../utils/admin-utils');
|
|
const config = require('../../core/shared/config');
|
|
let request;
|
|
|
|
function assertCorrectHeaders(res) {
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
should.exist(res.headers.date);
|
|
}
|
|
|
|
describe('Admin Routing', function () {
|
|
before(async function () {
|
|
adminUtils.stubAdminFiles();
|
|
|
|
await testUtils.startGhost();
|
|
request = supertest.agent(config.get('url'));
|
|
});
|
|
|
|
describe('Assets', function () {
|
|
it('should return 404 for unknown assets', async function () {
|
|
await request.get('/ghost/assets/not-found.js')
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
.expect(404)
|
|
.expect(assertCorrectHeaders);
|
|
});
|
|
|
|
it('should retrieve built assets', async function () {
|
|
await request.get('/ghost/assets/vendor.js')
|
|
.expect('Cache-Control', testUtils.cacheRules.yearImmutable)
|
|
.expect(200)
|
|
.expect(assertCorrectHeaders);
|
|
});
|
|
});
|
|
|
|
describe('Admin Redirects', function () {
|
|
it('should redirect /GHOST/ to /ghost/', async function () {
|
|
await request.get('/GHOST/')
|
|
.expect('Location', '/ghost/')
|
|
.expect(301)
|
|
.expect(assertCorrectHeaders);
|
|
});
|
|
});
|
|
|
|
// we'll use X-Forwarded-Proto: https to simulate an 'https://' request behind a proxy
|
|
describe('Require HTTPS - redirect', function () {
|
|
before(async function () {
|
|
configUtils.set('url', 'https://localhost:2390');
|
|
urlUtils.stubUrlUtilsFromConfig();
|
|
|
|
await testUtils.startGhost({forceStart: true});
|
|
request = supertest.agent(config.get('server:host') + ':' + config.get('server:port'));
|
|
});
|
|
|
|
after(async function () {
|
|
urlUtils.restore();
|
|
await configUtils.restore();
|
|
});
|
|
|
|
it('should redirect admin access over non-HTTPS', async function () {
|
|
await request.get('/ghost/')
|
|
.expect('Location', /^https:\/\/localhost:2390\/ghost\//)
|
|
.expect(301)
|
|
.expect(assertCorrectHeaders);
|
|
});
|
|
|
|
it('should allow admin access over HTTPS', async function () {
|
|
await request.get('/ghost/')
|
|
.set('X-Forwarded-Proto', 'https')
|
|
.expect(200)
|
|
.expect(assertCorrectHeaders);
|
|
});
|
|
});
|
|
|
|
describe('built template', function () {
|
|
beforeEach(function () {
|
|
const configPaths = configUtils.config.get('paths');
|
|
configPaths.adminAssets = path.resolve('test/utils/fixtures/admin-build');
|
|
configUtils.set('paths', configPaths);
|
|
});
|
|
|
|
afterEach(async function () {
|
|
await configUtils.restore();
|
|
});
|
|
|
|
it('serves assets in production', async function () {
|
|
configUtils.set('env', 'production');
|
|
|
|
const prodTemplate = fs.readFileSync(path.resolve('test/utils/fixtures/admin-build/index.html')).toString();
|
|
|
|
const res = await request.get('/ghost/')
|
|
.set('X-Forwarded-Proto', 'https')
|
|
.expect(200);
|
|
|
|
res.text.should.equal(prodTemplate);
|
|
});
|
|
|
|
it('serves assets when not in production', async function () {
|
|
const devTemplate = fs.readFileSync(path.resolve('test/utils/fixtures/admin-build/index.html')).toString();
|
|
|
|
const res = await request.get('/ghost/')
|
|
.set('X-Forwarded-Proto', 'https')
|
|
.expect(200);
|
|
|
|
res.text.should.equal(devTemplate);
|
|
});
|
|
|
|
it('generates it\'s own ETag header from file contents', async function () {
|
|
const res = await request.get('/ghost/')
|
|
.set('X-Forwarded-Proto', 'https')
|
|
.expect(200);
|
|
|
|
should.exist(res.headers.etag);
|
|
res.headers.etag.should.equal('8793333e8e91cde411b1336c58ec6ef3');
|
|
});
|
|
});
|
|
});
|