gpii-universal
Version:
Cross platform, core components of the GPII personalization infrastructure.
219 lines (193 loc) • 8.02 kB
JavaScript
/*!
GPII Access Requester
Copyright 2017 OCAD University
Licensed under the New BSD license. You may not use this file except in
compliance with this License.
You may obtain a copy of the License at
https://github.com/GPII/universal/blob/master/LICENSE.txt
*/
"use strict";
var fluid = require("infusion"),
gpii = fluid.registerNamespace("gpii"),
kettle = require("kettle"),
nock = require("nock");
require("accessRequester");
fluid.require("%gpii-universal/gpii/node_modules/testing/src/NockUtils.js");
kettle.loadTestingSupport();
fluid.registerNamespace("gpii.tests.accessRequester");
gpii.tests.accessRequester.hostname = "http://gpii.net";
gpii.tests.accessRequester.path = "/access_token";
gpii.tests.accessRequester.gpiiKey = "gpii-key-test";
// Set up mock ajax responses
gpii.tests.accessRequester.setUpNock = function (config) {
var cloudMock = nock(gpii.tests.accessRequester.hostname);
// log nock matches
cloudMock.log(console.log);
// mock POST requests to "/access_token"
cloudMock.post(gpii.tests.accessRequester.path, config.request)
.reply(config.status, config.response);
};
// The customized accessRequester component for tests
fluid.defaults("gpii.tests.accessRequester", {
gradeNames: ["gpii.accessRequester"],
gpiiKey: gpii.tests.accessRequester.gpiiKey,
url: gpii.tests.accessRequester.hostname + gpii.tests.accessRequester.path,
clientCredentialDataSourceGrade: "gpii.accessRequester.clientCredentialDataSource.file"
});
// The base testEnvironment grade to be inherited by all tests
fluid.defaults("gpii.tests.accessRequesterTests", {
gradeNames: ["fluid.test.testEnvironment"],
clientCredentialFilePath: null, // supplied by individual tests
testCaseHolderGrade: null, // supplied by individual tests
distributeOptions: {
clientCredentialFilePath: {
source: "{that}.options.clientCredentialFilePath",
target: "{that clientCredentialDataSource}.options.path"
},
testCaseHolderGrade: {
source: "{that}.options.testCaseHolderGrade",
target: "{that > testCaseHolder}.type"
}
},
components: {
accessRequester: {
type: "gpii.tests.accessRequester"
},
testCaseHolder: {
type: "fluid.test.testCaseHolder"
}
}
});
// 1. A successful workflow
gpii.tests.accessRequester.success = {
clientCredentialFilePath: "%gpii-universal/gpii/node_modules/accessRequester/test/data/clientCredential-correct.json",
nockConfig: {
request: {
"grant_type": "password",
"password": "dummy",
"client_id": "pilot-computer",
"client_secret": "pilot-computer-secret",
"username": gpii.tests.accessRequester.gpiiKey
},
status: 200,
response: {
access_token: "access-token-test-in-accessRequester",
expiresIn: 3600,
token_type: "Bearer"
}
}
};
fluid.defaults("gpii.tests.accessRequester.testCaseHolder.success", {
gradeNames: "fluid.test.testCaseHolder",
modules: [{
name: "The access requester module tests - a successful workflow",
expect: 1,
tests: [{
name: "An access token is received",
sequence: [{
task: "{accessRequester}.getAccessToken",
args: [gpii.tests.accessRequester.gpiiKey],
resolve: "jqUnit.assertDeepEq",
resolveArgs: ["The response is expected with an access token", gpii.tests.accessRequester.success.nockConfig.response, "{arguments}.0"]
}]
}]
}]
});
fluid.defaults("gpii.tests.accessRequesterTests.success", {
gradeNames: ["gpii.tests.accessRequesterTests", "gpii.test.testWithNock"],
clientCredentialFilePath: gpii.tests.accessRequester.success.clientCredentialFilePath,
testCaseHolderGrade: "gpii.tests.accessRequester.testCaseHolder.success",
invokers: {
setUpNock: {
funcName: "gpii.tests.accessRequester.setUpNock",
args: gpii.tests.accessRequester.success.nockConfig
}
}
});
// 2. Error occurs due to the wrong client credentials
gpii.tests.accessRequester.error_wrongClientCredential = {
clientCredentialFilePath: "%gpii-universal/gpii/node_modules/accessRequester/test/data/clientCredential-wrong.json",
nockConfig: {
url: gpii.tests.accessRequester.path,
type: "post",
request: {
"grant_type": "password",
"password": "dummy",
"client_id": "wrong-client-id",
"client_secret": "wrong-client-secret",
"username": gpii.tests.accessRequester.gpiiKey
},
status: 401,
response: {
isError: true,
message: "Unauthorized"
}
},
expected: {
statusCode: 401,
isError: true
}
};
fluid.defaults("gpii.tests.accessRequester.testCaseHolder.error.wrongClientCredential", {
gradeNames: "fluid.test.testCaseHolder",
modules: [{
name: "The access requester module tests - a failed workflow due to the wrong client credential",
expect: 1,
tests: [{
name: "The request for an access token is rejected due to the wrong client credential",
sequence: [{
task: "{accessRequester}.getAccessToken",
args: [gpii.tests.accessRequester.gpiiKey],
reject: "jqUnit.assertLeftHand",
rejectArgs: ["The error is received", gpii.tests.accessRequester.error_wrongClientCredential.expected, "{arguments}.0"]
}]
}]
}]
});
fluid.defaults("gpii.tests.accessRequesterTests.error.wrongClientCredential", {
gradeNames: ["gpii.tests.accessRequesterTests", "gpii.test.testWithNock"],
clientCredentialFilePath: gpii.tests.accessRequester.error_wrongClientCredential.clientCredentialFilePath,
testCaseHolderGrade: "gpii.tests.accessRequester.testCaseHolder.error.wrongClientCredential",
accessRequesterGrade: "gpii.tests.accessRequester.configNock",
invokers: {
setUpNock: {
funcName: "gpii.tests.accessRequester.setUpNock",
args: gpii.tests.accessRequester.error_wrongClientCredential.nockConfig
}
}
});
// 3. Error occurs due to the missing client credential file
gpii.tests.accessRequester.error_missingClientCredentialFile = {
clientCredentialFilePath: "non-existing.json",
expected: {
statusCode: 404,
isError: true
}
};
fluid.defaults("gpii.tests.accessRequester.testCaseHolder.error.missingClientCredentialFile", {
gradeNames: "fluid.test.testCaseHolder",
modules: [{
name: "The access requester module tests - a failed workflow due to the missing client credential file",
expect: 1,
tests: [{
name: "The request for an access token is rejected due to the missing client credential file",
sequence: [{
task: "{accessRequester}.getAccessToken",
args: [gpii.tests.accessRequester.gpiiKey],
reject: "jqUnit.assertLeftHand",
rejectArgs: ["The response is expected with an access token", gpii.tests.accessRequester.error_missingClientCredentialFile.expected, "{arguments}.0"]
}]
}]
}]
});
fluid.defaults("gpii.tests.accessRequesterTests.error.missingClientCredentialFile", {
gradeNames: ["gpii.tests.accessRequesterTests"],
clientCredentialFilePath: gpii.tests.accessRequester.error_missingClientCredentialFile.clientCredentialFilePath,
testCaseHolderGrade: "gpii.tests.accessRequester.testCaseHolder.error.missingClientCredentialFile"
});
// Run all tests
fluid.test.runTests([
"gpii.tests.accessRequesterTests.success",
"gpii.tests.accessRequesterTests.error.wrongClientCredential",
"gpii.tests.accessRequesterTests.error.missingClientCredentialFile"
]);