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('auth', () => { |
19
|
1 |
|
before(() => { |
20
|
1 |
|
db.run("DELETE FROM apiKeys", (err) => { |
21
|
2 |
|
if (err) { |
22
|
|
|
console.log("Could not empty test DB apiKeys", err.message); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
}); |
25
|
|
|
|
26
|
1 |
|
db.run("DELETE FROM users", (err) => { |
27
|
2 |
|
if (err) { |
28
|
|
|
console.log("Could not empty test DB users", err.message); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
}); |
31
|
|
|
}); |
32
|
|
|
|
33
|
1 |
|
describe('GET /api_key', () => { |
34
|
1 |
|
it('should get 401 as we do not provide an email address', (done) => { |
35
|
1 |
|
chai.request(server) |
36
|
|
|
.get("/api_key") |
37
|
|
|
.end((err, res) => { |
38
|
1 |
|
res.should.have.status(401); |
39
|
1 |
|
res.body.should.be.a("object"); |
40
|
1 |
|
res.body.errors.status.should.be.eql(401); |
41
|
1 |
|
done(); |
42
|
|
|
}); |
43
|
|
|
}); |
44
|
|
|
|
45
|
1 |
|
it('should get 401 as we do not provide a valid email address', (done) => { |
46
|
1 |
|
chai.request(server) |
47
|
|
|
.get("/api_key?email=test") |
48
|
|
|
.end((err, res) => { |
49
|
1 |
|
res.should.have.status(401); |
50
|
1 |
|
res.body.should.be.a("object"); |
51
|
1 |
|
res.body.errors.status.should.be.eql(401); |
52
|
1 |
|
done(); |
53
|
|
|
}); |
54
|
|
|
}); |
55
|
|
|
|
56
|
1 |
|
it('should get 200 HAPPY PATH', (done) => { |
57
|
1 |
|
chai.request(server) |
58
|
|
|
.get("/[email protected]") |
59
|
|
|
.end((err, res) => { |
60
|
1 |
|
res.should.have.status(200); |
61
|
1 |
|
res.body.should.be.a("object"); |
62
|
1 |
|
res.body.data.should.be.a("object"); |
63
|
1 |
|
res.body.data.should.have.property("key"); |
64
|
|
|
|
65
|
1 |
|
apiKey = res.body.data.key; |
66
|
|
|
|
67
|
1 |
|
done(); |
68
|
|
|
}); |
69
|
|
|
}); |
70
|
|
|
|
71
|
1 |
|
it('should get 200 email already used', (done) => { |
72
|
1 |
|
chai.request(server) |
73
|
|
|
.get("/[email protected]") |
74
|
|
|
.end((err, res) => { |
75
|
1 |
|
res.should.have.status(200); |
76
|
1 |
|
res.body.should.be.a("object"); |
77
|
1 |
|
res.body.data.should.be.a("object"); |
78
|
1 |
|
res.body.data.should.have.property("apiKey"); |
79
|
1 |
|
res.body.data.should.have.property("message"); |
80
|
1 |
|
res.body.data.message.should.equal( |
81
|
|
|
"Email address already used for api key." |
82
|
|
|
); |
83
|
|
|
|
84
|
1 |
|
done(); |
85
|
|
|
}); |
86
|
|
|
}); |
87
|
|
|
}); |
88
|
|
|
|
89
|
1 |
|
describe('POST /register', () => { |
90
|
1 |
|
it('should get 401 as we do not provide valid api_key', (done) => { |
91
|
1 |
|
let user = { |
92
|
|
|
email: "[email protected]", |
93
|
|
|
password: "123test", |
94
|
|
|
// api_key: apiKey |
95
|
|
|
}; |
96
|
|
|
|
97
|
1 |
|
chai.request(server) |
98
|
|
|
.post("/register") |
99
|
|
|
.send(user) |
100
|
|
|
.end((err, res) => { |
101
|
1 |
|
res.should.have.status(401); |
102
|
1 |
|
res.body.should.be.an("object"); |
103
|
1 |
|
res.body.errors.status.should.be.equal(401); |
104
|
1 |
|
done(); |
105
|
|
|
}); |
106
|
|
|
}); |
107
|
|
|
|
108
|
1 |
|
it('should get 401 as we do not provide email', (done) => { |
109
|
1 |
|
let user = { |
110
|
|
|
//email: "[email protected]", |
111
|
|
|
password: "123test", |
112
|
|
|
api_key: apiKey |
113
|
|
|
}; |
114
|
|
|
|
115
|
1 |
|
chai.request(server) |
116
|
|
|
.post("/register") |
117
|
|
|
.send(user) |
118
|
|
|
.end((err, res) => { |
119
|
1 |
|
res.should.have.status(401); |
120
|
1 |
|
res.body.should.be.an("object"); |
121
|
1 |
|
res.body.errors.status.should.be.equal(401); |
122
|
1 |
|
done(); |
123
|
|
|
}); |
124
|
|
|
}); |
125
|
|
|
|
126
|
1 |
|
it('should get 401 as we do not provide password', (done) => { |
127
|
1 |
|
let user = { |
128
|
|
|
email: "[email protected]", |
129
|
|
|
// password: "123test", |
130
|
|
|
api_key: apiKey |
131
|
|
|
}; |
132
|
|
|
|
133
|
1 |
|
chai.request(server) |
134
|
|
|
.post("/register") |
135
|
|
|
.send(user) |
136
|
|
|
.end((err, res) => { |
137
|
1 |
|
res.should.have.status(401); |
138
|
1 |
|
res.body.should.be.an("object"); |
139
|
1 |
|
res.body.errors.status.should.be.equal(401); |
140
|
1 |
|
done(); |
141
|
|
|
}); |
142
|
|
|
}); |
143
|
|
|
|
144
|
1 |
|
it('should get 201 HAPPY PATH', (done) => { |
145
|
1 |
|
let user = { |
146
|
|
|
email: "[email protected]", |
147
|
|
|
password: "123test", |
148
|
|
|
api_key: apiKey |
149
|
|
|
}; |
150
|
|
|
|
151
|
1 |
|
chai.request(server) |
152
|
|
|
.post("/register") |
153
|
|
|
.send(user) |
154
|
|
|
.end((err, res) => { |
155
|
1 |
|
res.should.have.status(201); |
156
|
1 |
|
res.body.should.be.an("object"); |
157
|
1 |
|
res.body.should.have.property("data"); |
158
|
1 |
|
res.body.data.should.have.property("message"); |
159
|
1 |
|
res.body.data.message.should.equal("User successfully registered."); |
160
|
|
|
|
161
|
1 |
|
done(); |
162
|
|
|
}); |
163
|
|
|
}); |
164
|
|
|
}); |
165
|
|
|
|
166
|
1 |
|
describe('POST /login', () => { |
167
|
1 |
|
it('should get 401 as we do not provide valid api_key', (done) => { |
168
|
1 |
|
let user = { |
169
|
|
|
email: "[email protected]", |
170
|
|
|
password: "123test", |
171
|
|
|
// api_key: apiKey |
172
|
|
|
}; |
173
|
|
|
|
174
|
1 |
|
chai.request(server) |
175
|
|
|
.post("/login") |
176
|
|
|
.send(user) |
177
|
|
|
.end((err, res) => { |
178
|
1 |
|
res.should.have.status(401); |
179
|
1 |
|
res.body.should.be.an("object"); |
180
|
1 |
|
res.body.errors.status.should.be.equal(401); |
181
|
1 |
|
done(); |
182
|
|
|
}); |
183
|
|
|
}); |
184
|
|
|
|
185
|
1 |
|
it('should get 401 as we do not provide email', (done) => { |
186
|
1 |
|
let user = { |
187
|
|
|
//email: "[email protected]", |
188
|
|
|
password: "123test", |
189
|
|
|
api_key: apiKey |
190
|
|
|
}; |
191
|
|
|
|
192
|
1 |
|
chai.request(server) |
193
|
|
|
.post("/login") |
194
|
|
|
.send(user) |
195
|
|
|
.end((err, res) => { |
196
|
1 |
|
res.should.have.status(401); |
197
|
1 |
|
res.body.should.be.an("object"); |
198
|
1 |
|
res.body.errors.status.should.be.equal(401); |
199
|
1 |
|
done(); |
200
|
|
|
}); |
201
|
|
|
}); |
202
|
|
|
|
203
|
1 |
|
it('should get 401 as we do not provide password', (done) => { |
204
|
1 |
|
let user = { |
205
|
|
|
email: "[email protected]", |
206
|
|
|
// password: "123test", |
207
|
|
|
api_key: apiKey |
208
|
|
|
}; |
209
|
|
|
|
210
|
1 |
|
chai.request(server) |
211
|
|
|
.post("/login") |
212
|
|
|
.send(user) |
213
|
|
|
.end((err, res) => { |
214
|
1 |
|
res.should.have.status(401); |
215
|
1 |
|
res.body.should.be.an("object"); |
216
|
1 |
|
res.body.errors.status.should.be.equal(401); |
217
|
1 |
|
done(); |
218
|
|
|
}); |
219
|
|
|
}); |
220
|
|
|
|
221
|
1 |
|
it('should get 401 as user not found', (done) => { |
222
|
1 |
|
let user = { |
223
|
|
|
email: "[email protected]", |
224
|
|
|
password: "123test", |
225
|
|
|
api_key: apiKey |
226
|
|
|
}; |
227
|
|
|
|
228
|
1 |
|
chai.request(server) |
229
|
|
|
.post("/login") |
230
|
|
|
.send(user) |
231
|
|
|
.end((err, res) => { |
232
|
1 |
|
res.should.have.status(401); |
233
|
1 |
|
res.body.should.be.an("object"); |
234
|
1 |
|
res.body.errors.status.should.be.equal(401); |
235
|
1 |
|
done(); |
236
|
|
|
}); |
237
|
|
|
}); |
238
|
|
|
|
239
|
1 |
|
it('should get 401 incorrect password', (done) => { |
240
|
1 |
|
let user = { |
241
|
|
|
email: "[email protected]", |
242
|
|
|
password: "wrongpassword", |
243
|
|
|
api_key: apiKey |
244
|
|
|
}; |
245
|
|
|
|
246
|
1 |
|
chai.request(server) |
247
|
|
|
.post("/login") |
248
|
|
|
.send(user) |
249
|
|
|
.end((err, res) => { |
250
|
1 |
|
res.should.have.status(401); |
251
|
1 |
|
res.body.should.be.an("object"); |
252
|
1 |
|
res.body.errors.status.should.be.equal(401); |
253
|
1 |
|
done(); |
254
|
|
|
}); |
255
|
|
|
}); |
256
|
|
|
|
257
|
1 |
|
it('should get 201 HAPPY PATH', (done) => { |
258
|
1 |
|
let user = { |
259
|
|
|
email: "[email protected]", |
260
|
|
|
password: "123test", |
261
|
|
|
api_key: apiKey |
262
|
|
|
}; |
263
|
|
|
|
264
|
1 |
|
chai.request(server) |
265
|
|
|
.post("/login") |
266
|
|
|
.send(user) |
267
|
|
|
.end((err, res) => { |
268
|
1 |
|
res.should.have.status(200); |
269
|
1 |
|
res.body.should.be.an("object"); |
270
|
1 |
|
res.body.should.have.property("data"); |
271
|
1 |
|
res.body.data.should.have.property("type"); |
272
|
1 |
|
res.body.data.type.should.equal("success"); |
273
|
1 |
|
res.body.data.should.have.property("type"); |
274
|
|
|
|
275
|
1 |
|
done(); |
276
|
|
|
}); |
277
|
|
|
}); |
278
|
|
|
}); |
279
|
|
|
}); |
280
|
|
|
|