Test Failed
Push — master ( 2f48cb...59db98 )
by Emil
03:56
created

v2/test/copier.js   D

Complexity

Total Complexity 59
Complexity/F 1.11

Size

Lines of Code 463
Function Count 53

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 96.3%

Importance

Changes 0
Metric Value
wmc 59
eloc 301
mnd 6
bc 6
fnc 53
dl 0
loc 463
ccs 208
cts 216
cp 0.963
rs 4.08
bpm 0.1132
cpm 1.1132
noi 0
c 0
b 0
f 0

How to fix   Complexity   

Complexity

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