From 8f5186995d9e9e311108197d02f9c5ce36a5a5ae Mon Sep 17 00:00:00 2001 From: Naz Date: Mon, 4 Oct 2021 16:44:23 +0200 Subject: [PATCH] Added unit test coverage for invalid redirects config refs https://linear.app/tryghost/issue/CORE-86/fix-failing-site-instance-when-redirects-file-is-invalid refs https://github.com/TryGhost/Ghost/commit/260a47da834def9fdd8f497db2084e50d33d1119 - Refed commit was missing a unit test coverage. - The approach here introduces a new pattern - using `supertest` in unit tests. I've found this to be the most expressive way to test an express app which receives certain middleware dynamically. Because there are very few moving parts the test is still extremely quick to run --- .../middleware/custom-redirects.test.js | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/test/unit/web/shared/middleware/custom-redirects.test.js b/test/unit/web/shared/middleware/custom-redirects.test.js index 6ed5fbdacf..65d9a9bafa 100644 --- a/test/unit/web/shared/middleware/custom-redirects.test.js +++ b/test/unit/web/shared/middleware/custom-redirects.test.js @@ -5,6 +5,7 @@ const express = require('express'); const customRedirects = rewire('../../../../../core/server/web/shared/middlewares/custom-redirects'); const registerRoutes = customRedirects.__get__('_private.registerRoutes'); +const supertest = require('supertest'); describe('UNIT: custom redirects', function () { let res; @@ -17,7 +18,8 @@ describe('UNIT: custom redirects', function () { }; res = { redirect: sinon.spy(), - set: sinon.spy() + set: sinon.spy(), + writeHead: sinon.spy() }; next = sinon.spy(); @@ -46,4 +48,22 @@ describe('UNIT: custom redirects', function () { 'Cache-Control': `public, max-age=0` }); }); + + it('the parent app functions even when the middleware gets an invalid redirects configuration', function (done) { + const redirectsConfig = [{ + permanent: true, + from: '/invalid_regex/(/size/[a-zA-Z0-9_-.]*/[a-zA-Z0-9_-.]*/[0-9]*/[0-9]*/)([a-zA-Z0-9_-.]*)', + to: '/' + }]; + const redirectsService = customRedirects.__get__('redirectsService'); + sinon.stub(redirectsService, 'loadRedirectsFile').returns(redirectsConfig); + + const app = express('test'); + customRedirects.use(app); + app.get('/', (_req, _res) => _res.status(200).end()); + + supertest(app) + .get('/') + .expect(200, done); + }); });