-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_routes.py
More file actions
134 lines (80 loc) · 4.54 KB
/
test_routes.py
File metadata and controls
134 lines (80 loc) · 4.54 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
import pytest, requests
from fastapi import status
from routes import app
from fastapi.testclient import TestClient
ENDPOINT = "http://127.0.0.1:8000"
@pytest.fixture(scope="module")
def client():
with TestClient(app) as c:
yield c
@pytest.fixture(scope="module")
def test_user():
return {"username": "Safdar", "password": "tim1234"}
def test_login(client, test_user):
response = client.post(ENDPOINT + "/token", data=test_user)
assert response.status_code == status.HTTP_200_OK
token = response.json()["access_token"]
assert token is not None
return token
def test_delete_post_failed(client, test_user):
token = test_login(client, test_user)
response = client.delete(ENDPOINT + "/delete/post/689", headers={"Authorization": f"Bearer {token}"})
assert response.status_code == status.HTTP_404_NOT_FOUND
def test_delete_post_success(client, test_user):
token = test_login(client, test_user)
response = client.delete(ENDPOINT + "/delete/post/7", headers={"Authorization": f"Bearer {token}"})
assert response.status_code == status.HTTP_200_OK
def test_update_post_failed(client, test_user):
token = test_login(client, test_user)
post_body = {"title": "test_case", "content": "test_content"}
response = client.put(ENDPOINT + "/update/post/689", json=post_body, headers={"Authorization": f"Bearer {token}"})
assert response.status_code == status.HTTP_404_NOT_FOUND
def test_update_post_success(client, test_user):
token = test_login(client, test_user)
post_body = {"title": "test_case", "content": "test_content"}
response = client.put(ENDPOINT + "/update/post/8", json=post_body, headers={"Authorization": f"Bearer {token}"})
assert response.status_code == status.HTTP_200_OK
def test_search_post_failed(client, test_user):
token = test_login(client, test_user)
response = client.get(ENDPOINT + "/search/post/689", headers={"Authorization": f"Bearer {token}"})
assert response.status_code == status.HTTP_404_NOT_FOUND
def test_search_post_success(client, test_user):
token = test_login(client, test_user)
response = client.get(ENDPOINT + "/search/post/8", headers={"Authorization": f"Bearer {token}"})
assert response.status_code == status.HTTP_200_OK
def test_user_posts_failed(client, test_user):
token = test_login(client, test_user)
response = client.get(ENDPOINT + "/user/posts", headers={"Authorization": f"Bearer {token}"})
assert response.status_code == status.HTTP_404_NOT_FOUND
def test_user_posts_success(client, test_user):
token = test_login(client, test_user)
response = client.get(ENDPOINT + "/user/posts", headers={"Authorization": f"Bearer {token}"})
assert response.status_code == status.HTTP_200_OK
def test_insert_post_success(client, test_user):
token = test_login(client, test_user)
post_body = {"title": "test_case", "content": "test_content"}
response = client.post(ENDPOINT + "/insert/post", json=post_body, headers={"Authorization": f"Bearer {token}"})
assert response.status_code == status.HTTP_200_OK
def test_insert_post_failed(client, test_user):
token = test_login(client, test_user)
post_body = {"title": "test_case", "content": "test_content"}
response = client.post(ENDPOINT + "/insert/post", json=post_body, headers={"Authorization": f"Bearer {token}"})
assert response.status_code == status.HTTP_404_NOT_FOUND
def test_posts_success(client, test_user):
token = test_login(client, test_user)
response = client.get(ENDPOINT + "/posts", headers={"Authorization": f"Bearer {token}"})
assert response.status_code == status.HTTP_200_OK
def test_user_signup_success():
temp_user = { "username": "strrrr", "email": "user@example.com", "password": "stringggggg" }
response = requests.post(ENDPOINT + "/signup", json=temp_user)
assert response.status_code == status.HTTP_200_OK
def test_user_signup_failed(client, test_user):
token = test_login(client, test_user)
temp_user = { "username": "Safdar", "email": "user@example.com", "password": "tim1234" }
response = client.post(ENDPOINT + "/signup", json=temp_user, headers={"Authorization": f"Bearer {token}"})
assert (response.status_code == status.HTTP_406_NOT_ACCEPTABLE)\
or (response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY)
def test_reading_own_items_success(client, test_user):
token = test_login(client, test_user)
response = client.get(ENDPOINT + "/users/me/items", headers={"Authorization": f"Bearer {token}"})
assert response.status_code == status.HTTP_200_OK