Test Failed
Push — master ( 853a48...344ee9 )
by Emil
02:45
created

v2/test/copier.js   A

Complexity

Total Complexity 29
Complexity/F 1.21

Size

Lines of Code 157
Function Count 24

Duplication

Duplicated Lines 157
Ratio 100 %

Importance

Changes 0
Metric Value
cc 0
eloc 99
nc 1
dl 157
loc 157
rs 10
c 0
b 0
f 0
wmc 29
mnd 1
bc 29
fnc 24
bpm 1.2083
cpm 1.2083
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B copier.js ➔ ??? 140 140 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
/* global it describe before */
2
3 View Code Duplication
process.env.NODE_ENV = 'test';
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4
5
//Require the dev-dependencies
6
const chai = require('chai');
7
const chaiHttp = require('chai-http');
8
const server = require('../../app.js');
9
10
chai.should();
11
12
const db = require("../db/database.js");
13
14
const { exec } = require('child_process');
15
16
chai.use(chaiHttp);
17
18
let apiKey = "";
19
20
describe('copier', () => {
21
    before(() => {
22
        return new Promise((resolve) => {
23
            db.run("DELETE FROM products", (err) => {
24
                if (err) {
25
                    console.error("Could not empty test DB table orders", err.message);
26
                }
27
28
                db.run("DELETE FROM orders", (err) => {
29
                    if (err) {
30
                        console.error("Could not empty test DB table orders", err.message);
31
                    }
32
33
                    db.run("DELETE FROM order_items", (err) => {
34
                        if (err) {
35
                            console.error("Could not empty test DB table orders", err.message);
36
                        }
37
38
                        exec(
39
                            'cat v2/db/seed.sql | sqlite3 v2/db/test.sqlite',
40
                            (error, stdout, stderr) => {
41
                                if (error) {
42
                                    console.error(error.message);
43
                                    return;
44
                                }
45
46
                                if (stderr) {
47
                                    console.error(stderr);
48
                                    return;
49
                                }
50
51
                                resolve();
52
                            });
53
                    });
54
                });
55
            });
56
        });
57
    });
58
59
    describe('POST /copier/products', () => {
60
        it('should get 401 as we do not provide valid api_key', (done) => {
61
            chai.request(server)
62
                .post("/v2/copier/products")
63
                .end((err, res) => {
64
                    res.should.have.status(401);
65
                    res.body.should.be.an("object");
66
                    res.body.errors.status.should.be.equal(401);
67
68
                    done();
69
                });
70
        });
71
72
        it('should get 200 HAPPY PATH FROM GETTING API KEY', (done) => {
73
            chai.request(server)
74
                .get("/v2/auth/[email protected]")
75
                .end((err, res) => {
76
                    res.should.have.status(200);
77
                    res.body.should.be.an("object");
78
                    res.body.data.should.be.an("object");
79
                    res.body.data.should.have.property("key");
80
81
                    apiKey = res.body.data.key;
82
83
                    done();
84
                });
85
        });
86
87
        it('should get 201 HAPPY PATH, 10 products should have been created', (done) => {
88
            chai.request(server)
89
                .post("/v2/copier/products")
90
                .send({ api_key: apiKey })
91
                .end((err, res) => {
92
                    res.should.have.status(201);
93
                    res.body.should.be.an("object");
94
                    res.body.data.should.be.an("array");
95
                    res.body.data.length.should.equal(10);
96
97
                    done();
98
                });
99
        });
100
    });
101
102
    describe('POST /copy_orders', () => {
103
        it('should get 201 HAPPY PATH, 4 orders should have been created', (done) => {
104
            chai.request(server)
105
                .post("/v2/copier/orders")
106
                .send({ api_key: apiKey })
107
                .end((err, res) => {
108
                    res.should.have.status(201);
109
                    res.body.should.be.an("object");
110
                    res.body.data.should.be.an("array");
111
                    res.body.data.length.should.equal(4);
112
113
                    done();
114
                });
115
        });
116
    });
117
118
    describe("POST /copy_all", () => {
119
        it('should get 401 as we do not provide valid api_key', (done) => {
120
            chai.request(server)
121
                .post("/v2/copier/all")
122
                .end((err, res) => {
123
                    res.should.have.status(401);
124
                    res.body.should.be.an("object");
125
                    res.body.errors.status.should.be.equal(401);
126
127
                    done();
128
                });
129
        });
130
131
        it('should get 200 HAPPY PATH FROM GETTING API KEY', (done) => {
132
            chai.request(server)
133
                .get("/v2/auth/[email protected]")
134
                .end((err, res) => {
135
                    res.should.have.status(200);
136
                    res.body.should.be.an("object");
137
                    res.body.data.should.be.an("object");
138
                    res.body.data.should.have.property("key");
139
140
                    apiKey = res.body.data.key;
141
142
                    done();
143
                });
144
        });
145
146
        it('should get 201 HAPPY PATH', (done) => {
147
            chai.request(server)
148
                .post("/v2/copier/all")
149
                .send({ api_key: apiKey })
150
                .end((err, res) => {
151
                    res.should.have.status(201);
152
                    res.body.should.be.an("object");
153
                    res.body.data.should.have.property("message");
154
155
                    done();
156
                });
157
        });
158
    });
159
});
160