-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
227 lines (168 loc) · 9.35 KB
/
Copy pathtest_api.py
File metadata and controls
227 lines (168 loc) · 9.35 KB
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
#!/usr/bin/env python3
import random
import string
import unittest
from api import API
"""TODO: Import the `API` class from the api.py file"""
def generate_random_text(l=10):
""" Helper to generate random text for creating new tasks.
This is helpful and will ensure that when you run your tests,
a new text string is created. It is also good for determining
that two tasks are unique.
Keyword arguments:
l (int): How long the generated text should be (default 10)
Returns:
A randomly-generated string of length `l`
"""
chars = string.hexdigits
return "".join(random.choice(chars) for i in range(l)).lower()
def generate_random_date(year=None, month=None, date=None):
""" Helper to generate random date for creating new tasks.
This is helpful as another way of generating random tasks
Keyword arguments:
year: Specify a year (default None)
month: Specify a month (default None)
date: Specify a date (default None)
Returns:
A randomly-generated string representation of a date
"""
if not year:
year = str(random.randint(2000, 2025))
if not month:
month = str(random.randint(1, 12))
if not date:
date = str(random.randint(1, 28))
return str(year) + "-" + str(month).zfill(2) + "-" + str(date).zfill(2) + "T00:00:00.000Z"
class TestAPI(unittest.TestCase):
# TODO: update the cookie value and uncomment the desired `base_url, cookie` pair when ready to test
base_url, cookie = "https://botnetcentral.hopto.org:1337", "s%3AzFTHQBea96vfkTAfZ2Ee_5P7NDaOLWA5.Es3%2BJkG1WVs2lPgX5HK1IUHv2Y9lrW%2B9o2oObBgUCEE" # For s1
#base_url, cookie = "https://s2.byu-itc-210.net:1338", "s%3AZUSn3Ibaj8MGoAFT5sLtHrrHvsainRU3.3BVwcwP5ngPfnSBHyei2ZkcHkDmdj63HgO7IdQIbA2s" # For s2
#base_url, cookie = "https://s3.byu-itc-210.net:1339", "s%3AZUSn3Ibaj8MGoAFT5sLtHrrHvsainRU3.3BVwcwP5ngPfnSBHyei2ZkcHkDmdj63HgO7IdQIbA2s" # For s3
#base_url, cookie = "https://s4.byu-itc-210.net:1340", "s%3AZUSn3Ibaj8MGoAFT5sLtHrrHvsainRU3.3BVwcwP5ngPfnSBHyei2ZkcHkDmdj63HgO7IdQIbA2s" # For s4
# This will be ran once, when you start your tests.
@classmethod
def setUpClass(self):
super().setUpClass()
self.api = API(self.base_url)
def test_create_task(self):
""" Tests creating a task is successful.
This is an example test:
- Create the task w/dummy data
- Verify that the task was created
- Delete the task we created
You will be required to implement the other tests
that are defined in BaseTestCase. They will be marked
with @abc.abstractmethod.
"""
Text = generate_random_text()
Date = generate_random_date()
create_resp = self.api.create_task(self.cookie, Text, Date)
self.assertTrue(create_resp.ok, msg=f"The Create Task failed: {create_resp.reason}.")
task = create_resp.json()
self.assertEqual(task["Text"], Text, msg="The task's Text did not match the expected Text.")
self.assertEqual(task["Date"], Date, msg="The task's Date did not match the expected Date.")
self.assertFalse(task["Done"], msg="The task's Done returned True, expected False.")
self.assertIn("UserId", task, msg=f"All tasks should have a UserId, matching the Id of the user who created it.")
# cleanup - we don't want to conflict with other tests
# or have a test task in our database.
self.api.delete_task(self.cookie, task["_id"])
def test_read_all_tasks(self):
Text1 = generate_random_text()
Date1 = generate_random_date()
create_resp = self.api.create_task(self.cookie, Text1, Date1)
self.assertTrue(create_resp.ok, msg=f"The Create Task failed: {create_resp.reason}.")
task1 = create_resp.json()
userId = task1["UserId"]
Text2 = generate_random_text()
Date2 = generate_random_date()
self.api.create_task(self.cookie, Text2, Date2)
read_all_resp = self.api.read_all_tasks(self.cookie)
tasks = read_all_resp.json()
for x in tasks:
self.assertEqual(x.get("UserId"), userId, msg =f"The tasks have different user ID's. Should have the same user ID: {read_all_resp.reason}.")
def test_read_one_task(self):
Text = generate_random_text()
Date = generate_random_date()
create_resp = self.api.create_task(self.cookie, Text, Date)
self.assertTrue(create_resp.ok, msg=f"The Create Task failed: {create_resp.reason}.")
task = create_resp.json()
read_resp = self.api.read_task(self.cookie, task["_id"])
self.assertTrue(read_resp.ok, msg=f"The Read-One Task failed: {read_resp.reason}.")
task = read_resp.json()
self.assertEqual(task["Text"], Text, msg="The task's Text did not match the expected Text.")
self.assertEqual(task["Date"], Date, msg="The task's Date did not match the expected Date.")
self.assertFalse(task["Done"], msg="The task's Done returned True, expected False.")
self.assertIn("UserId", task, msg=f"All tasks should have a UserId, matching the Id of the user who created it.")
self.api.delete_task(self.cookie, task["_id"])
def test_update_task(self):
Text = generate_random_text()
Date = generate_random_date()
create_resp = self.api.create_task(self.cookie, Text, Date)
self.assertTrue(create_resp.ok, msg=f"The Create Task failed: {create_resp.reason}.")
task = create_resp.json()
update_Resp = self.api.update_task(self.cookie, task["_id"], task["Done"])
self.assertTrue(update_Resp.ok, msg=f"The Update Task failed: {update_Resp.reason}.")
updatedTask = update_Resp.json()
self.assertFalse(updatedTask["Done"], msg =f"Task was not marked to done.")
self.api.delete_task(self.cookie, task["_id"])
self.api.delete_task(self.cookie, updatedTask["_id"])
def test_delete_task(self):
Text = generate_random_text()
Date = generate_random_date()
create_resp = self.api.create_task(self.cookie, Text, Date)
self.assertTrue(create_resp.ok, msg=f"The Create Task failed: {create_resp.reason}.")
task = create_resp.json()
delete_resp = self.api.delete_task(self.cookie, task["_id"])
self.assertTrue(delete_resp.ok, msg=f"The Delete Task failed: {delete_resp.reason}.")
read_resp = self.api.read_task(self.cookie, task["_id"])
self.assertFalse(read_resp.ok, msg=f"Read-one Task didn't fail and it should have: {read_resp.reason}.")
self.api.delete_task(self.cookie, task["_id"])
def test_read_current_user(self):
read_user_resp = self.api.read_current_user(self.cookie)
self.assertTrue(read_user_resp.ok, msg=f"Read Current User failed: {read_user_resp.reason}.")
task = read_user_resp.json()
self.assertIn("Id", task, msg = "User has no ID")
self.assertIn("UserName", task, msg = "User has no email on file")
self.assertIn("Email", task, msg = "User has no username")
def test_read_one_fail(self) :
id = generate_random_text(24)
resp = self.api.read_task(self.cookie, id)
self.assertFalse(resp.ok, msg=f"The Read-one function cannot read tasks that aren't there: {resp.reason}.")
print(resp.status_code)
def test_delete_fail(self) :
id = generate_random_text(24)
resp = self.api.delete_task(self.cookie, id)
self.assertFalse(resp.ok, msg=f"The Delete function cannot delete tasks that aren't there: {resp.reason}.")
print(resp.status_code)
def test_update_fail(self) :
id = generate_random_text(24)
resp = self.api.update_task(self.cookie, id, False)
self.assertFalse(resp.ok, msg=f"The Update function cannot update tasks that aren't there: {resp.reason}.")
print(resp.status_code)
def test_delete_fail_invalid(self) :
id = generate_random_text(23)
resp = self.api.delete_task(self.cookie, id)
self.assertFalse(resp.ok, msg=f"The task's ID is totally invalid: {resp.reason}.")
print(resp.status_code)
def test_read_all_no_cookie(self) :
cookie = generate_random_text(24).lower()
resp = self.api.read_all_tasks(cookie)
self.assertFalse(resp.ok, msg=f"There is no cookie because the user is not logged in: {resp.reason}.")
print(resp.status_code)
def test_create_fail_insufficient_data(self) :
Text = generate_random_text
Date = generate_random_date
resp = self.api.create_task(self.cookie, Text, "")
self.assertFalse(resp.ok, msg=f"Task should not have been created without necessary date: {resp.reason}.")
resp = self.api.create_task(self.cookie, "", Date)
self.assertFalse(resp.ok, msg=f"Task should not have been created without necessary text: {resp.reason}.")
resp = self.api.create_task(self.cookie, "", "")
self.assertFalse(resp.ok, msg=f"Task should not have been created without necessary text and date: {resp.reason}.")
print(resp.status_code)
# Make more methods that begin with 'test` to test all endpoints
# properly work and fail when you expect them to.
# Inside this `if` statement will only run if we call the program as
# the top-level module, i.e. when we run this file, not when we import
# this file
if __name__ == "__main__":
unittest.main()