code quality run
This commit is contained in:
@@ -6,21 +6,21 @@ import requests
|
||||
# API Base URL
|
||||
BASE_URL = "http://localhost:8000"
|
||||
|
||||
|
||||
def register_user(email, username, password):
|
||||
"""Register a new user"""
|
||||
response = requests.post(f"{BASE_URL}/register", json={
|
||||
"email": email,
|
||||
"username": username,
|
||||
"password": password
|
||||
})
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/register",
|
||||
json={"email": email, "username": username, "password": password},
|
||||
)
|
||||
return response.json()
|
||||
|
||||
|
||||
def login_user(username, password):
|
||||
"""Login and get JWT token"""
|
||||
response = requests.post(f"{BASE_URL}/login", json={
|
||||
"username": username,
|
||||
"password": password
|
||||
})
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/login", json={"username": username, "password": password}
|
||||
)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
return data["access_token"]
|
||||
@@ -28,24 +28,30 @@ def login_user(username, password):
|
||||
print(f"Login failed: {response.json()}")
|
||||
return None
|
||||
|
||||
|
||||
def get_user_info(token):
|
||||
"""Get current user info"""
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
response = requests.get(f"{BASE_URL}/me", headers=headers)
|
||||
return response.json()
|
||||
|
||||
|
||||
def get_products(token, skip=0, limit=10):
|
||||
"""Get products (requires authentication)"""
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
response = requests.get(f"{BASE_URL}/products?skip={skip}&limit={limit}", headers=headers)
|
||||
response = requests.get(
|
||||
f"{BASE_URL}/products?skip={skip}&limit={limit}", headers=headers
|
||||
)
|
||||
return response.json()
|
||||
|
||||
|
||||
def create_product(token, product_data):
|
||||
"""Create a new product (requires authentication)"""
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
response = requests.post(f"{BASE_URL}/products", json=product_data, headers=headers)
|
||||
return response.json()
|
||||
|
||||
|
||||
# Example usage
|
||||
if __name__ == "__main__":
|
||||
# 1. Register a new user
|
||||
@@ -55,18 +61,18 @@ if __name__ == "__main__":
|
||||
print(f"User registered: {user_result}")
|
||||
except Exception as e:
|
||||
print(f"Registration failed: {e}")
|
||||
|
||||
|
||||
# 2. Login with default admin user
|
||||
print("\n2. Logging in as admin...")
|
||||
admin_token = login_user("admin", "admin123")
|
||||
if admin_token:
|
||||
print(f"Admin login successful! Token: {admin_token[:50]}...")
|
||||
|
||||
|
||||
# 3. Get user info
|
||||
print("\n3. Getting admin user info...")
|
||||
user_info = get_user_info(admin_token)
|
||||
print(f"User info: {user_info}")
|
||||
|
||||
|
||||
# 4. Create a sample product
|
||||
print("\n4. Creating a sample product...")
|
||||
sample_product = {
|
||||
@@ -75,30 +81,32 @@ if __name__ == "__main__":
|
||||
"description": "A test product for demonstration",
|
||||
"price": "19.99",
|
||||
"brand": "Test Brand",
|
||||
"availability": "in stock"
|
||||
"availability": "in stock",
|
||||
}
|
||||
|
||||
|
||||
product_result = create_product(admin_token, sample_product)
|
||||
print(f"Product created: {product_result}")
|
||||
|
||||
|
||||
# 5. Get products list
|
||||
print("\n5. Getting products list...")
|
||||
products = get_products(admin_token)
|
||||
print(f"Products: {products}")
|
||||
|
||||
|
||||
# 6. Login with regular user
|
||||
print("\n6. Logging in as regular user...")
|
||||
user_token = login_user("testuser", "password123")
|
||||
if user_token:
|
||||
print(f"User login successful! Token: {user_token[:50]}...")
|
||||
|
||||
|
||||
# Regular users can also access protected endpoints
|
||||
user_info = get_user_info(user_token)
|
||||
print(f"Regular user info: {user_info}")
|
||||
|
||||
|
||||
products = get_products(user_token, limit=5)
|
||||
print(f"Products accessible to regular user: {len(products.get('products', []))} products")
|
||||
|
||||
print(
|
||||
f"Products accessible to regular user: {len(products.get('products', []))} products"
|
||||
)
|
||||
|
||||
print("\nAuthentication example completed!")
|
||||
|
||||
# Example cURL commands:
|
||||
@@ -126,4 +134,4 @@ curl -X POST "http://localhost:8000/products" \
|
||||
-H "Authorization: Bearer YOUR_JWT_TOKEN_HERE" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"product_id": "TEST001", "title": "Test Product", "price": "19.99"}'
|
||||
"""
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user