2020-04-02 17:27:31 +03:00
|
|
|
const sessionMiddleware = require('../../../../../core/server/services/auth').session;
|
2020-03-30 18:26:47 +03:00
|
|
|
const models = require('../../../../../core/server/models');
|
2018-10-02 11:35:23 +03:00
|
|
|
const sinon = require('sinon');
|
|
|
|
const should = require('should');
|
|
|
|
|
|
|
|
describe('Session Service', function () {
|
|
|
|
before(function () {
|
|
|
|
models.init();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2018-10-02 11:35:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const fakeReq = function fakeReq() {
|
|
|
|
return {
|
|
|
|
session: {
|
|
|
|
destroy() {}
|
|
|
|
},
|
2020-04-06 12:49:14 +03:00
|
|
|
user: null,
|
2018-10-02 11:35:23 +03:00
|
|
|
body: {},
|
|
|
|
get() {}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const fakeRes = function fakeRes() {
|
|
|
|
return {
|
|
|
|
sendStatus() {}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('createSession', function () {
|
2018-10-10 16:07:31 +03:00
|
|
|
it('sets req.session.origin from the Referer header', function (done) {
|
|
|
|
const req = fakeReq();
|
|
|
|
const res = fakeRes();
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(req, 'get')
|
2018-10-10 16:07:31 +03:00
|
|
|
.withArgs('user-agent').returns('')
|
|
|
|
.withArgs('origin').returns('')
|
|
|
|
.withArgs('referrer').returns('http://ghost.org/path');
|
|
|
|
|
|
|
|
req.ip = '127.0.0.1';
|
|
|
|
req.user = models.User.forge({id: 23});
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(res, 'sendStatus')
|
2020-04-06 12:49:14 +03:00
|
|
|
.callsFake(function () {
|
2018-10-10 16:07:31 +03:00
|
|
|
should.equal(req.session.origin, 'http://ghost.org');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2019-01-18 19:03:03 +03:00
|
|
|
sessionMiddleware.createSession(req, res);
|
2018-10-10 16:07:31 +03:00
|
|
|
});
|
|
|
|
|
2018-10-02 11:35:23 +03:00
|
|
|
it('sets req.session.user_id,origin,user_agent,ip and calls sendStatus with 201 if the check succeeds', function (done) {
|
|
|
|
const req = fakeReq();
|
|
|
|
const res = fakeRes();
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(req, 'get')
|
2018-10-02 11:35:23 +03:00
|
|
|
.withArgs('origin').returns('http://host.tld')
|
|
|
|
.withArgs('user-agent').returns('bububang');
|
|
|
|
|
|
|
|
req.ip = '127.0.0.1';
|
|
|
|
req.user = models.User.forge({id: 23});
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(res, 'sendStatus')
|
2018-10-02 11:35:23 +03:00
|
|
|
.callsFake(function (statusCode) {
|
|
|
|
should.equal(req.session.user_id, 23);
|
|
|
|
should.equal(req.session.origin, 'http://host.tld');
|
|
|
|
should.equal(req.session.user_agent, 'bububang');
|
|
|
|
should.equal(req.session.ip, '127.0.0.1');
|
|
|
|
should.equal(statusCode, 201);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2019-01-18 19:03:03 +03:00
|
|
|
sessionMiddleware.createSession(req, res);
|
2018-10-02 11:35:23 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('destroySession', function () {
|
2020-04-02 17:27:31 +03:00
|
|
|
it('calls req.session.destroy', function (done) {
|
2018-10-02 11:35:23 +03:00
|
|
|
const req = fakeReq();
|
|
|
|
const res = fakeRes();
|
2020-04-02 17:27:31 +03:00
|
|
|
const destroyStub = sinon.stub(req.session, 'destroy')
|
|
|
|
.callsFake(function (fn) {
|
|
|
|
fn();
|
|
|
|
});
|
2018-10-02 11:35:23 +03:00
|
|
|
|
2020-04-02 17:27:31 +03:00
|
|
|
sinon.stub(res, 'sendStatus')
|
2020-04-06 12:49:14 +03:00
|
|
|
.callsFake(function () {
|
2020-04-02 17:27:31 +03:00
|
|
|
should.equal(destroyStub.callCount, 1);
|
|
|
|
done();
|
|
|
|
});
|
2018-10-02 11:35:23 +03:00
|
|
|
|
2020-04-02 17:27:31 +03:00
|
|
|
sessionMiddleware.destroySession(req, res);
|
2018-10-02 11:35:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('calls next with InternalServerError if destroy errors', function (done) {
|
|
|
|
const req = fakeReq();
|
|
|
|
const res = fakeRes();
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(req.session, 'destroy')
|
2018-10-02 11:35:23 +03:00
|
|
|
.callsFake(function (fn) {
|
|
|
|
fn(new Error('oops'));
|
|
|
|
});
|
|
|
|
|
2019-01-18 19:03:03 +03:00
|
|
|
sessionMiddleware.destroySession(req, res, function next(err) {
|
2020-04-06 12:49:14 +03:00
|
|
|
should.equal(err.errorType, 'InternalServerError');
|
2018-10-02 11:35:23 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls sendStatus with 204 if destroy does not error', function (done) {
|
|
|
|
const req = fakeReq();
|
|
|
|
const res = fakeRes();
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(req.session, 'destroy')
|
2018-10-02 11:35:23 +03:00
|
|
|
.callsFake(function (fn) {
|
|
|
|
fn();
|
|
|
|
});
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(res, 'sendStatus')
|
2018-10-02 11:35:23 +03:00
|
|
|
.callsFake(function (status) {
|
|
|
|
should.equal(status, 204);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2019-01-18 19:03:03 +03:00
|
|
|
sessionMiddleware.destroySession(req, res);
|
2018-10-02 11:35:23 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|