forked from mtusi2026/mtusi2026lab2
feat-(additional test succes)
This commit is contained in:
+32
-25
@@ -2,11 +2,38 @@ import os
|
|||||||
import pytest
|
import pytest
|
||||||
import uuid
|
import uuid
|
||||||
import requests
|
import requests
|
||||||
|
import base64
|
||||||
|
|
||||||
|
|
||||||
BASE_URL = os.environ.get("BASE_URL", "http://localhost:8080")
|
BASE_URL = os.environ.get("BASE_URL", "http://localhost:8080")
|
||||||
|
TEST_PASSWORD = "SecurePassword7654321"
|
||||||
|
|
||||||
|
|
||||||
|
def create_and_login_user(base_url, display_name_prefix):
|
||||||
|
username = f"st_{display_name_prefix.lower()}_{uuid.uuid4().hex[:8]}"
|
||||||
|
|
||||||
|
registr_response = requests.post(f"{base_url}/api/v1/users",
|
||||||
|
json={
|
||||||
|
"username": username,
|
||||||
|
"display_name": f"{display_name_prefix} Test",
|
||||||
|
"password": TEST_PASSWORD})
|
||||||
|
assert registr_response.status_code == 201, f"Registration failed: {registr_response.text}"
|
||||||
|
user_data = registr_response.json()
|
||||||
|
|
||||||
|
credentials = base64.b64encode(f"{username}:{TEST_PASSWORD}".encode()).decode()
|
||||||
|
login_response = requests.post(f"{base_url}/api/v1/auth/sessions",
|
||||||
|
headers={"Authorization": f"Basic {credentials}"})
|
||||||
|
assert login_response.status_code == 201, f"Login failed: {login_response.text}"
|
||||||
|
token = login_response.json()["token"]
|
||||||
|
|
||||||
|
return {
|
||||||
|
"id": user_data["id"],
|
||||||
|
"username": username,
|
||||||
|
"user": user_data,
|
||||||
|
"headers": {"Authorization": f"Bearer {token}"}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
def base_url():
|
def base_url():
|
||||||
return BASE_URL
|
return BASE_URL
|
||||||
@@ -14,43 +41,23 @@ def base_url():
|
|||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
def alice(base_url):
|
def alice(base_url):
|
||||||
username = f"st_alice_{uuid.uuid4().hex[:8]}"
|
return create_and_login_user(base_url, "Alice")
|
||||||
response = requests.post(f"{base_url}/api/v1/users", json={
|
|
||||||
"username": username,
|
|
||||||
"display_name": "Alice Student",
|
|
||||||
})
|
|
||||||
assert response.status_code == 201, response.text
|
|
||||||
return {"id": response.json()["id"], "username": username, "user": response.json()}
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
def bob(base_url):
|
def bob(base_url):
|
||||||
username = f"st_bob_{uuid.uuid4().hex[:8]}"
|
return create_and_login_user(base_url, "Bob")
|
||||||
response = requests.post(f"{base_url}/api/v1/users", json={
|
|
||||||
"username": username,
|
|
||||||
"display_name": "Bob Student",
|
|
||||||
})
|
|
||||||
assert response.status_code == 201, response.text
|
|
||||||
return {"id": response.json()["id"], "username": username, "user": response.json()}
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
def eve(base_url):
|
def eve(base_url):
|
||||||
username = f"st_eve_{uuid.uuid4().hex[:8]}"
|
return create_and_login_user(base_url, "Eve")
|
||||||
response = requests.post(f"{base_url}/api/v1/users", json={
|
|
||||||
"username": username,
|
|
||||||
"display_name": "Eve Student",
|
|
||||||
})
|
|
||||||
assert response.status_code == 201, response.text
|
|
||||||
return {"id": response.json()["id"], "username": username, "user": response.json()}
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
def chat(base_url, alice, bob):
|
def chat(base_url, alice, bob):
|
||||||
|
|
||||||
response = requests.post(f"{base_url}/api/v1/chats", headers={
|
response = requests.post(f"{base_url}/api/v1/chats", headers=alice["headers"], json={
|
||||||
"X-User-Id": alice["id"]
|
|
||||||
}, json={
|
|
||||||
"title": "Student Test Chat",
|
"title": "Student Test Chat",
|
||||||
"member_ids": [alice["id"], bob["id"]]
|
"member_ids": [alice["id"], bob["id"]]
|
||||||
})
|
})
|
||||||
@@ -58,4 +65,4 @@ def chat(base_url, alice, bob):
|
|||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
def auth(actor):
|
def auth(actor):
|
||||||
return {"X-User-Id": actor["id"]}
|
return actor["headers"]
|
||||||
|
|||||||
@@ -62,7 +62,8 @@ def test_duplicate_username(base_url, alice):
|
|||||||
response = requests.post(f"{base_url}/api/v1/users", json={
|
response = requests.post(f"{base_url}/api/v1/users", json={
|
||||||
"username": alice["username"],
|
"username": alice["username"],
|
||||||
"display_name": "Another Alice",
|
"display_name": "Another Alice",
|
||||||
})
|
"password": "SecurePassword7654321"}
|
||||||
|
)
|
||||||
|
|
||||||
assert response.status_code == 409
|
assert response.status_code == 409
|
||||||
assert response.json()["error"]["code"] == "conflict"
|
assert response.json()["error"]["code"] == "conflict"
|
||||||
|
|||||||
Reference in New Issue
Block a user