-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_rest_end_points.py
executable file
·185 lines (151 loc) · 6.31 KB
/
test_rest_end_points.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
"""
Unit test to test REST api end points
"""
import unittest
import os
import json
from file_sync import FLASK_APP, DB
BASE_URL = "http://127.0.0.1:8080/"
class BucketlistTestCase(unittest.TestCase):
"""
This class represents the file_sync API test case
"""
def setUp(self):
"""
Test variables and app initialization
"""
self.app = FLASK_APP
self.app_url = BASE_URL
self.client = self.app.test_client
self.file_name = "some_file"
self.user_name = "some_user"
# binds the app to the current context
with self.app.app_context():
DB.create_all()
def tearDown(self):
"""
teardown all initialized variables.
"""
with self.app.app_context():
DB.session.remove()
DB.drop_all()
try:
os.remove(
os.path.join(FLASK_APP.config["COMMON_STORAGE_LOCATION"],
"%s_%s" % (self.file_name, self.user_name)))
except OSError:
pass
def test_add_new_file_entry_invalid_input(self):
"""
Test API response for new file entry when invalid request is made, say
unexpected content type
"""
res = self.client().post('/file',
data=json.dumps({"user_name": self.user_name}))
error_dict = {"error": "Invalid Input",
"error_code": 1002}
self.assertEqual(res.status_code, 500)
self.assertDictEqual(error_dict, res.json)
def test_add_new_file_entry_success(self):
"""
Test API can create a new file entry to be synced
using a post request
"""
res = self.client().post('/file',
data=json.dumps({"user_name": self.user_name,
"file_name": self.file_name}),
content_type="application/json")
self.assertEqual(res.status_code, 200)
self.assertIn("token", res.data)
def test_add_new_file_entry_missing_file_name(self):
"""
Test API response for new file entry when file_name missing in payload
"""
res = self.client().post('/file',
data=json.dumps({"user_name": self.user_name}),
content_type="application/json")
error_dict = {"error": "Missing required input 'file_name'",
"error_code": 1001}
self.assertEqual(res.status_code, 500)
self.assertDictEqual(error_dict, res.json)
def test_add_new_file_entry_missing_user_name(self):
"""
Test API response for new file entry when user_name missing in payload
"""
res = self.client().post('/file',
data=json.dumps({"file_name": self.file_name}),
content_type="application/json")
error_dict = {"error": "Missing required input 'user_name'",
"error_code": 1001}
self.assertEqual(res.status_code, 500)
self.assertDictEqual(error_dict, res.json)
def test_get_file_content_invalid_token(self):
"""
Test API response for get file content file entry when invalid token
is provided
"""
res = self.client().get('/file/content/invalid_token',
content_type="application/json")
error_dict = {"error": "No record exists with token 'invalid_token'",
"error_code": 1004}
self.assertEqual(res.status_code, 500)
self.assertDictEqual(error_dict, res.json)
def test_get_file_content_valid_token_response(self):
"""
Test API response for get file content file entry when a valid token
is provided
"""
res = self.client().post('/file',
data=json.dumps({"user_name": self.user_name,
"file_name": self.file_name}),
content_type="application/json")
res = self.client().get('/file/content/%s' % res.json["token"],
content_type="application/json")
self.assertEqual(res.status_code, 200)
self.assertIn("contents", res.json)
def test_sync_file_content_invalid_token(self):
"""
Test API response for sync file content invalid token
is provided
"""
res = self.client().patch('/file/content/invalid_token',
data=json.dumps({"contents": "some_content"}),
content_type="application/json")
error_dict = {"error": "No record exists with token 'invalid_token'",
"error_code": 1004}
self.assertEqual(res.status_code, 500)
self.assertDictEqual(error_dict, res.json)
def test_sync_file_content_valid_token_response(self):
"""
Test API response for sync file content when a valid token
is provided
"""
res = self.client().post('/file',
data=json.dumps({"user_name": self.user_name,
"file_name": self.file_name}),
content_type="application/json")
res = self.client().patch('/file/content/%s' % res.json["token"],
data=json.dumps({"contents": "some_content"}),
content_type="application/json")
expected_response = {"status": True}
self.assertEqual(res.status_code, 200)
self.assertDictEqual(expected_response, res.json)
def test_sync_file_content_get_response(self):
"""
Test synced file content getting appended to the file
"""
res = self.client().post('/file',
data=json.dumps({"user_name": self.user_name,
"file_name": self.file_name}),
content_type="application/json")
token = res.json["token"]
res = self.client().patch('/file/content/%s' % token,
data=json.dumps({"contents": "some_content"}),
content_type="application/json")
res = self.client().get('/file/content/%s' % token,
content_type="application/json")
expected_response = {"contents": "some_content"}
self.assertEqual(res.status_code, 200)
self.assertDictEqual(expected_response, res.json)
if __name__ == "__main__":
unittest.main()