Completed
Push — master ( 95d401...5dafee )
by Emil
02:58
created

v2/test/invoices.js   C

Complexity

Total Complexity 57
Complexity/F 1.04

Size

Lines of Code 470
Function Count 55

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 99.01%

Importance

Changes 0
Metric Value
wmc 57
eloc 323
mnd 2
bc 2
fnc 55
dl 0
loc 470
ccs 200
cts 202
cp 0.9901
rs 5.04
bpm 0.0363
cpm 1.0363
noi 0
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like v2/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 HTMLParser = require('node-html-parser');
9
10 1
const server = require('../../app.js');
11
12 1
chai.should();
13
14 1
const db = require("../db/database.js");
15
16 1
chai.use(chaiHttp);
17
18 1
let apiKey = "";
19 1
let token = "";
20
21 1
describe('invoices', () => {
22 1
    before(() => {
23 1
        return new Promise((resolve) => {
24 1
            db.run("DELETE FROM invoices", (err) => {
25 2
                if (err) {
26
                    console.error("Could not empty test DB table invoices", err.message);
27
                }
28
29 1
                db.run("DELETE FROM orders", (err) => {
30 2
                    if (err) {
31
                        console.error("Could not empty test DB table orders", err.message);
32
                    }
33
34 1
                    resolve();
35
                });
36
            });
37
        });
38
    });
39
40 1
    describe('GET /invoices', () => {
41 1
        it('should get 401 as we do not provide valid api_key', (done) => {
42 1
            chai.request(server)
43
                .get("/v2/invoices")
44
                .end((err, res) => {
45 1
                    res.should.have.status(401);
46 1
                    res.body.should.be.an("object");
47 1
                    res.body.errors.status.should.be.equal(401);
48 1
                    done();
49
                });
50
        });
51
52 1
        it('should get 200 as we get apiKey', (done) => {
53 1
            let user = {
54
                email: "[email protected]",
55
                gdpr: "gdpr"
56
            };
57
58 1
            chai.request(server)
59
                .post("/v2/auth/api_key/confirmation")
60
                .send(user)
61
                .end((err, res) => {
62 1
                    res.should.have.status(200);
63
64 1
                    let HTMLResponse = HTMLParser.parse(res.text);
65 1
                    let apiKeyElement = HTMLResponse.querySelector('#apikey');
66
67 1
                    apiKey = apiKeyElement.childNodes[0].rawText;
68
69 1
                    done();
70
                });
71
        });
72
73 1
        it('should get 401 as we have not logged in', (done) => {
74 1
            chai.request(server)
75
                .get("/v2/invoices?api_key=" + apiKey)
76
                .end((err, res) => {
77 1
                    res.should.have.status(401);
78 1
                    res.body.should.be.an("object");
79 1
                    res.body.errors.status.should.be.equal(401);
80 1
                    done();
81
                });
82
        });
83
84 1
        it('should get 201 HAPPY PATH registering', (done) => {
85 1
            let user = {
86
                api_key: apiKey,
87
                email: "[email protected]",
88
                password: "testinginvoice"
89
            };
90
91 1
            chai.request(server)
92
                .post("/v2/auth/register")
93
                .send(user)
94
                .end((err, res) => {
95 1
                    res.should.have.status(201);
96 1
                    res.body.should.be.an("object");
97 1
                    res.body.should.have.property("data");
98
99 1
                    done();
100
                });
101
        });
102
103 1
        it('should get 401 UNIQUE CONSTRAINT', (done) => {
104 1
            let user = {
105
                api_key: apiKey,
106
                email: "[email protected]",
107
                password: "testinginvoice"
108
            };
109
110 1
            chai.request(server)
111
                .post("/v2/auth/register")
112
                .send(user)
113
                .end((err, res) => {
114 1
                    res.should.have.status(500);
115 1
                    res.body.should.be.an("object");
116 1
                    res.body.errors.status.should.be.equal(500);
117
118 1
                    done();
119
                });
120
        });
121
122 1
        it('should get 200 HAPPY PATH logging in', (done) => {
123 1
            let user = {
124
                api_key: apiKey,
125
                email: "[email protected]",
126
                password: "testinginvoice"
127
            };
128
129 1
            chai.request(server)
130
                .post("/v2/auth/login")
131
                .send(user)
132
                .end((err, res) => {
133 1
                    res.should.have.status(200);
134 1
                    res.body.should.be.an("object");
135 1
                    res.body.should.have.property("data");
136 1
                    res.body.data.should.have.property("type");
137 1
                    res.body.data.type.should.equal("success");
138 1
                    res.body.data.should.have.property("token");
139
140 1
                    token = res.body.data.token;
141
142 1
                    done();
143
                });
144
        });
145
146 1
        it('should get 200 HAPPY PATH getting no invoices', (done) => {
147 1
            chai.request(server)
148
                .get("/v2/invoices?api_key=" + apiKey)
149
                .set("x-access-token", token)
150
                .end((err, res) => {
151 1
                    res.should.have.status(200);
152 1
                    res.body.should.be.an("object");
153 1
                    res.body.data.should.be.an("array");
154 1
                    res.body.data.length.should.be.equal(0);
155
156 1
                    done();
157
                });
158
        });
159
    });
160
161 1
    describe('POST /invoice', () => {
162 1
        it('should get 201 HAPPY PATH creating order', (done) => {
163 1
            let order = {
164
                name: "Anders",
165
                api_key: apiKey
166
            };
167
168 1
            chai.request(server)
169
                .post("/v2/orders")
170
                .send(order)
171
                .end((err, res) => {
172 1
                    res.should.have.status(201);
173 1
                    res.body.should.be.an("object");
174 1
                    res.body.should.have.property("data");
175
176 1
                    done();
177
                });
178
        });
179
180 1
        it('should get 500 as we do not supply order_id', (done) => {
181 1
            let invoice = {
182
                // order_id: 1,
183
                total_price: 100,
184
                api_key: apiKey
185
            };
186
187 1
            chai.request(server)
188
                .post("/v2/invoices")
189
                .set("x-access-token", token)
190
                .send(invoice)
191
                .end((err, res) => {
192 1
                    res.should.have.status(500);
193 1
                    res.body.should.be.an("object");
194 1
                    res.body.should.have.property("errors");
195 1
                    res.body.errors.should.have.property("status");
196 1
                    res.body.errors.status.should.be.equal(500);
197 1
                    res.body.errors.should.have.property("detail");
198
199 1
                    done();
200
                });
201
        });
202
203 1
        it('should get 500 as we do not supply total_price', (done) => {
204 1
            let invoice = {
205
                order_id: 1,
206
                // total_price: 100,
207
                api_key: apiKey
208
            };
209
210 1
            chai.request(server)
211
                .post("/v2/invoices")
212
                .set("x-access-token", token)
213
                .send(invoice)
214
                .end((err, res) => {
215 1
                    res.should.have.status(500);
216 1
                    res.body.should.be.an("object");
217 1
                    res.body.should.have.property("errors");
218 1
                    res.body.errors.should.have.property("status");
219 1
                    res.body.errors.status.should.be.equal(500);
220 1
                    res.body.errors.should.have.property("detail");
221
222 1
                    done();
223
                });
224
        });
225
226 1
        it('should get 401 not providing token', (done) => {
227 1
            let invoice = {
228
                order_id: 1,
229
                total_price: 100,
230
                api_key: apiKey
231
            };
232
233 1
            chai.request(server)
234
                .post("/v2/invoices")
235
                .send(invoice)
236
                .end((err, res) => {
237 1
                    res.should.have.status(401);
238 1
                    res.body.should.be.an("object");
239 1
                    res.body.errors.status.should.be.equal(401);
240
241 1
                    done();
242
                });
243
        });
244
245 1
        it('should get 201 HAPPY PATH', (done) => {
246 1
            let invoice = {
247
                order_id: 1,
248
                total_price: 100,
249
                api_key: apiKey
250
            };
251
252 1
            chai.request(server)
253
                .post("/v2/invoices")
254
                .set("x-access-token", token)
255
                .send(invoice)
256
                .end((err, res) => {
257 1
                    res.should.have.status(201);
258 1
                    res.body.should.be.an("object");
259 1
                    res.body.should.have.property("data");
260
261 1
                    done();
262
                });
263
        });
264
265 1
        it('should get 200 HAPPY PATH getting the one invoice we just created', (done) => {
266 1
            chai.request(server)
267
                .get("/v2/invoices?api_key=" + apiKey)
268
                .set("x-access-token", token)
269
                .end((err, res) => {
270 1
                    res.should.have.status(200);
271 1
                    res.body.should.be.an("object");
272 1
                    res.body.data.should.be.an("array");
273 1
                    res.body.data.length.should.be.equal(1);
274
275 1
                    done();
276
                });
277
        });
278
279 1
        it('should get 201 Creating invoice with creation_date', (done) => {
280 1
            let invoice = {
281
                order_id: 1,
282
                total_price: 100,
283
                creation_date: "2019-02-13",
284
                api_key: apiKey
285
            };
286
287 1
            chai.request(server)
288
                .post("/v2/invoices")
289
                .set("x-access-token", token)
290
                .send(invoice)
291
                .end((err, res) => {
292 1
                    res.should.have.status(201);
293 1
                    res.body.should.be.an("object");
294 1
                    res.body.should.have.property("data");
295 1
                    res.body.data.should.have.property("creation_date");
296 1
                    res.body.data.creation_date.should.equal("2019-02-13");
297
298 1
                    done();
299
                });
300
        });
301
302 1
        it('should get 201 Creating invoice with due_date', (done) => {
303 1
            let invoice = {
304
                order_id: 1,
305
                total_price: 100,
306
                due_date: "2019-03-13",
307
                api_key: apiKey
308
            };
309
310 1
            chai.request(server)
311
                .post("/v2/invoices")
312
                .set("x-access-token", token)
313
                .send(invoice)
314
                .end((err, res) => {
315 1
                    res.should.have.status(201);
316 1
                    res.body.should.be.an("object");
317 1
                    res.body.should.have.property("data");
318 1
                    res.body.data.should.have.property("due_date");
319 1
                    res.body.data.due_date.should.equal("2019-03-13");
320
321 1
                    done();
322
                });
323
        });
324
325 1
        it('should get 201 Creating invoice with creation_date and due_date', (done) => {
326 1
            let invoice = {
327
                order_id: 1,
328
                total_price: 100,
329
                creation_date: "2019-02-13",
330
                due_date: "2019-03-13",
331
                api_key: apiKey
332
            };
333
334 1
            chai.request(server)
335
                .post("/v2/invoices")
336
                .set("x-access-token", token)
337
                .send(invoice)
338
                .end((err, res) => {
339 1
                    res.should.have.status(201);
340 1
                    res.body.should.be.an("object");
341 1
                    res.body.should.have.property("data");
342 1
                    res.body.data.should.have.property("creation_date");
343 1
                    res.body.data.creation_date.should.equal("2019-02-13");
344 1
                    res.body.data.should.have.property("due_date");
345 1
                    res.body.data.due_date.should.equal("2019-03-13");
346
347 1
                    done();
348
                });
349
        });
350
    });
351
352 1
    describe('GET /invoice', () => {
353 1
        it('should get 400 string id supplied', (done) => {
354 1
            chai.request(server)
355
                .get("/v2/invoices/test?api_key=" + apiKey)
356
                .set("x-access-token", token)
357
                .end((err, res) => {
358 1
                    res.should.have.status(400);
359
360 1
                    done();
361
                });
362
        });
363
364 1
        it('should get 401 not providing token', (done) => {
365 1
            chai.request(server)
366
                .get("/v2/invoices/1?api_key=" + apiKey)
367
                .end((err, res) => {
368 1
                    res.should.have.status(401);
369 1
                    res.body.should.be.an("object");
370 1
                    res.body.errors.status.should.be.equal(401);
371
372 1
                    done();
373
                });
374
        });
375
376 1
        it('should get 200 HAPPY PATH', (done) => {
377 1
            chai.request(server)
378
                .get("/v2/invoices/1?api_key=" + apiKey)
379
                .set("x-access-token", token)
380
                .end((err, res) => {
381 1
                    res.should.have.status(200);
382 1
                    res.body.should.be.an("object");
383 1
                    res.body.data.should.be.an("object");
384 1
                    res.body.data.should.have.property("id");
385 1
                    res.body.data.id.should.be.equal(1);
386
387 1
                    done();
388
                });
389
        });
390
391 1
        it('should get 200, but empty data object', (done) => {
392 1
            chai.request(server)
393
                .get("/v2/invoices/99?api_key=" + apiKey)
394
                .set("x-access-token", token)
395
                .end((err, res) => {
396 1
                    res.should.have.status(200);
397 1
                    res.body.should.be.eql({});
398
399 1
                    done();
400
                });
401
        });
402
    });
403
404 1
    describe('PUT /invoice', () => {
405 1
        it('should get 201 HAPPY PATH creating invoice', (done) => {
406 1
            let invoice = {
407
                order_id: 1,
408
                total_price: 100,
409
                creation_date: "2019-02-13",
410
                due_date: "2019-03-13",
411
                api_key: apiKey
412
            };
413
414 1
            chai.request(server)
415
                .post("/v2/invoices")
416
                .set("x-access-token", token)
417
                .send(invoice)
418
                .end((err, res) => {
419 1
                    res.should.have.status(201);
420 1
                    res.body.should.be.an("object");
421 1
                    res.body.should.have.property("data");
422
423 1
                    done();
424
                });
425
        });
426
427 1
        it('should get 204 HAPPY PATH changing total price, adding dates', (done) => {
428 1
            let invoice = {
429
                id: 1,
430
                order_id: 1,
431
                total_price: 200,
432
                creation_date: "2019-02-13",
433
                due_date: "2019-03-13",
434
                api_key: apiKey
435
            };
436
437 1
            chai.request(server)
438
                .put("/v2/invoices")
439
                .set("x-access-token", token)
440
                .send(invoice)
441
                .end((err, res) => {
442 1
                    res.should.have.status(204);
443
444 1
                    done();
445
                });
446
        });
447
448 1
        it('should get 200 HAPPY PATH', (done) => {
449 1
            chai.request(server)
450
                .get("/v2/invoices/1?api_key=" + apiKey)
451
                .set("x-access-token", token)
452
                .end((err, res) => {
453 1
                    res.should.have.status(200);
454 1
                    res.body.should.be.an("object");
455 1
                    res.body.data.should.be.an("object");
456 1
                    res.body.data.should.have.property("id");
457 1
                    res.body.data.id.should.be.equal(1);
458
459 1
                    res.body.data.should.have.property("total_price");
460 1
                    res.body.data.total_price.should.be.equal(200);
461
462 1
                    res.body.data.should.have.property("due_date");
463 1
                    res.body.data.due_date.should.be.equal("2019-03-13");
464
465 1
                    res.body.data.should.have.property("creation_date");
466 1
                    res.body.data.creation_date.should.be.equal("2019-02-13");
467
468 1
                    done();
469
                });
470
        });
471
    });
472
});
473