Skip to content

Commit 88f8267

Browse files
committed
fix(User model): Now creates search tags properly
1 parent 589e32b commit 88f8267

File tree

7 files changed

+167
-54
lines changed

7 files changed

+167
-54
lines changed

TODO.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,4 @@
44
| routes/index.js | 214 | Implement API Generator
55
| routes/initialize.js | 10 | Test initialize route
66
| routes/users.js | 43 | Test users route
7-
| services/queue/clock.js | 11 | work on a clock functionality so kue can support scheduled jobs
8-
| services/queue/jobs.js | 80 | Test saveToTrash job
9-
| services/queue/jobs.js | 93 | Test Webhook Event
10-
| services/queue/jobs.js | 133 | Test Secure Webhooks
11-
| services/queue/jobs.js | 140 | Test Unsecure Webhooks
12-
| services/queue/jobs.js | 165 | Test sendHTTPRequest Job
7+
| services/queue/clock.js | 11 | work on a clock functionality so kue can support scheduled jobs

services/queue/jobs.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ jobs.createSearchTags = function(data, done){
5959
}
6060
}
6161

62+
split = _.flattenDeep(split);
63+
6264
var task;
6365
if(update){
6466
task = models[model].update(data,{ $set: { updatedAt: new Date().toISOString() }, $addToSet: {tags: {$each: split}} });
@@ -77,7 +79,6 @@ jobs.createSearchTags = function(data, done){
7779
};
7880

7981
// Backup Data to Trash
80-
// ToDo: Test saveToTrash job
8182
jobs.saveToTrash = function(data, done){
8283
log.info('Saving '+data.data._id+' to Trash...');
8384
models.Trash.create(data)
@@ -90,9 +91,8 @@ jobs.saveToTrash = function(data, done){
9091
};
9192

9293
// Send Webhook Event
93-
// ToDo: Test Webhook Event
9494
jobs.sendWebhook = function(data, done){
95-
log.info('Sending Webhook to '+data.url+' (Secure mode: '+ data.secure+') with data => '+data.data);
95+
log.info('Sending Webhook to '+data.url+' (Secure mode: '+ data.secure+') with data => '+JSON.stringify(data.data));
9696
var hookData = {};
9797
// Expected data
9898
// {
@@ -126,18 +126,16 @@ jobs.sendWebhook = function(data, done){
126126
return encryption.encrypt(stringData, key);
127127
})
128128
.then(function(resp){
129-
hookData.data = resp.encryptedData;
129+
hookData.data = resp.encryptedText;
130130
hookData.truth = resp.truth;
131131
return hookData;
132132
});
133-
// ToDo: Test Secure Webhooks
134133
}else{
135134
hookPromise = q.fcall(function(){
136135
hookData.secure = false;
137136
hookData.data = data.data;
138137
return hookData;
139138
});
140-
// ToDo: Test Unsecure Webhooks
141139
}
142140

143141
hookPromise
@@ -162,9 +160,9 @@ jobs.sendWebhook = function(data, done){
162160
// This is for jobs that can be configured from an admin dashboard. So an admin an configure the system to all an api at a particular time daily.
163161
// This can be used within the code too, to do some jobs.
164162
// Supports POST or GET
165-
// ToDo: Test sendHTTPRequest Job
163+
// Other methods not quaranteed
166164
jobs.sendHTTPRequest = function(data, done){
167-
log.info('Sending HTTP' +data.method+' request to '+data.url+' with data => '+data.data+' and headers => '+data.headers);
165+
log.info('Sending HTTP ' +data.method+' request to '+data.url+' with data => '+JSON.stringify(data.data)+' and headers => '+JSON.stringify(data.headers));
168166
// Expected data
169167
// {
170168
// url: 'http://string.com',

test/controllers.js

Whitespace-only changes.

test/controllers/users.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ var chaiAsPromised = require("chai-as-promised");
77
chai.use(chaiAsPromised);
88
var users = require('../../controllers/Users.js');
99
var workers = require('../../services/queue/workers');
10+
var workers2 = require('../../services/queue/workers');
11+
var workers3 = require('../../services/queue/workers');
1012
var _ = require('lodash');
1113
var db = require('../../models');
1214

@@ -211,11 +213,11 @@ describe('Users controller', function(){
211213
forDelete = _.map(data,function(value){
212214
return value._id.toString();
213215
});
214-
data.should.be.an.object; /* jslint ignore:line */
216+
data.length.should.be.above(0); /* jslint ignore:line */
215217
done();
216218
};
217219
var req = {};
218-
req.query = {_id: {$gt: lastId}};
220+
req.query = {lastId: lastId.toString()};
219221
users.find(req, res, next);
220222
};
221223
var req = {};

test/models/users.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ describe('User Model',function(){
164164
});
165165

166166
it('should tag database entries properly', function(done){
167-
var myuser = User.create({name: 'femi',someOtherStringData: 'random stuff'});
167+
var myuser = User.create({name: 'femi',someOtherStringData: 'stuff'});
168168

169169
setTimeout(function(){
170170
myuser.then(function(res){

test/routes/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ describe('Test rate limiting', function(){
6565
before(function(){ /* jslint ignore:line */
6666
var workers = require('../../services/queue/workers');
6767
var workers2 = require('../../services/queue/workers');
68+
var workers3 = require('../../services/queue/workers');
6869
});
6970

7071
it('should reach request rate limit', function(done){
@@ -144,7 +145,7 @@ it('should save rate limit error on request log', function(done){
144145
.catch(function(err){
145146
done(err);
146147
});
147-
},18000);
148+
},5000);
148149

149150
});
150151

test/services/queue.js

Lines changed: 153 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ chai.use(sinonChai);
1616
var jobs;
1717
describe('#Queue service', function(){
1818
before(function() { /* jslint ignore:line */
19-
queue.testMode.enter();
20-
jobs = require('../../services/queue/jobs');
21-
});
19+
queue.testMode.enter();
20+
jobs = require('../../services/queue/jobs');
21+
});
2222

2323
afterEach(function() { /* jslint ignore:line */
24-
queue.testMode.clear();
25-
});
24+
queue.testMode.clear();
25+
});
2626

2727
after(function() { /* jslint ignore:line */
28-
queue.testMode.exit();
29-
});
28+
queue.testMode.exit();
29+
});
3030

3131
it('should return an object', function(done){
3232
queue.should.be.an('object');
@@ -37,12 +37,12 @@ describe('#Queue service', function(){
3737
});
3838

3939
it('should pass basic smoke test', function() {
40-
queue.createJob('myJob', 'foo').save();
41-
queue.createJob('anotherJob', { baz: 'bip' }).save();
42-
queue.testMode.jobs.length.should.equal(2);
43-
queue.testMode.jobs[0].type.should.equal('myJob');
44-
queue.testMode.jobs[0].data.should.equal('foo');
45-
});
40+
queue.createJob('myJob', 'foo').save();
41+
queue.createJob('anotherJob', { baz: 'bip' }).save();
42+
queue.testMode.jobs.length.should.equal(2);
43+
queue.testMode.jobs[0].type.should.equal('myJob');
44+
queue.testMode.jobs[0].data.should.equal('foo');
45+
});
4646

4747
it('should load processes', function() {
4848
var process = require('../../services/queue/workers');
@@ -78,33 +78,150 @@ describe('#Queue service', function(){
7878
jobs.updateRequestLog(myrequestlog,done);
7979
});
8080
it('should run createSearchTags successfully for saving data', function(done){
81-
var myrequestlog = {
82-
RequestId: 'gdfd6563',
83-
ipAddress: '192.168.90.9',
84-
url: 'http://google.com',
85-
method: 'POST',
86-
body: {name: 'femi'},
87-
createdAt: new Date()
88-
};
81+
var myrequestlog = {
82+
RequestId: 'gdfd6563',
83+
ipAddress: '192.168.90.9',
84+
url: 'http://google.com',
85+
method: 'POST',
86+
body: {name: 'femi'},
87+
createdAt: new Date()
88+
};
8989

90-
myrequestlog.model = 'RequestLogs';
91-
jobs.createSearchTags(myrequestlog,done);
92-
});
90+
myrequestlog.model = 'RequestLogs';
91+
jobs.createSearchTags(myrequestlog,done);
92+
});
9393

9494
it('should run createSearchTags successfully for updating data', function(done){
95-
var myrequestlog = {
96-
RequestId: 'gdfd6563',
97-
ipAddress: '192.168.90.9',
98-
url: 'http://google.com',
99-
method: 'POST',
100-
body: {name: 'femi'},
101-
createdAt: new Date()
102-
};
95+
var myrequestlog = {
96+
RequestId: 'gdfd6563',
97+
ipAddress: '192.168.90.9',
98+
url: 'http://google.com',
99+
method: 'POST',
100+
body: {name: 'femi'},
101+
createdAt: new Date()
102+
};
103103

104-
myrequestlog.model = 'RequestLogs';
105-
myrequestlog.update = true;
106-
jobs.createSearchTags(myrequestlog,done);
107-
});
104+
myrequestlog.model = 'RequestLogs';
105+
myrequestlog.update = true;
106+
jobs.createSearchTags(myrequestlog,done);
107+
});
108+
109+
it('should run saveToTrash successfully for backing up data', function(done){
110+
var backup = {
111+
data: {
112+
_id: '789878',
113+
name: 'foo'
114+
}
115+
};
116+
117+
jobs.saveToTrash(backup,done);
118+
});
119+
120+
it('should run sendWebhook successfully for sending realtime HTTP notifications', function(done){
121+
var data = {
122+
url: 'https://httpbin.org/anything',
123+
secure: false, // true or false
124+
data: {
125+
someData: 'this',
126+
someOtherData: 'and this'
127+
}
128+
};
129+
130+
jobs.sendWebhook(data,done);
131+
});
132+
133+
it('should run sendWebhook successfully for sending realtime HTTP notifications securely', function(done){
134+
var data = {
135+
url: 'https://httpbin.org/anything',
136+
secure: true, // true or false
137+
data: {
138+
someData: 'this',
139+
someOtherData: 'and this'
140+
}
141+
};
142+
143+
jobs.sendWebhook(data,done);
144+
});
145+
146+
it('should run sendHTTPRequest successfully for calling web services with POST method', function(done){
147+
var data = {
148+
url: 'https://httpbin.org/anything',
149+
method: 'POST', // or any http method
150+
headers: {
151+
'User-Agent': 'Femi'
152+
},
153+
data: {
154+
someData: 'this',
155+
someOtherData: 'and this'
156+
}
157+
};
158+
159+
jobs.sendHTTPRequest(data,done);
160+
});
161+
162+
it('should run sendHTTPRequest successfully for calling web services with GET method', function(done){
163+
var data = {
164+
url: 'https://httpbin.org/anything',
165+
method: 'GET', // or any http method
166+
headers: {
167+
'User-Agent': 'Femi'
168+
},
169+
data: {
170+
someData: 'this',
171+
someOtherData: 'and this'
172+
}
173+
};
174+
175+
jobs.sendHTTPRequest(data,done);
176+
});
177+
178+
it('should run sendHTTPRequest successfully for calling web services with PUT method', function(done){
179+
var data = {
180+
url: 'https://httpbin.org/anything',
181+
method: 'PUT', // or any http method
182+
headers: {
183+
'User-Agent': 'Femi'
184+
},
185+
data: {
186+
someData: 'this',
187+
someOtherData: 'and this'
188+
}
189+
};
190+
191+
jobs.sendHTTPRequest(data,done);
192+
});
193+
194+
it('should run sendHTTPRequest successfully for calling web services with DELETE method', function(done){
195+
var data = {
196+
url: 'https://httpbin.org/anything',
197+
method: 'DELETE', // or any http method
198+
headers: {
199+
'User-Agent': 'Femi'
200+
},
201+
data: {
202+
someData: 'this',
203+
someOtherData: 'and this'
204+
}
205+
};
206+
207+
jobs.sendHTTPRequest(data,done);
208+
});
209+
210+
it('should run sendHTTPRequest successfully for calling web services with PATCH method', function(done){
211+
var data = {
212+
url: 'https://httpbin.org/anything',
213+
method: 'PATCH', // or any http method
214+
headers: {
215+
'User-Agent': 'Femi'
216+
},
217+
data: {
218+
someData: 'this',
219+
someOtherData: 'and this'
220+
}
221+
};
222+
223+
jobs.sendHTTPRequest(data,done);
224+
});
108225

109226
});
110227
});

0 commit comments

Comments
 (0)