v1/test/deliveries.js   A
last analyzed

Complexity

Total Complexity 26
Complexity/F 1.04

Size

Lines of Code 227
Function Count 25

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 99%

Importance

Changes 0
Metric Value
wmc 26
eloc 158
mnd 1
bc 1
fnc 25
dl 0
loc 227
ccs 99
cts 100
cp 0.99
rs 10
bpm 0.04
cpm 1.04
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 server = require('../../app.js');
9
10 1
chai.should();
11
12 1
const db = require("../db/database.js");
13
14 1
chai.use(chaiHttp);
15
16 1
let apiKey = "";
17
18 1
describe('deliveries', () => {
19 1
    before(() => {
20 1
        db.run("DELETE FROM deliveries", (err) => {
21 2
            if (err) {
22
                console.log("Could not empty test DB table deliveries", err.message);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
23
            }
24
        });
25
    });
26
27 1
    describe('GET /deliveries', () => {
28 1
        it('should get 401 as we do not provide valid api_key', (done) => {
29 1
            chai.request(server)
30
                .get("/deliveries")
31
                .end((err, res) => {
32 1
                    res.should.have.status(401);
33 1
                    res.body.should.be.an("object");
34 1
                    res.body.errors.status.should.be.equal(401);
35 1
                    done();
36
                });
37
        });
38
39 1
        it('should get 200 HAPPY PATH FROM GETTING API KEY', (done) => {
40 1
            chai.request(server)
41
                .get("/[email protected]")
42
                .end((err, res) => {
43 1
                    res.should.have.status(200);
44 1
                    res.body.should.be.an("object");
45 1
                    res.body.data.should.be.an("object");
46 1
                    res.body.data.should.have.property("key");
47
48 1
                    apiKey = res.body.data.key;
49
50 1
                    done();
51
                });
52
        });
53
54 1
        it('should get 200 HAPPY PATH getting no deliveries', (done) => {
55 1
            chai.request(server)
56
                .get("/deliveries?api_key=" + apiKey)
57
                .end((err, res) => {
58 1
                    res.should.have.status(200);
59 1
                    res.body.should.be.an("object");
60 1
                    res.body.data.should.be.an("array");
61 1
                    res.body.data.length.should.be.equal(0);
62
63 1
                    done();
64
                });
65
        });
66
    });
67
68 1
    describe('POST /delivery', () => {
69 1
        it('should get 500 as we do not supply id', (done) => {
70 1
            let delivery = {
71
                // id: 1,
72
                product_id: 1,
73
                amount: 10,
74
                delivery_date: "2018-02-01",
75
                comment: "Leverans av skruvar",
76
                api_key: apiKey
77
            };
78
79 1
            chai.request(server)
80
                .post("/delivery")
81
                .send(delivery)
82
                .end((err, res) => {
83 1
                    res.should.have.status(500);
84 1
                    res.body.should.be.an("object");
85 1
                    res.body.should.have.property("errors");
86 1
                    res.body.errors.should.have.property("status");
87 1
                    res.body.errors.status.should.be.equal(500);
88 1
                    res.body.errors.should.have.property("detail");
89
90 1
                    done();
91
                });
92
        });
93
94 1
        it('should get 500 as we do not supply productId', (done) => {
95 1
            let delivery = {
96
                id: 1,
97
                // product_id: 1,
98
                amount: 10,
99
                delivery_date: "2018-02-01",
100
                comment: "Leverans av skruvar",
101
                api_key: apiKey
102
            };
103
104 1
            chai.request(server)
105
                .post("/delivery")
106
                .send(delivery)
107
                .end((err, res) => {
108 1
                    res.should.have.status(500);
109 1
                    res.body.should.be.an("object");
110 1
                    res.body.should.have.property("errors");
111 1
                    res.body.errors.should.have.property("status");
112 1
                    res.body.errors.status.should.be.equal(500);
113 1
                    res.body.errors.should.have.property("detail");
114
115 1
                    done();
116
                });
117
        });
118
119 1
        it('should get 500 as we do not supply amount', (done) => {
120 1
            let delivery = {
121
                id: 1,
122
                product_id: 1,
123
                // amount: 10,
124
                delivery_date: "2018-02-01",
125
                comment: "Leverans av skruvar",
126
                api_key: apiKey
127
            };
128
129 1
            chai.request(server)
130
                .post("/delivery")
131
                .send(delivery)
132
                .end((err, res) => {
133 1
                    res.should.have.status(500);
134 1
                    res.body.should.be.an("object");
135 1
                    res.body.should.have.property("errors");
136 1
                    res.body.errors.should.have.property("status");
137 1
                    res.body.errors.status.should.be.equal(500);
138 1
                    res.body.errors.should.have.property("detail");
139
140 1
                    done();
141
                });
142
        });
143
144 1
        it('should get 500 as we do not supply delivery_date', (done) => {
145 1
            let delivery = {
146
                id: 1,
147
                product_id: 1,
148
                amount: 10,
149
                // delivery_date: "2018-02-01",
150
                comment: "Leverans av skruvar",
151
                api_key: apiKey
152
            };
153
154 1
            chai.request(server)
155
                .post("/delivery")
156
                .send(delivery)
157
                .end((err, res) => {
158 1
                    res.should.have.status(500);
159 1
                    res.body.should.be.an("object");
160 1
                    res.body.should.have.property("errors");
161 1
                    res.body.errors.should.have.property("status");
162 1
                    res.body.errors.status.should.be.equal(500);
163 1
                    res.body.errors.should.have.property("detail");
164
165 1
                    done();
166
                });
167
        });
168
169 1
        it('should get 201 HAPPY PATH', (done) => {
170 1
            let delivery = {
171
                id: 1,
172
                product_id: 1,
173
                amount: 10,
174
                delivery_date: "2018-02-01",
175
                comment: "Leverans av skruvar",
176
                api_key: apiKey
177
            };
178
179 1
            chai.request(server)
180
                .post("/delivery")
181
                .send(delivery)
182
                .end((err, res) => {
183 1
                    res.should.have.status(201);
184 1
                    res.body.should.be.an("object");
185 1
                    res.body.should.have.property("data");
186
187 1
                    done();
188
                });
189
        });
190
191 1
        it('should get 200 HAPPY PATH getting the one product we just created', (done) => {
192 1
            chai.request(server)
193
                .get("/deliveries?api_key=" + apiKey)
194
                .end((err, res) => {
195 1
                    res.should.have.status(200);
196 1
                    res.body.should.be.an("object");
197 1
                    res.body.data.should.be.an("array");
198 1
                    res.body.data.length.should.be.equal(1);
199
200 1
                    done();
201
                });
202
        });
203
204 1
        it('should get 500 UNIQUE CONSTRAINT', (done) => {
205 1
            let delivery = {
206
                id: 1,
207
                product_id: 1,
208
                amount: 10,
209
                delivery_date: "2018-02-01",
210
                comment: "Leverans av skruvar",
211
                api_key: apiKey
212
            };
213
214 1
            chai.request(server)
215
                .post("/delivery")
216
                .send(delivery)
217
                .end((err, res) => {
218 1
                    res.should.have.status(500);
219 1
                    res.body.should.be.an("object");
220 1
                    res.body.should.have.property("errors");
221 1
                    res.body.errors.should.have.property("status");
222 1
                    res.body.errors.status.should.be.equal(500);
223 1
                    res.body.errors.should.have.property("detail");
224
225 1
                    done();
226
                });
227
        });
228
    });
229
});
230