Skip to content

Commit 5cf3512

Browse files
committed
enhancement(): wrote unit tests for initialize controller and user controller
1 parent 0ba5044 commit 5cf3512

File tree

7 files changed

+322
-21
lines changed

7 files changed

+322
-21
lines changed

config/development.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = {
1717
letsencryptSSLVerificationBody: process.env.LETSENCRYPT_VERIFICATION_BODY || 'xvArhQBSilF4V30dGUagNAZ96ASipB0b0ex0kXn0za8._v6aFbaRYWeOmSebtlD-X4Ixf5tPsyULMsXM8HjsK-Q',
1818
maxContentLength: process.env.MAX_CONTENT_LENGTH || '9999',
1919
enforceSSL: process.env.ENFORCE_SSL || 'no',
20-
gitOAuthToken: process.env.GIT_OAUTH_TOKEN || 'b5c90047c21b74b9dc0dbf90c2552cc5e419b9a2',
20+
gitOAuthToken: process.env.GIT_OAUTH_TOKEN || '24a751ec095255c9ac4c8ca6677d3cc790784dc2',
2121
queueUIUsername: process.env.QUEUE_UI_USERNAME || 'admin',
2222
queueUIPassword: process.env.QUEUE_UI_PASSWORD || 'password123/',
2323
queueUIPort: process.env.QUEUE_UI_PORT || 3000

config/production.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = {
1717
letsencryptSSLVerificationBody: process.env.LETSENCRYPT_VERIFICATION_BODY || 'xvArhQBSilF4V30dGUagNAZ96ASipB0b0ex0kXn0za8._v6aFbaRYWeOmSebtlD-X4Ixf5tPsyULMsXM8HjsK-Q',
1818
maxContentLength: process.env.MAX_CONTENT_LENGTH || '9999',
1919
enforceSSL: process.env.ENFORCE_SSL || 'no',
20-
gitOAuthToken: process.env.GIT_OAUTH_TOKEN || 'b5c90047c21b74b9dc0dbf90c2552cc5e419b9a2',
20+
gitOAuthToken: process.env.GIT_OAUTH_TOKEN || '24a751ec095255c9ac4c8ca6677d3cc790784dc2',
2121
queueUIUsername: process.env.QUEUE_UI_USERNAME || 'admin',
2222
queueUIPassword: process.env.QUEUE_UI_PASSWORD || 'password123/',
2323
queueUIPort: process.env.QUEUE_UI_PORT || 3000

controllers/Initialize.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,3 @@ module.exports = {
1414
});
1515
}
1616
};
17-
18-
// Todo: Test initialize controller

controllers/Users.js

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,36 @@ var Users = require('../models').Users;
44
var Trash = require('../models').Trash;
55
var q = require('q');
66
var queue = require('../services/queue');
7+
var debug = require('debug')('usersController');
78

89
var service = 'Users';
910

1011
var UsersController = {};
1112

12-
UsersController.buildProjection = function(projection){
13+
UsersController.buildProjection = function(projections){
14+
debug('starting build...');
15+
var projection = projections.split(','); // Projection should be comma separated. eg. name,location
1316
// ToDo: Test buildProjection function
1417
return q.Promise(function(resolve,reject,notify){
18+
debug('This is a promise...');
1519
var num = projection.length;
1620
var last = num - 1;
1721
var select = {};
1822
for(var n in projection){
1923
if(typeof projection[n] === 'string'){
24+
debug('Processing...', projection[n]);
2025
notify('Adding '+projection[n]+' to projection');
2126
select[projection[n]] = 1;
22-
if(n === last){
27+
if(n * 1 === last){
28+
debug('Coming out of the loop...', select);
29+
notify('Ending Build.');
2330
return resolve(select);
2431
}
2532
}else{
26-
if(n === last){
33+
debug('Skiping...', projection[n]);
34+
if(n * 1 === last){
35+
debug('Coming out of the loop......', select);
36+
notify('Ending Build..');
2737
return resolve(select);
2838
}
2939
}
@@ -34,7 +44,7 @@ UsersController.buildProjection = function(projection){
3444
UsersController.find = function(req,res,next){
3545
var query;
3646
if(req.query.search){
37-
query = req.query.query;
47+
query = req.query.search;
3848
Users.search(query)
3949
.then(function(resp){
4050
res.ok(resp);
@@ -45,26 +55,37 @@ UsersController.find = function(req,res,next){
4555
// ToDo: Test that search works
4656
}else{
4757
query = req.query;
48-
var projection = query.projection.split(',');
58+
var projection = query.projection; // Projection should be comma separated. eg. name,location
4959
var ourProjection;
50-
query.createdAt = {};
60+
5161
if(projection){
5262
ourProjection = this.buildProjection(projection);
5363
delete query.projection;
5464
}
55-
var limit = query.limit;
65+
var limit = query.limit * 1;
5666
if(limit){
5767
delete query.limit;
5868
}
59-
var to = query.to;
60-
if(to){
61-
delete query.to;
62-
}
69+
6370
var from = query.from;
71+
var to = query.to;
6472
if(from){
73+
query.createdAt = {};
6574
query.createdAt.$gt = from;
6675
delete query.from;
67-
if(!to){
76+
if(to){
77+
delete query.to;
78+
}else{
79+
to = new Date().toISOString();
80+
}
81+
query.createdAt.$lt = to;
82+
}else{
83+
query.createdAt = {};
84+
query.createdAt.$gt = new Date('1989-03-15T00:00:00').toISOString();
85+
delete query.from;
86+
if(to){
87+
delete query.to;
88+
}else{
6889
to = new Date().toISOString();
6990
}
7091
query.createdAt.$lt = to;
@@ -80,6 +101,9 @@ UsersController.find = function(req,res,next){
80101
delete query.sort;
81102
}
82103
var populate = query.populate; // Samples: 'name location' will populate name and location references. only supports this for now | 'name', 'firstname' will populate name referenece and only pick the firstname attribute
104+
if(populate){
105+
delete query.populate;
106+
}
83107
var total = Users.count(query);
84108
var question = Users.find(query);
85109

@@ -115,7 +139,12 @@ UsersController.find = function(req,res,next){
115139
}else{
116140
q.all([question,total])
117141
.spread(function(resp,total){
118-
var ourLastId = resp[resp.length - 1]._id;
142+
var ourLastId;
143+
if(resp.length === 0){
144+
ourLastId = null;
145+
}else{
146+
ourLastId = resp[resp.length - 1]._id;
147+
}
119148
var extraData = {};
120149
extraData.limit = limit * 1;
121150
extraData.total = total;
@@ -203,11 +232,11 @@ UsersController.delete = function(req,res,next){
203232

204233
queue.create('saveToTrash', backupData)
205234
.save();
206-
if(n === last){
235+
if(n * 1 === last){
207236
return resp;
208237
}
209238
}else{
210-
if(n === last){
239+
if(n * 1 === last){
211240
return resp;
212241
}
213242
}

models/Users.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@ var schemaObject = {
1414
},
1515
someOtherStringData: {
1616
type: 'String'
17+
},
18+
toPop: {
19+
type: db.Schema.Types.ObjectId,
20+
ref: 'Users'
1721
}
1822
};
1923

2024
schemaObject.createdAt = {
2125
type: 'Date',
22-
default: Date.now
26+
default: new Date().toISOString()
2327
};
2428

2529
schemaObject.updatedAt = {
2630
type: 'Date'
27-
// default: Date.now
31+
// default: new Date().toISOString()
2832
};
2933

3034
schemaObject.owner = {

test/controllers/initialize.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
var chai = require('chai');
4+
chai.should();
5+
var chaiAsPromised = require("chai-as-promised");
6+
chai.use(chaiAsPromised);
7+
var initialize = require('../../controllers/Initialize.js');
8+
9+
describe('Initialize controller', function(){
10+
it('should return a string', function(done){
11+
var next = function(err){
12+
done(err);
13+
};
14+
var res = {};
15+
res.ok = function(data){
16+
data.should.be.an.object; /* jslint ignore:line */
17+
data.should.have.property('x-tag');
18+
data['x-tag'].should.be.a.string; /* jslint ignore:line */
19+
done();
20+
};
21+
var req;
22+
initialize.init(req, res, next);
23+
});
24+
});

0 commit comments

Comments
 (0)