Total Complexity | 50 |
Complexity/F | 1.04 |
Lines of Code | 393 |
Function Count | 48 |
Duplicated Lines | 393 |
Ratio | 100 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like v2/test/invoices.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* global it describe before */ |
||
3 | View Code Duplication | 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 |