allow admins to manipulate time on test servers

This commit is contained in:
Phillip Thelen
2024-02-15 13:09:46 +01:00
committed by Sabe Jones
parent 1b12e9d8b7
commit 593524905e
6 changed files with 109 additions and 37 deletions

View File

@@ -1,4 +1,6 @@
import _ from 'lodash';
import nconf from 'nconf';
import moment from 'moment';
import { authWithHeaders } from '../../middlewares/auth';
import ensureDevelpmentMode from '../../middlewares/ensureDevelpmentMode';
import { BadRequest } from '../../libs/errors';
@@ -201,4 +203,57 @@ api.questProgress = {
},
};
let clock;
if (nconf.get('ENABLE_TIME_TRAVEL')) {
(async () => {
const sinon = await import('sinon');
const time = new Date();
clock = sinon.useFakeTimers({
now: time,
shouldAdvanceTime: true,
});
})();
api.timeTravelTime = {
method: 'GET',
url: '/debug/time-travel-time',
middlewares: [authWithHeaders()],
async handler (req, res) {
const { user } = res.locals;
if (!user.permissions.fullAccess) {
throw new BadRequest('You do not have permission to time travel.');
}
res.respond(200, {
time: new Date(),
});
},
}
api.timeTravelAdjust = {
method: 'POST',
url: '/debug/jump-time',
middlewares: [authWithHeaders()],
async handler (req, res) {
const { user } = res.locals;
if (!user.permissions.fullAccess) {
throw new BadRequest('You do not have permission to time travel.');
}
const { offsetDays } = req.body;
if (offsetDays > 0) {
clock.jump(offsetDays * 24 * 60 * 60 * 1000)
} else {
clock.setSystemTime(moment().add(offsetDays, 'days').toDate());
}
res.respond(200, {
time: new Date(),
});
},
}
}
export default api;