mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
Removed redirects regression tests in favor of unit test
refs https://linear.app/tryghost/issue/CORE-84/have-a-look-at-the-eggs-redirects-refactor-branch - Removed "download" regression tests as those cases were ported over to much faster unit tests
This commit is contained in:
parent
1c4dea00b9
commit
f4e725a7ef
4 changed files with 68 additions and 134 deletions
|
@ -37,67 +37,6 @@ describe('Redirects API', function () {
|
|||
});
|
||||
};
|
||||
|
||||
describe('Download', function () {
|
||||
afterEach(function () {
|
||||
configUtils.config.set('paths:contentPath', originalContentPath);
|
||||
});
|
||||
|
||||
it('file exists', async function () {
|
||||
await startGhost({
|
||||
redirectsFile: true,
|
||||
redirectsFileExt: '.json',
|
||||
forceStart: true
|
||||
});
|
||||
|
||||
await request
|
||||
.get(localUtils.API.getApiQuery('redirects/download/'))
|
||||
.set('Origin', config.get('url'))
|
||||
.expect('Content-Type', /application\/json/)
|
||||
.expect('Content-Disposition', 'Attachment; filename="redirects.json"')
|
||||
.expect(200)
|
||||
.then((res) => {
|
||||
res.headers['content-disposition'].should.eql('Attachment; filename="redirects.json"');
|
||||
res.headers['content-type'].should.eql('application/json; charset=utf-8');
|
||||
|
||||
should.deepEqual(res.body, require('../../../../utils/fixtures/data/redirects.json'));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Download yaml', function () {
|
||||
// beforeEach(function () {
|
||||
// testUtils.setupRedirectsFile(config.get('paths:contentPath'), '.yaml');
|
||||
// });
|
||||
|
||||
// afterEach(function () {
|
||||
// testUtils.setupRedirectsFile(config.get('paths:contentPath'), '.json');
|
||||
// });
|
||||
|
||||
// 'file does not exist' doesn't have to be tested because it always returns .json file.
|
||||
// TODO: But it should be written when the default redirects file type is changed to yaml.
|
||||
|
||||
it('file exists', async function () {
|
||||
await startGhost({
|
||||
redirectsFile: true,
|
||||
redirectsFileExt: '.yaml',
|
||||
forceStart: true
|
||||
});
|
||||
|
||||
await request
|
||||
.get(localUtils.API.getApiQuery('redirects/download/'))
|
||||
.set('Origin', config.get('url'))
|
||||
.expect('Content-Type', /text\/html/)
|
||||
.expect('Content-Disposition', 'Attachment; filename="redirects.yaml"')
|
||||
.expect(200)
|
||||
.then((res) => {
|
||||
res.headers['content-disposition'].should.eql('Attachment; filename="redirects.yaml"');
|
||||
res.headers['content-type'].should.eql('text/html; charset=utf-8');
|
||||
|
||||
should.deepEqual(res.text, fs.readFileSync(path.join(__dirname, '../../../../utils/fixtures/data/redirects.yaml')).toString());
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Upload', function () {
|
||||
describe('Error cases', function () {
|
||||
it('syntax error', function () {
|
||||
|
|
|
@ -27,27 +27,6 @@ describe('Redirects API', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('Download', function () {
|
||||
afterEach(function () {
|
||||
configUtils.config.set('paths:contentPath', originalContentPath);
|
||||
});
|
||||
|
||||
it('file exists', function () {
|
||||
return request
|
||||
.get(localUtils.API.getApiQuery('redirects/json/'))
|
||||
.set('Origin', config.get('url'))
|
||||
.expect('Content-Type', /application\/json/)
|
||||
.expect('Content-Disposition', 'Attachment; filename="redirects.json"')
|
||||
.expect(200)
|
||||
.then((res) => {
|
||||
res.headers['content-disposition'].should.eql('Attachment; filename="redirects.json"');
|
||||
res.headers['content-type'].should.eql('application/json; charset=utf-8');
|
||||
|
||||
should.deepEqual(res.body, require('../../../../utils/fixtures/data/redirects.json'));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Upload', function () {
|
||||
describe('Error cases', function () {
|
||||
it('syntax error', function () {
|
||||
|
|
|
@ -37,55 +37,6 @@ describe('Redirects API', function () {
|
|||
});
|
||||
};
|
||||
|
||||
describe('Download', function () {
|
||||
afterEach(function () {
|
||||
configUtils.config.set('paths:contentPath', originalContentPath);
|
||||
});
|
||||
|
||||
it('file exists', function () {
|
||||
return request
|
||||
.get(localUtils.API.getApiQuery('redirects/json/'))
|
||||
.set('Origin', config.get('url'))
|
||||
.expect('Content-Type', /application\/json/)
|
||||
.expect('Content-Disposition', 'Attachment; filename="redirects.json"')
|
||||
.expect(200)
|
||||
.then((res) => {
|
||||
res.headers['content-disposition'].should.eql('Attachment; filename="redirects.json"');
|
||||
res.headers['content-type'].should.eql('application/json; charset=utf-8');
|
||||
|
||||
should.deepEqual(res.body, require('../../../../utils/fixtures/data/redirects.json'));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Download yaml', function () {
|
||||
beforeEach(function () {
|
||||
testUtils.setupRedirectsFile(config.get('paths:contentPath'), '.yaml');
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
testUtils.setupRedirectsFile(config.get('paths:contentPath'), '.json');
|
||||
});
|
||||
|
||||
// 'file does not exist' doesn't have to be tested because it always returns .json file.
|
||||
// TODO: But it should be written when the default redirects file type is changed to yaml.
|
||||
|
||||
it('file exists', function () {
|
||||
return request
|
||||
.get(localUtils.API.getApiQuery('redirects/json/'))
|
||||
.set('Origin', config.get('url'))
|
||||
.expect('Content-Type', /text\/html/)
|
||||
.expect('Content-Disposition', 'Attachment; filename="redirects.yaml"')
|
||||
.expect(200)
|
||||
.then((res) => {
|
||||
res.headers['content-disposition'].should.eql('Attachment; filename="redirects.yaml"');
|
||||
res.headers['content-type'].should.eql('text/html; charset=utf-8');
|
||||
|
||||
should.deepEqual(res.text, fs.readFileSync(path.join(__dirname, '../../../../utils/fixtures/data/redirects.yaml')).toString());
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Upload', function () {
|
||||
describe('Error cases', function () {
|
||||
it('syntax error', function () {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
const should = require('should');
|
||||
const sinon = require('sinon');
|
||||
const path = require('path');
|
||||
const fs = require('fs-extra');
|
||||
|
||||
|
@ -6,10 +7,21 @@ const DynamicRedirectManager = require('@tryghost/express-dynamic-redirects');
|
|||
const CustomRedirectsAPI = require('../../../../../core/server/services/redirects/api');
|
||||
|
||||
describe('UNIT: redirects CustomRedirectsAPI class', function () {
|
||||
beforeEach(function () {
|
||||
sinon.stub(fs, 'pathExists');
|
||||
sinon.stub(fs, 'readFile');
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe('get', function () {
|
||||
it('returns nothing if file does not exist', async function () {
|
||||
// Just set any content folder, which does not contain a redirects file.
|
||||
const basePath = path.join(__dirname, '../../../utils/fixtures/does-not-exist/');
|
||||
it('returns empty array if file does not exist', async function () {
|
||||
const basePath = path.join(__dirname, '../../../../utils/fixtures/data/');
|
||||
fs.pathExists.withArgs(`${basePath}redirects.yaml`).resolves(false);
|
||||
fs.pathExists.withArgs(`${basePath}redirects.json`).resolves(false);
|
||||
|
||||
const redirectManager = new DynamicRedirectManager({
|
||||
permanentMaxAge: 100,
|
||||
getSubdirectoryURL: (pathname) => {
|
||||
|
@ -25,5 +37,58 @@ describe('UNIT: redirects CustomRedirectsAPI class', function () {
|
|||
|
||||
should.deepEqual(file, []);
|
||||
});
|
||||
|
||||
it('returns a redirects YAML file if it exists', async function () {
|
||||
const basePath = path.join(__dirname, '../../../../utils/fixtures/data/');
|
||||
fs.pathExists.withArgs(`${basePath}redirects.yaml`).resolves(true);
|
||||
fs.pathExists.withArgs(`${basePath}redirects.json`).resolves(false);
|
||||
|
||||
fs.readFile.withArgs(`${basePath}redirects.yaml`, 'utf-8').resolves('yaml content');
|
||||
fs.readFile.withArgs(`${basePath}redirects.json`, 'utf-8').resolves(null);
|
||||
|
||||
const redirectManager = new DynamicRedirectManager({
|
||||
permanentMaxAge: 100,
|
||||
getSubdirectoryURL: (pathname) => {
|
||||
return `/ghost/${pathname}`;
|
||||
}
|
||||
});
|
||||
|
||||
const customRedirectsAPI = new CustomRedirectsAPI({
|
||||
basePath
|
||||
}, redirectManager);
|
||||
|
||||
const file = await customRedirectsAPI.get();
|
||||
|
||||
should.deepEqual(file, 'yaml content');
|
||||
});
|
||||
|
||||
it('returns a redirects JSON file if YAML does not exists', async function () {
|
||||
const basePath = path.join(__dirname, '../../../../utils/fixtures/data/');
|
||||
const redirectJSONFixture = [{
|
||||
from: '^/post/[0-9]+/([a-z0-9\\-]+)',
|
||||
to: '/$1'
|
||||
}];
|
||||
|
||||
fs.pathExists.withArgs(`${basePath}redirects.yaml`).resolves(false);
|
||||
fs.pathExists.withArgs(`${basePath}redirects.json`).resolves(true);
|
||||
|
||||
fs.readFile.withArgs(`${basePath}redirects.yaml`, 'utf-8').resolves(null);
|
||||
fs.readFile.withArgs(`${basePath}redirects.json`, 'utf-8').resolves(JSON.stringify(redirectJSONFixture));
|
||||
|
||||
const redirectManager = new DynamicRedirectManager({
|
||||
permanentMaxAge: 100,
|
||||
getSubdirectoryURL: (pathname) => {
|
||||
return `/ghost/${pathname}`;
|
||||
}
|
||||
});
|
||||
|
||||
const customRedirectsAPI = new CustomRedirectsAPI({
|
||||
basePath
|
||||
}, redirectManager);
|
||||
|
||||
const file = await customRedirectsAPI.get();
|
||||
|
||||
should.deepEqual(file, redirectJSONFixture);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue