0
Fork 0
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:
Naz 2021-11-25 15:30:56 +04:00 committed by naz
parent b807be9699
commit 125901b466

View file

@ -4,7 +4,6 @@ const path = require('path');
const fs = require('fs-extra'); const fs = require('fs-extra');
const logging = require('@tryghost/logging'); const logging = require('@tryghost/logging');
const DynamicRedirectManager = require('@tryghost/express-dynamic-redirects');
const CustomRedirectsAPI = require('../../../../../core/server/services/redirects/api'); const CustomRedirectsAPI = require('../../../../../core/server/services/redirects/api');
describe('UNIT: redirects CustomRedirectsAPI class', function () { describe('UNIT: redirects CustomRedirectsAPI class', function () {
@ -13,12 +12,10 @@ describe('UNIT: redirects CustomRedirectsAPI class', function () {
const basePath = path.join(__dirname, '../../../../utils/fixtures/data/'); const basePath = path.join(__dirname, '../../../../utils/fixtures/data/');
before(function () { before(function () {
redirectManager = new DynamicRedirectManager({ redirectManager = {
permanentMaxAge: 100, removeAllRedirects: sinon.stub(),
getSubdirectoryURL: (pathname) => { addRedirect: sinon.stub()
return `/ghost/${pathname}`; };
}
});
customRedirectsAPI = new CustomRedirectsAPI({ customRedirectsAPI = new CustomRedirectsAPI({
basePath, basePath,
@ -30,7 +27,11 @@ describe('UNIT: redirects CustomRedirectsAPI class', function () {
beforeEach(function () { beforeEach(function () {
sinon.stub(fs, 'pathExists'); sinon.stub(fs, 'pathExists');
sinon.stub(fs, 'writeFile');
sinon.stub(fs, 'readFile'); sinon.stub(fs, 'readFile');
sinon.stub(fs, 'unlink');
sinon.stub(fs, 'move');
sinon.stub(fs, 'copy');
sinon.spy(logging, 'error'); sinon.spy(logging, 'error');
}); });
@ -40,12 +41,10 @@ describe('UNIT: redirects CustomRedirectsAPI class', function () {
describe('init', function () { describe('init', function () {
it('initializes without errors when redirects file is not present', async function () { it('initializes without errors when redirects file is not present', async function () {
redirectManager = new DynamicRedirectManager({ redirectManager = {
permanentMaxAge: 100, removeAllRedirects: sinon.stub(),
getSubdirectoryURL: (pathname) => { addRedirect: sinon.stub()
return `/ghost/${pathname}`; };
}
});
customRedirectsAPI = new CustomRedirectsAPI({ customRedirectsAPI = new CustomRedirectsAPI({
basePath, 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/); 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();
});
}); });
}); });