v2/test/copier.js   F
last analyzed

Complexity

Total Complexity 60
Complexity/F 1.13

Size

Lines of Code 471
Function Count 53

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 96.35%

Importance

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