2020-03-30 18:26:47 +03:00
|
|
|
const SessionStore = require('../../../../../core/server/services/auth/session/store');
|
|
|
|
const models = require('../../../../../core/server/models');
|
2018-10-02 11:35:23 +03:00
|
|
|
const EventEmitter = require('events');
|
|
|
|
const {Store} = require('express-session');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const should = require('should');
|
|
|
|
|
|
|
|
describe('Auth Service SessionStore', 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
|
|
|
});
|
|
|
|
|
|
|
|
describe('inheritance', function () {
|
|
|
|
it('Is an instance of EventEmitter', function () {
|
|
|
|
const store = new SessionStore();
|
|
|
|
should.equal(store instanceof EventEmitter, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Is an instance of Store', function () {
|
|
|
|
const store = new SessionStore();
|
|
|
|
should.equal(store instanceof Store, true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('SessionStore#destroy', function () {
|
|
|
|
it('calls destroy on the model with the session_id `sid`', function (done) {
|
2019-01-21 19:53:44 +03:00
|
|
|
const destroyStub = sinon.stub(models.Session, 'destroy')
|
2018-10-02 11:35:23 +03:00
|
|
|
.resolves();
|
|
|
|
|
|
|
|
const store = new SessionStore(models.Session);
|
|
|
|
const sid = 1;
|
|
|
|
store.destroy(sid, function () {
|
|
|
|
const destroyStubCall = destroyStub.getCall(0);
|
|
|
|
should.equal(destroyStubCall.args[0].session_id, sid);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls back with null if destroy resolve', function (done) {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(models.Session, 'destroy')
|
2018-10-02 11:35:23 +03:00
|
|
|
.resolves();
|
|
|
|
|
|
|
|
const store = new SessionStore(models.Session);
|
|
|
|
const sid = 1;
|
|
|
|
store.destroy(sid, function (err) {
|
|
|
|
should.equal(err, null);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls back with the error if destroy errors', function (done) {
|
|
|
|
const error = new Error('beam me up scotty');
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(models.Session, 'destroy')
|
2018-10-02 11:35:23 +03:00
|
|
|
.rejects(error);
|
|
|
|
|
|
|
|
const store = new SessionStore(models.Session);
|
|
|
|
const sid = 1;
|
|
|
|
store.destroy(sid, function (err) {
|
|
|
|
should.equal(err, error);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('SessionStore#get', function () {
|
|
|
|
it('calls findOne on the model with the session_id `sid`', function (done) {
|
2019-01-21 19:53:44 +03:00
|
|
|
const findOneStub = sinon.stub(models.Session, 'findOne')
|
2018-10-02 11:35:23 +03:00
|
|
|
.resolves();
|
|
|
|
|
|
|
|
const store = new SessionStore(models.Session);
|
|
|
|
const sid = 1;
|
|
|
|
store.get(sid, function () {
|
|
|
|
const findOneStubCall = findOneStub.getCall(0);
|
|
|
|
should.equal(findOneStubCall.args[0].session_id, sid);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('callsback with null, null if findOne does not return a model', function (done) {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(models.Session, 'findOne')
|
2018-10-02 11:35:23 +03:00
|
|
|
.resolves(null);
|
|
|
|
|
|
|
|
const store = new SessionStore(models.Session);
|
|
|
|
const sid = 1;
|
|
|
|
store.get(sid, function (err, session) {
|
|
|
|
should.equal(err, null);
|
|
|
|
should.equal(session, null);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('callsback with null, model.session_data if findOne does return a model', function (done) {
|
|
|
|
const model = models.Session.forge({
|
|
|
|
session_data: {
|
|
|
|
ice: 'cube'
|
|
|
|
}
|
|
|
|
});
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(models.Session, 'findOne')
|
2018-10-02 11:35:23 +03:00
|
|
|
.resolves(model);
|
|
|
|
|
|
|
|
const store = new SessionStore(models.Session);
|
|
|
|
const sid = 1;
|
|
|
|
store.get(sid, function (err, session) {
|
|
|
|
should.equal(err, null);
|
|
|
|
should.deepEqual(session, {
|
|
|
|
ice: 'cube'
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('callsback with an error if the findOne does error', function (done) {
|
|
|
|
const error = new Error('hot damn');
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(models.Session, 'findOne')
|
2018-10-02 11:35:23 +03:00
|
|
|
.rejects(error);
|
|
|
|
|
|
|
|
const store = new SessionStore(models.Session);
|
|
|
|
const sid = 1;
|
|
|
|
store.get(sid, function (err) {
|
|
|
|
should.equal(err, error);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('SessionStore#set', function () {
|
|
|
|
it('calls back with an error if there is no user_id on the session_data', function (done) {
|
|
|
|
const store = new SessionStore(models.Session);
|
|
|
|
const sid = 1;
|
|
|
|
const session_data = {};
|
|
|
|
store.set(sid, session_data, function (err) {
|
|
|
|
should.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls upsert on the model with the session_id and the session_data', function (done) {
|
2019-01-21 19:53:44 +03:00
|
|
|
const upsertStub = sinon.stub(models.Session, 'upsert')
|
2018-10-02 11:35:23 +03:00
|
|
|
.resolves();
|
|
|
|
|
|
|
|
const store = new SessionStore(models.Session);
|
|
|
|
const sid = 1;
|
|
|
|
const session_data = {user_id: 100};
|
|
|
|
store.set(sid, session_data, function () {
|
|
|
|
const upsertStubCall = upsertStub.getCall(0);
|
|
|
|
should.equal(upsertStubCall.args[0].session_data, session_data);
|
|
|
|
should.equal(upsertStubCall.args[1].session_id, sid);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls back with an error if upsert errors', function (done) {
|
|
|
|
const error = new Error('huuuuuurrr');
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(models.Session, 'upsert')
|
2018-10-02 11:35:23 +03:00
|
|
|
.rejects(error);
|
|
|
|
|
|
|
|
const store = new SessionStore(models.Session);
|
|
|
|
const sid = 1;
|
|
|
|
const session_data = {user_id: 100};
|
|
|
|
store.set(sid, session_data, function (err) {
|
|
|
|
should.equal(err, error);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls back with null, null if upsert succeed', function (done) {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(models.Session, 'upsert')
|
2018-10-02 11:35:23 +03:00
|
|
|
.resolves('success');
|
|
|
|
|
|
|
|
const store = new SessionStore(models.Session);
|
|
|
|
const sid = 1;
|
|
|
|
const session_data = {user_id: 100};
|
|
|
|
store.set(sid, session_data, function (err, data) {
|
|
|
|
should.equal(err, null);
|
|
|
|
should.equal(data, null);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|