v1/test/invoices.js   B
last analyzed

Complexity

Total Complexity 47
Complexity/F 1.02

Size

Lines of Code 371
Function Count 46

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 99.38%

Importance

Changes 0
Metric Value
wmc 47
eloc 254
mnd 1
bc 1
fnc 46
dl 0
loc 371
ccs 160
cts 161
cp 0.9938
rs 8.64
bpm 0.0217
cpm 1.0217
noi 1
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like v1/test/invoices.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
/* global it describe before */
2
3 1
process.env.NODE_ENV = 'test';
4
5
//Require the dev-dependencies
6 1
const chai = require('chai');
7 1
const chaiHttp = require('chai-http');
8 1
const server = require('../../app.js');
9
10 1
chai.should();
11
12 1
const db = require("../db/database.js");
13
14 1
chai.use(chaiHttp);
15
16 1
let apiKey = "";
17 1
let token = "";
18
19 1
describe('invoices', () => {
20 1
    before(() => {
21 1
        db.run("DELETE FROM invoices", (err) => {
22 2
            if (err) {
23
                console.log("Could not empty test DB table products", err.message);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
24
            }
25
        });
26
    });
27
28 1
    describe('GET /invoices', () => {
29 1
        it('should get 401 as we do not provide valid api_key', (done) => {
30 1
            chai.request(server)
31
                .get("/invoices")
32
                .end((err, res) => {
33 1
                    res.should.have.status(401);
34 1
                    res.body.should.be.an("object");
35 1
                    res.body.errors.status.should.be.equal(401);
36 1
                    done();
37
                });
38
        });
39
40 1
        it('should get 200 HAPPY PATH FROM GETTING API KEY', (done) => {
41 1
            chai.request(server)
42
                .get("/[email protected]")
43
                .end((err, res) => {
44 1
                    res.should.have.status(200);
45 1
                    res.body.should.be.an("object");
46 1
                    res.body.data.should.be.an("object");
47 1
                    res.body.data.should.have.property("key");
48
49 1
                    apiKey = res.body.data.key;
50
51 1
                    done();
52
                });
53
        });
54
55 1
        it('should get 401 as we have not logged in', (done) => {
56 1
            chai.request(server)
57
                .get("/invoices?api_key=" + apiKey)
58
                .end((err, res) => {
59 1
                    res.should.have.status(401);
60 1
                    res.body.should.be.an("object");
61 1
                    res.body.errors.status.should.be.equal(401);
62 1
                    done();
63
                });
64
        });
65
66 1
        it('should get 201 HAPPY PATH registering', (done) => {
67 1
            let user = {
68
                api_key: apiKey,
69
                email: "[email protected]",
70
                password: "testinginvoice"
71
            };
72
73 1
            chai.request(server)
74
                .post("/register")
75
                .send(user)
76
                .end((err, res) => {
77 1
                    res.should.have.status(201);
78 1
                    res.body.should.be.an("object");
79 1
                    res.body.should.have.property("data");
80
81 1
                    done();
82
                });
83
        });
84
85 1
        it('should get 401 UNIQUE CONSTRAINT', (done) => {
86 1
            let user = {
87
                api_key: apiKey,
88
                email: "[email protected]",
89
                password: "testinginvoice"
90
            };
91
92 1
            chai.request(server)
93
                .post("/register")
94
                .send(user)
95
                .end((err, res) => {
96 1
                    res.should.have.status(500);
97 1
                    res.body.should.be.an("object");
98 1
                    res.body.errors.status.should.be.equal(500);
99
100 1
                    done();
101
                });
102
        });
103
104 1
        it('should get 200 HAPPY PATH logging in', (done) => {
105 1
            let user = {
106
                api_key: apiKey,
107
                email: "[email protected]",
108
                password: "testinginvoice"
109
            };
110
111 1
            chai.request(server)
112
                .post("/login")
113
                .send(user)
114
                .end((err, res) => {
115 1
                    res.should.have.status(200);
116 1
                    res.body.should.be.an("object");
117 1
                    res.body.should.have.property("data");
118 1
                    res.body.data.should.have.property("type");
119 1
                    res.body.data.type.should.equal("success");
120 1
                    res.body.data.should.have.property("token");
121
122 1
                    token = res.body.data.token;
123
124 1
                    done();
125
                });
126
        });
127
128 1
        it('should get 200 HAPPY PATH getting no invoices', (done) => {
129 1
            chai.request(server)
130
                .get("/invoices?api_key=" + apiKey)
131
                .set("x-access-token", token)
132
                .end((err, res) => {
133 1
                    res.should.have.status(200);
134 1
                    res.body.should.be.an("object");
135 1
                    res.body.data.should.be.an("array");
136 1
                    res.body.data.length.should.be.equal(0);
137
138 1
                    done();
139
                });
140
        });
141
    });
142
143 1
    describe('POST /invoice', () => {
144 1
        it('should get 201 HAPPY PATH creating order', (done) => {
145 1
            let order = {
146
                id: 1,
147
                name: "Anders",
148
                api_key: apiKey
149
            };
150
151 1
            chai.request(server)
152
                .post("/order")
153
                .send(order)
154
                .end((err, res) => {
155 1
                    res.should.have.status(201);
156 1
                    res.body.should.be.an("object");
157 1
                    res.body.should.have.property("data");
158
159 1
                    done();
160
                });
161
        });
162
163 1
        it('should get 500 as we do not supply id', (done) => {
164 1
            let invoice = {
165
                // id: 1,
166
                order_id: 1,
167
                total_price: 100,
168
                api_key: apiKey
169
            };
170
171 1
            chai.request(server)
172
                .post("/invoice")
173
                .set("x-access-token", token)
174
                .send(invoice)
175
                .end((err, res) => {
176 1
                    res.should.have.status(500);
177 1
                    res.body.should.be.an("object");
178 1
                    res.body.should.have.property("errors");
179 1
                    res.body.errors.should.have.property("status");
180 1
                    res.body.errors.status.should.be.equal(500);
181 1
                    res.body.errors.should.have.property("detail");
182
183 1
                    done();
184
                });
185
        });
186
187 1
        it('should get 500 as we do not supply order_id', (done) => {
188 1
            let invoice = {
189
                id: 1,
190
                // order_id: 1,
191
                total_price: 100,
192
                api_key: apiKey
193
            };
194
195 1
            chai.request(server)
196
                .post("/invoice")
197
                .set("x-access-token", token)
198
                .send(invoice)
199
                .end((err, res) => {
200 1
                    res.should.have.status(500);
201 1
                    res.body.should.be.an("object");
202 1
                    res.body.should.have.property("errors");
203 1
                    res.body.errors.should.have.property("status");
204 1
                    res.body.errors.status.should.be.equal(500);
205 1
                    res.body.errors.should.have.property("detail");
206
207 1
                    done();
208
                });
209
        });
210
211 1
        it('should get 500 as we do not supply total_price', (done) => {
212 1
            let invoice = {
213
                id: 1,
214
                order_id: 1,
215
                // total_price: 100,
216
                api_key: apiKey
217
            };
218
219 1
            chai.request(server)
220
                .post("/invoice")
221
                .set("x-access-token", token)
222
                .send(invoice)
223
                .end((err, res) => {
224 1
                    res.should.have.status(500);
225 1
                    res.body.should.be.an("object");
226 1
                    res.body.should.have.property("errors");
227 1
                    res.body.errors.should.have.property("status");
228 1
                    res.body.errors.status.should.be.equal(500);
229 1
                    res.body.errors.should.have.property("detail");
230
231 1
                    done();
232
                });
233
        });
234
235 1
        it('should get 401 not providing token', (done) => {
236 1
            let invoice = {
237
                id: 1,
238
                order_id: 1,
239
                total_price: 100,
240
                api_key: apiKey
241
            };
242
243 1
            chai.request(server)
244
                .post("/invoice")
245
                .send(invoice)
246
                .end((err, res) => {
247 1
                    res.should.have.status(401);
248 1
                    res.body.should.be.an("object");
249 1
                    res.body.errors.status.should.be.equal(401);
250
251 1
                    done();
252
                });
253
        });
254
255 1
        it('should get 201 HAPPY PATH', (done) => {
256 1
            let invoice = {
257
                id: 1,
258
                order_id: 1,
259
                total_price: 100,
260
                api_key: apiKey
261
            };
262
263 1
            chai.request(server)
264
                .post("/invoice")
265
                .set("x-access-token", token)
266
                .send(invoice)
267
                .end((err, res) => {
268 1
                    res.should.have.status(201);
269 1
                    res.body.should.be.an("object");
270 1
                    res.body.should.have.property("data");
271
272 1
                    done();
273
                });
274
        });
275
276 1
        it('should get 200 HAPPY PATH getting the one invoice we just created', (done) => {
277 1
            chai.request(server)
278
                .get("/invoices?api_key=" + apiKey)
279
                .set("x-access-token", token)
280
                .end((err, res) => {
281 1
                    res.should.have.status(200);
282 1
                    res.body.should.be.an("object");
283 1
                    res.body.data.should.be.an("array");
284 1
                    res.body.data.length.should.be.equal(1);
285
286 1
                    done();
287
                });
288
        });
289
290 1
        it('should get 500 UNIQUE CONSTRAINT', (done) => {
291 1
            let invoice = {
292
                id: 1,
293
                order_id: 1,
294
                total_price: 100,
295
                api_key: apiKey
296
            };
297
298 1
            chai.request(server)
299
                .post("/invoice")
300
                .set("x-access-token", token)
301
                .send(invoice)
302
                .end((err, res) => {
303 1
                    res.should.have.status(500);
304 1
                    res.body.should.be.an("object");
305 1
                    res.body.errors.status.should.be.equal(500);
306
307 1
                    done();
308
                });
309
        });
310
    });
311
312 1
    describe('GET /invoice', () => {
313 1
        it('should get 404 no id supplied', (done) => {
314 1
            chai.request(server)
315
                .get("/invoice?api_key=" + apiKey)
316
                .end((err, res) => {
317 1
                    res.should.have.status(404);
318
319 1
                    done();
320
                });
321
        });
322
323 1
        it('should get 400 string id supplied', (done) => {
324 1
            chai.request(server)
325
                .get("/invoice/test?api_key=" + apiKey)
326
                .set("x-access-token", token)
327
                .end((err, res) => {
328 1
                    res.should.have.status(400);
329
330 1
                    done();
331
                });
332
        });
333
334 1
        it('should get 401 not providing token', (done) => {
335 1
            chai.request(server)
336
                .get("/invoice/1?api_key=" + apiKey)
337
                .end((err, res) => {
338 1
                    res.should.have.status(401);
339 1
                    res.body.should.be.an("object");
340 1
                    res.body.errors.status.should.be.equal(401);
341
342 1
                    done();
343
                });
344
        });
345
346 1
        it('should get 200 HAPPY PATH', (done) => {
347 1
            chai.request(server)
348
                .get("/invoice/1?api_key=" + apiKey)
349
                .set("x-access-token", token)
350
                .end((err, res) => {
351 1
                    res.should.have.status(200);
352 1
                    res.body.should.be.an("object");
353 1
                    res.body.data.should.be.an("object");
354 1
                    res.body.data.should.have.property("id");
355 1
                    res.body.data.id.should.be.equal(1);
356
357 1
                    done();
358
                });
359
        });
360
361 1
        it('should get 200, but empty data object', (done) => {
362 1
            chai.request(server)
363
                .get("/invoice/2?api_key=" + apiKey)
364
                .set("x-access-token", token)
365
                .end((err, res) => {
366 1
                    res.should.have.status(200);
367 1
                    res.body.should.be.eql({});
368
369 1
                    done();
370
                });
371
        });
372
    });
373
});
374