2017-05-17 04:17:46 +07:00
|
|
|
|
2017-05-25 20:09:29 +07:00
|
|
|
import * as BluebirdPromise from "bluebird";
|
2017-05-17 04:17:46 +07:00
|
|
|
import * as request from "request";
|
|
|
|
|
2017-05-25 20:09:29 +07:00
|
|
|
import Server from "../../src/server/lib/Server";
|
|
|
|
import { UserConfiguration } from "../../src/types/Configuration";
|
2017-05-21 00:16:57 +07:00
|
|
|
import { GlobalDependencies } from "../../src/types/Dependencies";
|
2017-05-17 04:17:46 +07:00
|
|
|
import * as tmp from "tmp";
|
2017-05-25 20:09:29 +07:00
|
|
|
import U2FMock = require("./mocks/u2f");
|
2017-06-15 05:22:16 +07:00
|
|
|
import { LdapjsClientMock } from "./mocks/ldapjs";
|
2017-05-17 04:17:46 +07:00
|
|
|
|
|
|
|
|
2017-05-25 20:09:29 +07:00
|
|
|
const requestp = BluebirdPromise.promisifyAll(request) as request.Request;
|
2017-05-17 04:17:46 +07: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 () {
|
2017-05-25 20:09:29 +07:00
|
|
|
let u2f: U2FMock.U2FMock;
|
2017-05-17 04:17:46 +07:00
|
|
|
let tmpDir: tmp.SynchrounousResult;
|
2017-06-15 05:22:16 +07:00
|
|
|
const ldapClient = LdapjsClientMock();
|
2017-05-17 04:17:46 +07:00
|
|
|
const ldap = {
|
|
|
|
createClient: sinon.spy(function () {
|
2017-06-15 05:22:16 +07:00
|
|
|
return ldapClient;
|
2017-05-17 04:17:46 +07:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
let config: UserConfiguration;
|
|
|
|
|
|
|
|
before(function () {
|
2017-05-25 20:09:29 +07:00
|
|
|
u2f = U2FMock.U2FMock();
|
2017-05-17 04:17:46 +07: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);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2017-06-15 05:22:16 +07:00
|
|
|
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-17 04:17:46 +07:00
|
|
|
"password").yields("error");
|
2017-06-15 05:22:16 +07:00
|
|
|
ldapClient.search.yields(undefined, search_res);
|
|
|
|
ldapClient.unbind.yields();
|
2017-05-17 04:17:46 +07: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 14:49:05 +07:00
|
|
|
username: "user@example.com",
|
|
|
|
password: "password"
|
2017-05-17 04:17:46 +07: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 = {};
|
2017-05-25 20:09:29 +07:00
|
|
|
u2f.request.returns(sign_request);
|
|
|
|
u2f.checkRegistration.returns(sign_status);
|
|
|
|
u2f.checkSignature.returns(registration_status);
|
2017-05-17 04:17:46 +07:00
|
|
|
|
|
|
|
const nodemailer = {
|
|
|
|
createTransport: sinon.spy(function () {
|
|
|
|
return transporter;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
const transporter = {
|
|
|
|
sendMail: sinon.stub().yields()
|
|
|
|
};
|
|
|
|
|
2017-06-30 00:41:05 +07:00
|
|
|
const deps: GlobalDependencies = {
|
2017-05-17 04:17:46 +07:00
|
|
|
u2f: u2f,
|
|
|
|
nedb: nedb,
|
|
|
|
nodemailer: nodemailer,
|
|
|
|
session: session,
|
|
|
|
winston: winston,
|
|
|
|
ldapjs: ldap,
|
2017-06-30 00:41:05 +07:00
|
|
|
speakeasy: speakeasy,
|
|
|
|
ConnectRedis: sinon.spy()
|
|
|
|
};
|
2017-05-17 04:17:46 +07: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) {
|
2017-05-25 20:09:29 +07:00
|
|
|
assert.equal(200, res.statusCode);
|
2017-05-17 04:17:46 +07:00
|
|
|
server.stop();
|
2017-05-25 20:09:29 +07:00
|
|
|
return BluebirdPromise.resolve();
|
2017-05-17 04:17:46 +07:00
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
console.error(err);
|
2017-05-25 20:09:29 +07:00
|
|
|
return BluebirdPromise.reject(err);
|
2017-05-17 04:17:46 +07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-05-25 20:09:29 +07:00
|
|
|
function start_server(config: UserConfiguration, deps: GlobalDependencies): BluebirdPromise<Server> {
|
|
|
|
return new BluebirdPromise<Server>(function (resolve, reject) {
|
2017-05-17 04:17:46 +07:00
|
|
|
const s = new Server();
|
|
|
|
s.start(config, deps);
|
|
|
|
resolve(s);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function stop_server(s: Server) {
|
2017-05-25 20:09:29 +07:00
|
|
|
return new BluebirdPromise(function (resolve, reject) {
|
2017-05-17 04:17:46 +07:00
|
|
|
s.stop();
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|