authelia/test/unit/server/DataPersistence.test.ts

173 lines
4.4 KiB
TypeScript
Raw Normal View History

2017-05-16 21:17:46 +00:00
import * as BluebirdPromise from "bluebird";
2017-05-16 21:17:46 +00:00
import * as request from "request";
import Server from "../../../src/server/lib/Server";
import { UserConfiguration } from "../../../src/types/Configuration";
import { GlobalDependencies } from "../../../src/types/Dependencies";
2017-05-16 21:17:46 +00:00
import * as tmp from "tmp";
import U2FMock = require("./mocks/u2f");
import { LdapjsClientMock } from "./mocks/ldapjs";
2017-05-16 21:17:46 +00:00
const requestp = BluebirdPromise.promisifyAll(request) as request.Request;
2017-05-16 21:17:46 +00:00
const assert = require("assert");
const speakeasy = require("speakeasy");
const sinon = require("sinon");
const nedb = require("nedb");
const session = require("express-session");
const winston = require("winston");
const PORT = 8050;
const requests = require("./requests")(PORT);
describe("test data persistence", function () {
let u2f: U2FMock.U2FMock;
2017-05-16 21:17:46 +00:00
let tmpDir: tmp.SynchrounousResult;
const ldapClient = LdapjsClientMock();
2017-05-16 21:17:46 +00:00
const ldap = {
createClient: sinon.spy(function () {
return ldapClient;
2017-05-16 21:17:46 +00:00
})
};
let config: UserConfiguration;
before(function () {
u2f = U2FMock.U2FMock();
2017-05-16 21:17:46 +00:00
const search_doc = {
object: {
mail: "test_ok@example.com"
}
};
const search_res = {
on: sinon.spy(function (event: string, fn: (s: object) => void) {
if (event != "error") fn(search_doc);
})
};
ldapClient.bind.withArgs("cn=test_ok,ou=users,dc=example,dc=com",
"password").yields();
ldapClient.bind.withArgs("cn=test_nok,ou=users,dc=example,dc=com",
2017-05-16 21:17:46 +00:00
"password").yields("error");
ldapClient.search.yields(undefined, search_res);
ldapClient.unbind.yields();
2017-05-16 21:17:46 +00:00
tmpDir = tmp.dirSync({ unsafeCleanup: true });
config = {
port: PORT,
ldap: {
url: "ldap://127.0.0.1:389",
base_dn: "ou=users,dc=example,dc=com",
user: "user",
password: "password"
},
session: {
secret: "session_secret",
expiration: 50000,
},
store_directory: tmpDir.name,
notifier: {
gmail: {
2017-05-20 07:49:05 +00:00
username: "user@example.com",
password: "password"
2017-05-16 21:17:46 +00:00
}
}
};
});
after(function () {
tmpDir.removeCallback();
});
it("should save a u2f meta and reload it after a restart of the server", function () {
let server: Server;
const sign_request = {};
const sign_status = {};
const registration_status = {};
u2f.request.returns(sign_request);
u2f.checkRegistration.returns(sign_status);
u2f.checkSignature.returns(registration_status);
2017-05-16 21:17:46 +00:00
const nodemailer = {
createTransport: sinon.spy(function () {
return transporter;
})
};
const transporter = {
sendMail: sinon.stub().yields()
};
const deps: GlobalDependencies = {
2017-05-16 21:17:46 +00:00
u2f: u2f,
nedb: nedb,
nodemailer: nodemailer,
session: session,
winston: winston,
ldapjs: ldap,
speakeasy: speakeasy,
ConnectRedis: sinon.spy()
};
2017-05-16 21:17:46 +00:00
const j1 = request.jar();
const j2 = request.jar();
return start_server(config, deps)
.then(function (s) {
server = s;
return requests.login(j1);
})
.then(function (res) {
return requests.first_factor(j1);
})
.then(function () {
return requests.u2f_registration(j1, transporter);
})
.then(function () {
return requests.u2f_authentication(j1);
})
.then(function () {
return stop_server(server);
})
.then(function () {
return start_server(config, deps);
})
.then(function (s) {
server = s;
return requests.login(j2);
})
.then(function () {
return requests.first_factor(j2);
})
.then(function () {
return requests.u2f_authentication(j2);
})
.then(function (res) {
assert.equal(200, res.statusCode);
2017-05-16 21:17:46 +00:00
server.stop();
return BluebirdPromise.resolve();
2017-05-16 21:17:46 +00:00
})
.catch(function (err) {
console.error(err);
return BluebirdPromise.reject(err);
2017-05-16 21:17:46 +00:00
});
});
function start_server(config: UserConfiguration, deps: GlobalDependencies): BluebirdPromise<Server> {
return new BluebirdPromise<Server>(function (resolve, reject) {
2017-05-16 21:17:46 +00:00
const s = new Server();
s.start(config, deps);
resolve(s);
});
}
function stop_server(s: Server) {
return new BluebirdPromise(function (resolve, reject) {
2017-05-16 21:17:46 +00:00
s.stop();
resolve();
});
}
});