All files / universal/gpii/node_modules/gpii-oauth2/gpii-oauth2-utilities/test/js OAuth2UtilitiesTests.js

100% Statements 71/71
100% Branches 0/0
100% Functions 22/22
100% Lines 70/70

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194                                    2x       2x 2x 2x   2x 14x             2x             2x                             2x 8x 8x 8x 8x 8x       2x   2x   2x 2x   2x       2x 2x 2x 8x       2x 2x 2x 6x       2x 2x 2x     2x 2x 2x 2x 2x 2x 2x   2x     2x 2x 2x 2x 2x 2x 2x 2x 2x   2x     2x 2x 2x 2x 2x 2x 2x   2x     2x 2x   2x                           2x 6x 6x   6x       2x 2x   2x                                                   2x 12x 12x   12x          
/*!
Copyright 2014-2017 OCAD university
 
Licensed under the New BSD license. You may not use this file except in
compliance with this License.
 
The research leading to these results has received funding from the European Union's
Seventh Framework Programme (FP7/2007-2013) under grant agreement no. 289016.
 
You may obtain a copy of the License at
https://github.com/GPII/universal/blob/master/LICENSE.txt
*/
 
/* eslint-env browser */
/* eslint strict: ["error", "function"] */
 
/* global jqUnit, fluid */
 
(function () {
 
    "use strict";
 
    var gpii = fluid.registerNamespace("gpii");
    fluid.registerNamespace("gpii.tests.oauth2");
    fluid.registerNamespace("gpii.tests.oauth2.testdata");
 
    gpii.tests.oauth2.makeRequestWithAuthorizationHeader = function (authHeaderValue) {
        return {
            headers: {
                authorization: authHeaderValue
            }
        };
    };
 
    gpii.tests.oauth2.testdata.badlyFormedAuthHeaderRequests = [
        gpii.tests.oauth2.makeRequestWithAuthorizationHeader(""),
        gpii.tests.oauth2.makeRequestWithAuthorizationHeader("Bearer"),
        gpii.tests.oauth2.makeRequestWithAuthorizationHeader("BearerAAA"),
        gpii.tests.oauth2.makeRequestWithAuthorizationHeader("Bearer AAA BBB")
    ];
 
    gpii.tests.oauth2.testdata.goodAuthHeaderRequests = [
        {
            req: gpii.tests.oauth2.makeRequestWithAuthorizationHeader("Bearer AAA"),
            expected: "AAA"
        },
        {
            req: gpii.tests.oauth2.makeRequestWithAuthorizationHeader("bearer BBB"),
            expected: "BBB"
        },
        {
            req: gpii.tests.oauth2.makeRequestWithAuthorizationHeader("Bearer  CCC"),
            expected: "CCC"
        }
    ];
 
    gpii.tests.oauth2.makeTestMiddleware = function (called, key) {
        return function (req, res, next) {
            jqUnit.assertEquals("request", "request", req);
            jqUnit.assertEquals("response", "response", res);
            called[key] = true;
            next();
        };
    };
 
    gpii.tests.oauth2.runOAuth2UtilitiesTests = function () {
 
        jqUnit.module("GPII OAuth2 Utilities");
 
        jqUnit.test("parseBearerAuthorizationHeader() returns undefined if no Authorization header", function () {
            jqUnit.assertUndefined("undefined for empty req",
                gpii.oauth2.parseBearerAuthorizationHeader({}));
            jqUnit.assertUndefined("undefined for empty headers",
                gpii.oauth2.parseBearerAuthorizationHeader({ headers: {} }));
        });
 
        jqUnit.test("parseBearerAuthorizationHeader() returns undefined for badly formed Authorization header", function () {
            jqUnit.expect(4);
            gpii.tests.oauth2.testdata.badlyFormedAuthHeaderRequests.forEach(function (req) {
                jqUnit.assertUndefined("expect undefined", gpii.oauth2.parseBearerAuthorizationHeader(req));
            });
        });
 
        jqUnit.test("parseBearerAuthorizationHeader() returns token from Authorization header", function () {
            jqUnit.expect(3);
            gpii.tests.oauth2.testdata.goodAuthHeaderRequests.forEach(function (pair) {
                jqUnit.assertEquals(pair.expected, pair.expected, gpii.oauth2.parseBearerAuthorizationHeader(pair.req));
            });
        });
 
        jqUnit.asyncTest("Walk an empty array of middleware", function () {
            jqUnit.expect(0);
            gpii.oauth2.walkMiddleware([], 0, "request", "response", function () { jqUnit.start(); });
        });
 
        jqUnit.asyncTest("Walk an array of 1 middleware", function () {
            jqUnit.expect(3);
            var called = {};
            var middleware1 = gpii.tests.oauth2.makeTestMiddleware(called, "middleware1");
            var check = function () {
                jqUnit.assertTrue("middleware1 called", called.middleware1);
                jqUnit.start();
            };
            gpii.oauth2.walkMiddleware([middleware1], 0, "request", "response", check);
        });
 
        jqUnit.asyncTest("Walk an array of 2 middleware", function () {
            jqUnit.expect(6);
            var called = {};
            var middleware1 = gpii.tests.oauth2.makeTestMiddleware(called, "middleware1");
            var middleware2 = gpii.tests.oauth2.makeTestMiddleware(called, "middleware2");
            var check = function () {
                jqUnit.assertTrue("middleware1 called", called.middleware1);
                jqUnit.assertTrue("middleware2 called", called.middleware2);
                jqUnit.start();
            };
            gpii.oauth2.walkMiddleware([middleware1, middleware2], 0, "request", "response", check);
        });
 
        jqUnit.asyncTest("Walk a single middleware function", function () {
            jqUnit.expect(3);
            var called = {};
            var middleware1 = gpii.tests.oauth2.makeTestMiddleware(called, "middleware1");
            var check = function () {
                jqUnit.assertTrue("middleware1 called", called.middleware1);
                jqUnit.start();
            };
            gpii.oauth2.walkMiddleware(middleware1, 0, "request", "response", check);
        });
 
        jqUnit.test("Test gpii.oauth2.getTimestampExpires()", function () {
            jqUnit.expect(3);
 
            var testCases = [{
                timestampStarts: new Date("2017-09-22"),
                expiresIn: 60,
                expected: "2017-09-22T00:01:00.000Z"
            }, {
                timestampStarts: null,
                expiresIn: 60,
                expected: undefined
            }, {
                timestampStarts: undefined,
                expiresIn: 60,
                expected: undefined
            }];
 
            fluid.each(testCases, function (testCase) {
                var result = gpii.oauth2.getTimestampExpires(testCase.timestampStarts, testCase.expiresIn);
                var msg = "The calculated timestampExpires for the start timestamp \"" + testCase.timestampExpires + "\" expiring in " + testCase.expiresIn + " seconds is: " + testCase.expected;
 
                jqUnit.assertEquals(msg, testCase.expected, result);
            });
        });
 
        jqUnit.test("Test gpii.oauth2.getExpiresIn()", function () {
            jqUnit.expect(6);
 
            var testCases = [{
                timestampStarts: new Date("2017-09-22"),
                timestampExpires: "2017-05-29T17:54:00.000Z",
                expected: 0
            }, {
                timestampStarts: null,
                timestampExpires: new Date("2017-09-22"),
                expected: undefined
            }, {
                timestampStarts: new Date("2017-09-22"),
                timestampExpires: null,
                expected: undefined
            }, {
                timestampStarts: undefined,
                timestampExpires: new Date("2017-09-22"),
                expected: undefined
            }, {
                timestampStarts: new Date("2017-09-22"),
                timestampExpires: undefined,
                expected: undefined
            }, {
                timestampStarts: new Date("2017-09-22"),
                timestampExpires: "2017-09-22T00:01:00.000Z",
                expected: 60
            }];
 
            fluid.each(testCases, function (testCase) {
                var result = gpii.oauth2.getExpiresIn(testCase.timestampStarts, testCase.timestampExpires);
                var msg = "The timestamp \"" + testCase.timestampExpires + "\" will expire in " + testCase.expected + " seconds";
 
                jqUnit.assertEquals(msg, testCase.expected, result);
            });
        });
    };
})();