forked from Automattic/kue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonapi.js
More file actions
113 lines (90 loc) · 2.86 KB
/
Copy pathjsonapi.js
File metadata and controls
113 lines (90 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var request = require('supertest'),
kue = require('../index'),
app = kue.app;
function jobsPopulate(type, count) {
var priority = [10, 0, -5, -10, -15],
jobs = [];
for (var i = 0; i < count; i++) {
jobs.push({
type: type,
data: {
title: i,
data: 'test'
},
options: {
// random priority
priority: priority[Math.floor(Math.random() * 5)]
}
});
}
// return array only if length > 1
return jobs.length === 1 ? jobs[0] : jobs;
}
describe('JSON API', function() {
describe('Create jobs', function() {
var jobs = null;
beforeEach(function(done) {
jobs = kue.createQueue();
jobs.promote(1);
done();
});
afterEach(function(done) {
jobs.shutdown(function(err) {
jobs = null;
done(err);
}, 500);
});
it('should insert a job and respond with an id', function(done) {
request(app)
.post('/job')
.send(jobsPopulate('insert a job', 1))
.expect(200)
.expect(function(res) {
res.body.message.should.equal('job created');
res.body.id.should.be.a.Number;
Object.keys(res.body).should.have.lengthOf(2);
})
.end(done);
});
it('should insert multiple jobs and respond with ids', function(done) {
var jobCount = Math.floor(Math.random()) * 10 + 2;
request(app)
.post('/job')
.send(jobsPopulate('insert jobs', jobCount))
.expect(200)
.expect(function(res) {
var created = res.body;
created.should.be.ok;
created.length.should.equal(jobCount);
for (var i = 0; i < jobCount; i++) {
var job = created[i];
job.message.should.be.equal('job created')
job.id.should.be.a.Number
Object.keys(job).should.have.lengthOf(2);
}
})
.end(done);
});
it('should insert jobs including an invaild job, respond with ids and error', function(done) {
var jobs = jobsPopulate('insert jobs including error', 3);
delete jobs[1].type;
request(app)
.post('/job')
.send(jobs)
.expect(400) // Expect a bad request
.expect(function(res) {
var created = res.body;
created.should.be.ok;
created.length.should.equal(2); // the second one failed
// The first one succeeded
created[0].message.should.be.equal('job created');
created[0].id.should.be.a.Number;
Object.keys(created[0]).should.have.lengthOf(2);
// The second one failed
created[1].error.should.equal('Must provide job type');
Object.keys(created[1]).should.have.lengthOf(1);
})
.end(done);
});
});
});