Files
bang-server/testclient.py
2025-06-06 19:56:06 -07:00

59 lines
1.8 KiB
Python

import requests
import json
def create_account(username, password):
url = "http://localhost:8080/users"
data = {"username": username, "password": password}
headers = {"Content-Type": "application/json"}
response = requests.post(url, data=json.dumps(data), headers=headers)
print("Create Account Status:", response.status_code)
try:
print("Create Account Response:", response.json())
except Exception:
print("Create Account Response Text:", response.text)
return response
def check_mailbox(username, password):
url = f"http://localhost:8080/mailbox?user={username}"
response = requests.get(url, auth=(username, password))
print("\nMailbox Status:", response.status_code)
try:
emails = response.json()
print(f"Mailbox for {username}:")
for email in emails:
print(json.dumps(email, indent=2))
except Exception:
print("Mailbox Response Text:", response.text)
return response
url = "http://localhost:8080/email"
email = {
"from": "alice",
"to": "eric",
"subject": "Hello from Python client",
"body": "This is a test email sent from the Python client."
}
headers = {"Content-Type": "application/json"}
# Set your test username and password here
username = "eric"
password = "1234" # Replace with the actual password for 'bob'
# Uncomment to create the account before sending email
# print(create_account(username, password))
response = requests.post(url, data=json.dumps(email), headers=headers, auth=(username, password))
print(check_mailbox(username, password))
print("Status Code:", response.status_code)
try:
print("Response:", response.json())
except Exception:
print("Response Text:", response.text)
# Uncomment to check mailbox after sending email
# check_mailbox(username, password)