Skip to content

Commit 5a53cb6

Browse files
committed
started testing routes
1 parent 88f8267 commit 5a53cb6

File tree

14 files changed

+283
-19
lines changed

14 files changed

+283
-19
lines changed

TODO.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
### TODOs
22
| Filename | line # | TODO
33
|:------|:------:|:------
4-
| routes/index.js | 214 | Implement API Generator
5-
| routes/initialize.js | 10 | Test initialize route
4+
| routes/index.js | 228 | Implement API Generator
65
| routes/users.js | 43 | Test users route
76
| services/queue/clock.js | 11 | work on a clock functionality so kue can support scheduled jobs

controllers/Users.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ UsersController.find = function(req,res,next){
5353
});
5454
}else{
5555
query = req.query;
56+
// Clean appId and userId
57+
delete query.appId;
58+
delete query.userId;
59+
delete query.developer;
5660
var projection = query.projection; // Projection should be comma separated. eg. name,location
5761
var ourProjection;
5862

@@ -107,10 +111,12 @@ UsersController.find = function(req,res,next){
107111
var question = Users.find(query);
108112

109113
if(limit){
110-
var ourLimit = limit * 1;
114+
totalResult = totalResult.limit(limit);
111115
question = question.limit(limit);
112116
}else{
113-
limit = 0;
117+
limit = 50;
118+
totalResult = totalResult.limit(limit);
119+
question = question.limit(limit);
114120
}
115121
if(sort){
116122
question = question.sort(sort);
@@ -131,6 +137,7 @@ UsersController.find = function(req,res,next){
131137
extraData.total = total;
132138
extraData.totalResult = totalResult;
133139
extraData.lastId = ourLastId;
140+
extraData.isLastPage = (totalResult < limit) ? true : false;
134141
res.ok(resp, false, extraData);
135142
})
136143
.catch(function(err){
@@ -150,6 +157,7 @@ UsersController.find = function(req,res,next){
150157
extraData.total = total;
151158
extraData.lastId = ourLastId;
152159
extraData.totalResult = totalResult;
160+
extraData.isLastPage = (totalResult < limit) ? true : false;
153161
res.ok(resp, false, extraData);
154162
})
155163
.catch(function(err){

models/.gitkeep

Whitespace-only changes.

models/Users.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var collection = 'Users';
99
var debug = require('debug')(collection);
1010

1111
var schemaObject = {
12+
// ++++++++++++++ Modify to your own schema ++++++++++++++++++
1213
name: {
1314
type: 'String'
1415
},
@@ -19,6 +20,8 @@ var schemaObject = {
1920
type: db.Schema.Types.ObjectId,
2021
ref: 'Users'
2122
}
23+
24+
// ++++++++++++++ Modify to your own schema ++++++++++++++++++
2225
};
2326

2427
schemaObject.createdAt = {

models/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
"use strict";
22

3-
var models = {
4-
RequestLogs: require('./RequestLogs'),
5-
Trash: require('./Trash'),
6-
Users: require('./Users')
7-
};
3+
var models = {};
4+
var normalizedPath = require("path").join(__dirname, "./");
5+
6+
require("fs").readdirSync(normalizedPath).forEach(function(file) {
7+
var splitFileName = file.split('.');
8+
if(splitFileName[0] !== 'index'){
9+
models[splitFileName[0]] = require('./'+splitFileName[0]);
10+
}
11+
});
812

913
module.exports = models;

routes/.gitkeep

Whitespace-only changes.

routes/index.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ router._enforceUserIdAndAppId = function(req,res,next){
5858
return res.badRequest(false,'No userId parameter was passed in the payload of this request. Please pass a userId.');
5959
}else if(!appId){
6060
return res.badRequest(false,'No appId parameter was passed in the payload of this request. Please pass an appId.');
61+
}else if(!developer){
62+
return res.badRequest(false,'No developer parameter was passed in the payload of this request. Please pass a developer id.');
6163
}else{
6264
req.userId = userId;
6365
req.appId = appId;
@@ -152,6 +154,9 @@ router.use(bodyParser.urlencoded({ extended: false }));
152154
router.use(bodyParser.json());
153155
router.use(bodyParser.raw());
154156
router.use(bodyParser.text());
157+
// load response handlers
158+
router.use(response);
159+
// Watch for encrypted requests
155160
router.use(encryption.interpreter);
156161
router.use(hpp());
157162
router.use(contentLength.validateMax({max: MAX_CONTENT_LENGTH_ACCEPTED, status: 400, message: "Stop! Maximum content length exceeded."})); // max size accepted for the content-length
@@ -170,14 +175,14 @@ limiter({
170175
}
171176
});
172177

173-
router.use(response);
178+
174179
router.use(expressValidator());
175180

176181
// no client side caching
177182
if(config.noFrontendCaching === 'yes'){
178183
router.use(helmet.noCache());
179184
}else{
180-
router.use(router._APICache);
185+
router.use(router._APICache);
181186
}
182187

183188
router.get('/', function (req, res) {
@@ -199,9 +204,18 @@ router.use('/', initialize);
199204
// Make userId compolsory in every request
200205
router.use(router._enforceUserIdAndAppId);
201206

207+
// Should automatically load routes
202208
// Other routes here
203-
//
204-
//
209+
var ourRoutes = {};
210+
var normalizedPath = require("path").join(__dirname, "./");
211+
212+
require("fs").readdirSync(normalizedPath).forEach(function(file) {
213+
var splitFileName = file.split('.');
214+
if(splitFileName[0] !== 'index' && splitFileName[0] !== 'initialize'){
215+
ourRoutes[splitFileName[0]] = require('./'+splitFileName[0]);
216+
router.use('/', ourRoutes[splitFileName[0]]);
217+
}
218+
});
205219

206220
router.use(function(req, res, next) { // jshint ignore:line
207221
res.notFound();

routes/initialize.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ var initializeController = require('../controllers/Initialize');
77
router.get('/initialize', initializeController.init);
88

99
module.exports = router;
10-
// Todo: Test initialize route

services/database/mongo.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ var config = require('../../config');
44
var log = require('../../services/logger');
55

66
// Connect to DB
7-
mongoose.connect(config.mongoURL);
7+
mongoose.connect(config.mongoURL, {
8+
useMongoClient: true
9+
});
810

911
// Set the prefered promise library
1012
mongoose.Promise = require('q').Promise;
@@ -13,7 +15,7 @@ var db = mongoose.connection;
1315

1416
db.on('error', log.error);
1517
db.once('open', function() {
16-
log.info('MongoDB database connection successful');
18+
log.info('MongoDB database connection successful');
1719
});
1820

1921
module.exports = mongoose;

test/controllers/users.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,15 @@ describe('Users controller', function(){
8989
req.query.search = 'i like pizza';
9090
users.find(req, res, next);
9191
});
92-
it('should limit the number of returned documents', function(done){
92+
it('should limit the number of returned documents and check if it is the last page', function(done){
9393
var next = function(err){
9494
done(err);
9595
};
9696
var res = {};
9797
res.ok = function(data, cache, extraData){
9898

9999
data.should.be.an.object; /* jslint ignore:line */
100+
extraData.isLastPage.should.exist; /* jslint ignore:line */
100101
data.length.should.equal(2);
101102
done();
102103
};
@@ -105,7 +106,7 @@ describe('Users controller', function(){
105106
req.query.limit = 2;
106107
users.find(req, res, next);
107108
});
108-
it('should contains count of total record for the query', function(done){
109+
it('should contain count of total record for the query', function(done){
109110
var next = function(err){
110111
done(err);
111112
};

0 commit comments

Comments
 (0)