Лаб6 - оценка 4
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import requests
|
||||
import json
|
||||
import time
|
||||
import base64
|
||||
|
||||
BASE = "http://localhost:8080"
|
||||
USERNAME = f"test{int(time.time())}"
|
||||
PASSWORD = "CorrectHorseBattery1"
|
||||
|
||||
def do_req(method, path, **kwargs):
|
||||
url = f"{BASE}{path}"
|
||||
r = requests.request(method, url, **kwargs)
|
||||
print(f"=== {method} {path} (Status: {r.status_code}) ===")
|
||||
try:
|
||||
print(json.dumps(r.json(), indent=2))
|
||||
except:
|
||||
print(r.text)
|
||||
print()
|
||||
return r
|
||||
|
||||
# 1. Create User
|
||||
r = do_req("POST", "/api/v1/users", json={"username": USERNAME, "display_name": "Test", "password": PASSWORD})
|
||||
assert r.status_code == 201
|
||||
|
||||
# 2. Login
|
||||
auth = base64.b64encode(f"{USERNAME}:{PASSWORD}".encode()).decode()
|
||||
r = do_req("POST", "/api/v1/auth/sessions", headers={"Authorization": f"Basic {auth}"})
|
||||
assert r.status_code == 201
|
||||
token = r.json()["token"]
|
||||
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
||||
|
||||
# 3. Get Me
|
||||
r = do_req("GET", "/api/v1/users/me", headers=headers)
|
||||
uid = r.json()["id"]
|
||||
|
||||
# 4. Create Chat
|
||||
r = do_req("POST", "/api/v1/chats", headers=headers, json={"title": "test", "member_ids": [uid]})
|
||||
assert r.status_code == 201
|
||||
chat_id = r.json()["id"]
|
||||
|
||||
# 5. Upload Attachment
|
||||
with open("../tests/fixtures/sample.png", "rb") as f:
|
||||
r = requests.post(f"{BASE}/api/v1/chats/{chat_id}/attachments?filename=test.png",
|
||||
headers={"Authorization": f"Bearer {token}", "Content-Type": "image/png"},
|
||||
data=f.read())
|
||||
print("=== POST /attachments (Status: {r.status_code}) ===")
|
||||
print(json.dumps(r.json(), indent=2))
|
||||
print()
|
||||
att_id = r.json()["id"]
|
||||
|
||||
# 6. Get Attachment Metadata
|
||||
r = do_req("GET", f"/api/v1/attachments/{att_id}", headers={"Authorization": f"Bearer {token}"})
|
||||
|
||||
# 7. Send Message with Attachment
|
||||
r = do_req("POST", f"/api/v1/chats/{chat_id}/messages",
|
||||
headers={**headers, "Idempotency-Key": "test-key-1"},
|
||||
json={"text": "hello", "attachment_ids": [att_id]})
|
||||
|
||||
# 8. Get Messages
|
||||
r = do_req("GET", f"/api/v1/chats/{chat_id}/messages", headers=headers)
|
||||
Reference in New Issue
Block a user