|
1
|
|
|
var mongoose = require('mongoose') |
|
2
|
|
|
// mongoose.Promise = global.Promise; // ES6 |
|
3
|
|
|
mongoose.Promise = require('bluebird') |
|
4
|
|
|
var Price = require('../../models/price') |
|
5
|
|
|
var logger = require('winston') |
|
6
|
|
|
|
|
7
|
|
|
var chai = require('chai') |
|
8
|
|
|
var server = require('../../app') |
|
9
|
|
|
/* eslint-disable no-unused-vars */ |
|
10
|
|
|
var should = chai.should() |
|
11
|
|
|
var sinon = require('sinon') |
|
12
|
|
|
|
|
13
|
|
|
describe('Test Prices API', function () { |
|
14
|
|
|
const prefix = '/api/prices' |
|
15
|
|
|
|
|
16
|
|
|
beforeEach(function (done) { // Before each test we empty the database |
|
17
|
|
|
Price.remove({}, function (err) { |
|
18
|
|
|
should.not.exist(err) |
|
19
|
|
|
done() |
|
20
|
|
|
}) |
|
21
|
|
|
}) |
|
22
|
|
|
|
|
23
|
|
|
it('should GET all the prices (none in DB)', function (done) { |
|
24
|
|
|
chai.request(server) |
|
25
|
|
|
.get(prefix) |
|
26
|
|
|
.end(function (err, res) { |
|
27
|
|
|
should.not.exist(err) |
|
28
|
|
|
res.should.have.status(200) |
|
29
|
|
|
res.body.should.be.a('object') |
|
30
|
|
|
res.body.prices.should.be.a('array') |
|
31
|
|
|
res.body.prices.length.should.be.eql(0) |
|
32
|
|
|
done() |
|
33
|
|
|
}) |
|
34
|
|
|
}) |
|
35
|
|
|
|
|
36
|
|
|
it('should GET all the prices (one in DB)', function (done) { |
|
37
|
|
|
var pri = new Price({ name: 'Gas Service' }) |
|
38
|
|
|
pri.save(function (err, price) { |
|
|
|
|
|
|
39
|
|
|
if (err) logger.error(err.stack) |
|
|
|
|
|
|
40
|
|
|
chai.request(server) |
|
41
|
|
|
.get(prefix) |
|
42
|
|
|
.end(function (err, res) { |
|
43
|
|
|
should.not.exist(err) |
|
44
|
|
|
res.should.have.status(200) |
|
45
|
|
|
res.body.should.be.a('object') |
|
46
|
|
|
res.body.prices.should.be.a('array') |
|
47
|
|
|
res.body.prices.length.should.be.eql(1) |
|
48
|
|
|
done() |
|
49
|
|
|
}) |
|
50
|
|
|
}) |
|
51
|
|
|
}) |
|
52
|
|
|
|
|
53
|
|
|
it('should GET all the prices (multiple in DB)', function (done) { |
|
54
|
|
|
var pr1 = new Price({ name: 'Gas Service' }) |
|
55
|
|
|
var pr2 = new Price({ name: 'Oil Service' }) |
|
56
|
|
|
Price.create(pr1, pr2, function (err, pr1, pr2) { |
|
|
|
|
|
|
57
|
|
|
if (err) logger.error(err.stack) |
|
|
|
|
|
|
58
|
|
|
chai.request(server) |
|
59
|
|
|
.get(prefix) |
|
60
|
|
|
.end(function (err, res) { |
|
61
|
|
|
should.not.exist(err) |
|
62
|
|
|
res.should.have.status(200) |
|
63
|
|
|
res.body.should.be.a('object') |
|
64
|
|
|
res.body.prices.should.be.a('array') |
|
65
|
|
|
res.body.prices.length.should.be.eql(2) |
|
66
|
|
|
done() |
|
67
|
|
|
}) |
|
68
|
|
|
}) |
|
69
|
|
|
}) |
|
70
|
|
|
|
|
71
|
|
|
it('should POST a price with just a name', function (done) { |
|
72
|
|
|
var pri = { |
|
73
|
|
|
name: 'Gas Service' |
|
74
|
|
|
} |
|
75
|
|
|
chai.request(server) |
|
76
|
|
|
.post(prefix) |
|
77
|
|
|
.send(pri) |
|
78
|
|
|
.end(function (err, res) { |
|
79
|
|
|
should.not.exist(err) |
|
80
|
|
|
res.should.have.status(200) |
|
81
|
|
|
res.body.should.be.a('object') |
|
82
|
|
|
res.body.should.have.property('price') |
|
83
|
|
|
res.body.price.should.be.a('object') |
|
84
|
|
|
done() |
|
85
|
|
|
}) |
|
86
|
|
|
}) |
|
87
|
|
|
|
|
88
|
|
|
it('should POST a price and get a DB error', function (done) { |
|
89
|
|
|
var err = new Error('mock error') |
|
90
|
|
|
var createStub = sinon.stub(mongoose.Model, 'create') |
|
91
|
|
|
createStub.yields(err) |
|
92
|
|
|
|
|
93
|
|
|
var pri = { |
|
94
|
|
|
name: 'Gas Service' |
|
95
|
|
|
} |
|
96
|
|
|
chai.request(server) |
|
97
|
|
|
.post(prefix) |
|
98
|
|
|
.send(pri) |
|
99
|
|
|
.end(function (err, res) { |
|
100
|
|
|
should.exist(err) |
|
101
|
|
|
res.should.have.status(500) |
|
102
|
|
|
err.should.have.property('message').eql('Internal Server Error') |
|
103
|
|
|
createStub.restore() |
|
104
|
|
|
done() |
|
105
|
|
|
}) |
|
106
|
|
|
}) |
|
107
|
|
|
|
|
108
|
|
|
it('should POST a price with junk property and it\'s not persisted', function (done) { |
|
109
|
|
|
var pri = { |
|
110
|
|
|
name: 'Gas Service', |
|
111
|
|
|
fake_property: 'ignored' |
|
112
|
|
|
} |
|
113
|
|
|
chai.request(server) |
|
114
|
|
|
.post(prefix) |
|
115
|
|
|
.send(pri) |
|
116
|
|
|
.end(function (err, res) { |
|
117
|
|
|
should.not.exist(err) |
|
118
|
|
|
res.should.have.status(200) |
|
119
|
|
|
res.body.should.be.a('object') |
|
120
|
|
|
res.body.should.have.property('price') |
|
121
|
|
|
res.body.price.should.be.a('object') |
|
122
|
|
|
res.body.price.should.have.property('name').eql('Gas Service') |
|
123
|
|
|
res.body.price.should.not.have.property('fake_property') |
|
124
|
|
|
done() |
|
125
|
|
|
}) |
|
126
|
|
|
}) |
|
127
|
|
|
|
|
128
|
|
|
it('should POST a price with same name and it\'s an error', function (done) { |
|
129
|
|
|
var pri = new Price({ name: 'Gas Service' }) |
|
130
|
|
|
var pr2 = { name: 'Gas Service' } |
|
131
|
|
|
pri.save(function (err, price) { |
|
|
|
|
|
|
132
|
|
|
if (err) logger.error(err.stack) |
|
|
|
|
|
|
133
|
|
|
chai.request(server) |
|
134
|
|
|
.post(prefix) |
|
135
|
|
|
.send(pr2) |
|
136
|
|
|
.end(function (err, res) { |
|
137
|
|
|
should.exist(err) |
|
138
|
|
|
res.should.have.status(404) |
|
139
|
|
|
err.should.have.property('message').eql('Not Found') |
|
140
|
|
|
done() |
|
141
|
|
|
}) |
|
142
|
|
|
}) |
|
143
|
|
|
}) |
|
144
|
|
|
|
|
145
|
|
|
it('should GET a price by the given id', function (done) { |
|
146
|
|
|
var pr = new Price({ name: 'Oil Price' }) |
|
147
|
|
|
|
|
148
|
|
|
pr.save(function (err, price) { |
|
149
|
|
|
if (err) logger.error(err.stack) |
|
|
|
|
|
|
150
|
|
|
chai.request(server) |
|
151
|
|
|
.get('/api/prices/' + price._id.toString()) |
|
152
|
|
|
.send(price) |
|
153
|
|
|
.end(function (err, res) { |
|
154
|
|
|
should.not.exist(err) |
|
155
|
|
|
res.should.have.status(200) |
|
156
|
|
|
res.body.should.be.a('object') |
|
157
|
|
|
res.body.should.have.property('price') |
|
158
|
|
|
res.body.price.should.have.property('name') |
|
159
|
|
|
res.body.price.should.have.property('_id').eql(price._id.toString()) |
|
160
|
|
|
done() |
|
161
|
|
|
}) |
|
162
|
|
|
}) |
|
163
|
|
|
}) |
|
164
|
|
|
|
|
165
|
|
|
it('should fail to GET a price by non-existing id', function (done) { |
|
166
|
|
|
chai.request(server) |
|
167
|
|
|
.get('/api/prices/' + 'non-existing-id') |
|
168
|
|
|
.send({}) |
|
169
|
|
|
.end(function (err, res) { |
|
170
|
|
|
should.exist(err) |
|
171
|
|
|
res.should.have.status(404) |
|
172
|
|
|
err.should.have.property('message').eql('Not Found') |
|
173
|
|
|
done() |
|
174
|
|
|
}) |
|
175
|
|
|
}) |
|
176
|
|
|
|
|
177
|
|
|
it('should UPDATE a price by the given id', function (done) { |
|
178
|
|
|
var priceToBeUpdated = new Price({ name: 'Gas Fire Price' }) |
|
179
|
|
|
|
|
180
|
|
|
priceToBeUpdated.save(function (err, price) { |
|
181
|
|
|
if (err) logger.error(err.stack) |
|
|
|
|
|
|
182
|
|
|
var updatesForPrice = { name: 'Gas Fire Price Updated' } |
|
183
|
|
|
chai.request(server) |
|
184
|
|
|
.put('/api/prices/' + price._id) |
|
185
|
|
|
.send(updatesForPrice) |
|
186
|
|
|
.end(function (err, res) { |
|
187
|
|
|
should.not.exist(err) |
|
188
|
|
|
res.should.have.status(200) |
|
189
|
|
|
res.body.should.be.a('object') |
|
190
|
|
|
res.body.price.should.have.property('_id').eql(price._id.toString()) |
|
191
|
|
|
res.body.price.should.have.property('name').eql('Gas Fire Price Updated') |
|
192
|
|
|
done() |
|
193
|
|
|
}) |
|
194
|
|
|
}) |
|
195
|
|
|
}) |
|
196
|
|
|
|
|
197
|
|
|
it('should fail to UPDATE a price with a non existant id', function (done) { |
|
198
|
|
|
var pr = { name: 'Gas Fire Price' } |
|
199
|
|
|
|
|
200
|
|
|
chai.request(server) |
|
201
|
|
|
.put('/api/prices/' + '41224d776a326fb40f000001') |
|
202
|
|
|
.send(pr) |
|
203
|
|
|
.end(function (err, res) { |
|
204
|
|
|
should.exist(err) |
|
205
|
|
|
res.should.have.status(404) |
|
206
|
|
|
err.should.have.property('message').eql('Not Found') |
|
207
|
|
|
done() |
|
208
|
|
|
}) |
|
209
|
|
|
}) |
|
210
|
|
|
|
|
211
|
|
|
it('should DELETE a price by the given id', function (done) { |
|
212
|
|
|
var pr = new Price({ name: 'Gas Fire Price' }) |
|
213
|
|
|
|
|
214
|
|
|
pr.save(function (err, price) { |
|
215
|
|
|
if (err) logger.error(err.stack) |
|
|
|
|
|
|
216
|
|
|
chai.request(server) |
|
217
|
|
|
.delete('/api/prices/' + price._id) |
|
218
|
|
|
.send(price) |
|
219
|
|
|
.end(function (err, res) { |
|
220
|
|
|
should.not.exist(err) |
|
221
|
|
|
res.should.have.status(200) |
|
222
|
|
|
res.body.should.be.a('object') |
|
223
|
|
|
res.body.should.not.have.property('price') |
|
224
|
|
|
done() |
|
225
|
|
|
}) |
|
226
|
|
|
}) |
|
227
|
|
|
}) |
|
228
|
|
|
|
|
229
|
|
|
it('should try to DELETE a price by non-existing id', function (done) { |
|
230
|
|
|
chai.request(server) |
|
231
|
|
|
.delete('/api/prices/' + 'non-existing-id') |
|
232
|
|
|
.send({}) |
|
233
|
|
|
.end(function (err, res) { |
|
234
|
|
|
should.exist(err) |
|
235
|
|
|
res.should.have.status(404) |
|
236
|
|
|
err.should.have.property('message').eql('Not Found') |
|
237
|
|
|
done() |
|
238
|
|
|
}) |
|
239
|
|
|
}) |
|
240
|
|
|
}) |
|
241
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.