Test Failed
Push — master ( 59db98...3c8e23 )
by Emil
05:37 queued 01:35
created

v2/test/copier.js   A

Complexity

Total Complexity 25
Complexity/F 1.32

Size

Lines of Code 142
Function Count 19

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 25
eloc 86
mnd 6
bc 6
fnc 19
dl 0
loc 142
ccs 60
cts 68
cp 0.8824
rs 10
bpm 0.3157
cpm 1.3157
noi 1
c 0
b 0
f 0
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;
0 ignored issues
show
Unused Code introduced by
The variable token seems to be never used. Consider removing it.
Loading history...
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