http-auth tests

Node.js package for HTTP basic and digest access authentication.

test-options

tests/test-options.js

Options module.

var opt = require('../lib/options');

Utils module.

var utils = require('../lib/utils');

Test for empty options.

exports['testEmptyOptions'] = function (test) {
	// Checking for empty options.
	test.throws(function () { 
		opt(); 
	}, Error, "Must throw an error when options are empty!");
	// Test is done.
	test.done();
};

Test for invalid authType.

exports['testInvalidAuthType'] = function (test) {
	// Checking for invalid authType error.
	test.throws(function () { 
		opt({authType : 'some other type'}); 
	}, Error, "Must throw an error when authType is invalid!");
	// Test is done.
	test.done();
};

Test for default authType.

exports['testDefaultAuthType'] = function (test) {
	// Checking for default authType.
	var options = opt({
		authRealm : "Private area with digest access authentication.",
		authList : ['aa2sdas:s3sss', 'gi33ra:makaura']			
	});
	// Default authType check.
	test.equals(options.authType, 'digest', "Default authType must be digest!")
	// Test is done.
	test.done();
};

Test for invalid algorithm.

exports['testInvalidAlgo'] = function (test) {
	// Checking for invalid algorithm error.
	test.throws(function () { 
		opt({
			authRealm : "Private area with digest access authentication.",
			authList : ['asdasw:ssqss', 'gidra:maakura'],
			algorithm : 'monkeyCrypt'			
		}); 
	}, Error, "Must throw an error when algorithm is invalid!");	
	// Test is done.
	test.done();
};

Test for default algorithm.

exports['testDefaultAlgo'] = function (test) {
	// Checking for default algorithm.
	var options = opt({
		authRealm : "Private area with digest access authentication.",
		authList : ['aa2sdasa:s3s2ss', 'gi3q3ra:mwakaura']			
	});
	// Default algorithm check.
	test.equals(options.algorithm, 'MD5', "Default algorithm must be MD5!")
	// Test is done.
	test.done();
};

Test for invalid realm.

exports['testInvalidRealm'] = function (test) {
	// Checking for invalid realm error.
	test.throws(function () { 
		opt({
			authRealm : "",
			authList : ['asdasw:ssqss', 'gidra:maakura']	
		}); 
	}, Error, "Must throw an error when authRealm is empty!");	
	// Test is done.
	test.done();
};

Test for empty authList.

exports['testInvalidAuthList'] = function (test) {
	// Checking for invalid authList error.
	test.throws(function () { 
		opt({
			authRealm : "Some realm",
			authList : []			
		}); 
	}, Error, "Must throw an error when authList is empty!");	
	// Test is done.
	test.done();
};

Test for valid authList - digest.

exports['testValidAuthListDigest'] = function (test) {
	// Checking for valid authList.
	var options = opt({
		authRealm : "Some realm",
		authList : ['karo:seed', 'samvel:beed']			
	});
	// Checking not empty.
	test.notEqual(options.authUsers, null, "authUsers must not be empty!");		
	// Checking for items.
	test.equals(options.authUsers['karo'], 'karo:Some realm:seed', "User item is wrong!");
	test.equals(options.authUsers['samvel'], 'samvel:Some realm:beed', "User item is wrong!");
	// Test is done.
	test.done();
};

Test for valid authList - basic.

exports['testValidAuthListBasic'] = function (test) {
	// Checking for valid authList.
	var options = opt({
		authRealm : "Some realm",
		authList : ['karo:seed', 'samvel:beed'],
		authType : 'basic'
	});
	// Checking not empty.
	test.notEqual(options.authUsers, null, "authUsers must not be empty!");		
	// Checking for items.
	test.equals(options.authUsers[0], utils.base64('karo:seed'), "User item is wrong!");
	test.equals(options.authUsers[1], utils.base64('samvel:beed'), "User item is wrong!");
	// Test is done.
	test.done();
};

Test for valid authFile.

exports['testValidAuthFile'] = function (test) {
	// Checking for valid authFile.
	var options = opt({
		authRealm : "Some realm",
		authList : ['karo:seed', 'samvel:beed'],
		authFile : __dirname + '/../examples/users.htpasswd',
		authType : 'basic'
	});
	// Checking not empty.
	test.notEqual(options.authUsers, null, "authUsers must not be empty!");		
	// Checking for items.
	test.equals(options.authUsers[0], utils.base64('Sarah:testpass'), "User item is wrong!");
	test.equals(options.authUsers[1], utils.base64('John:itismypass'), "User item is wrong!");
	test.equals(options.authUsers[2], utils.base64('Shanon:noneof'), "User item is wrong!");
	test.equals(options.authUsers[3], utils.base64('Mike:pass123'), "User item is wrong!");
	// Test is done.
	test.done();
};

test-http-auth

tests/test-http-auth.js

HTTP authentication module.

var auth = require('../lib/http-auth');

Test for basic access authentication.

exports['testBasicAuth'] = function (test) {
	// Requests basic access authentication instance.
	var basic = auth({
		authRealm : "Private area with basic access authentication.",
		authList : ['Shi:many222', 'Lota:123456'],
		authType : 'basic'
	});
	// Checking instance itself.
	test.notEqual(basic, null, "Basic access authentication instance is empty!");
	// Checking apply method.
	test.notEqual(basic.apply, null, "Basic access authentication instance has no apply method!");
	// Test is done.
	test.done();
};

Test for digest access authentication.

exports['testDigestAuth'] = function (test) {
	// Requests digest access authentication instance.
	var digest = auth({
		authRealm : "Private area with digest access authentication.",
		authList : ['2Shi:ma22y222', '3Lota:1123456'],
		authType : 'digest'
	});
	// Checking instance itself.
	test.notEqual(digest, null, "Digest access authentication instance is empty!");
	// Checking apply method.
	test.notEqual(digest.apply, null, "Digest access authentication instance has no apply method!");
	// Test is done.
	test.done();
};

test-provider

tests/test-provider.js

Provider module.

var provider = require('../lib/provider');

Test for valid basic access authentication.

exports['testValidBasicAuth'] = function (test) {
	// Requests basic access authentication instance.
	var basic = provider.newInstance({
		authRealm : "Private area with basic access authentication.",
		authList : ['Kuka:pi2', 'suma:kramoke'],
		authType : 'basic'
	});
	// Checking instance itself.
	test.notEqual(basic, null, "Basic access authentication instance is empty!");
	// Checking apply method.
	test.notEqual(basic.apply, null, "Basic access authentication instance has no apply method!");
	// Test is done.
	test.done();
};

Test for valid digest access authentication.

exports['testValidDigestAuth'] = function (test) {
	// Requests digest access authentication instance.
	var digest = provider.newInstance({
		authRealm : "Private area with digest access authentication.",
		authList : ['asdas:ssss', 'gira:makura'],
		authType : 'digest'
	});
	// Checking instance itself.
	test.notEqual(digest, null, "Digest access authentication instance is empty!");
	// Checking apply method.
	test.notEqual(digest.apply, null, "Digest access authentication instance has no apply method!");
	// Test is done.
	test.done();
};

Test for empty options.

exports['testInvalidAuthOptions'] = function (test) {
	// Checking for null options.
	test.throws(function() {
		provider.newInstance();
	}, Error, "Must throw an error when no options are provided!");
	// Test is done.
	test.done();
};

Test for invalid authType.

exports['testInvalidAuthType'] = function (test) {
	// Checking for wrong authType.
	test.throws(function () { 
		provider.newInstance({authType : 'some type'}); 
	}, Error, "Must throw an error when authType is wrong!");
	// Test is done.
	test.done();
};

test-utils

tests/test-utils.js

Utility module.

var utils = require('../lib/utils');

Base64 test with ASCII.

exports['testBase64ASCII'] = function (test) {
	// Checking encoded string.
	test.equal(utils.base64("some text"), "c29tZSB0ZXh0", "ASCII string is not encoded correctly!");
	// Test is done.
	test.done();
};

Base64 test with unicode.

exports['testBase64Unicode'] = function (test) {
	// Checking encoded string.
	test.equal(utils.base64("այսպես"), "aHVtdW91brVpdW9", "Unicode string is not encoded correctly!");
	// Test is done.
	test.done();
};

MD5 test with ASCII.

exports['testMD5ASCII'] = function (test) {
	// Checking generated hash.
	test.equal(utils.base64("other text"), "b3RoZXIgdGV4dA==", "MD5 hash is not correct for ASCII string!");
	// Test is done.
	test.done();
};

MD5 test with unicode.

exports['testMD5Unicode'] = function (test) {
	// Checking generated hash.
	test.equal(utils.base64("այնպես"), "aHVtdW21brVpdW9", "MD5 hash is not correct for unicode string!");
	// Test is done.
	test.done();
};