This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
/
test_app.py
382 lines (285 loc) · 16.1 KB
/
test_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
'''
Created on Sep 7, 2018
@author: MaxShapiro32@ibm.com
Automated test bed for testing app
'''
import unittest
import app.web
from app.services import utils
import docker
from pymongo import MongoClient
import json
class TestScenarios(unittest.TestCase):
def setUp(self):
utils.mongoCredentials = MongoClient("mongodb://localhost:27017/credentials")
utils.mongoLog = MongoClient("mongodb://localhost:27017/log")
self.app = app.web.create_app().test_client()
self.app.testing = True
self.client = docker.from_env()
cont = self.client.containers.list(all=True)
for c in cont:
if c.name == 'mongo':
c.kill()
c.remove()
self.container = self.client.containers.run('mongo',
name='mongo',
ports={'27017':'27017'},
restart_policy={'Name':'always'},
detach=True)
def tearDown(self):
self.container.kill()
self.container.remove(v=True)
self.client.close()
def test_create_user(self):
print("Test Create User")
'''Create New User'''
response = self.app.put('http://localhost:5000/api/v1/user/create',
data=json.dumps({"username": "test", "password": "abc"}),
content_type='application/json')
self.assertEqual(response.status_code, 200)
'''Creating New User with Existing User Name'''
response = self.app.put('http://localhost:5000/api/v1/user/create',
data=json.dumps({"username": "test", "password": "abc"}),
content_type='application/json')
self.assertEqual(response.status_code, 400)
'''Invalid User Creating'''
response = self.app.put('http://localhost:5000/api/v1/user/create',
data=json.dumps({"password": "abc"}),
content_type='application/json')
self.assertEqual(response.status_code, 400)
def test_delete_user(self):
print("Test Delete User")
'''Create New User'''
self.app.put('http://localhost:5000/api/v1/user/create',
data=json.dumps({"username": "test", "password": "abc"}),
content_type='application/json')
'''User Not Deleted'''
response = self.app.delete('http://localhost:5000/api/v1/user/test')
self.assertEqual(response.status_code, 404)
'''Delete User'''
response = self.app.delete('http://localhost:5000/api/v1/user/test?deleteuser=true')
self.assertEqual(response.status_code, 200)
'''Invalid Delete User'''
response = self.app.delete('http://localhost:5000/api/v1/user/test')
self.assertEqual(response.status_code, 403)
def test_login(self):
print("Test Login")
'''Create New User'''
self.app.put('http://localhost:5000/api/v1/user/create',
data=json.dumps({"username": "test", "password": "abc"}),
content_type='application/json')
'''Successful Login'''
response = self.app.post('http://localhost:5000/api/v1/login',
data=json.dumps({"username": "test", "password": "abc"}),
content_type='application/json')
self.assertEqual(response.status_code, 200)
'''Invalid Login'''
response = self.app.post('http://localhost:5000/api/v1/login',
data=json.dumps({"username": "test", "password": "123"}),
content_type='application/json')
self.assertEqual(response.status_code, 400)
def test_reset_password(self):
print("Test Reset Password")
'''Create New User'''
self.app.put('http://localhost:5000/api/v1/user/create',
data=json.dumps({"username": "test", "password": "abc"}),
content_type='application/json')
'''Successful Password Reset'''
response = self.app.put('http://localhost:5000/api/v1/user/test/reset',
data=json.dumps({"new_password": "123", "password": "abc"}),
content_type='application/json')
self.assertEqual(response.status_code, 200)
'''Failed Password Reset'''
response = self.app.put('http://localhost:5000/api/v1/user/test/reset',
data=json.dumps({"new_password": "123", "password": "abc"}),
content_type='application/json')
self.assertEqual(response.status_code, 403)
def test_all_work_log_data(self):
print("Test All Work Log Data")
'''Create New User'''
self.app.put('http://localhost:5000/api/v1/user/create',
data=json.dumps({"username": "test", "password": "abc"}),
content_type='application/json')
'''Create Test Data'''
self.app.post('http://localhost:5000/api/v1/user/test?date=2017-01-01&type=office')
self.app.post('http://localhost:5000/api/v1/user/test?date=2018-01-01&type=office')
'''View All Work Log Data'''
response = self.app.get('http://localhost:5000/api/v1/user/test')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.get_json()['years']),2)
'''Not Logged In'''
response = self.app.get('http://localhost:5000/api/v1/user/test2')
self.assertEqual(response.status_code, 403)
'''Invalid Delete All Work Log Data'''
response = self.app.delete('http://localhost:5000/api/v1/user/test')
self.assertEqual(response.status_code, 404)
'''Delete All Work Log Data'''
response = self.app.delete('http://localhost:5000/api/v1/user/test?deleteall=true')
self.assertEqual(response.status_code, 200)
'''Not Logged In'''
response = self.app.delete('http://localhost:5000/api/v1/user/test2')
self.assertEqual(response.status_code, 403)
def test_year(self):
print("Test Year")
'''Create New User'''
self.app.put('http://localhost:5000/api/v1/user/create',
data=json.dumps({"username": "test", "password": "abc"}),
content_type='application/json')
'''Create Test Data'''
self.app.post('http://localhost:5000/api/v1/user/test?date=2018-01-01&type=office')
'''View Year Data'''
response = self.app.get('http://localhost:5000/api/v1/user/test?year=2018')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.get_json()['year'],2018)
'''Year Data Does Not Exist'''
response = self.app.get('http://localhost:5000/api/v1/user/test?year=2017')
self.assertEqual(response.status_code, 404)
'''Invalid Year'''
response = self.app.get('http://localhost:5000/api/v1/user/test?year=not2018')
self.assertEqual(response.status_code, 400)
'''Not Logged In'''
response = self.app.get('http://localhost:5000/api/v1/user/test2?year=2018')
self.assertEqual(response.status_code, 403)
'''Year Data Not Deleted'''
response = self.app.delete('http://localhost:5000/api/v1/user/test?deleteyear=true')
self.assertEqual(response.status_code, 404)
'''Delete Year'''
response = self.app.delete('http://localhost:5000/api/v1/user/test?year=2018&deleteyear=true')
self.assertEqual(response.status_code, 200)
'''Invalid Year'''
response = self.app.delete('http://localhost:5000/api/v1/user/test?year=not2018&deleteyear=true')
self.assertEqual(response.status_code, 400)
'''Not Logged In'''
response = self.app.delete('http://localhost:5000/api/v1/user/test2?year=2018&deleteyear=true')
self.assertEqual(response.status_code, 403)
def test_entry(self):
print("Test Entry")
'''Create New User'''
self.app.put('http://localhost:5000/api/v1/user/create',
data=json.dumps({"username": "test", "password": "abc"}),
content_type='application/json')
'''Create Entry'''
'''Create Office Entry'''
response = self.app.post('http://localhost:5000/api/v1/user/test?date=2018-01-01&type=office',
data=json.dumps({"notes": "These are notes"}),
content_type='application/json')
self.assertEqual(response.status_code, 200)
'''Create Remote Entry'''
response = self.app.post('http://localhost:5000/api/v1/user/test?date=2018-01-02&type=remote&location=New York')
self.assertEqual(response.status_code, 200)
'''Create Vacation Entry'''
response = self.app.post('http://localhost:5000/api/v1/user/test?date=2018-01-03&type=vacation')
self.assertEqual(response.status_code, 200)
'''Create Holiday Entry'''
response = self.app.post('http://localhost:5000/api/v1/user/test?date=2018-01-04&type=holiday')
self.assertEqual(response.status_code, 200)
'''Create Sick Entry'''
response = self.app.post('http://localhost:5000/api/v1/user/test?date=2018-01-05&type=sick')
self.assertEqual(response.status_code, 200)
'''Entry for Date Already Exists'''
response = self.app.post('http://localhost:5000/api/v1/user/test?date=2018-01-01&type=office')
self.assertEqual(response.status_code, 400)
'''Invalid Date'''
response = self.app.post('http://localhost:5000/api/v1/user/test?date=2018-40-01&type=office')
self.assertEqual(response.status_code, 400)
'''Invalid Type'''
response = self.app.post('http://localhost:5000/api/v1/user/test?date=2018-01-09&type=notoffice')
self.assertEqual(response.status_code, 400)
'''No Date Specified'''
response = self.app.post('http://localhost:5000/api/v1/user/test?type=office')
self.assertEqual(response.status_code, 400)
'''No Type Specified'''
response = self.app.post('http://localhost:5000/api/v1/user/test?date=2018-01-09')
self.assertEqual(response.status_code, 400)
'''No Location Specified'''
response = self.app.post('http://localhost:5000/api/v1/user/test?date=2018-01-09&type=remote')
self.assertEqual(response.status_code, 400)
'''Not Logged In'''
response = self.app.post('http://localhost:5000/api/v1/user/test2?date=2018-01-01&type=office')
self.assertEqual(response.status_code, 403)
'''Update Entry'''
'''Successful Update Entry'''
response = self.app.put('http://localhost:5000/api/v1/user/test?date=2018-01-02&type=office',
data=json.dumps({"notes": "These are new notes"}),
content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.get_json()['type'],"office")
'''Invalid Date'''
response = self.app.put('http://localhost:5000/api/v1/user/test?date=2018-40-01&type=office')
self.assertEqual(response.status_code, 400)
'''Invalid Type'''
response = self.app.put('http://localhost:5000/api/v1/user/test?date=2018-01-02&type=notoffice')
self.assertEqual(response.status_code, 400)
'''No Date Specified'''
response = self.app.put('http://localhost:5000/api/v1/user/test?type=office')
self.assertEqual(response.status_code, 400)
'''No Type Specified'''
response = self.app.put('http://localhost:5000/api/v1/user/test?date=2018-01-02')
self.assertEqual(response.status_code, 400)
'''No Location Specified'''
response = self.app.put('http://localhost:5000/api/v1/user/test?date=2018-01-02&type=remote')
self.assertEqual(response.status_code, 400)
'''No Date Found'''
response = self.app.put('http://localhost:5000/api/v1/user/test?date=2018-02-01&type=office')
self.assertEqual(response.status_code, 404)
'''Not Logged In'''
response = self.app.put('http://localhost:5000/api/v1/user/test2?date=2018-01-01&type=office')
self.assertEqual(response.status_code, 403)
'''View Entry'''
'''Successful View Entry'''
response = self.app.get('http://localhost:5000/api/v1/user/test?date=2018-01-01')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.get_json()['type'],"office")
'''No Date Found'''
response = self.app.get('http://localhost:5000/api/v1/user/test?date=2018-02-01')
self.assertEqual(response.status_code, 404)
'''Invalid Date'''
response = self.app.get('http://localhost:5000/api/v1/user/test?date=2018-40-01')
self.assertEqual(response.status_code, 400)
'''Not Logged In'''
response = self.app.get('http://localhost:5000/api/v1/user/test2?date=2018-01-01&type=office')
self.assertEqual(response.status_code, 403)
'''Delete Entry'''
'''Invalid Date'''
response = self.app.delete('http://localhost:5000/api/v1/user/test?date=2018-40-01')
self.assertEqual(response.status_code, 400)
'''No Date Specified'''
response = self.app.delete('http://localhost:5000/api/v1/user/test')
self.assertEqual(response.status_code, 404)
'''No Date Data Deleted'''
response = self.app.delete('http://localhost:5000/api/v1/user/test?date=2018-02-01')
self.assertEqual(response.status_code, 404)
'''Successful Delete Entry'''
response = self.app.delete('http://localhost:5000/api/v1/user/test?date=2018-01-01')
self.assertEqual(response.status_code, 200)
'''Not Logged In'''
response = self.app.delete('http://localhost:5000/api/v1/user/test2?date=2018-01-01')
self.assertEqual(response.status_code, 403)
def test_settings(self):
print("Test Settings")
'''Create New User'''
self.app.put('http://localhost:5000/api/v1/user/create',
data=json.dumps({"username": "test", "password": "abc"}),
content_type='application/json')
'''Update Settings'''
'''Successful Update Settings'''
response = self.app.put('http://localhost:5000/api/v1/user/test/settings',
data=json.dumps({"vacation": 10}),
content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.get_json()['settings']['total']["vacation"],10)
'''No Settings Data Specified'''
response = self.app.put('http://localhost:5000/api/v1/user/test/settings')
self.assertEqual(response.status_code, 400)
'''Not Logged In'''
response = self.app.put('http://localhost:5000/api/v1/user/test2/settings')
self.assertEqual(response.status_code, 403)
'''View Settings'''
'''Successful View Settings'''
response = self.app.get('http://localhost:5000/api/v1/user/test/settings')
self.assertEqual(response.status_code, 200)
'''Not Logged In'''
response = self.app.get('http://localhost:5000/api/v1/user/test2/settings')
self.assertEqual(response.status_code, 403)
if __name__ == "__main__":
unittest.main()