Passed
Push — master ( 6f3ec2...7ccabd )
by Emil
02:59
created

v2/test/copier.js   A

Complexity

Total Complexity 29
Complexity/F 1.21

Size

Lines of Code 171
Function Count 24

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 91.36%

Importance

Changes 0
Metric Value
cc 0
eloc 106
nc 1
dl 0
loc 171
ccs 74
cts 81
cp 0.9136
crap 0
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 ➔ ??? 0 152 1
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 1
    before(() => {
24 1
        return new Promise((resolve) => {
25 1
            db.run("DELETE FROM products", (err) => {
26 2
                if (err) {
27
                    console.error("Could not empty test DB table orders", err.message);
28
                }
29
30 1
                db.run("DELETE FROM orders", (err) => {
31 2
                    if (err) {
32
                        console.error("Could not empty test DB table orders", err.message);
33
                    }
34
35 1
                    db.run("DELETE FROM order_items", (err) => {
36 2
                        if (err) {
37
                            console.error("Could not empty test DB table orders", err.message);
38
                        }
39
40 1
                        exec(
41
                            'cat v2/db/seed.sql | sqlite3 v2/db/test.sqlite',
42
                            (error, stdout, stderr) => {
43 2
                                if (error) {
44
                                    console.error(error.message);
45
                                    return;
46
                                }
47
48 2
                                if (stderr) {
49
                                    console.error(stderr);
50
                                    return;
51
                                }
52
53 1
                                resolve();
54
                            });
55
                    });
56
                });
57
            });
58
        });
59
    });
60
61 1
    describe('POST /copier/products', () => {
62 1
        it('should get 401 as we do not provide valid api_key', (done) => {
63 1
            chai.request(server)
64
                .post("/v2/copier/products")
65
                .end((err, res) => {
66 1
                    res.should.have.status(401);
67 1
                    res.body.should.be.an("object");
68 1
                    res.body.errors.status.should.be.equal(401);
69
70 1
                    done();
71
                });
72
        });
73
74 1
        it('should get 200 as we get apiKey', (done) => {
75 1
            let user = {
76
                email: "[email protected]",
77
                gdpr: "gdpr"
78
            };
79
80 1
            chai.request(server)
81
                .post("/v2/auth/api_key/confirmation")
82
                .send(user)
83
                .end((err, res) => {
84 1
                    res.should.have.status(200);
85
86 1
                    let HTMLResponse = HTMLParser.parse(res.text);
87 1
                    let apiKeyElement = HTMLResponse.querySelector('#apikey');
88
89 1
                    apiKey = apiKeyElement.childNodes[0].rawText;
90
91 1
                    done();
92
                });
93
        });
94
95 1
        it('should get 201 HAPPY PATH, 10 products should have been created', (done) => {
96 1
            chai.request(server)
97
                .post("/v2/copier/products")
98
                .send({ api_key: apiKey })
99
                .end((err, res) => {
100 1
                    res.should.have.status(201);
101 1
                    res.body.should.be.an("object");
102 1
                    res.body.data.should.be.an("array");
103 1
                    res.body.data.length.should.equal(10);
104
105 1
                    done();
106
                });
107
        });
108
    });
109
110 1
    describe('POST /copy_orders', () => {
111 1
        it('should get 201 HAPPY PATH, 4 orders should have been created', (done) => {
112 1
            chai.request(server)
113
                .post("/v2/copier/orders")
114
                .send({ api_key: apiKey })
115
                .end((err, res) => {
116 1
                    res.should.have.status(201);
117 1
                    res.body.should.be.an("object");
118 1
                    res.body.data.should.be.an("array");
119 1
                    res.body.data.length.should.equal(4);
120
121 1
                    done();
122
                });
123
        });
124
    });
125
126 1
    describe("POST /copy_all", () => {
127 1
        it('should get 401 as we do not provide valid api_key', (done) => {
128 1
            chai.request(server)
129
                .post("/v2/copier/all")
130
                .end((err, res) => {
131 1
                    res.should.have.status(401);
132 1
                    res.body.should.be.an("object");
133 1
                    res.body.errors.status.should.be.equal(401);
134
135 1
                    done();
136
                });
137
        });
138
139 1
        it('should get 200 as we get apiKey', (done) => {
140 1
            let user = {
141
                email: "[email protected]",
142
                gdpr: "gdpr"
143
            };
144
145 1
            chai.request(server)
146
                .post("/v2/auth/api_key/confirmation")
147
                .send(user)
148
                .end((err, res) => {
149 1
                    res.should.have.status(200);
150
151 1
                    let HTMLResponse = HTMLParser.parse(res.text);
152 1
                    let apiKeyElement = HTMLResponse.querySelector('#apikey');
153
154 1
                    apiKey = apiKeyElement.childNodes[0].rawText;
155
156 1
                    done();
157
                });
158
        });
159
160 1
        it('should get 201 HAPPY PATH', (done) => {
161 1
            chai.request(server)
162
                .post("/v2/copier/all")
163
                .send({ api_key: apiKey })
164
                .end((err, res) => {
165 1
                    res.should.have.status(201);
166 1
                    res.body.should.be.an("object");
167 1
                    res.body.data.should.have.property("message");
168
169 1
                    done();
170
                });
171
        });
172
    });
173
});
174