-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_jwt.py
More file actions
69 lines (61 loc) · 2.43 KB
/
test_jwt.py
File metadata and controls
69 lines (61 loc) · 2.43 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
import requests
import json
# Base URL for the API
BASE_URL = 'http://127.0.0.1:8000'
def test_jwt_authentication():
print("Testing JWT Authentication...")
# Test 1: Try to access a protected endpoint without authentication
print("\n1. Testing access to protected endpoint without authentication:")
response = requests.get(f'{BASE_URL}/api/products/')
print(f"Status Code: {response.status_code}")
print(f"Response: {response.json()}")
# Test 2: Register a new user
print("\n2. Registering a new user:")
register_data = {
'username': 'testuser',
'email': 'test@example.com',
'password': 'StrongPass123!',
'role': 'buyer'
}
response = requests.post(f'{BASE_URL}/api/auth/register/',
json=register_data)
print(f"Status Code: {response.status_code}")
print(f"Response: {response.json()}")
# Test 3: Obtain JWT token
print("\n3. Obtaining JWT token:")
token_data = {
'username': 'testuser',
'password': 'StrongPass123!'
}
response = requests.post(f'{BASE_URL}/api/token/',
json=token_data)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
tokens = response.json()
access_token = tokens['access']
refresh_token = tokens['refresh']
print(f"Access Token: {access_token[:50]}...")
print(f"Refresh Token: {refresh_token[:50]}...")
else:
print(f"Response: {response.json()}")
return
# Test 4: Access protected endpoint with valid token
print("\n4. Accessing protected endpoint with valid token:")
headers = {'Authorization': f'Bearer {access_token}'}
response = requests.get(f'{BASE_URL}/api/products/', headers=headers)
print(f"Status Code: {response.status_code}")
print(f"Response: {response.json()}")
# Test 5: Refresh token
print("\n5. Refreshing token:")
refresh_data = {'refresh': refresh_token}
response = requests.post(f'{BASE_URL}/api/token/refresh/',
json=refresh_data)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
new_tokens = response.json()
new_access_token = new_tokens['access']
print(f"New Access Token: {new_access_token[:50]}...")
else:
print(f"Response: {response.json()}")
if __name__ == '__main__':
test_jwt_authentication()