PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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