2017-07-16 19:55:01 +07:00
|
|
|
import SessionConfigurationBuilder from "../../../src/server/lib/SessionConfigurationBuilder";
|
|
|
|
import { AppConfiguration } from "../../../src/types/Configuration";
|
|
|
|
import { GlobalDependencies } from "../../../src/types/Dependencies";
|
2017-07-03 03:24:51 +07:00
|
|
|
import ExpressSession = require("express-session");
|
|
|
|
import ConnectRedis = require("connect-redis");
|
|
|
|
import sinon = require("sinon");
|
|
|
|
import Assert = require("assert");
|
|
|
|
|
2017-07-14 04:04:08 +07:00
|
|
|
describe("test session configuration builder", function () {
|
2017-07-03 03:24:51 +07:00
|
|
|
it("should return session options without redis options", function () {
|
|
|
|
const configuration: AppConfiguration = {
|
|
|
|
access_control: {
|
|
|
|
default: [],
|
|
|
|
users: {},
|
|
|
|
groups: {}
|
|
|
|
},
|
|
|
|
ldap: {
|
|
|
|
url: "ldap://ldap",
|
|
|
|
base_dn: "dc=example,dc=com",
|
|
|
|
user: "user",
|
|
|
|
password: "password"
|
|
|
|
},
|
|
|
|
logs_level: "debug",
|
|
|
|
notifier: {
|
|
|
|
filesystem: {
|
|
|
|
filename: "/test"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
port: 8080,
|
|
|
|
session: {
|
|
|
|
domain: "example.com",
|
|
|
|
expiration: 3600,
|
|
|
|
secret: "secret"
|
|
|
|
},
|
|
|
|
store_in_memory: true
|
|
|
|
};
|
|
|
|
|
|
|
|
const deps: GlobalDependencies = {
|
|
|
|
ConnectRedis: sinon.spy() as any,
|
|
|
|
ldapjs: sinon.spy() as any,
|
|
|
|
nedb: sinon.spy() as any,
|
|
|
|
nodemailer: sinon.spy() as any,
|
|
|
|
session: sinon.spy() as any,
|
|
|
|
speakeasy: sinon.spy() as any,
|
|
|
|
u2f: sinon.spy() as any,
|
|
|
|
winston: sinon.spy() as any
|
|
|
|
};
|
|
|
|
|
|
|
|
const options = SessionConfigurationBuilder.build(configuration, deps);
|
|
|
|
|
|
|
|
const expectedOptions = {
|
|
|
|
secret: "secret",
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: true,
|
|
|
|
cookie: {
|
|
|
|
secure: false,
|
|
|
|
maxAge: 3600,
|
|
|
|
domain: "example.com"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Assert.deepEqual(expectedOptions, options);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return session options with redis options", function () {
|
|
|
|
const configuration: AppConfiguration = {
|
|
|
|
access_control: {
|
|
|
|
default: [],
|
|
|
|
users: {},
|
|
|
|
groups: {}
|
|
|
|
},
|
|
|
|
ldap: {
|
|
|
|
url: "ldap://ldap",
|
|
|
|
base_dn: "dc=example,dc=com",
|
|
|
|
user: "user",
|
|
|
|
password: "password"
|
|
|
|
},
|
|
|
|
logs_level: "debug",
|
|
|
|
notifier: {
|
|
|
|
filesystem: {
|
|
|
|
filename: "/test"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
port: 8080,
|
|
|
|
session: {
|
|
|
|
domain: "example.com",
|
|
|
|
expiration: 3600,
|
|
|
|
secret: "secret",
|
|
|
|
redis: {
|
|
|
|
host: "redis.example.com",
|
|
|
|
port: 6379
|
|
|
|
}
|
|
|
|
},
|
|
|
|
store_in_memory: true
|
|
|
|
};
|
|
|
|
|
2017-07-14 04:04:08 +07:00
|
|
|
const RedisStoreMock = sinon.spy();
|
|
|
|
|
2017-07-03 03:24:51 +07:00
|
|
|
const deps: GlobalDependencies = {
|
2017-07-14 04:04:08 +07:00
|
|
|
ConnectRedis: sinon.stub().returns(RedisStoreMock) as any,
|
2017-07-03 03:24:51 +07:00
|
|
|
ldapjs: sinon.spy() as any,
|
|
|
|
nedb: sinon.spy() as any,
|
|
|
|
nodemailer: sinon.spy() as any,
|
|
|
|
session: sinon.spy() as any,
|
|
|
|
speakeasy: sinon.spy() as any,
|
|
|
|
u2f: sinon.spy() as any,
|
|
|
|
winston: sinon.spy() as any
|
|
|
|
};
|
|
|
|
|
|
|
|
const options = SessionConfigurationBuilder.build(configuration, deps);
|
|
|
|
|
|
|
|
const expectedOptions: ExpressSession.SessionOptions = {
|
|
|
|
secret: "secret",
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: true,
|
|
|
|
cookie: {
|
|
|
|
secure: false,
|
|
|
|
maxAge: 3600,
|
|
|
|
domain: "example.com"
|
2017-07-14 04:04:08 +07:00
|
|
|
},
|
|
|
|
store: sinon.match.object as any
|
2017-07-03 03:24:51 +07:00
|
|
|
};
|
|
|
|
|
2017-07-14 04:04:08 +07:00
|
|
|
Assert((deps.ConnectRedis as sinon.SinonStub).calledWith(deps.session));
|
|
|
|
Assert.equal(options.secret, expectedOptions.secret);
|
|
|
|
Assert.equal(options.resave, expectedOptions.resave);
|
|
|
|
Assert.equal(options.saveUninitialized, expectedOptions.saveUninitialized);
|
|
|
|
Assert.deepEqual(options.cookie, expectedOptions.cookie);
|
|
|
|
Assert(options.store != undefined);
|
2017-07-03 03:24:51 +07:00
|
|
|
});
|
|
|
|
});
|