/server/test/mongo-strategy.js

https://github.com/debdayal/TestApp · JavaScript · 78 lines · 66 code · 10 blank · 2 comment · 0 complexity · 6053a4b7b67671b2f31ee488b9728998 MD5 · raw file

  1. var rewire = require("rewire");
  2. var MongoDBStrategy = rewire('../lib/mongo-strategy');
  3. var config = {
  4. dbUrl: 'https://api.mongolab.com/api/1',
  5. dbName: 'ascrum',
  6. dbCollection: 'users',
  7. apiKey: '4fb51e55e4b02e56a67b0b66',
  8. testId : '5054bb33e4b024584b8f3419',
  9. testUser: { _id: { '$oid': '5054bb33e4b024584b8f3419' },
  10. lastName: 'Bloggs',
  11. firstName: 'Jo',
  12. login: 'jo',
  13. email: 'jo@bloggs.com',
  14. password: 'XX'
  15. }
  16. };
  17. // This method uses the rewire technology to override the local variable "rest" in the test subject
  18. // so that we don't have to actually call out to the server - Yay!!
  19. function mockupRestInterface(test, expectedUrl, expectedOptions, expectedEvent, expectedResult) {
  20. MongoDBStrategy.__set__('rest', {
  21. get: function(url, options, callback) {
  22. test.equal(url, expectedUrl, 'rest.get fn received invalid parameter');
  23. test.deepEqual(options, expectedOptions, 'rest.get fn received invalid parameter');
  24. callback(null, null, expectedResult);
  25. }
  26. });
  27. }
  28. var baseUrl = config.dbUrl + '/databases/' + config.dbName + '/collections/' + config.dbCollection + '/';
  29. module.exports = {
  30. testGet: function(test) {
  31. mockupRestInterface(test, baseUrl + config.testId, { json: {}, qs: { apiKey: config.apiKey }}, 'success', config.testUser);
  32. var db = new MongoDBStrategy(config.dbUrl, config.apiKey, 'ascrum', 'users');
  33. db.get(config.testId, function(err, result) {
  34. test.ok(!err);
  35. test.ok(result);
  36. test.equal(result.email, 'jo@bloggs.com');
  37. test.done();
  38. });
  39. },
  40. testFindByEmail_found: function(test) {
  41. var db = new MongoDBStrategy(config.dbUrl, config.apiKey, 'ascrum', 'users');
  42. mockupRestInterface(test, baseUrl, { json: {}, qs: { apiKey: config.apiKey, q: JSON.stringify({email:"jo@bloggs.com"}) }}, 'success', [config.testUser]);
  43. db.findByEmail('jo@bloggs.com', function(err, result) {
  44. test.ok(!err);
  45. test.ok(result !== null);
  46. test.equal(result.email, 'jo@bloggs.com');
  47. test.done();
  48. });
  49. },
  50. testFindByEmail_notfound: function(test) {
  51. var db = new MongoDBStrategy(config.dbUrl, config.apiKey, 'ascrum', 'users');
  52. mockupRestInterface(test, baseUrl, { json: {}, qs: { apiKey: config.apiKey, q: JSON.stringify({email:"jo@bloggs.com"}) }}, 'success', []);
  53. db.findByEmail('jo@bloggs.com', function(err, result) {
  54. test.ok(!err);
  55. test.ok(result === null);
  56. test.done();
  57. });
  58. },
  59. testVerifyUser: function(test) {
  60. mockupRestInterface(test, baseUrl, { json: {}, qs: { apiKey: config.apiKey, q: JSON.stringify({email:"jo@bloggs.com"}) }}, 'success', [config.testUser]);
  61. var db = new MongoDBStrategy(config.dbUrl, config.apiKey, 'ascrum', 'users');
  62. db.verifyUser('jo@bloggs.com', 'XX', function(err, user) {
  63. test.ok(!err);
  64. test.ok(user);
  65. test.done();
  66. });
  67. }
  68. };