Passed
Push — master ( e72244...71251c )
by Emil
04:02
created

v2/test/copier.js   C

Complexity

Total Complexity 53
Complexity/F 1.13

Size

Lines of Code 426
Function Count 47

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 96.02%

Importance

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