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

invoices.js ➔ ???   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 355
Code Lines 245

Duplication

Lines 355
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 245
nc 1
nop 0
dl 355
loc 355
rs 7
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B invoices.js ➔ ... ➔ ??? 7 7 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
chai.use(chaiHttp);
15
16
let apiKey = "";
17
let token = "";
18
19
describe('invoices', () => {
20
    before(() => {
21
        db.run("DELETE FROM invoices", (err) => {
22
            if (err) {
23
                console.log("Could not empty test DB table products", 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...
24
            }
25
        });
26
    });
27
28
    describe('GET /invoices', () => {
29
        it('should get 401 as we do not provide valid api_key', (done) => {
30
            chai.request(server)
31
                .get("/v1/invoices")
32
                .end((err, res) => {
33
                    res.should.have.status(401);
34
                    res.body.should.be.an("object");
35
                    res.body.errors.status.should.be.equal(401);
36
                    done();
37
                });
38
        });
39
40
        it('should get 200 HAPPY PATH FROM GETTING API KEY', (done) => {
41
            chai.request(server)
42
                .get("/v1/[email protected]")
43
                .end((err, res) => {
44
                    res.should.have.status(200);
45
                    res.body.should.be.an("object");
46
                    res.body.data.should.be.an("object");
47
                    res.body.data.should.have.property("key");
48
49
                    apiKey = res.body.data.key;
50
51
                    done();
52
                });
53
        });
54
55
        it('should get 401 as we have not logged in', (done) => {
56
            chai.request(server)
57
                .get("/v1/invoices?api_key=" + apiKey)
58
                .end((err, res) => {
59
                    res.should.have.status(401);
60
                    res.body.should.be.an("object");
61
                    res.body.errors.status.should.be.equal(401);
62
                    done();
63
                });
64
        });
65
66
        it('should get 201 HAPPY PATH registering', (done) => {
67
            let user = {
68
                api_key: apiKey,
69
                email: "[email protected]",
70
                password: "testinginvoice"
71
            };
72
73
            chai.request(server)
74
                .post("/v1/register")
75
                .send(user)
76
                .end((err, res) => {
77
                    res.should.have.status(201);
78
                    res.body.should.be.an("object");
79
                    res.body.should.have.property("data");
80
81
                    done();
82
                });
83
        });
84
85
        it('should get 401 UNIQUE CONSTRAINT', (done) => {
86
            let user = {
87
                api_key: apiKey,
88
                email: "[email protected]",
89
                password: "testinginvoice"
90
            };
91
92
            chai.request(server)
93
                .post("/v1/register")
94
                .send(user)
95
                .end((err, res) => {
96
                    res.should.have.status(500);
97
                    res.body.should.be.an("object");
98
                    res.body.errors.status.should.be.equal(500);
99
100
                    done();
101
                });
102
        });
103
104
        it('should get 200 HAPPY PATH logging in', (done) => {
105
            let user = {
106
                api_key: apiKey,
107
                email: "[email protected]",
108
                password: "testinginvoice"
109
            };
110
111
            chai.request(server)
112
                .post("/v1/login")
113
                .send(user)
114
                .end((err, res) => {
115
                    res.should.have.status(200);
116
                    res.body.should.be.an("object");
117
                    res.body.should.have.property("data");
118
                    res.body.data.should.have.property("type");
119
                    res.body.data.type.should.equal("success");
120
                    res.body.data.should.have.property("token");
121
122
                    token = res.body.data.token;
123
124
                    done();
125
                });
126
        });
127
128
        it('should get 200 HAPPY PATH getting no invoices', (done) => {
129
            chai.request(server)
130
                .get("/v1/invoices?api_key=" + apiKey)
131
                .set("x-access-token", token)
132
                .end((err, res) => {
133
                    res.should.have.status(200);
134
                    res.body.should.be.an("object");
135
                    res.body.data.should.be.an("array");
136
                    res.body.data.length.should.be.equal(0);
137
138
                    done();
139
                });
140
        });
141
    });
142
143
    describe('POST /invoice', () => {
144
        it('should get 201 HAPPY PATH creating order', (done) => {
145
            let order = {
146
                id: 1,
147
                name: "Anders",
148
                api_key: apiKey
149
            };
150
151
            chai.request(server)
152
                .post("/v1/order")
153
                .send(order)
154
                .end((err, res) => {
155
                    res.should.have.status(201);
156
                    res.body.should.be.an("object");
157
                    res.body.should.have.property("data");
158
159
                    done();
160
                });
161
        });
162
163
        it('should get 500 as we do not supply id', (done) => {
164
            let invoice = {
165
                // id: 1,
166
                order_id: 1,
167
                total_price: 100,
168
                api_key: apiKey
169
            };
170
171
            chai.request(server)
172
                .post("/v1/invoice")
173
                .set("x-access-token", token)
174
                .send(invoice)
175
                .end((err, res) => {
176
                    res.should.have.status(500);
177
                    res.body.should.be.an("object");
178
                    res.body.should.have.property("errors");
179
                    res.body.errors.should.have.property("status");
180
                    res.body.errors.status.should.be.equal(500);
181
                    res.body.errors.should.have.property("detail");
182
183
                    done();
184
                });
185
        });
186
187
        it('should get 500 as we do not supply order_id', (done) => {
188
            let invoice = {
189
                id: 1,
190
                // order_id: 1,
191
                total_price: 100,
192
                api_key: apiKey
193
            };
194
195
            chai.request(server)
196
                .post("/v1/invoice")
197
                .set("x-access-token", token)
198
                .send(invoice)
199
                .end((err, res) => {
200
                    res.should.have.status(500);
201
                    res.body.should.be.an("object");
202
                    res.body.should.have.property("errors");
203
                    res.body.errors.should.have.property("status");
204
                    res.body.errors.status.should.be.equal(500);
205
                    res.body.errors.should.have.property("detail");
206
207
                    done();
208
                });
209
        });
210
211
        it('should get 500 as we do not supply total_price', (done) => {
212
            let invoice = {
213
                id: 1,
214
                order_id: 1,
215
                // total_price: 100,
216
                api_key: apiKey
217
            };
218
219
            chai.request(server)
220
                .post("/v1/invoice")
221
                .set("x-access-token", token)
222
                .send(invoice)
223
                .end((err, res) => {
224
                    res.should.have.status(500);
225
                    res.body.should.be.an("object");
226
                    res.body.should.have.property("errors");
227
                    res.body.errors.should.have.property("status");
228
                    res.body.errors.status.should.be.equal(500);
229
                    res.body.errors.should.have.property("detail");
230
231
                    done();
232
                });
233
        });
234
235
        it('should get 401 not providing token', (done) => {
236
            let invoice = {
237
                id: 1,
238
                order_id: 1,
239
                total_price: 100,
240
                api_key: apiKey
241
            };
242
243
            chai.request(server)
244
                .post("/v1/invoice")
245
                .send(invoice)
246
                .end((err, res) => {
247
                    res.should.have.status(401);
248
                    res.body.should.be.an("object");
249
                    res.body.errors.status.should.be.equal(401);
250
251
                    done();
252
                });
253
        });
254
255
        it('should get 201 HAPPY PATH', (done) => {
256
            let invoice = {
257
                id: 1,
258
                order_id: 1,
259
                total_price: 100,
260
                api_key: apiKey
261
            };
262
263
            chai.request(server)
264
                .post("/v1/invoice")
265
                .set("x-access-token", token)
266
                .send(invoice)
267
                .end((err, res) => {
268
                    res.should.have.status(201);
269
                    res.body.should.be.an("object");
270
                    res.body.should.have.property("data");
271
272
                    done();
273
                });
274
        });
275
276
        it('should get 200 HAPPY PATH getting the one invoice we just created', (done) => {
277
            chai.request(server)
278
                .get("/v1/invoices?api_key=" + apiKey)
279
                .set("x-access-token", token)
280
                .end((err, res) => {
281
                    res.should.have.status(200);
282
                    res.body.should.be.an("object");
283
                    res.body.data.should.be.an("array");
284
                    res.body.data.length.should.be.equal(1);
285
286
                    done();
287
                });
288
        });
289
290
        it('should get 500 UNIQUE CONSTRAINT', (done) => {
291
            let invoice = {
292
                id: 1,
293
                order_id: 1,
294
                total_price: 100,
295
                api_key: apiKey
296
            };
297
298
            chai.request(server)
299
                .post("/v1/invoice")
300
                .set("x-access-token", token)
301
                .send(invoice)
302
                .end((err, res) => {
303
                    res.should.have.status(500);
304
                    res.body.should.be.an("object");
305
                    res.body.errors.status.should.be.equal(500);
306
307
                    done();
308
                });
309
        });
310
    });
311
312
    describe('GET /invoice', () => {
313
        it('should get 404 no id supplied', (done) => {
314
            chai.request(server)
315
                .get("/v1/invoice?api_key=" + apiKey)
316
                .end((err, res) => {
317
                    res.should.have.status(404);
318
319
                    done();
320
                });
321
        });
322
323
        it('should get 400 string id supplied', (done) => {
324
            chai.request(server)
325
                .get("/v1/invoice/test?api_key=" + apiKey)
326
                .set("x-access-token", token)
327
                .end((err, res) => {
328
                    res.should.have.status(400);
329
330
                    done();
331
                });
332
        });
333
334
        it('should get 401 not providing token', (done) => {
335
            chai.request(server)
336
                .get("/v1/invoice/1?api_key=" + apiKey)
337
                .end((err, res) => {
338
                    res.should.have.status(401);
339
                    res.body.should.be.an("object");
340
                    res.body.errors.status.should.be.equal(401);
341
342
                    done();
343
                });
344
        });
345
346
        it('should get 200 HAPPY PATH', (done) => {
347
            chai.request(server)
348
                .get("/v1/invoice/1?api_key=" + apiKey)
349
                .set("x-access-token", token)
350
                .end((err, res) => {
351
                    res.should.have.status(200);
352
                    res.body.should.be.an("object");
353
                    res.body.data.should.be.an("object");
354
                    res.body.data.should.have.property("id");
355
                    res.body.data.id.should.be.equal(1);
356
357
                    done();
358
                });
359
        });
360
361
        it('should get 200, but empty data object', (done) => {
362
            chai.request(server)
363
                .get("/v1/invoice/2?api_key=" + apiKey)
364
                .set("x-access-token", token)
365
                .end((err, res) => {
366
                    res.should.have.status(200);
367
                    res.body.should.be.eql({});
368
369
                    done();
370
                });
371
        });
372
    });
373
});
374