Ghost/test/unit/models/single-use-token_spec.js
Fabien 'egg' O'Carroll 7fdddf34b3
🐛 Added multiple use grace period to tokens (#12519)
closes https://github.com/TryGhost/Ghost/issues/12347

This change allows a token to be used multiple times for the first 10
seconds after its initial use, this will stop dynamic link checking
software from invaliding magic links.
2021-01-18 17:03:41 +00:00

42 lines
1.1 KiB
JavaScript

const models = require('../../../core/server/models');
const should = require('should');
const sinon = require('sinon');
let clock;
let sandbox;
describe('Unit: models/single-use-token', function () {
before(function () {
models.init();
sandbox = sinon.createSandbox();
clock = sandbox.useFakeTimers();
});
after(function () {
clock.restore();
sandbox.restore();
});
describe('fn: findOne', function () {
it('Calls destroy after the grace period', async function () {
const data = {};
const options = {};
const fakeModel = {
id: 'fake_id'
};
const findOneSuperStub = sandbox.stub(models.Base.Model, 'findOne').resolves(fakeModel);
const destroyStub = sandbox.stub(models.SingleUseToken, 'destroy').resolves();
await models.SingleUseToken.findOne(data, options);
should.ok(findOneSuperStub.calledWith(data, options), 'super.findOne was called');
clock.tick(10000);
should.ok(destroyStub.called, 'destroy was called after 10 seconds');
});
});
});