test/integration/price.js   B
last analyzed

Complexity

Total Complexity 41
Complexity/F 1.17

Size

Lines of Code 240
Function Count 35

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
c 2
b 0
f 0
nc 1
dl 0
loc 240
rs 8.2769
wmc 41
mnd 1
bc 35
fnc 35
bpm 1
cpm 1.1714
noi 10

1 Function

Rating   Name   Duplication   Size   Complexity  
B price.js ➔ describe(ꞌTest Prices APIꞌ) 0 228 1

How to fix   Complexity   

Complexity

Complex classes like test/integration/price.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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) {
0 ignored issues
show
Unused Code introduced by
The parameter price is not used and could be removed.

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.

Loading history...
39
      if (err) logger.error(err.stack)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter pr1 is not used and could be removed.

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.

Loading history...
Unused Code introduced by
The parameter pr2 is not used and could be removed.

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.

Loading history...
57
      if (err) logger.error(err.stack)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter price is not used and could be removed.

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.

Loading history...
132
      if (err) logger.error(err.stack)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
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)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
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)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
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)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
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