-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_user_test.py
156 lines (124 loc) · 3.96 KB
/
rest_user_test.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
import requests
import getpass
import json
username = input("email: ")
password = getpass.getpass()
base_uri = 'https://weitblicker.org'
photo = '/home/spuetz/Pictures/puetz.jpeg'
#base_uri = 'http://localhost:8000'
login_uri = '/rest/auth/login/'
logout_uri = '/rest/auth/logout/'
user_data_uri = '/rest/auth/user/'
user_pw_change_uri = '/rest/auth/password/change/'
cycle_tours_uri = '/rest/cycle/tours/'
cycle_ranking_uri = '/rest/cycle/ranking/'
cycle_add_segment_uri = '/rest/cycle/segment/'
reset_password_uri = '/rest/auth/password/reset/'
def login(username, password):
login_request_header = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
login_request_body = {
'email': username,
'password': password
}
print("login...")
response = requests.post(
base_uri+login_uri,
data=json.dumps(login_request_body),
headers=login_request_header
)
print(response.content)
return json.loads(response.content)['key']
def logout(token):
print("logout...")
header = {'Authorization': 'Token ' + token}
response = requests.post(base_uri+logout_uri, headers=header)
print(response.content)
def user_data(token):
print("user data...")
header = {'Authorization': 'Token ' + token}
response = requests.get(base_uri+user_data_uri, headers=header)
print(response.content)
return response.content
def upload_user_photo(data, token, photo):
print("upload user photo...")
header = {
'Authorization': 'Token ' + token,
'Accept': 'application/json',
}
response = requests.post(
base_uri+user_data_uri,
headers=header,
files={'image': open(photo, 'rb')},
data=json.loads(data)
)
print(response.content)
def change_password(token):
print("change password...")
header = {
'Authorization': 'Token ' + token,
'Accept': 'application/json',
}
new_password1 = input("New Password: ")
new_password2 = input("Password repeat: ")
pw_data = {
'new_password1': new_password1,
'new_password2': new_password2
}
response = requests.post(
base_uri + user_pw_change_uri,
headers=header,
data=pw_data)
print("response: ", response.content)
def cycle_user_tours(token):
print("user tours...")
header = {
'Authorization': 'Token ' + token,
'Accept': 'application/json',
}
response = requests.get(base_uri + cycle_tours_uri, headers=header)
print("response: ", response.content)
def cycle_ranking(token=None):
print("cycle ranking...")
header = {'Accept': 'application/json'}
if token:
header['Authorization'] = 'Token ' + token
response = requests.get(base_uri + cycle_ranking_uri, headers=header)
print("response: ", response.content)
def cycle_add_segment(token, ):
print("cycle add segment...")
header = {
'Authorization': 'Token ' + token,
'Content-Type': 'application/json',
'Accept': 'application/json',
}
segment = {
"start": "2019-10-01T07:08:04Z",
"end": "2019-10-01T07:08:14Z",
"distance": 0.16,
"project": 1,
"tour": 0,
}
response = requests.post(base_uri + cycle_add_segment_uri, headers=header, data=json.dumps(segment))
print("response: ", response.content)
def reset_password(email):
print("reset password...")
header = {'Accept': 'application/json',
'Content-Type': 'application/json'}
data = {'email': email}
response = requests.post(base_uri + reset_password_uri, headers=header, data=json.dumps(data))
print("response: ", response.content)
token = login(username, password)
data = user_data(token)
reset_password(username)
#cycle_user_tours(token)
#cycle_ranking(token)
#cycle_add_segment(token)
#logout(token)
#cycle_ranking()
#logout(token)
#token = login(username, password)
#upload_user_photo(data, token, photo)
#change_password(token)