-
Notifications
You must be signed in to change notification settings - Fork 0
/
APITester.py
161 lines (139 loc) · 5.64 KB
/
APITester.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
import requests # Use the 'requests' library to make HTTP requests
from base64 import b64encode, b64decode
BASE_URL = 'http://0.0.0.0:5100' # Base URL for the API
def request_members(cookies=None):
try:
response = requests.get(f'{BASE_URL}/members', cookies=cookies)
print(response)
return response.json()
except requests.exceptions.RequestException as e:
return {"error": str(e)}
# Function to log in
def login(username, password):
login_url = f'{BASE_URL}/login'
data = {'username': username, 'password': password}
response = requests.post(login_url, json=data)
if response.status_code == 200:
print(f"Logged in as {username}")
# Inspect cookies received from server after login
print("Cookies received after login:", response.cookies)
return response.cookies # Return cookies to maintain session
else:
print("Login failed:", response.json())
return None
# Example of a create user function
def create_user(cookies, istID, memberNumber, name, username, password, join_date, course, description, mail):
create_member_url = f'{BASE_URL}/members'
data = {
"istID": istID,
"memberNumber": memberNumber,
"name": name,
"username": username,
"password": password,
"join_date": join_date,
"course": course,
"description": description,
"mail": mail
}
response = requests.post(create_member_url, json=data, cookies=cookies)
print(response.json())
def edit_user(cookies, memberNumber, name):
edit_member_url = f'{BASE_URL}/members/fpicarras'
data = {
"memberNumber": memberNumber,
"name": name,
"password": "password"
}
response = requests.put(edit_member_url, json=data, cookies=cookies)
print(response.json())
def delete_user(cookies, username_to_delete):
response = requests.delete(f'{BASE_URL}/members/{username_to_delete}', cookies=cookies)
print(response.json())
def create_project(cookies, name, description, state, date):
create_project_url = f'{BASE_URL}/projects'
data = {
"name": name,
"description": description,
"start_date": date,
"state": state
}
response = requests.post(create_project_url, json=data, cookies=cookies)
print(response.json())
def request_projects(cookies):
response = requests.get(f'{BASE_URL}/projects', cookies=cookies)
print(response.json())
def edit_project(cookies, proj_id, description):
edit_member_url = f'{BASE_URL}/projects/{proj_id}'
data = {
"description": description
}
response = requests.put(edit_member_url, json=data, cookies=cookies)
print(response.json())
def delete_project(cookies, proj_id):
response = requests.delete(f'{BASE_URL}/projects/{proj_id}', cookies=cookies)
print(response.json())
def send_image(cookies, username, image_file):
with open(image_file, 'rb') as file:
image_data = file.read()
image_data = b64encode(image_data).decode('utf-8')
image_url = f'{BASE_URL}/projects/{username}/logo'
data = {
"image": image_data
}
response = requests.post(image_url, json=data, cookies=cookies)
print(response.json())
def recieve_image(cookies, username):
image_url = f'{BASE_URL}/projects/{username}/logo'
response = requests.get(image_url, cookies=cookies)
if 'image' in response.json():
image_data = response.json()['image']
image_data = b64decode(image_data)
with open(f'returned-{username}.png', 'wb') as file:
file.write(image_data)
else:
print(response.json())
def link_member_project(cookies, username, proj_id, entry_date, contributions, exit_date):
link_url = f'{BASE_URL}/link/{username}/{proj_id}'
data = {
"entry_date": entry_date,
"contributions": contributions,
"exit_date": exit_date
}
response = requests.post(link_url, json=data, cookies=cookies)
print(response.json())
def edit_member_project(cookies, username, proj_id, entry_date, contributions, exit_date):
link_url = f'{BASE_URL}/link/{username}/{proj_id}'
data = {
"contributions": contributions,
}
response = requests.put(link_url, json=data, cookies=cookies)
print(response.json())
def delete_member_project(cookies, username, proj_id):
link_url = f'{BASE_URL}/link/{username}/{proj_id}'
response = requests.delete(link_url, cookies=cookies)
print(response.json())
# Trying the request without cookies
print(request_members())
cookies = login('fpicarras', 'password')
# Make request with cookies to see if authentication works
print(request_members(cookies))
create_user(cookies, "ist123", "69", "João Pinheiros de Sá", "jpinhas", "123", "7/10/2024", "LEFT", "they/them", "[email protected]")
edit_user(cookies, "44", "Filipe Correia")
# Make another request to test if cookies still work
input("Press a key to continue...")
print(request_members(cookies))
create_project(cookies, "Test Project", "ahahahah", "Almost", "7/10/2024")
request_projects(cookies)
edit_project(cookies, 3, "testi")
request_projects(cookies)
delete_project(cookies, 3)
delete_user(cookies, "jpinhas")
print(request_members(cookies))
request_projects(cookies)
send_image(cookies, "1", "img_teste.png")
recieve_image(cookies, "1")
link_member_project(cookies, "fpicarras", 1, "7/10/2024", "All", "7/10/2025")
input("Press a key to continue...")
edit_member_project(cookies, "fpicarras", 1, "7/10/2024", "None, this sucker did nothing", "7/10/2025")
delete_member_project(cookies, "fpicarras", 1)
edit_member_project(cookies, "fpicarras", 1, "7/10/2024", "This should not work, link does not exist", "7/10/2025")