WIP(shops): safer debug mode

This commit is contained in:
Sabe Jones
2024-05-21 09:11:18 -05:00
parent 36b589e92d
commit c0d6338eba
16 changed files with 118 additions and 90 deletions

View File

@@ -0,0 +1,50 @@
/* eslint-disable global-require */
import nconf from 'nconf';
import {
generateRes,
generateReq,
generateNext,
} from '../../../helpers/api-unit.helper';
import ensureDevelopmentMode from '../../../../website/server/middlewares/ensureDevelopmentMode';
import { NotFound } from '../../../../website/server/libs/errors';
describe('developmentMode middleware', () => {
let res; let req; let
next;
beforeEach(() => {
res = generateRes();
req = generateReq();
next = generateNext();
});
it('returns not found when on production URL', () => {
sandbox.stub(nconf, 'get').withArgs('DEBUG_ENABLED').returns(true);
sandbox.stub(nconf, 'get').withArgs('BASE_URL').returns('https://habitica.com');
ensureDevelopmentMode(req, res, next);
const calledWith = next.getCall(0).args;
expect(calledWith[0] instanceof NotFound).to.equal(true);
});
it('returns not found when intentionally disabled', () => {
sandbox.stub(nconf, 'get').withArgs('DEBUG_ENABLED').returns(false);
sandbox.stub(nconf, 'get').withArgs('BASE_URL').returns('http://localhost:3000');
ensureDevelopmentMode(req, res, next);
const calledWith = next.getCall(0).args;
expect(calledWith[0] instanceof NotFound).to.equal(true);
});
it('passes when enabled and on non-production URL', () => {
sandbox.stub(nconf, 'get').withArgs('DEBUG_ENABLED').returns(true);
sandbox.stub(nconf, 'get').withArgs('BASE_URL').returns('http://localhost:3000');
ensureDevelopmentMode(req, res, next);
expect(next).to.be.calledOnce;
expect(next.args[0]).to.be.empty;
});
});

View File

@@ -1,38 +0,0 @@
/* eslint-disable global-require */
import nconf from 'nconf';
import {
generateRes,
generateReq,
generateNext,
} from '../../../helpers/api-unit.helper';
import ensureDevelpmentMode from '../../../../website/server/middlewares/ensureDevelpmentMode';
import { NotFound } from '../../../../website/server/libs/errors';
describe('developmentMode middleware', () => {
let res; let req; let
next;
beforeEach(() => {
res = generateRes();
req = generateReq();
next = generateNext();
});
it('returns not found when in production mode', () => {
sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(true);
ensureDevelpmentMode(req, res, next);
const calledWith = next.getCall(0).args;
expect(calledWith[0] instanceof NotFound).to.equal(true);
});
it('passes when not in production', () => {
sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(false);
ensureDevelpmentMode(req, res, next);
expect(next).to.be.calledOnce;
expect(next.args[0]).to.be.empty;
});
});

View File

@@ -18,8 +18,19 @@ describe('timetravelMode middleware', () => {
next = generateNext();
});
it('returns not found when using production URL', () => {
sandbox.stub(nconf, 'get').withArgs('TIME_TRAVEL_ENABLED').returns(false);
sandbox.stub(nconf, 'get').withArgs('BASE_URL').returns('https://habitica.com');
ensureTimeTravelMode(req, res, next);
const calledWith = next.getCall(0).args;
expect(calledWith[0] instanceof NotFound).to.equal(true);
});
it('returns not found when not in time travel mode', () => {
sandbox.stub(nconf, 'get').withArgs('ENABLE_TIME_TRAVEL').returns(false);
sandbox.stub(nconf, 'get').withArgs('TIME_TRAVEL_ENABLED').returns(false);
sandbox.stub(nconf, 'get').withArgs('BASE_URL').returns('http://localhost:3000');
ensureTimeTravelMode(req, res, next);
@@ -28,7 +39,8 @@ describe('timetravelMode middleware', () => {
});
it('passes when in time travel mode', () => {
sandbox.stub(nconf, 'get').withArgs('ENABLE_TIME_TRAVEL').returns(true);
sandbox.stub(nconf, 'get').withArgs('TIME_TRAVEL_ENABLED').returns(true);
sandbox.stub(nconf, 'get').withArgs('BASE_URL').returns('http://localhost:3000');
ensureTimeTravelMode(req, res, next);