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