Skip to content

Commit

Permalink
fix(server): replace "restler" with "request" to solve bug with node …
Browse files Browse the repository at this point in the history
…0.10.0
  • Loading branch information
petebacondarwin committed Mar 28, 2013
1 parent 847cc83 commit 46eafb9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 50 deletions.
23 changes: 12 additions & 11 deletions server/initDB.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var config = require('./config.js');
var rest = require('restler');
var rest = require('request');
var apiKey = config.mongo.apiKey;
var baseUrl = config.mongo.dbUrl + '/databases/' + config.security.dbName + '/collections/';

Expand All @@ -9,19 +9,20 @@ var checkDocument = function(collection, query, done) {
var url = baseUrl + collection + '/';
console.log(url);
var params = { apiKey:apiKey, q: JSON.stringify(query) };
var request = rest.get(url, { query: params });
request.on('error', function(err, response) { console.log('error', err, response); done(err, null); });
request.on('fail', function(err, response) { console.log('fail', err, response); done(err, null); });
request.on('success', function(data) { console.log('success', data); done(null, data); });
var request = rest.get(url, { qs: params, json: {} }, function(err, response, data) {
done(null, data);
});
};

var createDocument = function(collection, document, done) {
var url = baseUrl + collection + '/?apiKey=' + apiKey;
var createDocument = function(collection, doc, done) {
var url = baseUrl + collection + '/';
console.log(url);
var request = rest.postJson(url, document);
request.on('error', function(err, response) { console.log('error', err, response); done(err, null); });
request.on('fail', function(err, response) { console.log('fail', err, response); done(err, null); });
request.on('success', function(data) { console.log('success', data); done(null, data); });
var request = rest.post(url, { qs: { apiKey:apiKey }, json: doc }, function(err, response, data) {
if ( !err ) {
console.log('Document created', data);
}
done(err, data);
});
};

var adminUser = { email: '[email protected]', password: 'changeme', admin: true};
Expand Down
19 changes: 7 additions & 12 deletions server/lib/mongo-strategy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var util = require('util');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var rest = require('restler');
var rest = require('request');

function MongoDBStrategy(dbUrl, apiKey, dbName, collection) {
this.dbUrl = dbUrl;
Expand Down Expand Up @@ -34,22 +34,17 @@ MongoDBStrategy.name = "mongo";
// Query the users collection
MongoDBStrategy.prototype.query = function(query, done) {
query.apiKey = this.apiKey; // Add the apiKey to the passed in query
var request = rest.get(this.baseUrl, { query: query });
request.on('error', function(err, response) { done(err, null); });
request.on('fail', function(err, response) { done(err, null); });
request.on('success', function(data) { done(null, data); });
var request = rest.get(this.baseUrl, { qs: query, json: {} }, function(err, response, body) {
done(err, body);
});
};

// Get a user by id
MongoDBStrategy.prototype.get = function(id, done) {
var request = rest.get(this.baseUrl + id, {
query: {
apiKey: this.apiKey
}
var query = { apiKey: this.apiKey };
var request = rest.get(this.baseUrl + id, { qs: query, json: {} }, function(err, response, body) {
done(err, body);
});
request.on('error', function(err, response) { done(err, null); });
request.on('fail', function(err, response) { done(err, null); });
request.on('success', function(data) { done(null, data); });
};

// Find a user by their email
Expand Down
4 changes: 2 additions & 2 deletions server/lib/protectJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module.exports = function(req, res, next) {
res.send = function(body) {
var contentType = res.getHeader('Content-Type');
if ( contentType && contentType.indexOf('application/json') !== -1 ) {
if (2 == arguments.length) {
if (2 === arguments.length) {
// res.send(body, status) backwards compat
if ('number' != typeof body && 'number' == typeof arguments[1]) {
if ('number' !== typeof body && 'number' === typeof arguments[1]) {
this.statusCode = arguments[1];
} else {
this.statusCode = body;
Expand Down
4 changes: 2 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"express": "~3.0",
"passport": "~0.1.12",
"passport-local": "~0.1.6",
"restler": "~2.0.1",
"express-namespace": "~0.1.1",
"open": "0.0.3"
"open": "0.0.3",
"request": "~2.16.6"
},
"devDependencies": {
"rewire": "~1.0.3",
Expand Down
18 changes: 6 additions & 12 deletions server/test/mongo-strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,10 @@ var config = {
// so that we don't have to actually call out to the server - Yay!!
function mockupRestInterface(test, expectedUrl, expectedOptions, expectedEvent, expectedResult) {
MongoDBStrategy.__set__('rest', {
get: function(url, options) {
get: function(url, options, callback) {
test.equal(url, expectedUrl, 'rest.get fn received invalid parameter');
test.deepEqual(options, expectedOptions, 'rest.get fn received invalid parameter');
return {
on: function(eventString, callback) {
if ( eventString === expectedEvent ) {
callback(expectedResult);
}
}
};
callback(null, null, expectedResult);
}
});
}
Expand All @@ -39,7 +33,7 @@ var baseUrl = config.dbUrl + '/databases/' + config.dbName + '/collections/' + c

module.exports = {
testGet: function(test) {
mockupRestInterface(test, baseUrl + config.testId, { query: { apiKey: config.apiKey }}, 'success', config.testUser);
mockupRestInterface(test, baseUrl + config.testId, { json: {}, qs: { apiKey: config.apiKey }}, 'success', config.testUser);

var db = new MongoDBStrategy(config.dbUrl, config.apiKey, 'ascrum', 'users');
db.get(config.testId, function(err, result) {
Expand All @@ -52,7 +46,7 @@ module.exports = {

testFindByEmail_found: function(test) {
var db = new MongoDBStrategy(config.dbUrl, config.apiKey, 'ascrum', 'users');
mockupRestInterface(test, baseUrl, { query: { apiKey: config.apiKey, q: JSON.stringify({email:"[email protected]"}) }}, 'success', [config.testUser]);
mockupRestInterface(test, baseUrl, { json: {}, qs: { apiKey: config.apiKey, q: JSON.stringify({email:"[email protected]"}) }}, 'success', [config.testUser]);
db.findByEmail('[email protected]', function(err, result) {
test.ok(!err);
test.ok(result !== null);
Expand All @@ -64,7 +58,7 @@ module.exports = {

testFindByEmail_notfound: function(test) {
var db = new MongoDBStrategy(config.dbUrl, config.apiKey, 'ascrum', 'users');
mockupRestInterface(test, baseUrl, { query: { apiKey: config.apiKey, q: JSON.stringify({email:"[email protected]"}) }}, 'success', []);
mockupRestInterface(test, baseUrl, { json: {}, qs: { apiKey: config.apiKey, q: JSON.stringify({email:"[email protected]"}) }}, 'success', []);
db.findByEmail('[email protected]', function(err, result) {
test.ok(!err);
test.ok(result === null);
Expand All @@ -73,7 +67,7 @@ module.exports = {
},

testVerifyUser: function(test) {
mockupRestInterface(test, baseUrl, { query: { apiKey: config.apiKey, q: JSON.stringify({email:"[email protected]"}) }}, 'success', [config.testUser]);
mockupRestInterface(test, baseUrl, { json: {}, qs: { apiKey: config.apiKey, q: JSON.stringify({email:"[email protected]"}) }}, 'success', [config.testUser]);
var db = new MongoDBStrategy(config.dbUrl, config.apiKey, 'ascrum', 'users');
db.verifyUser('[email protected]', 'XX', function(err, user) {
test.ok(!err);
Expand Down
22 changes: 11 additions & 11 deletions server/test/security.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ module.exports = {
// Setup mocks
var req = {};
var res = {
send: function() { sendCalled = true; }
json: function() { jsonCalled = true; }
};
var nextCalled = false;
var sendCalled = false;
var jsonCalled = false;
var next = function() { nextCalled = true; };

// Test when user is unauthenticated
req.isAuthenticated = function() { return false; };
security.authenticationRequired(req, res, next);
test.ok(sendCalled);
test.ok(jsonCalled);

// Test when user is authenticated
req.isAuthenticated = function() { return true; };
Expand All @@ -79,12 +79,12 @@ module.exports = {
adminRequired: function(test) {
// Setup mocks
var nextCalled = false;
var sendCalled = false;
var jsonCalled = false;
var req = {};
var res = {
send: function(status) {
json: function(status) {
test.equal(status, 401);
sendCalled = true;
jsonCalled = true;
}
};
var next = function() {
Expand All @@ -94,12 +94,12 @@ module.exports = {
// Test when user is unauthenticated
req.user = null;
security.adminRequired(req, res, next);
test.ok(sendCalled);
test.ok(jsonCalled);

// Test when user is authenticated but not admin
req.user = mockUpUser(false);
security.adminRequired(req, res, next);
test.ok(sendCalled);
test.ok(jsonCalled);

// Test when user is admin
req.user = mockUpUser(true);
Expand All @@ -110,18 +110,18 @@ module.exports = {
},

sendCurrentUser: function(test) {
var sendCalled = false;
var jsonCalled = false;
var req = { user : mockUpUser(false) };
var res = {
json: function(status, userInfo) {
test.equal(status, 200);
test.equal(userInfo.user.id, req.user._id.$oid);
sendCalled = true;
jsonCalled = true;
},
end: function() {}
};
security.sendCurrentUser(req, res, null);
test.ok(sendCalled);
test.ok(jsonCalled);
test.done();
},

Expand Down

0 comments on commit 46eafb9

Please sign in to comment.