all files / modules/middleware/__tests__/ basicAuth-test.js

100% Statements 25/25
100% Branches 2/2
100% Functions 15/15
100% Lines 25/25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47                                            
const expect = require("expect");
const callApp = require("../../utils/callApp");
const basicAuth = require("../basicAuth");
 
function ok() {
    return 200;
}
 
function validateCredentials(username, password) {
    return username === "michael" && password === "password";
}
 
describe("middleware/basicAuth", function () {
    let app;
    beforeEach(function () {
        app = basicAuth(ok, validateCredentials);
    });
 
    describe("when no authentication credentials are given", function () {
        it("returns 401 Unauthorized", function () {
            return callApp(app).then(function (conn) {
                expect(conn.status).toEqual(401);
            });
        });
    });
 
    describe("when invalid authentication credentials are given", function () {
        it("returns 401 Unauthorized", function () {
            return callApp(app, "/", function (conn) {
                conn.auth = "michael:wrongPassword";
            }).then(function (conn) {
                expect(conn.status).toEqual(401);
            });
        });
    });
 
    describe("when valid credentials are given", function () {
        it("returns 200 OK", function () {
            return callApp(app, "/", function (conn) {
                conn.auth = "michael:password";
            }).then(function (conn) {
                expect(conn.status).toEqual(200);
            });
        });
    });
});