mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Rewrote validation regression tests to be unit tests
refs refs https://linear.app/tryghost/issue/CORE-84/have-a-look-at-the-eggs-redirects-refactor-branch - These regression tests are slow and should have been unit tests to start with
This commit is contained in:
parent
d101ef5293
commit
a9952b2437
4 changed files with 44 additions and 220 deletions
|
@ -5,15 +5,12 @@ const Promise = require('bluebird');
|
|||
const path = require('path');
|
||||
const testUtils = require('../../../../utils');
|
||||
const localUtils = require('./utils');
|
||||
const configUtils = require('../../../../utils/configUtils');
|
||||
const config = require('../../../../../core/shared/config');
|
||||
|
||||
const ghost = testUtils.startGhost;
|
||||
let request;
|
||||
|
||||
describe('Redirects API', function () {
|
||||
let originalContentPath;
|
||||
|
||||
before(function () {
|
||||
return ghost({redirectsFile: true})
|
||||
.then(() => {
|
||||
|
@ -21,9 +18,6 @@ describe('Redirects API', function () {
|
|||
})
|
||||
.then(() => {
|
||||
return localUtils.doAuth(request);
|
||||
})
|
||||
.then(() => {
|
||||
originalContentPath = configUtils.config.get('paths:contentPath');
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -38,65 +32,7 @@ describe('Redirects API', function () {
|
|||
};
|
||||
|
||||
describe('Upload', function () {
|
||||
describe('Error cases', function () {
|
||||
it('syntax error', function () {
|
||||
fs.writeFileSync(path.join(config.get('paths:contentPath'), 'redirects.json'), 'something');
|
||||
|
||||
return request
|
||||
.post(localUtils.API.getApiQuery('redirects/upload/'))
|
||||
.set('Origin', config.get('url'))
|
||||
.attach('redirects', path.join(config.get('paths:contentPath'), 'redirects.json'))
|
||||
.expect('Content-Type', /application\/json/)
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('wrong format: no from/to', function () {
|
||||
fs.writeFileSync(path.join(config.get('paths:contentPath'), 'redirects.json'), JSON.stringify([{to: 'd'}]));
|
||||
|
||||
return request
|
||||
.post(localUtils.API.getApiQuery('redirects/upload/'))
|
||||
.set('Origin', config.get('url'))
|
||||
.attach('redirects', path.join(config.get('paths:contentPath'), 'redirects.json'))
|
||||
.expect('Content-Type', /application\/json/)
|
||||
.expect(422);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Ensure re-registering redirects works', function () {
|
||||
it('no redirects file exists', async function () {
|
||||
await startGhost({
|
||||
redirectsFile: true,
|
||||
redirectsFileExt: null,
|
||||
forceStart: true
|
||||
});
|
||||
|
||||
// Provide a redirects file in the root directory of the content test folder
|
||||
await fs.writeFileSync(path.join(config.get('paths:contentPath'), 'redirects-init.json'), JSON.stringify([{
|
||||
from: 'k',
|
||||
to: 'l'
|
||||
}]));
|
||||
|
||||
await 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(1);
|
||||
});
|
||||
});
|
||||
|
||||
it('override', function () {
|
||||
return startGhost({forceStart: true})
|
||||
.then(() => {
|
||||
|
@ -172,54 +108,7 @@ describe('Redirects API', function () {
|
|||
});
|
||||
|
||||
describe('Upload yaml', function () {
|
||||
describe('Error cases', function () {
|
||||
it('syntax error', function () {
|
||||
fs.writeFileSync(path.join(config.get('paths:contentPath'), 'redirects.yaml'), 'x');
|
||||
|
||||
return request
|
||||
.post(localUtils.API.getApiQuery('redirects/upload/'))
|
||||
.set('Origin', config.get('url'))
|
||||
.attach('redirects', path.join(config.get('paths:contentPath'), 'redirects.yaml'))
|
||||
.expect('Content-Type', /application\/json/)
|
||||
.expect(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Ensure re-registering redirects works', function () {
|
||||
it('no redirects file exists', function () {
|
||||
return startGhost({redirectsFile: false, 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.yaml'), '302:\n k: 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.yaml'))
|
||||
.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(1);
|
||||
});
|
||||
});
|
||||
|
||||
it('override', function () {
|
||||
// We want to test if we can override old redirects.json with new redirects.yaml
|
||||
// That's why we start with .json.
|
||||
|
|
|
@ -5,15 +5,12 @@ const Promise = require('bluebird');
|
|||
const path = require('path');
|
||||
const testUtils = require('../../../../utils');
|
||||
const localUtils = require('./utils');
|
||||
const configUtils = require('../../../../utils/configUtils');
|
||||
const config = require('../../../../../core/shared/config');
|
||||
|
||||
const ghost = testUtils.startGhost;
|
||||
let request;
|
||||
|
||||
describe('Redirects API', function () {
|
||||
let originalContentPath;
|
||||
|
||||
before(function () {
|
||||
return ghost({redirectsFile: true})
|
||||
.then(() => {
|
||||
|
@ -21,37 +18,10 @@ describe('Redirects API', function () {
|
|||
})
|
||||
.then(() => {
|
||||
return localUtils.doAuth(request);
|
||||
})
|
||||
.then(() => {
|
||||
originalContentPath = configUtils.config.get('paths:contentPath');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Upload', function () {
|
||||
describe('Error cases', function () {
|
||||
it('syntax error', function () {
|
||||
fs.writeFileSync(path.join(config.get('paths:contentPath'), 'redirects.json'), 'something');
|
||||
|
||||
return request
|
||||
.post(localUtils.API.getApiQuery('redirects/json/'))
|
||||
.set('Origin', config.get('url'))
|
||||
.attach('redirects', path.join(config.get('paths:contentPath'), 'redirects.json'))
|
||||
.expect('Content-Type', /application\/json/)
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('wrong format: no from/to', function () {
|
||||
fs.writeFileSync(path.join(config.get('paths:contentPath'), 'redirects.json'), JSON.stringify([{to: 'd'}]));
|
||||
|
||||
return request
|
||||
.post(localUtils.API.getApiQuery('redirects/json/'))
|
||||
.set('Origin', config.get('url'))
|
||||
.attach('redirects', path.join(config.get('paths:contentPath'), 'redirects.json'))
|
||||
.expect('Content-Type', /application\/json/)
|
||||
.expect(422);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Ensure re-registering redirects works', function () {
|
||||
const startGhost = (options) => {
|
||||
return ghost(options)
|
||||
|
|
|
@ -5,15 +5,12 @@ const Promise = require('bluebird');
|
|||
const path = require('path');
|
||||
const testUtils = require('../../../../utils');
|
||||
const localUtils = require('./utils');
|
||||
const configUtils = require('../../../../utils/configUtils');
|
||||
const config = require('../../../../../core/shared/config');
|
||||
|
||||
const ghost = testUtils.startGhost;
|
||||
let request;
|
||||
|
||||
describe('Redirects API', function () {
|
||||
let originalContentPath;
|
||||
|
||||
before(function () {
|
||||
return ghost({redirectsFile: true})
|
||||
.then(() => {
|
||||
|
@ -21,9 +18,6 @@ describe('Redirects API', function () {
|
|||
})
|
||||
.then(() => {
|
||||
return localUtils.doAuth(request);
|
||||
})
|
||||
.then(() => {
|
||||
originalContentPath = configUtils.config.get('paths:contentPath');
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -38,30 +32,6 @@ describe('Redirects API', function () {
|
|||
};
|
||||
|
||||
describe('Upload', function () {
|
||||
describe('Error cases', function () {
|
||||
it('syntax error', function () {
|
||||
fs.writeFileSync(path.join(config.get('paths:contentPath'), 'redirects.json'), 'something');
|
||||
|
||||
return request
|
||||
.post(localUtils.API.getApiQuery('redirects/json/'))
|
||||
.set('Origin', config.get('url'))
|
||||
.attach('redirects', path.join(config.get('paths:contentPath'), 'redirects.json'))
|
||||
.expect('Content-Type', /application\/json/)
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('wrong format: no from/to', function () {
|
||||
fs.writeFileSync(path.join(config.get('paths:contentPath'), 'redirects.json'), JSON.stringify([{to: 'd'}]));
|
||||
|
||||
return request
|
||||
.post(localUtils.API.getApiQuery('redirects/json/'))
|
||||
.set('Origin', config.get('url'))
|
||||
.attach('redirects', path.join(config.get('paths:contentPath'), 'redirects.json'))
|
||||
.expect('Content-Type', /application\/json/)
|
||||
.expect(422);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Ensure re-registering redirects works', function () {
|
||||
it('no redirects file exists', function () {
|
||||
return startGhost({redirectsFile: false, forceStart: true})
|
||||
|
@ -175,19 +145,6 @@ describe('Redirects API', function () {
|
|||
});
|
||||
|
||||
describe('Upload yaml', function () {
|
||||
describe('Error cases', function () {
|
||||
it('syntax error', function () {
|
||||
fs.writeFileSync(path.join(config.get('paths:contentPath'), 'redirects.yaml'), 'x');
|
||||
|
||||
return request
|
||||
.post(localUtils.API.getApiQuery('redirects/json/'))
|
||||
.set('Origin', config.get('url'))
|
||||
.attach('redirects', path.join(config.get('paths:contentPath'), 'redirects.yaml'))
|
||||
.expect('Content-Type', /application\/json/)
|
||||
.expect(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Ensure re-registering redirects works', function () {
|
||||
it('no redirects file exists', function () {
|
||||
return startGhost({redirectsFile: false, forceStart: true})
|
||||
|
|
|
@ -7,6 +7,22 @@ const DynamicRedirectManager = require('@tryghost/express-dynamic-redirects');
|
|||
const CustomRedirectsAPI = require('../../../../../core/server/services/redirects/api');
|
||||
|
||||
describe('UNIT: redirects CustomRedirectsAPI class', function () {
|
||||
let customRedirectsAPI;
|
||||
const basePath = path.join(__dirname, '../../../../utils/fixtures/data/');
|
||||
|
||||
before(function () {
|
||||
const redirectManager = new DynamicRedirectManager({
|
||||
permanentMaxAge: 100,
|
||||
getSubdirectoryURL: (pathname) => {
|
||||
return `/ghost/${pathname}`;
|
||||
}
|
||||
});
|
||||
|
||||
customRedirectsAPI = new CustomRedirectsAPI({
|
||||
basePath
|
||||
}, redirectManager);
|
||||
});
|
||||
|
||||
beforeEach(function () {
|
||||
sinon.stub(fs, 'pathExists');
|
||||
sinon.stub(fs, 'readFile');
|
||||
|
@ -18,52 +34,27 @@ describe('UNIT: redirects CustomRedirectsAPI class', function () {
|
|||
|
||||
describe('get', function () {
|
||||
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) => {
|
||||
return `/ghost/${pathname}`;
|
||||
}
|
||||
});
|
||||
|
||||
const customRedirectsAPI = new CustomRedirectsAPI({
|
||||
basePath
|
||||
}, redirectManager);
|
||||
|
||||
const file = await customRedirectsAPI.get();
|
||||
|
||||
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'
|
||||
|
@ -75,20 +66,37 @@ describe('UNIT: redirects CustomRedirectsAPI class', function () {
|
|||
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);
|
||||
});
|
||||
});
|
||||
|
||||
describe('setFromFilePath', function () {
|
||||
it('throws a syntax error when setting invalid JSON redirects file', async function () {
|
||||
const invalidFilePath = path.join(__dirname, '/invalid/redirects/path.json');
|
||||
fs.readFile.withArgs(invalidFilePath, 'utf-8').resolves('{invalid json');
|
||||
|
||||
try {
|
||||
await customRedirectsAPI.setFromFilePath(invalidFilePath, '.json');
|
||||
should.fail('setFromFilePath did not throw');
|
||||
} catch (err) {
|
||||
should.exist(err);
|
||||
err.message.should.eql('Could not parse JSON: Unexpected token i in JSON at position 1.');
|
||||
}
|
||||
});
|
||||
|
||||
it('throws a syntax error when setting invalid YAML redirects file', async function () {
|
||||
const invalidFilePath = path.join(__dirname, '/invalid/redirects/yaml.json');
|
||||
fs.readFile.withArgs(invalidFilePath, 'utf-8').resolves('x');
|
||||
|
||||
try {
|
||||
await customRedirectsAPI.setFromFilePath(invalidFilePath, '.yaml');
|
||||
should.fail('setFromFilePath did not throw');
|
||||
} catch (err) {
|
||||
should.exist(err);
|
||||
err.message.should.eql('YAML input cannot be a plain string. Check the format of your YAML file.');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue