mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
allow admins to manipulate time on test servers
This commit is contained in:
committed by
Sabe Jones
parent
1b12e9d8b7
commit
593524905e
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user