mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 18:31:57 +03:00
6a2afbd859
- this has the nice side effect of increasing code coverage by checking the exports of the package
45 lines
1.5 KiB
JavaScript
45 lines
1.5 KiB
JavaScript
const express = require('express');
|
|
const sinon = require('sinon');
|
|
const should = require('should');
|
|
const SessionFromToken = require('../');
|
|
|
|
describe('SessionFromToken', function () {
|
|
it('Parses the request, matches the user to the token, sets the user on req.user and calls createSession', async function () {
|
|
const createSession = sinon.spy(async (req, res, user) => {
|
|
req.session = user;
|
|
});
|
|
const findUserByLookup = sinon.spy(async email => ({id: '1', email}));
|
|
const getTokenFromRequest = sinon.spy(async req => req.token);
|
|
const getLookupFromToken = sinon.spy(async token => token.email);
|
|
|
|
const handler = SessionFromToken({
|
|
getTokenFromRequest,
|
|
getLookupFromToken,
|
|
findUserByLookup,
|
|
createSession,
|
|
callNextWithError: true
|
|
});
|
|
|
|
const req = Object.create(express.request);
|
|
const res = Object.create(express.response);
|
|
const next = sinon.spy();
|
|
|
|
req.token = {
|
|
email: 'user@host.tld'
|
|
};
|
|
|
|
await handler(req, res, next);
|
|
|
|
should.ok(getTokenFromRequest.calledOnceWith(req));
|
|
const token = await getTokenFromRequest.returnValues[0];
|
|
|
|
should.ok(getLookupFromToken.calledOnceWith(token));
|
|
const email = await getLookupFromToken.returnValues[0];
|
|
|
|
should.ok(findUserByLookup.calledOnceWith(email));
|
|
const foundUser = await findUserByLookup.returnValues[0];
|
|
|
|
should.ok(createSession.calledOnceWith(req, res, foundUser));
|
|
});
|
|
});
|