-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_todoAPI.py
126 lines (93 loc) · 4.05 KB
/
test_todoAPI.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
from faker import Faker
from faker.providers import user_agent
import requests
BASEURL = 'https://todo.pixegami.io'
fake = Faker()
fake.add_provider(user_agent)
def new_task_payload(user_id=fake.user_name()):
return {'content': fake.sentence(),
'user_id': user_id,
'is_done': False}
def create_new_task(todo_payload):
return requests.put(BASEURL + '/create-task', json=todo_payload)
def update_task(updated_payload):
return requests.put(BASEURL + '/update-task', json=updated_payload)
def get_task(todo_task_id):
return requests.get(BASEURL + f'/get-task/{todo_task_id}')
def get_tasks_list(user_id):
return requests.get(BASEURL + f'/list-tasks/{user_id}')
def delete_task(todo_task_id):
return requests.delete(BASEURL + f'/delete-task/{todo_task_id}')
def test_create_task():
# Creating a new task
task_payload = new_task_payload()
new_task_response = create_new_task(task_payload)
task_data = new_task_response.json()
#verify the response
assert new_task_response.status_code == 200
assert task_data['task']['task_id'] is not None
assert task_data['task']['content'] == task_payload['content']
assert task_data['task']['user_id'] == task_payload['user_id']
def test_get_task():
# Creating a task
task_payload = new_task_payload()
new_task_response = create_new_task(task_payload)
task_data = new_task_response.json()
task_id = task_data['task']['task_id']
assert task_id is not None
# Getting a task
get_todo_task_response = get_task(task_id)
assert get_todo_task_response.status_code == 200
assert task_data['task']['content'] == task_payload['content']
assert task_data['task']['user_id'] == task_payload['user_id']
def test_update_task():
# Creating a new task
task_payload = new_task_payload()
new_task_response = create_new_task(task_payload)
assert new_task_response.status_code == 200
task_data = new_task_response.json()['task']
task_id = task_data['task_id']
assert task_id is not None
# Updating the task
updated_payload = {
'user_id': task_payload['user_id'],
'task_id': task_id,
'content': 'new new new updated content',
'is_done': True
}
update_task_response = update_task(updated_payload)
assert update_task_response.status_code == 200
# Checking the updated task
get_task_response = get_task(task_id)
assert get_task_response.status_code == 200
updated_task_data = get_task_response.json()
assert updated_task_data['content'] == updated_payload['content']
assert updated_task_data['is_done'] == updated_payload['is_done']
def test_get_tasks_list():
# Creating tasks for the same user
user_id = fake.user_name()
tasks_number = 3
for _ in range(tasks_number):
task_payload = new_task_payload(user_id=user_id)
new_task_response = create_new_task(task_payload)
assert new_task_response.status_code == 200
# Checking user tasks list
tasks_list_response = get_tasks_list(user_id)
assert tasks_list_response.status_code == 200
tasks = tasks_list_response.json()['tasks']
assert len(tasks) == tasks_number
def test_delete_task():
# Creating a task
task_payload = new_task_payload()
new_task_response = create_new_task(task_payload)
task_data = new_task_response.json()
task_id = task_data['task']['task_id']
assert task_id is not None
# Deleting a task
delete_task_response = delete_task(task_id)
assert delete_task_response.status_code == 200
delete_data = delete_task_response.json()
assert delete_data['deleted_task_id'] == task_id
# Checking the task is not there anymore
getting_deleted_task_response = get_task(task_id)
assert getting_deleted_task_response.status_code == 404