habitica-self-host/test/api/unit/middlewares/ensureTimeTravelMode.js

52 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2024-02-28 16:54:42 +00:00
/* eslint-disable global-require */
import nconf from 'nconf';
import {
generateRes,
generateReq,
generateNext,
} from '../../../helpers/api-unit.helper';
import { NotFound } from '../../../../website/server/libs/errors';
2024-02-28 17:22:55 +00:00
import ensureTimeTravelMode from '../../../../website/server/middlewares/ensureTimeTravelMode';
2024-02-28 16:54:42 +00:00
describe('timetravelMode middleware', () => {
2024-06-19 14:02:03 +00:00
let res; let req; let next;
let nconfStub;
2024-02-28 16:54:42 +00:00
beforeEach(() => {
res = generateRes();
req = generateReq();
next = generateNext();
2024-06-19 14:02:03 +00:00
nconfStub = sandbox.stub(nconf, 'get');
2024-02-28 16:54:42 +00:00
});
2024-05-21 14:11:18 +00:00
it('returns not found when using production URL', () => {
2024-06-19 14:02:03 +00:00
nconfStub.withArgs('TIME_TRAVEL_ENABLED').returns(false);
nconfStub.withArgs('BASE_URL').returns('https://habitica.com');
2024-05-21 14:11:18 +00:00
ensureTimeTravelMode(req, res, next);
const calledWith = next.getCall(0).args;
expect(calledWith[0] instanceof NotFound).to.equal(true);
});
2024-02-28 16:54:42 +00:00
it('returns not found when not in time travel mode', () => {
2024-06-19 14:02:03 +00:00
nconfStub.withArgs('TIME_TRAVEL_ENABLED').returns(false);
nconfStub.withArgs('BASE_URL').returns('http://localhost:3000');
2024-02-28 16:54:42 +00:00
2024-02-28 17:22:55 +00:00
ensureTimeTravelMode(req, res, next);
2024-02-28 16:54:42 +00:00
const calledWith = next.getCall(0).args;
expect(calledWith[0] instanceof NotFound).to.equal(true);
});
it('passes when in time travel mode', () => {
2024-06-19 14:02:03 +00:00
nconfStub.withArgs('TIME_TRAVEL_ENABLED').returns(true);
nconfStub.withArgs('BASE_URL').returns('http://localhost:3000');
2024-02-28 16:54:42 +00:00
2024-02-28 17:22:55 +00:00
ensureTimeTravelMode(req, res, next);
2024-02-28 16:54:42 +00:00
expect(next).to.be.calledOnce;
expect(next.args[0]).to.be.empty;
});
});