Merge pull request #144 from clems4ever/test-forward-headers

Fix unhandled rejections in unit tests
This commit is contained in:
Clément Michaud 2017-10-15 01:55:31 +02:00 committed by GitHub
commit 9fe202f227
4 changed files with 20 additions and 16 deletions

View File

@ -24,7 +24,7 @@
}, },
"dependencies": { "dependencies": {
"ajv": "^5.2.3", "ajv": "^5.2.3",
"bluebird": "^3.4.7", "bluebird": "3.5.0",
"body-parser": "^1.15.2", "body-parser": "^1.15.2",
"connect-redis": "^3.3.0", "connect-redis": "^3.3.0",
"dovehash": "0.0.5", "dovehash": "0.0.5",

View File

@ -1,5 +1,5 @@
import exceptions = require("../../Exceptions"); import Exceptions = require("../../Exceptions");
import objectPath = require("object-path"); import objectPath = require("object-path");
import BluebirdPromise = require("bluebird"); import BluebirdPromise = require("bluebird");
import express = require("express"); import express = require("express");
@ -21,19 +21,20 @@ export default function (req: express.Request, res: express.Response): BluebirdP
const logger = ServerVariablesHandler.getLogger(req.app); const logger = ServerVariablesHandler.getLogger(req.app);
const ldap = ServerVariablesHandler.getLdapAuthenticator(req.app); const ldap = ServerVariablesHandler.getLdapAuthenticator(req.app);
const config = ServerVariablesHandler.getConfiguration(req.app); const config = ServerVariablesHandler.getConfiguration(req.app);
if (!username || !password) {
return BluebirdPromise.reject(new Error("No username or password."));
}
const regulator = ServerVariablesHandler.getAuthenticationRegulator(req.app); const regulator = ServerVariablesHandler.getAuthenticationRegulator(req.app);
const accessController = ServerVariablesHandler.getAccessController(req.app); const accessController = ServerVariablesHandler.getAccessController(req.app);
const authenticationMethodsCalculator = const authenticationMethodsCalculator =
ServerVariablesHandler.getAuthenticationMethodCalculator(req.app); ServerVariablesHandler.getAuthenticationMethodCalculator(req.app);
let authSession: AuthenticationSession.AuthenticationSession; let authSession: AuthenticationSession.AuthenticationSession;
logger.info(req, "Starting authentication of user \"%s\"", username); return BluebirdPromise.resolve()
return AuthenticationSession.get(req) .then(function () {
if (!username || !password) {
return BluebirdPromise.reject(new Error("No username or password."));
}
logger.info(req, "Starting authentication of user \"%s\"", username);
return AuthenticationSession.get(req);
})
.then(function (_authSession: AuthenticationSession.AuthenticationSession) { .then(function (_authSession: AuthenticationSession.AuthenticationSession) {
authSession = _authSession; authSession = _authSession;
return regulator.regulate(username); return regulator.regulate(username);
@ -92,7 +93,7 @@ export default function (req: express.Request, res: express.Response): BluebirdP
} }
return BluebirdPromise.resolve(); return BluebirdPromise.resolve();
}) })
.catch(exceptions.LdapBindError, function (err: Error) { .catch(Exceptions.LdapBindError, function (err: Error) {
regulator.mark(username, false); regulator.mark(username, false);
return ErrorReplies.replyWithError200(req, res, logger, UserMessages.OPERATION_FAILED)(err); return ErrorReplies.replyWithError200(req, res, logger, UserMessages.OPERATION_FAILED)(err);
}) })

View File

@ -120,6 +120,7 @@ describe("test the first factor validation route", function () {
it("should return error message when LDAP authenticator throws", function () { it("should return error message when LDAP authenticator throws", function () {
(serverVariables.ldapAuthenticator as any).authenticate.withArgs("username", "password") (serverVariables.ldapAuthenticator as any).authenticate.withArgs("username", "password")
.returns(BluebirdPromise.reject(new exceptions.LdapBindError("Bad credentials"))); .returns(BluebirdPromise.reject(new exceptions.LdapBindError("Bad credentials")));
return FirstFactorPost.default(req as any, res as any) return FirstFactorPost.default(req as any, res as any)
.then(function () { .then(function () {
Assert.equal(res.status.getCall(0).args[0], 200); Assert.equal(res.status.getCall(0).args[0], 200);

View File

@ -23,7 +23,7 @@ describe("test totp route", function () {
const app_get = sinon.stub(); const app_get = sinon.stub();
req = { req = {
app: { app: {
get: sinon.stub().returns({ logger: winston }) get: sinon.stub().returns({ logger: winston })
}, },
body: { body: {
token: "abc" token: "abc"
@ -66,13 +66,15 @@ describe("test totp route", function () {
}); });
}); });
it("should send status code 401 when totp is not valid", function () { it("should send error message when totp is not valid", function () {
totpValidator.validate.returns(BluebirdPromise.reject(new exceptions.InvalidTOTPError("Bad TOTP token"))); totpValidator.validate.returns(BluebirdPromise.reject(new exceptions.InvalidTOTPError("Bad TOTP token")));
SignPost.default(req as any, res as any) return SignPost.default(req as any, res as any)
.then(function () { return BluebirdPromise.reject(new Error("It should fail")); }) .then(function () {
.catch(function () {
assert.equal(false, authSession.second_factor); assert.equal(false, authSession.second_factor);
assert.equal(401, res.status.getCall(0).args[0]); assert.equal(res.status.getCall(0).args[0], 200);
assert.deepEqual(res.send.getCall(0).args[0], {
error: "Operation failed."
});
return BluebirdPromise.resolve(); return BluebirdPromise.resolve();
}); });
}); });