unit tests using describe+it plus jshint fixes
This commit is contained in:
parent
b6f71eb180
commit
546100a950
@ -1,11 +1,9 @@
|
||||
require('../helper');
|
||||
|
||||
var _ = require('underscore')
|
||||
, ApikeyAuth = require('../../app/auth/apikey')
|
||||
, assert = require('assert')
|
||||
;
|
||||
var ApikeyAuth = require('../../app/auth/apikey');
|
||||
var assert = require('assert');
|
||||
|
||||
suite('has credentials', function() {
|
||||
describe('has credentials', function() {
|
||||
|
||||
var noCredentialsRequests = [
|
||||
{
|
||||
@ -35,8 +33,8 @@ suite('has credentials', function() {
|
||||
];
|
||||
|
||||
noCredentialsRequests.forEach(function(request) {
|
||||
test('has no credentials if ' + request.des, function() {
|
||||
testCredentials(request.req, false)
|
||||
it('has no credentials if ' + request.des, function() {
|
||||
testCredentials(request.req, false);
|
||||
});
|
||||
});
|
||||
|
||||
@ -60,8 +58,8 @@ suite('has credentials', function() {
|
||||
];
|
||||
|
||||
credentialsRequests.forEach(function(request) {
|
||||
test('has credentials if ' + request.des, function() {
|
||||
testCredentials(request.req, true)
|
||||
it('has credentials if ' + request.des, function() {
|
||||
testCredentials(request.req, true);
|
||||
});
|
||||
});
|
||||
|
||||
@ -72,13 +70,13 @@ suite('has credentials', function() {
|
||||
|
||||
});
|
||||
|
||||
suite('verify credentials', function() {
|
||||
describe('verifyCredentials', function() {
|
||||
|
||||
test('verifyCredentials callbacks with true value when request api_key is the same', function(done) {
|
||||
it('callbacks with true value when request api_key is the same', function(done) {
|
||||
testVerifyCredentials({query:{api_key: 'foo'}}, {apiKey: 'foo'}, true, done);
|
||||
});
|
||||
|
||||
test('verifyCredentials callbacks with true value when request api_key is different', function(done) {
|
||||
it('callbacks with false value when request api_key is different', function(done) {
|
||||
testVerifyCredentials({query:{api_key: 'foo'}}, {apiKey: 'bar'}, false, done);
|
||||
});
|
||||
|
||||
|
@ -1,8 +1,7 @@
|
||||
require('../helper')
|
||||
require('../helper');
|
||||
|
||||
var assert = require('assert'),
|
||||
_ = require('underscore'),
|
||||
HealthCheck = require('../../app/monitoring/health_check');
|
||||
var assert = require('assert');
|
||||
var HealthCheck = require('../../app/monitoring/health_check');
|
||||
|
||||
var metadataBackend = {};
|
||||
|
||||
@ -12,34 +11,34 @@ function PSQL(dbParams) {
|
||||
|
||||
var healthCheck = new HealthCheck(metadataBackend, PSQL);
|
||||
|
||||
suite('health checks', function() {
|
||||
describe('health checks', function() {
|
||||
|
||||
test('error if disabled file exists', function(done) {
|
||||
it('errors if disabled file exists', function(done) {
|
||||
var fs = require('fs');
|
||||
|
||||
var readFileFn = fs.readFile;
|
||||
fs.readFile = function(filename, callback) {
|
||||
callback(null, "Maintenance");
|
||||
}
|
||||
healthCheck.check('fake', 'select 1', function(err, result) {
|
||||
};
|
||||
healthCheck.check('fake', 'select 1', function(err/*, result*/) {
|
||||
assert.equal(err.message, "Maintenance");
|
||||
assert.equal(err.http_status, 503);
|
||||
done();
|
||||
fs.readFile = readFileFn;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
test('not err if disabled file does not exists', function(done) {
|
||||
it('does not err if disabled file does not exists', function(done) {
|
||||
var fs = require('fs');
|
||||
|
||||
var readFileFn = fs.readFile;
|
||||
fs.readFile = function(filename, callback) {
|
||||
callback(new Error("ENOENT"), null);
|
||||
}
|
||||
healthCheck.check('fake', 'select 1', function(err, result) {
|
||||
};
|
||||
healthCheck.check('fake', 'select 1', function(err/*, result*/) {
|
||||
assert.equal(err, null);
|
||||
done();
|
||||
fs.readFile = readFileFn;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -1,30 +1,30 @@
|
||||
require('../../helper');
|
||||
var assert = require('assert');
|
||||
|
||||
var ArrayBufferSer = require('../../../app/models/bin_encoder')
|
||||
var ArrayBufferSer = require('../../../app/models/bin_encoder');
|
||||
|
||||
suite('ArrayBufferSer', function() {
|
||||
describe('ArrayBufferSer', function() {
|
||||
|
||||
test('calculate size for basic types', function() {
|
||||
var b = new ArrayBufferSer(ArrayBufferSer.INT16, [1,2,3,4])
|
||||
it('calculate size for basic types', function() {
|
||||
var b = new ArrayBufferSer(ArrayBufferSer.INT16, [1,2,3,4]);
|
||||
assert.equal(4*2, b.getDataSize());
|
||||
|
||||
b = new ArrayBufferSer(ArrayBufferSer.INT8, [1,2,3,4])
|
||||
assert.equal(4*1, b.getDataSize());
|
||||
b = new ArrayBufferSer(ArrayBufferSer.INT8, [1,2,3,4]);
|
||||
assert.equal(4, b.getDataSize());
|
||||
|
||||
b = new ArrayBufferSer(ArrayBufferSer.INT32, [1,2,3,4])
|
||||
b = new ArrayBufferSer(ArrayBufferSer.INT32, [1,2,3,4]);
|
||||
assert.equal(4*4, b.getDataSize());
|
||||
});
|
||||
|
||||
|
||||
test('calculate size for arrays', function() {
|
||||
var b = new ArrayBufferSer(ArrayBufferSer.STRING, ["test","kease"])
|
||||
it('calculate size for arrays', function() {
|
||||
var b = new ArrayBufferSer(ArrayBufferSer.STRING, ["test","kease"]);
|
||||
assert.equal((b.headerSize + 4 + 5)*2, b.getDataSize());
|
||||
|
||||
var ba = new ArrayBufferSer(ArrayBufferSer.INT16, [1,2,3,4])
|
||||
var bc = new ArrayBufferSer(ArrayBufferSer.INT16, [1,4])
|
||||
var ba = new ArrayBufferSer(ArrayBufferSer.INT16, [1,2,3,4]);
|
||||
var bc = new ArrayBufferSer(ArrayBufferSer.INT16, [1,4]);
|
||||
|
||||
b = new ArrayBufferSer(ArrayBufferSer.BUFFER, [ba, bc])
|
||||
b = new ArrayBufferSer(ArrayBufferSer.BUFFER, [ba, bc]);
|
||||
assert.equal((b.headerSize + 4 + 2)*2, b.getDataSize());
|
||||
assert.equal(b.type, ArrayBufferSer.BUFFER);
|
||||
});
|
||||
@ -36,28 +36,28 @@ suite('ArrayBufferSer', function() {
|
||||
}
|
||||
}
|
||||
|
||||
test('binary data is ok', function() {
|
||||
var b = new ArrayBufferSer(ArrayBufferSer.INT16, [1,2,3,4])
|
||||
it('binary data is ok', function() {
|
||||
var b = new ArrayBufferSer(ArrayBufferSer.INT16, [1,2,3,4]);
|
||||
var bf = new Buffer([0, 0, 0, ArrayBufferSer.INT16, 0, 0, 0, 8, 1, 0, 2, 0, 3, 0, 4, 0]);
|
||||
assert_buffer_equals(bf, b.buffer);
|
||||
});
|
||||
|
||||
test('binary data is ok with arrays', function() {
|
||||
var ba = new ArrayBufferSer(ArrayBufferSer.INT16, [1,2, 3, 4])
|
||||
var bc = new ArrayBufferSer(ArrayBufferSer.INT16, [1,4])
|
||||
it('binary data is ok with arrays', function() {
|
||||
var ba = new ArrayBufferSer(ArrayBufferSer.INT16, [1,2, 3, 4]);
|
||||
var bc = new ArrayBufferSer(ArrayBufferSer.INT16, [1,4]);
|
||||
|
||||
var b = new ArrayBufferSer(ArrayBufferSer.BUFFER, [ba, bc])
|
||||
var b = new ArrayBufferSer(ArrayBufferSer.BUFFER, [ba, bc]);
|
||||
var bf = new Buffer([
|
||||
0, 0, 0, ArrayBufferSer.BUFFER, // type
|
||||
0, 0, 0, 28,
|
||||
0, 0, 0, ArrayBufferSer.INT16, 0, 0, 0, 8, 1, 0, 2, 0, 3, 0, 4, 0,
|
||||
0, 0, 0, ArrayBufferSer.INT16, 0, 0, 0, 4, 1, 0, 4, 0])
|
||||
0, 0, 0, ArrayBufferSer.INT16, 0, 0, 0, 4, 1, 0, 4, 0]);
|
||||
assert_buffer_equals(bf, b.buffer);
|
||||
});
|
||||
|
||||
test('binary data is ok with strings', function() {
|
||||
var s = 'test'
|
||||
var b = new ArrayBufferSer(ArrayBufferSer.STRING, [s])
|
||||
it('binary data is ok with strings', function() {
|
||||
var s = 'test';
|
||||
var b = new ArrayBufferSer(ArrayBufferSer.STRING, [s]);
|
||||
var bf = new Buffer([
|
||||
0, 0, 0, ArrayBufferSer.STRING, // type
|
||||
0, 0, 0, 16,
|
||||
|
@ -1,26 +1,39 @@
|
||||
require('../helper');
|
||||
|
||||
var _ = require('underscore')
|
||||
, OAuthAuth = require('../../app/auth/oauth')
|
||||
, MetadataDB = require('cartodb-redis')
|
||||
, oAuth = require('../../app/auth/oauth').backend
|
||||
, assert = require('assert')
|
||||
, tests = module.exports = {}
|
||||
, oauth_data_1 = {
|
||||
var _ = require('underscore');
|
||||
var OAuthAuth = require('../../app/auth/oauth');
|
||||
var MetadataDB = require('cartodb-redis');
|
||||
var oAuth = require('../../app/auth/oauth').backend;
|
||||
var assert = require('assert');
|
||||
var oauth_data_1 = {
|
||||
oauth_consumer_key: "dpf43f3p2l4k3l03",
|
||||
oauth_token: "nnch734d00sl2jdk",
|
||||
oauth_signature_method: "HMAC-SHA1",
|
||||
oauth_signature: "tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D",
|
||||
oauth_timestamp:"1191242096",
|
||||
oauth_nonce:"kllo9940pd9333jh"
|
||||
}
|
||||
, oauth_data_2 = { oauth_version:"1.0" }
|
||||
, oauth_data = _.extend(oauth_data_1, oauth_data_2)
|
||||
, real_oauth_header = 'OAuth realm="http://vizzuality.testhost.lan/",oauth_consumer_key="fZeNGv5iYayvItgDYHUbot1Ukb5rVyX6QAg8GaY2",oauth_token="l0lPbtP68ao8NfStCiA3V3neqfM03JKhToxhUQTR",oauth_signature_method="HMAC-SHA1", oauth_signature="o4hx4hWP6KtLyFwggnYB4yPK8xI%3D",oauth_timestamp="1313581372",oauth_nonce="W0zUmvyC4eVL8cBd4YwlH1nnPTbxW0QBYcWkXTwe4",oauth_version="1.0"'
|
||||
, oauth_header_tokens = 'oauth_consumer_key="dpf43f3p2l4k3l03",oauth_token="nnch734d00sl2jdk",oauth_signature_method="HMAC-SHA1", oauth_signature="tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D",oauth_timestamp="1191242096",oauth_nonce="kllo9940pd9333jh",oauth_version="1.0"'
|
||||
, full_oauth_header = 'OAuth realm="http://photos.example.net/"' + oauth_header_tokens;
|
||||
};
|
||||
var oauth_data_2 = { oauth_version:"1.0" };
|
||||
var oauth_data = _.extend(oauth_data_1, oauth_data_2);
|
||||
var real_oauth_header = 'OAuth ' +
|
||||
'realm="http://vizzuality.testhost.lan/",' +
|
||||
'oauth_consumer_key="fZeNGv5iYayvItgDYHUbot1Ukb5rVyX6QAg8GaY2",' +
|
||||
'oauth_token="l0lPbtP68ao8NfStCiA3V3neqfM03JKhToxhUQTR",' +
|
||||
'oauth_signature_method="HMAC-SHA1", ' +
|
||||
'oauth_signature="o4hx4hWP6KtLyFwggnYB4yPK8xI%3D",' +
|
||||
'oauth_timestamp="1313581372",' +
|
||||
'oauth_nonce="W0zUmvyC4eVL8cBd4YwlH1nnPTbxW0QBYcWkXTwe4",' +
|
||||
'oauth_version="1.0"';
|
||||
var oauth_header_tokens = 'oauth_consumer_key="dpf43f3p2l4k3l03",' +
|
||||
'oauth_token="nnch734d00sl2jdk",' +
|
||||
'oauth_signature_method="HMAC-SHA1", ' +
|
||||
'oauth_signature="tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D",' +
|
||||
'oauth_timestamp="1191242096",' +
|
||||
'oauth_nonce="kllo9940pd9333jh",' +
|
||||
'oauth_version="1.0"';
|
||||
var full_oauth_header = 'OAuth realm="http://photos.example.net/"' + oauth_header_tokens;
|
||||
|
||||
var metadataBackend = MetadataDB({
|
||||
var metadataBackend = new MetadataDB({
|
||||
host: global.settings.redis_host,
|
||||
port: global.settings.redis_port,
|
||||
max: global.settings.redisPool,
|
||||
@ -28,44 +41,43 @@ var metadataBackend = MetadataDB({
|
||||
reapIntervalMillis: global.settings.redisReapIntervalMillis
|
||||
});
|
||||
|
||||
suite('oauth', function() {
|
||||
describe('oauth', function() {
|
||||
|
||||
test('test database number', function(){
|
||||
it('test database number', function(){
|
||||
assert.equal(oAuth.oauth_database, 3);
|
||||
});
|
||||
|
||||
test('test oauth database key', function(){
|
||||
it('test oauth database key', function(){
|
||||
assert.equal(oAuth.oauth_user_key, "rails:oauth_access_tokens:<%= oauth_access_key %>");
|
||||
});
|
||||
|
||||
test('test parse tokens from full headers does not raise exception', function(){
|
||||
it('test parse tokens from full headers does not raise exception', function(){
|
||||
var req = {query:{}, headers:{authorization:full_oauth_header}};
|
||||
assert.doesNotThrow(function(){ oAuth.parseTokens(req) }, /incomplete oauth tokens in request/);
|
||||
assert.doesNotThrow(function(){ oAuth.parseTokens(req); }, /incomplete oauth tokens in request/);
|
||||
});
|
||||
|
||||
test('test parse all normal tokens raises no exception', function(){
|
||||
it('test parse all normal tokens raises no exception', function(){
|
||||
var req = {query:oauth_data, headers:{}};
|
||||
assert.doesNotThrow(function(){ oAuth.parseTokens(req) }, /incomplete oauth tokens in request/);
|
||||
assert.doesNotThrow(function(){ oAuth.parseTokens(req); }, /incomplete oauth tokens in request/);
|
||||
});
|
||||
|
||||
test('test headers take presedence over query parameters', function(){
|
||||
it('test headers take presedence over query parameters', function(){
|
||||
var req = {query:{oauth_signature_method: "MY_HASH"}, headers:{authorization:full_oauth_header}};
|
||||
var tokens = oAuth.parseTokens(req);
|
||||
assert.equal(tokens.oauth_signature_method, "HMAC-SHA1");
|
||||
});
|
||||
|
||||
test('test can access oauth hash for a user based on access token (oauth_token)', function(done){
|
||||
it('test can access oauth hash for a user based on access token (oauth_token)', function(done){
|
||||
var req = {query:{}, headers:{authorization:real_oauth_header}};
|
||||
var tokens = oAuth.parseTokens(req);
|
||||
|
||||
oAuth.getOAuthHash(metadataBackend, tokens.oauth_token, function(err, data){
|
||||
console.log(data);
|
||||
assert.equal(tokens.oauth_consumer_key, data.consumer_key);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
test('test non existant oauth hash for a user based on oauth_token returns empty hash', function(done){
|
||||
it('test non existant oauth hash for a user based on oauth_token returns empty hash', function(done){
|
||||
var req = {query:{}, headers:{authorization:full_oauth_header}};
|
||||
var tokens = oAuth.parseTokens(req);
|
||||
|
||||
@ -76,7 +88,7 @@ test('test non existant oauth hash for a user based on oauth_token returns empty
|
||||
});
|
||||
});
|
||||
|
||||
test('can return user for verified signature', function(done){
|
||||
it('can return user for verified signature', function(done){
|
||||
var req = {query:{},
|
||||
headers:{authorization:real_oauth_header, host: 'vizzuality.testhost.lan' },
|
||||
protocol: 'http',
|
||||
@ -91,7 +103,7 @@ test('can return user for verified signature', function(done){
|
||||
});
|
||||
});
|
||||
|
||||
test('returns null user for unverified signatures', function(done){
|
||||
it('returns null user for unverified signatures', function(done){
|
||||
var req = {query:{},
|
||||
headers:{authorization:real_oauth_header, host: 'vizzuality.testyhost.lan' },
|
||||
protocol: 'http',
|
||||
@ -105,7 +117,7 @@ test('returns null user for unverified signatures', function(done){
|
||||
});
|
||||
});
|
||||
|
||||
test('returns null user for no oauth', function(done){
|
||||
it('returns null user for no oauth', function(done){
|
||||
var req = {
|
||||
query:{},
|
||||
headers:{},
|
||||
@ -120,14 +132,14 @@ test('returns null user for no oauth', function(done){
|
||||
});
|
||||
});
|
||||
|
||||
test('OAuthAuth reports it has credentials', function(done) {
|
||||
it('OAuthAuth reports it has credentials', function(done) {
|
||||
var req = {query:{}, headers:{authorization:real_oauth_header}};
|
||||
var oAuthAuth = new OAuthAuth(req);
|
||||
assert.ok(oAuthAuth.hasCredentials());
|
||||
done();
|
||||
});
|
||||
|
||||
test('OAuthAuth reports it has no credentials', function(done) {
|
||||
it('OAuthAuth reports it has no credentials', function(done) {
|
||||
var req = {query:{}, headers:{}};
|
||||
var oAuthAuth = new OAuthAuth(req);
|
||||
assert.equal(oAuthAuth.hasCredentials(), false);
|
||||
|
Loading…
Reference in New Issue
Block a user