-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
294 lines (265 loc) · 7.44 KB
/
test.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
var request = require('supertest');
var assert = require('better-assert');
var mongoose = require('mongoose');
var async = require('async');
var config = require('./config').test;
var app = require('./app');
var Book = require('./book');
var db;
var insert_30_books = function(done,callback) {
//remove all books
Book.remove({}, function(err) {
if (err) return done(err);
//insert 30 books and wait for save()
var calls = [];
for (var i = 1; i < 31; i++) {
(function(i) {
calls.push(function(callback) {
var b = new Book({isbn: ''+i, title: 't'+i, author: 'a'+i});
b.save(function(err){
if (err) {return callback(err)}
callback(null,i);
});
})
})(i);
}
async.parallel(calls, function(err, result) {
if (err) {
done(err);
} else {
callback();
}
})
})
};
describe('Sample REST API', function() {
var book1 = {isbn: '978-0-19-923276-5',
title: 'War and Piece',
author: 'Leo Tolstoy',
cover: 'http://blog.oup.com/wp-content/uploads/2010/09/War-and-Peace.jpg'};
var book2 = {isbn: '123-123-123',
title: 'Crime and Punishment',
author: 'Fyodor Dostoyevsky'};
var book3 = {isbn: '456-456-456',
title: 'The Master and Margarita',
author: 'Mikhail Bulgakov'};
before(function (done) {
mongoose.connect(config.db, function() {
console.log('Connected to '+config.db)
done();
})
});
beforeEach(function(done){
var db = mongoose.connection;
Book.remove({}, function(err) {
if (err) return done(err);
var b1 = new Book(book1);
var b2 = new Book(book2);
b1.save(function(err){
b2.save(function(err) {
done();
})
})
})
})
it('should return 404 when invalid path given', function(done){
request(app)
.get("/invalid")
.send({})
.expect(404, {}, done)
})
it('should return all books', function(done){
request(app)
.get("/book")
.send({})
.expect(200)
.end(function(err,res) {
if (err) {
done(err);
} else {
assert(res.body[1].title == book1.title);
assert(res.body[1].isbn == book1.isbn);
assert(res.body[1].author == book1.author);
assert(res.body[0].isbn == book2.isbn);
assert(res.body[0].title == book2.title);
done();
}
})
})
it('should return one book by ISBN', function(done){
request(app)
.get("/book/123-123-123")
.send({})
.expect(200)
.end(function(err,res) {
if (err) {
done(err);
} else {
assert(res.body.title == book2.title);
assert(res.body.isbn == book2.isbn);
assert(res.body.author == book2.author);
done();
}
})
})
it('should add new book', function(done) {
request(app)
.post("/book")
.send(book3)
.expect(200)
.end(function(err,res) {
if (err) {
done(err);
} else {
assert(res.body.title == book3.title);
assert(res.body.isbn == book3.isbn);
assert(res.body.author == book3.author);
Book.count(function(err,c) {
if (err) {
done(err);
} else {
assert(c == 3);
}
})
done();
}
})
})
it('should fail to add new book without mandatory params', function(done) {
request(app)
.post("/book")
.send({isbn: '456-456-456'})
.expect(422,'insert failed',done)
})
it('should fail to add new book when ISBN is used before', function(done) {
request(app)
.post("/book")
.send(book2)
.expect(422,'insert failed',done)
})
it('should fail to add new book when ISBN is not valid (1)', function(done) {
request(app)
.post("/book")
.send({isbn: '-456-456-456',author:'a',title:'t'})
.expect(422,'insert failed',done)
})
it('should fail to add new book when ISBN is not valid (2)', function(done) {
request(app)
.post("/book")
.send({isbn: 'XXX',author:'a',title:'t'})
.expect(422,'insert failed',done)
})
it('should fail to add new book when ISBN is not valid (3)', function(done) {
request(app)
.post("/book")
.send({isbn: '000-000-000',author:'a',title:'t'})
.expect(422,'insert failed',done)
})
it('should fail to add new book when cover url is not valid', function(done) {
request(app)
.post("/book")
.send({isbn: '100-000-000',author:'a',title:'t',cover:'invalid'})
.expect(422,'insert failed',done)
})
it('should update one book', function(done) {
request(app)
.put("/book/123-123-123")
.send({author:'a',title:'t'})
.expect(200)
.end(function(err,res) {
if (err) {
done(err);
} else {
assert(res.body.title == 't');
assert(res.body.isbn == '123-123-123');
assert(res.body.author == 'a');
Book.count(function(err,c) {
if (err) {
done(err);
} else {
assert(c == 2);
}
})
done();
}
})
})
it('should fail to remove mandatory fields', function(done) {
request(app)
.put("/book/123-123-123")
.send({author:''})
.expect(422,'update failed',done)
})
it('should delete one book', function(done) {
request(app)
.del("/book/123-123-123")
.send()
.expect(200,'OK')
.end(function(err,res) {
if (err) {
done(err);
} else {
Book.count(function(err,c) {
if (err) {
done(err);
} else {
assert(c == 1);
}
})
done();
}
})
})
it('should fail to remove invalid book', function(done) {
request(app)
.del("/book/6548")
.send()
.expect(422,'delete failed')
.end(function(err,res) {
if (err) {
done(err);
} else {
Book.count(function(err,c) {
if (err) {
done(err);
} else {
assert(c == 2);
}
})
done();
}
})
})
it('should return first ten books only', function(done) {
insert_30_books(done,function(){
request(app)
.get("/book?limit=10")
.send()
.expect(200)
.end(function(err,res) {
if (err) {
done(err);
} else {
assert(res.body.length == 10);
done();
}
})
})
})
it('should return last five books only', function(done) {
insert_30_books(done,function(){
request(app)
.get("/book?limit=10&skip=25")
.send()
.expect(200)
.end(function(err,res) {
if (err) {
done(err);
} else {
assert(res.body.length == 5);
done();
}
})
})
})
})