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 |
|
const { exec } = require('child_process'); |
15
|
|
|
|
16
|
1 |
|
chai.use(chaiHttp); |
17
|
|
|
|
18
|
1 |
|
let apiKey = ""; |
19
|
|
|
|
20
|
1 |
|
describe('copier', () => { |
21
|
1 |
|
before(() => { |
22
|
1 |
|
return new Promise((resolve) => { |
23
|
1 |
|
db.run("DELETE FROM products", (err) => { |
24
|
2 |
|
if (err) { |
25
|
|
|
console.log("Could not empty test DB table orders", err.message); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
db.run("DELETE FROM orders", (err) => { |
29
|
2 |
|
if (err) { |
30
|
|
|
console.log("Could not empty test DB table orders", err.message); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
db.run("DELETE FROM order_items", (err) => { |
34
|
2 |
|
if (err) { |
35
|
|
|
console.log("Could not empty test DB table orders", err.message); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
exec( |
39
|
|
|
'cat v1/db/seed.sql | sqlite3 v1/db/test.sqlite', |
40
|
|
|
(error, stdout, stderr) => { |
41
|
2 |
|
if (error) { |
42
|
|
|
console.log(error.message); |
|
|
|
|
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
if (stderr) { |
47
|
|
|
console.log(stderr); |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
resolve(); |
52
|
|
|
}); |
53
|
|
|
}); |
54
|
|
|
}); |
55
|
|
|
}); |
56
|
|
|
}); |
57
|
|
|
}); |
58
|
|
|
|
59
|
1 |
|
describe('POST /copy_products', () => { |
60
|
1 |
|
it('should get 401 as we do not provide valid api_key', (done) => { |
61
|
1 |
|
chai.request(server) |
62
|
|
|
.post("/copy_products") |
63
|
|
|
.end((err, res) => { |
64
|
1 |
|
res.should.have.status(401); |
65
|
1 |
|
res.body.should.be.an("object"); |
66
|
1 |
|
res.body.errors.status.should.be.equal(401); |
67
|
|
|
|
68
|
1 |
|
done(); |
69
|
|
|
}); |
70
|
|
|
}); |
71
|
|
|
|
72
|
1 |
|
it('should get 200 HAPPY PATH FROM GETTING API KEY', (done) => { |
73
|
1 |
|
chai.request(server) |
74
|
|
|
.get("/[email protected]") |
75
|
|
|
.end((err, res) => { |
76
|
1 |
|
res.should.have.status(200); |
77
|
1 |
|
res.body.should.be.an("object"); |
78
|
1 |
|
res.body.data.should.be.an("object"); |
79
|
1 |
|
res.body.data.should.have.property("key"); |
80
|
|
|
|
81
|
1 |
|
apiKey = res.body.data.key; |
82
|
|
|
|
83
|
1 |
|
done(); |
84
|
|
|
}); |
85
|
|
|
}); |
86
|
|
|
|
87
|
1 |
|
it('should get 201 HAPPY PATH, 10 products should have been created', (done) => { |
88
|
1 |
|
chai.request(server) |
89
|
|
|
.post("/copy_products") |
90
|
|
|
.send({ api_key: apiKey }) |
91
|
|
|
.end((err, res) => { |
92
|
1 |
|
res.should.have.status(201); |
93
|
1 |
|
res.body.should.be.an("object"); |
94
|
1 |
|
res.body.data.should.be.an("array"); |
95
|
1 |
|
res.body.data.length.should.equal(10); |
96
|
|
|
|
97
|
1 |
|
done(); |
98
|
|
|
}); |
99
|
|
|
}); |
100
|
|
|
}); |
101
|
|
|
|
102
|
1 |
|
describe('POST /copy_orders', () => { |
103
|
1 |
|
it('should get 201 HAPPY PATH, 4 orders should have been created', (done) => { |
104
|
1 |
|
chai.request(server) |
105
|
|
|
.post("/copy_orders") |
106
|
|
|
.send({ api_key: apiKey }) |
107
|
|
|
.end((err, res) => { |
108
|
1 |
|
res.should.have.status(201); |
109
|
1 |
|
res.body.should.be.an("object"); |
110
|
1 |
|
res.body.data.should.be.an("array"); |
111
|
1 |
|
res.body.data.length.should.equal(4); |
112
|
|
|
|
113
|
1 |
|
done(); |
114
|
|
|
}); |
115
|
|
|
}); |
116
|
|
|
}); |
117
|
|
|
|
118
|
1 |
|
describe("POST /copy_all", () => { |
119
|
1 |
|
it('should get 401 as we do not provide valid api_key', (done) => { |
120
|
1 |
|
chai.request(server) |
121
|
|
|
.post("/copy_all") |
122
|
|
|
.end((err, res) => { |
123
|
1 |
|
res.should.have.status(401); |
124
|
1 |
|
res.body.should.be.an("object"); |
125
|
1 |
|
res.body.errors.status.should.be.equal(401); |
126
|
|
|
|
127
|
1 |
|
done(); |
128
|
|
|
}); |
129
|
|
|
}); |
130
|
|
|
|
131
|
1 |
|
it('should get 200 HAPPY PATH FROM GETTING API KEY', (done) => { |
132
|
1 |
|
chai.request(server) |
133
|
|
|
.get("/[email protected]") |
134
|
|
|
.end((err, res) => { |
135
|
1 |
|
res.should.have.status(200); |
136
|
1 |
|
res.body.should.be.an("object"); |
137
|
1 |
|
res.body.data.should.be.an("object"); |
138
|
1 |
|
res.body.data.should.have.property("key"); |
139
|
|
|
|
140
|
1 |
|
apiKey = res.body.data.key; |
141
|
|
|
|
142
|
1 |
|
done(); |
143
|
|
|
}); |
144
|
|
|
}); |
145
|
|
|
|
146
|
1 |
|
it('should get 201 HAPPY PATH', (done) => { |
147
|
1 |
|
chai.request(server) |
148
|
|
|
.post("/copy_all") |
149
|
|
|
.send({ api_key: apiKey }) |
150
|
|
|
.end((err, res) => { |
151
|
1 |
|
res.should.have.status(201); |
152
|
1 |
|
res.body.should.be.an("object"); |
153
|
1 |
|
res.body.data.should.have.property("message"); |
154
|
|
|
|
155
|
1 |
|
done(); |
156
|
|
|
}); |
157
|
|
|
}); |
158
|
|
|
}); |
159
|
|
|
}); |
160
|
|
|
|