Лаб6 - оценка 4
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import requests
|
||||
import json
|
||||
import time
|
||||
import base64
|
||||
import hashlib
|
||||
|
||||
BASE = "http://localhost:8080"
|
||||
USERNAME = f"test{int(time.time())}"
|
||||
PASSWORD = "CorrectHorseBattery1"
|
||||
|
||||
print("1. Creating user...")
|
||||
r = requests.post(f"{BASE}/api/v1/users", json={"username": USERNAME, "display_name": "Test", "password": PASSWORD})
|
||||
print(" Status:", r.status_code)
|
||||
|
||||
print("2. Logging in...")
|
||||
auth = base64.b64encode(f"{USERNAME}:{PASSWORD}".encode()).decode()
|
||||
r = requests.post(f"{BASE}/api/v1/auth/sessions", headers={"Authorization": f"Basic {auth}"})
|
||||
token = r.json()["token"]
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
|
||||
print("3. Creating chat...")
|
||||
r = requests.get(f"{BASE}/api/v1/users/me", headers=headers)
|
||||
uid = r.json()["id"]
|
||||
r = requests.post(f"{BASE}/api/v1/chats", headers=headers, json={"title": "test", "member_ids": [uid]})
|
||||
chat_id = r.json()["id"]
|
||||
|
||||
print("4. Uploading attachment...")
|
||||
with open("../tests/fixtures/sample.png", "rb") as f:
|
||||
original_data = f.read()
|
||||
|
||||
r = requests.post(f"{BASE}/api/v1/chats/{chat_id}/attachments?filename=sample.png",
|
||||
headers={**headers, "Content-Type": "image/png"},
|
||||
data=original_data)
|
||||
att_id = r.json()["id"]
|
||||
print(" Attachment ID:", att_id)
|
||||
|
||||
print("5. Downloading attachment...")
|
||||
# Увеличиваем таймаут, чтобы исключить преждевременное отключение
|
||||
r = requests.get(f"{BASE}/api/v1/attachments/{att_id}/content", headers=headers, timeout=30)
|
||||
print(" Download Status:", r.status_code)
|
||||
print(" Content-Type:", r.headers.get("Content-Type"))
|
||||
print(" Content-Length header:", r.headers.get("Content-Length"))
|
||||
|
||||
downloaded_data = r.content
|
||||
print(" Original length:", len(original_data))
|
||||
print(" Downloaded length:", len(downloaded_data))
|
||||
|
||||
orig_hash = hashlib.sha256(original_data).hexdigest()
|
||||
down_hash = hashlib.sha256(downloaded_data).hexdigest()
|
||||
print(" Original SHA256: ", orig_hash)
|
||||
print(" Downloaded SHA256:", down_hash)
|
||||
|
||||
if original_data == downloaded_data:
|
||||
print("\n SUCCESS: Files match perfectly!")
|
||||
else:
|
||||
print("\n FAIL: Files differ!")
|
||||
print(" First 100 bytes of downloaded data:", downloaded_data[:100])
|
||||
Reference in New Issue
Block a user