All files / universal/gpii/node_modules/gpii-oauth2/gpii-oauth2-authz-server/test/js AuthorizationServiceTests.js

100% Statements 10/10
100% Branches 0/0
100% Functions 2/2
100% Lines 10/10

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                                  2x   2x     2x             2x 2x       2x                                                       2x                                   2x                                     2x                                   2x                                                                                                                                            
/*!
Copyright 2015-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
*/
 
/* global fluid */
 
"use strict";
 
(function () {
 
    var gpii = fluid.registerNamespace("gpii");
 
    // The mock codeGenerator for testing
    fluid.defaults("gpii.tests.oauth2.mockCodeGenerator", {
        gradeNames: ["fluid.component"],
        invokers: {
            generateAccessToken: "gpii.tests.oauth2.mockCodeGenerator.generateAccessToken"
        }
    });
 
    gpii.tests.oauth2.mockCodeGenerator.generateAccessToken = function () {
        return "test-access-token";
    };
 
    // The base test enviornment without any pouch data being imported
    fluid.defaults("gpii.tests.oauth2.authorizationService.testEnvironment", {
        gradeNames: ["gpii.tests.oauth2.pouchBackedTestEnvironment"],
        dbViewsLocation: "../../../gpii-oauth2-datastore/dbViews/views.json",
        dbName: "auth",
        components: {
            authorizationService: {
                type: "gpii.oauth2.authorizationService",
                createOnEvent: "onFixturesConstructed",
                options: {
                    gradeNames: ["gpii.tests.oauth2.dbDataStore.base"],
                    dbViews: "{arguments}.0",
                    components: {
                        dataStore: {
                            type: "gpii.oauth2.dbDataStore"
                        },
                        codeGenerator: {
                            type: "gpii.tests.oauth2.mockCodeGenerator"
                        }
                    }
                }
            },
            caseHolder: {
                type: "gpii.tests.inBrowserPouchDB.baseTestCaseHolder"
            }
        }
    });
 
    // All expected results
    gpii.tests.oauth2.authorizationService.expected = {
        success: {
            accessToken: "test-access-token",
            expiresIn: 3600
        },
        unauthorized: {
            message: "Unauthorized",
            statusCode: 401,
            isError: true
        },
        missingInput: {
            message: "The input field \"GPII token or client ID\" is undefined",
            statusCode: 400,
            isError: true
        }
    };
 
    // Tests with an empty data store
    fluid.defaults("gpii.tests.oauth2.authorizationService.emptyDataStore", {
        gradeNames: ["gpii.tests.oauth2.authorizationService.testEnvironment"],
        rawModules: [{
            name: "Test grantGpiiAppInstallationAuthorization()",
            tests: [{
                name: "grantGpiiAppInstallationAuthorization() returns an error with an empty dataStore",
                sequence: [{
                    func: "gpii.tests.oauth2.invokePromiseProducer",
                    args: ["{authorizationService}.grantGpiiAppInstallationAuthorization", ["alice_gpii_token", "client-1"], "{that}"]
                }, {
                    listener: "jqUnit.assertDeepEq",
                    args: ["Unauthorized error should be received with an empty data store", gpii.tests.oauth2.authorizationService.expected.unauthorized, "{arguments}.0"],
                    event: "{that}.events.onError"
                }]
            }]
        }]
    });
 
    // Tests with a data store having test data
    gpii.tests.oauth2.authorizationService.testData = [{
        "_id": "gpiiToken-1",
        "type": "gpiiToken",
        "gpiiToken": "alice_gpii_token"
    }, {
        "_id": "client-1",
        "type": "gpiiAppInstallationClient",
        "name": "AJC1",
        "oauth2ClientId": "client_id_AJC1",
        "oauth2ClientSecret": "client_secret_AJC1"
    }, {
        "_id": "client-2",
        "type": "unknownClient",
        "name": "test",
        "oauth2ClientId": "client_id_test",
        "oauth2ClientSecret": "client_secret_test"
    }];
 
    fluid.defaults("gpii.tests.oauth2.authorizationService.withData.grantGpiiAppInstallationAuthorization", {
        gradeNames: ["gpii.tests.oauth2.authorizationService.testEnvironment"],
        pouchData: gpii.tests.oauth2.authorizationService.testData,
        rawModules: [{
            name: "Test grantGpiiAppInstallationAuthorization()",
            tests: [{
                name: "grantGpiiAppInstallationAuthorization() returns an access token",
                sequence: [{
                    func: "gpii.tests.oauth2.invokePromiseProducer",
                    args: ["{authorizationService}.grantGpiiAppInstallationAuthorization", ["alice_gpii_token", "client-1"], "{that}"]
                }, {
                    listener: "jqUnit.assertDeepEq",
                    args: ["The access token should be received in an expected format", gpii.tests.oauth2.authorizationService.expected.success, "{arguments}.0"],
                    event: "{that}.events.onResponse"
                }]
            }, {
                name: "grantGpiiAppInstallationAuthorization() returns error when a gpii token is not provided in the argument list",
                sequence: [{
                    func: "gpii.tests.oauth2.invokePromiseProducer",
                    args: ["{authorizationService}.grantGpiiAppInstallationAuthorization", [undefined, "client-1"], "{that}"]
                }, {
                    listener: "jqUnit.assertDeepEq",
                    args: ["The error is returned when a gpii token is missing", gpii.tests.oauth2.authorizationService.expected.missingInput, "{arguments}.0"],
                    event: "{that}.events.onError"
                }]
            }, {
                name: "grantGpiiAppInstallationAuthorization() returns error when a client id is not provided in the argument list",
                sequence: [{
                    func: "gpii.tests.oauth2.invokePromiseProducer",
                    args: ["{authorizationService}.grantGpiiAppInstallationAuthorization", ["alice_gpii_token", undefined], "{that}"]
                }, {
                    listener: "jqUnit.assertDeepEq",
                    args: ["The error is returned when a client id is missing", gpii.tests.oauth2.authorizationService.expected.missingInput, "{arguments}.0"],
                    event: "{that}.events.onError"
                }]
            }, {
                name: "grantGpiiAppInstallationAuthorization() returns error when the gpii token record is not found in the database",
                sequence: [{
                    func: "gpii.tests.oauth2.invokePromiseProducer",
                    args: ["{authorizationService}.grantGpiiAppInstallationAuthorization", ["non-existent-gpii-token", "client-1"], "{that}"]
                }, {
                    listener: "jqUnit.assertDeepEq",
                    args: ["The error is returned when the gpii token record is not found in the database", gpii.tests.oauth2.authorizationService.expected.unauthorized, "{arguments}.0"],
                    event: "{that}.events.onError"
                }]
            }, {
                name: "grantGpiiAppInstallationAuthorization() returns error when the client record is not found in the database",
                sequence: [{
                    func: "gpii.tests.oauth2.invokePromiseProducer",
                    args: ["{authorizationService}.grantGpiiAppInstallationAuthorization", ["alice_gpii_token", "non-existent-client-id"], "{that}"]
                }, {
                    listener: "jqUnit.assertDeepEq",
                    args: ["The error is returned when the client record is not found in the database", gpii.tests.oauth2.authorizationService.expected.unauthorized, "{arguments}.0"],
                    event: "{that}.events.onError"
                }]
            }, {
                name: "grantGpiiAppInstallationAuthorization() returns error when the client type is not \"gpiiAppInstallationClient\"",
                sequence: [{
                    func: "gpii.tests.oauth2.invokePromiseProducer",
                    args: ["{authorizationService}.grantGpiiAppInstallationAuthorization", ["alice_gpii_token", "client-2"], "{that}"]
                }, {
                    listener: "jqUnit.assertDeepEq",
                    args: ["The error is returned when the client type is not \"gpiiAppInstallationClient\"", gpii.tests.oauth2.authorizationService.expected.unauthorized, "{arguments}.0"],
                    event: "{that}.events.onError"
                }]
            }]
        }]
    });
 
})();