0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Fixed redirects integration tests

refs refs https://linear.app/tryghost/issue/CORE-84/have-a-look-at-the-eggs-redirects-refactor-branch

- The tests needed to have a clean state with empty redirects file, which was previously ensured through "configUtils". Because configUtils don't play ball with the class initialization pattern this approach was chosen
- It's an end-to-end test with lots of logic and pobably would be enough to run against single API endpoint. Leaving it as is and to be improved in the future
This commit is contained in:
Naz 2021-11-02 22:19:52 +04:00 committed by naz
parent a9952b2437
commit c080f4b77d
3 changed files with 89 additions and 37 deletions

View file

@ -3,6 +3,8 @@ const supertest = require('supertest');
const fs = require('fs-extra');
const Promise = require('bluebird');
const path = require('path');
const os = require('os');
const uuid = require('uuid');
const testUtils = require('../../../../utils');
const localUtils = require('./utils');
const config = require('../../../../../core/shared/config');
@ -11,16 +13,6 @@ const ghost = testUtils.startGhost;
let request;
describe('Redirects API', function () {
before(function () {
return ghost({redirectsFile: true})
.then(() => {
request = supertest.agent(config.get('url'));
})
.then(() => {
return localUtils.doAuth(request);
});
});
const startGhost = (options) => {
return ghost(options)
.then(() => {
@ -33,6 +25,56 @@ describe('Redirects API', function () {
describe('Upload', function () {
describe('Ensure re-registering redirects works', function () {
it('no redirects file exists', function () {
// NOTE: this dance with content folder is here because we need to test a clean state
// which is currently impossible with available test utils.
// The test itself should be broken down into a unit test for the
// Redirects service class.
const contentFolder = path.join(os.tmpdir(), uuid.v4(), 'ghost-test');
fs.ensureDirSync(contentFolder);
fs.ensureDirSync(path.join(contentFolder, 'data'));
fs.writeFileSync(path.join(contentFolder, 'data', 'redirects.json'), JSON.stringify([]));
return startGhost({
redirectsFile: false,
contentFolder: contentFolder,
forceStart: true
})
.then(() => {
return request
.get('/my-old-blog-post/')
.expect(404);
})
.then(() => {
// Provide a redirects file in the root directory of the content test folder
fs.writeFileSync(path.join(config.get('paths:contentPath'), 'redirects-init.json'), JSON.stringify([{
from: 'k',
to: 'l'
}]));
})
.then(() => {
return request
.post(localUtils.API.getApiQuery('redirects/upload/'))
.set('Origin', config.get('url'))
.attach('redirects', path.join(config.get('paths:contentPath'), 'redirects-init.json'))
.expect('Content-Type', /application\/json/)
.expect(200);
})
.then((res) => {
res.headers['x-cache-invalidate'].should.eql('/*');
return request
.get('/k/')
.expect(302);
})
.then((response) => {
response.headers.location.should.eql('/l');
const dataFiles = fs.readdirSync(config.getContentPath('data'));
dataFiles.join(',').match(/(redirects)/g).length.should.eql(2);
});
});
it('override', function () {
return startGhost({forceStart: true})
.then(() => {

View file

@ -3,6 +3,8 @@ const supertest = require('supertest');
const fs = require('fs-extra');
const Promise = require('bluebird');
const path = require('path');
const os = require('os');
const uuid = require('uuid');
const testUtils = require('../../../../utils');
const localUtils = require('./utils');
const config = require('../../../../../core/shared/config');
@ -11,30 +13,33 @@ const ghost = testUtils.startGhost;
let request;
describe('Redirects API', function () {
before(function () {
return ghost({redirectsFile: true})
const startGhost = (options) => {
return ghost(options)
.then(() => {
request = supertest.agent(config.get('url'));
})
.then(() => {
return localUtils.doAuth(request);
});
});
};
describe('Upload', function () {
describe('Ensure re-registering redirects works', function () {
const startGhost = (options) => {
return ghost(options)
.then(() => {
request = supertest.agent(config.get('url'));
})
.then(() => {
return localUtils.doAuth(request);
});
};
it('no redirects file exists', function () {
return startGhost({redirectsFile: false, forceStart: true})
// NOTE: this dance with content folder is here because we need to test a clean state
// which is currently impossible with available test utils.
// The test itself should be broken down into a unit test for the
// Redirects service class.
const contentFolder = path.join(os.tmpdir(), uuid.v4(), 'ghost-test');
fs.ensureDirSync(contentFolder);
fs.ensureDirSync(path.join(contentFolder, 'data'));
fs.writeFileSync(path.join(contentFolder, 'data', 'redirects.json'), JSON.stringify([]));
return startGhost({
redirectsFile: false,
contentFolder: contentFolder,
forceStart: true
})
.then(() => {
return request
.get('/my-old-blog-post/')
@ -66,7 +71,7 @@ describe('Redirects API', function () {
response.headers.location.should.eql('/l');
const dataFiles = fs.readdirSync(config.getContentPath('data'));
dataFiles.join(',').match(/(redirects)/g).length.should.eql(1);
dataFiles.join(',').match(/(redirects)/g).length.should.eql(2);
});
});

View file

@ -3,6 +3,8 @@ const supertest = require('supertest');
const fs = require('fs-extra');
const Promise = require('bluebird');
const path = require('path');
const os = require('os');
const uuid = require('uuid');
const testUtils = require('../../../../utils');
const localUtils = require('./utils');
const config = require('../../../../../core/shared/config');
@ -11,16 +13,6 @@ const ghost = testUtils.startGhost;
let request;
describe('Redirects API', function () {
before(function () {
return ghost({redirectsFile: true})
.then(() => {
request = supertest.agent(config.get('url'));
})
.then(() => {
return localUtils.doAuth(request);
});
});
const startGhost = (options) => {
return ghost(options)
.then(() => {
@ -34,7 +26,20 @@ describe('Redirects API', function () {
describe('Upload', function () {
describe('Ensure re-registering redirects works', function () {
it('no redirects file exists', function () {
return startGhost({redirectsFile: false, forceStart: true})
// NOTE: this dance with content folder is here because we need to test a clean state
// which is currently impossible with available test utils.
// The test itself should be broken down into a unit test for the
// Redirects service class.
const contentFolder = path.join(os.tmpdir(), uuid.v4(), 'ghost-test');
fs.ensureDirSync(contentFolder);
fs.ensureDirSync(path.join(contentFolder, 'data'));
fs.writeFileSync(path.join(contentFolder, 'data', 'redirects.json'), JSON.stringify([]));
return startGhost({
redirectsFile: false,
contentFolder: contentFolder,
forceStart: true
})
.then(() => {
return request
.get('/my-old-blog-post/')
@ -66,7 +71,7 @@ describe('Redirects API', function () {
response.headers.location.should.eql('/l');
const dataFiles = fs.readdirSync(config.getContentPath('data'));
dataFiles.join(',').match(/(redirects)/g).length.should.eql(1);
dataFiles.join(',').match(/(redirects)/g).length.should.eql(2);
});
});