forked from delster1/communityGarden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_server.py
86 lines (71 loc) · 3.1 KB
/
test_server.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
import requests
def test_index():
url = "http://127.0.0.1:5000/"
response = requests.get(url)
assert response.status_code == 200
print("Index test passed")
def test_get_user_plant():
url = "http://127.0.0.1:5000/garden/dylan"
response = requests.get(url)
assert response.status_code == 200
def test_login():
url = "http://127.0.0.1:5000/login"
payload = {"username": "testuser", "password": "testpass"}
headers = {'Content-Type': 'application/json'}
response = requests.post(url, json=payload, headers=headers)
try:
response_data = response.json()
print("Login response:", response_data)
assert response.status_code == 200, "Login failed, status code was not 200"
return response_data['access_token'] # Assuming JWT is returned under 'access_token' key
except ValueError:
print("Login response isn't JSON or JSON Decode Error")
return None
def test_grow(access_token):
url = "http://127.0.0.1:5000/grow"
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {access_token}' # Authorization header with the JWT
}
response = requests.post(url, headers=headers) # Assuming GET for this example
try:
response_data = response.json()
print("User-specific action response:", response_data)
assert response.status_code == 200, "Failed to perform user-specific action, status code was not 200"
except ValueError:
print("User-specific action response isn't JSON or JSON Decode Error")
def test_swap(access_token):
url = "http://127.0.0.1:5000/swap"
payload = {"task_id" : "7902699be42c8a8e46fbbb4501726517e86b22c56a189f7625a6da49081b2451"}
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {access_token}' # Authorization header with the JWT
}
response = requests.post(url, json=payload, headers=headers) # Assuming GET for this example
try:
response_data = response.json()
print("User-specific action response:", response_data)
assert response.status_code == 200, "Failed to perform user-specific action, status code was not 200"
except ValueError:
print("User-specific action response isn't JSON or JSON Decode Error")
def test_register():
url = "http://127.0.0.1:5000/register"
payload = {"username": "testuser", "password": "testpass"}
headers = {'Content-Type': 'application/json'}
response = requests.post(url, json=payload, headers=headers)
print(response)
# print("Status Code:", response.status_code)
try:
response_data = response.json()
print("Response Body:", response_data)
assert response.status_code == 200, "Status code was not 200"
except ValueError: # includes simplejson.decoder.JSONDecodeError
print("Response isn't JSON or JSON Decode Error")
if __name__ == "__main__":
# test_index()
# test_register()
# test_get_user_plant()
access_token = test_login()
if access_token:
test_grow(access_token)
# test_swap(access_token)