Skip to content

Commit

Permalink
Merge pull request #5 from TryxAPI/feature-basic-authentication
Browse files Browse the repository at this point in the history
Add basic authentication to tests.
  • Loading branch information
toaster33 committed May 6, 2015
2 parents f032a12 + 0470ad3 commit 5204260
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 226 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ LRS Conformance Tests

### Description

This is a NodeJS project that tests the 'MUST' requirements of the [xAPI Spec](https://github.com/adlnet/xAPI-Spec) and is based on the ADL [testing requirements](https://github.com/adlnet/xAPI_LRS_Test/blob/master/TestingRequirements.md) repository. This is actively being developed and new tests will be periodically added based on the testing requirements. Currently, this test suite does not support authenticated LRS endpoints. This test suite should also not run against a production LRS endpoint because the data is persisted and never voided.
This is a NodeJS project that tests the 'MUST' requirements of the [xAPI Spec](https://github.com/adlnet/xAPI-Spec) and is based on the ADL [testing requirements](https://github.com/adlnet/xAPI_LRS_Test/blob/master/TestingRequirements.md) repository. This is actively being developed and new tests will be periodically added based on the testing requirements. Currently, this test suite only supports basic authentication. This test suite should also not run against a production LRS endpoint because the data is persisted and never voided.

### Installation

Expand All @@ -26,15 +26,20 @@ Options:

-h, --help output usage information
-V, --version output the version number
-e, --endpoint <path> The LRS connection string
-e, --endpoint <path> the LRS connection string
-a, --basicAuth <true / false> enables basic authentication
-u, --authUser <username> sets user name (required when basic authentication enabled)
-p, --authPass <password> sets password (required when basic authentication enabled)
```

### Running Test Suite

Example:

```bash
$ lrs-test --endpoint http://localhost/lrs
```
bash $ lrs-test --endpoint http://localhost/lrs --basicAuth true --authUser username --authPass password
bash $ lrs-test -e http://localhost/lrs -a true -u username -p password
```

### Creating/Extending Test Suite
Expand Down
18 changes: 15 additions & 3 deletions bin/lrs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@
.version(packageJson.version)
.usage('[options]')
.option('-d, --directory [path]', 'test directory, default: test/v1_0_2')
.option('-e, --endpoint <path>', 'LRS connection string')
.option('-e, --endpoint <path>', 'the connection string')
.option('-a, --basicAuth <true/false>', 'enables basic authentication')
.option('-u, --authUser <username>', 'sets user name (required when basic authentication enabled)')
.option('-p, --authPass <password>', 'sets password (required when basic authentication enabled)')
.parse(process.argv);

var deferred = Q.defer(),
optionsValidator = Joi.object({
directory: Joi.string(),
/* See [RFC-3986](http://tools.ietf.org/html/rfc3986#page-17) */
endpoint: Joi.string().regex(/^[a-zA-Z][a-zA-Z0-9+\.-]*:.+/, 'URI').required()
endpoint: Joi.string().regex(/^[a-zA-Z][a-zA-Z0-9+\.-]*:.+/, 'URI').required(),
basicAuth: Joi.any(true, false),
authUser: Joi.string().when('basicAuth', { is: 'true', then: Joi.required() }),
authPass: Joi.string().when('basicAuth', { is: 'true', then: Joi.required() })
}).unknown(false),
mocha = new Mocha({
uii: 'bdd',
Expand All @@ -28,14 +34,20 @@
process.nextTick(function () {
var options = {
directory: program.directory || 'test/v1_0_2',
endpoint: program.endpoint
endpoint: program.endpoint,
basicAuth: program.basicAuth,
authUser: program.authUser,
authPass: program.authPass
},
validOptions = Joi.validate(options, optionsValidator);

if (validOptions.error) {
deferred.reject(validOptions.error);
} else {
process.env.LRS_ENDPOINT = options.endpoint;
process.env.BASIC_AUTH_ENABLED = options.basicAuth;
process.env.BASIC_AUTH_USER = options.authUser;
process.env.BASIC_AUTH_PASSWORD = options.authPass;
var testDirectory = options.directory;
fs.readdirSync(testDirectory).filter(function (file) {
return file.substr(-3) === '.js';
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@
},
"homepage": "https://github.com/TryxAPI/lrs-conformance-tests",
"dependencies": {
"chai": "^1.9.2",
"chai": "^2.3.0",
"chai-as-promised": "^4.1.1",
"commander": "^2.6.0",
"crypto": "0.0.3",
"dirty-chai": "^1.0.0",
"exit": "^0.1.2",
"extend": "^2.0.0",
"form-urlencoded": "0.0.7",
"joi": "^5.1.0",
"joi": "^6.4.1",
"lodash-node": "^3.2.0",
"mocha": "^1.20.1",
"moment": "^2.8.4",
Expand Down
5 changes: 4 additions & 1 deletion test/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
LRS_ENDPOINT=http://asdf.elmnts-test.com:8001/lrs
XAPI_VERSION=1.0.1
XAPI_VERSION=1.0.1
BASIC_AUTH_ENABLED=false
BASIC_AUTH_USER=username
BASIC_AUTH_PASSWORD=password
24 changes: 23 additions & 1 deletion test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,37 @@ if (!process.env.EB_NODE_COMMAND) {
var XAPI_VERSION = process.env.XAPI_VERSION;

module.exports = {
/**
* Adds all headers.
* @returns {Object}
*/
addAllHeaders: function (header) {
var newHeader = extend(true, {}, header);
newHeader = module.exports.addHeaderXapiVersion(newHeader);
newHeader = module.exports.addBasicAuthenicationHeader(newHeader);
return newHeader;
},
/**
* Adds xAPI header version.
* @returns {String}
* @returns {Object}
*/
addHeaderXapiVersion: function (header) {
var newHeader = extend(true, {}, header);
newHeader['X-Experience-API-Version'] = module.exports.getXapiVersion();
return newHeader;
},
/**
* Adds basic authentication.
* @returns {Object}
*/
addBasicAuthenicationHeader: function (header) {
var newHeader = extend(true, {}, header);
if (process.env.BASIC_AUTH_ENABLED === 'true') {
var userPass = new Buffer(process.env.BASIC_AUTH_USER + ':' + process.env.BASIC_AUTH_PASSWORD).toString('base64');
newHeader['Authorization'] = 'Basic ' + userPass;
}
return newHeader;
},
/**
* Convert template mapping to JSON objects. Assumes there is one key / value and templates
* are denoted by wrapping the folder.filename (without extension) with double curly braces.
Expand Down
19 changes: 7 additions & 12 deletions test/v1_0_2/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
(function (process, request, should, helper, qs, validUrl) {
'use strict';

var LRS_ENDPOINT = process.env.LRS_ENDPOINT || 'http://asdf.elmnts-test.com:8001/lrs';
var request = request(LRS_ENDPOINT);
var request = request(helper.getEndpoint());

/**
* Sends an HTTP request using supertest
Expand All @@ -22,11 +21,14 @@
*/
function sendRequest(type, url, params, body, expect) {
var reqUrl = params ? (url + '?' + qs.stringify(params)) : url;
var pre = request[type](reqUrl)
.set('X-Experience-API-Version', '1.0.1');

var headers = helper.addAllHeaders({});
var pre = request[type](reqUrl);
if (body) {
pre.send(body);
}
pre.set('X-Experience-API-Version', headers['X-Experience-API-Version'])
.set('Authorization', headers['Authorization']);
return pre.expect(expect);
}

Expand Down Expand Up @@ -55,14 +57,7 @@
var parameters = {
objectType: 'Person'
};

request
.get('/agents?' + qs.stringify(parameters))
.set('X-Experience-API-Version', '1.0.1')
.send({})
.expect(200, function (err, res) {
done();
});
return sendRequest('get', '/agents', parameters, undefined, 200);
});

it('An LRS has an Agent Profile API with endpoint "base IRI"+"/agents/profile" (7.3.table1.row3.a, 7.3.table1.row3.c)', function () {
Expand Down
Loading

0 comments on commit 5204260

Please sign in to comment.