Code Duplication    Length = 371-393 lines in 2 locations

v2/test/invoices.js 1 location

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

v1/test/invoices.js 1 location

@@ 3-373 (lines=371) @@
1
/* global it describe before */
2
3
process.env.NODE_ENV = 'test';
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);
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