mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Added a test covering backup redirects functionality
refs https://github.com/TryGhost/Toolbox/issues/139 - This test is meant to partially substitute existing regression test suite for redirects
This commit is contained in:
parent
b807be9699
commit
125901b466
1 changed files with 96 additions and 13 deletions
|
@ -4,7 +4,6 @@ const path = require('path');
|
|||
const fs = require('fs-extra');
|
||||
|
||||
const logging = require('@tryghost/logging');
|
||||
const DynamicRedirectManager = require('@tryghost/express-dynamic-redirects');
|
||||
const CustomRedirectsAPI = require('../../../../../core/server/services/redirects/api');
|
||||
|
||||
describe('UNIT: redirects CustomRedirectsAPI class', function () {
|
||||
|
@ -13,12 +12,10 @@ describe('UNIT: redirects CustomRedirectsAPI class', function () {
|
|||
const basePath = path.join(__dirname, '../../../../utils/fixtures/data/');
|
||||
|
||||
before(function () {
|
||||
redirectManager = new DynamicRedirectManager({
|
||||
permanentMaxAge: 100,
|
||||
getSubdirectoryURL: (pathname) => {
|
||||
return `/ghost/${pathname}`;
|
||||
}
|
||||
});
|
||||
redirectManager = {
|
||||
removeAllRedirects: sinon.stub(),
|
||||
addRedirect: sinon.stub()
|
||||
};
|
||||
|
||||
customRedirectsAPI = new CustomRedirectsAPI({
|
||||
basePath,
|
||||
|
@ -30,7 +27,11 @@ describe('UNIT: redirects CustomRedirectsAPI class', function () {
|
|||
|
||||
beforeEach(function () {
|
||||
sinon.stub(fs, 'pathExists');
|
||||
sinon.stub(fs, 'writeFile');
|
||||
sinon.stub(fs, 'readFile');
|
||||
sinon.stub(fs, 'unlink');
|
||||
sinon.stub(fs, 'move');
|
||||
sinon.stub(fs, 'copy');
|
||||
sinon.spy(logging, 'error');
|
||||
});
|
||||
|
||||
|
@ -40,12 +41,10 @@ describe('UNIT: redirects CustomRedirectsAPI class', function () {
|
|||
|
||||
describe('init', function () {
|
||||
it('initializes without errors when redirects file is not present', async function () {
|
||||
redirectManager = new DynamicRedirectManager({
|
||||
permanentMaxAge: 100,
|
||||
getSubdirectoryURL: (pathname) => {
|
||||
return `/ghost/${pathname}`;
|
||||
}
|
||||
});
|
||||
redirectManager = {
|
||||
removeAllRedirects: sinon.stub(),
|
||||
addRedirect: sinon.stub()
|
||||
};
|
||||
|
||||
customRedirectsAPI = new CustomRedirectsAPI({
|
||||
basePath,
|
||||
|
@ -144,5 +143,89 @@ describe('UNIT: redirects CustomRedirectsAPI class', function () {
|
|||
err.message.should.match(/Could not parse YAML: can not read an implicit mapping pair/);
|
||||
}
|
||||
});
|
||||
|
||||
it('creates a backup file from existing redirects.json file', async function () {
|
||||
const incomingFilePath = path.join(__dirname, '/valid/path/redirects_incoming.json');
|
||||
const existingRedirectsFilePath = `${basePath}redirects.json`;
|
||||
const backupFilePath = path.join(basePath, 'backup.json');
|
||||
|
||||
const redirectsJSONConfig = JSON.stringify([{
|
||||
from: 'e',
|
||||
to: 'b'
|
||||
}]);
|
||||
|
||||
// redirects.json file already exits
|
||||
fs.pathExists.withArgs(existingRedirectsFilePath).resolves(true);
|
||||
fs.pathExists.withArgs(`${basePath}redirects.yaml`).resolves(false);
|
||||
// incoming redirects file
|
||||
fs.readFile.withArgs(incomingFilePath, 'utf-8').resolves(redirectsJSONConfig);
|
||||
// backup file already exists
|
||||
fs.pathExists.withArgs(backupFilePath).resolves(true);
|
||||
fs.unlink.withArgs(backupFilePath).resolves(true);
|
||||
fs.move.withArgs(incomingFilePath, backupFilePath).resolves(true);
|
||||
|
||||
customRedirectsAPI = new CustomRedirectsAPI({
|
||||
basePath,
|
||||
redirectManager,
|
||||
getBackupFilePath: () => backupFilePath,
|
||||
validate: () => {}
|
||||
});
|
||||
|
||||
await customRedirectsAPI.setFromFilePath(incomingFilePath, '.json');
|
||||
|
||||
// backed up file with the same name already exists so remove it
|
||||
fs.unlink.called.should.be.true();
|
||||
fs.unlink.calledWith(backupFilePath).should.be.true();
|
||||
|
||||
// backed up current routes file
|
||||
fs.move.called.should.be.true();
|
||||
fs.move.calledWith(existingRedirectsFilePath, backupFilePath).should.be.true();
|
||||
|
||||
// written new routes file
|
||||
fs.writeFile.calledWith(existingRedirectsFilePath, redirectsJSONConfig, 'utf-8').should.be.true();
|
||||
});
|
||||
|
||||
it('creates a backup file from existing redirects.yaml file', async function () {
|
||||
const incomingFilePath = path.join(__dirname, '/valid/path/redirects_incoming.yaml');
|
||||
|
||||
const backupFilePath = path.join(basePath, 'backup.yaml');
|
||||
|
||||
const redirectsYamlConfig = `
|
||||
301:
|
||||
/my-old-blog-post/: /revamped-url/
|
||||
|
||||
302:
|
||||
/another-old-blog-post/: /hello-there/
|
||||
`;
|
||||
|
||||
// redirects.json file already exits
|
||||
fs.pathExists.withArgs(`${basePath}redirects.json`).resolves(false);
|
||||
fs.pathExists.withArgs(`${basePath}redirects.yaml`).resolves(true);
|
||||
// incoming redirects file
|
||||
fs.readFile.withArgs(incomingFilePath, 'utf-8').resolves(redirectsYamlConfig);
|
||||
// backup file DOES not exists yet
|
||||
fs.pathExists.withArgs(backupFilePath).resolves(false);
|
||||
// should not be called
|
||||
fs.unlink.withArgs(backupFilePath).resolves(false);
|
||||
fs.move.withArgs(`${basePath}redirects.yaml`, backupFilePath).resolves(true);
|
||||
|
||||
customRedirectsAPI = new CustomRedirectsAPI({
|
||||
basePath,
|
||||
redirectManager,
|
||||
getBackupFilePath: () => backupFilePath,
|
||||
validate: () => {}
|
||||
});
|
||||
|
||||
await customRedirectsAPI.setFromFilePath(incomingFilePath, '.yaml');
|
||||
|
||||
// no existing backup file name match, did not remove any files
|
||||
fs.unlink.called.should.not.be.true();
|
||||
|
||||
// backed up current routes file
|
||||
fs.move.called.should.be.true();
|
||||
|
||||
// overwritten with incoming routes.yaml file
|
||||
fs.copy.calledWith(incomingFilePath, `${basePath}redirects.yaml`).should.be.true();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue