order button
This commit is contained in:
176
trade-hub/backend/tests/test_orders.py
Normal file
176
trade-hub/backend/tests/test_orders.py
Normal file
@@ -0,0 +1,176 @@
|
||||
"""Public order board: authenticated creation, public read, owner-only edits."""
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.api import app
|
||||
|
||||
PASSWORD = "correct horse battery staple"
|
||||
CARROCK = "ANVL_Carrack"
|
||||
RIFLE = "BEHR_Rifle_Ballistic_01"
|
||||
IRON_ORE = "Carryable_TBO_FL_1SCU_Commodity_Metal_Ore_Iron"
|
||||
CONSTRUCTION = "Carryable_TBO_FL_1SCU_Commodity_ProcessedGoods_ConstructionMaterials"
|
||||
|
||||
|
||||
def authed_client(username: str) -> TestClient:
|
||||
client = TestClient(app)
|
||||
resp = client.post(
|
||||
"/api/v1/auth/register", json={"username": username, "password": PASSWORD}
|
||||
)
|
||||
assert resp.status_code == 201, resp.text
|
||||
return client
|
||||
|
||||
|
||||
def place(
|
||||
client: TestClient,
|
||||
class_name: str,
|
||||
quantity: int = 1,
|
||||
notes: str = "",
|
||||
quality: int | None = None,
|
||||
):
|
||||
payload = {"class_name": class_name, "quantity": quantity, "notes": notes}
|
||||
if quality is not None:
|
||||
payload["quality"] = quality
|
||||
return client.post("/api/v1/orders", json=payload)
|
||||
|
||||
|
||||
def public_orders(**params):
|
||||
return TestClient(app).get("/api/v1/orders", params=params).json()
|
||||
|
||||
|
||||
def test_order_board_is_public_and_empty_by_default():
|
||||
body = public_orders()
|
||||
assert body["total"] == 0
|
||||
assert body["items"] == []
|
||||
assert body["page"] == 1
|
||||
|
||||
|
||||
def test_placing_an_order_requires_authentication():
|
||||
assert place(TestClient(app), CARROCK).status_code == 401
|
||||
|
||||
|
||||
def test_placing_an_order_returns_enriched_public_entry():
|
||||
alice = authed_client("alice")
|
||||
resp = place(alice, CARROCK, quantity=2, notes="Need a hauler")
|
||||
assert resp.status_code == 201, resp.text
|
||||
order = resp.json()
|
||||
assert order["class_name"] == CARROCK
|
||||
assert order["name"] == "Anvil Carrack"
|
||||
assert order["category"] == "ships"
|
||||
assert order["manufacturer"]["code"] == "ANVL"
|
||||
assert order["quantity"] == 2
|
||||
assert order["notes"] == "Need a hauler"
|
||||
assert order["username"] == "alice"
|
||||
assert order["status"] == "open"
|
||||
assert order["available"] is True
|
||||
|
||||
public = public_orders()
|
||||
assert public["total"] == 1
|
||||
assert public["items"][0]["id"] == order["id"]
|
||||
assert public["items"][0]["username"] == "alice"
|
||||
|
||||
|
||||
def test_orders_from_every_user_are_visible():
|
||||
place(authed_client("alice"), CARROCK)
|
||||
place(authed_client("bob"), RIFLE)
|
||||
|
||||
items = public_orders(sort="name")["items"]
|
||||
assert [(i["username"], i["class_name"]) for i in items] == [
|
||||
("alice", CARROCK),
|
||||
("bob", RIFLE),
|
||||
]
|
||||
|
||||
|
||||
def test_order_quality_is_stored_and_public_for_ore():
|
||||
alice = authed_client("alice")
|
||||
resp = place(alice, IRON_ORE, quantity=10, quality=875)
|
||||
assert resp.status_code == 201, resp.text
|
||||
assert resp.json()["quality"] == 875
|
||||
assert resp.json()["quality_applicable"] is True
|
||||
|
||||
item = public_orders()["items"][0]
|
||||
assert item["quality"] == 875
|
||||
assert item["quality_applicable"] is True
|
||||
|
||||
|
||||
def test_quality_is_rejected_for_non_mineable_items():
|
||||
alice = authed_client("alice")
|
||||
assert place(alice, CONSTRUCTION, quality=500).status_code == 422
|
||||
assert place(alice, CARROCK, quality=100).status_code == 422
|
||||
assert place(alice, IRON_ORE, quality=1001).status_code == 422
|
||||
|
||||
|
||||
def test_unknown_catalog_item_is_rejected():
|
||||
assert place(authed_client("alice"), "NOPE_Does_Not_Exist").status_code == 404
|
||||
|
||||
|
||||
def test_listing_filters_by_category_search_and_status():
|
||||
alice = authed_client("alice")
|
||||
bob = authed_client("bob")
|
||||
first = place(alice, CARROCK).json()
|
||||
place(bob, RIFLE)
|
||||
place(alice, IRON_ORE, quality=400)
|
||||
|
||||
assert [i["class_name"] for i in public_orders(category="ships")["items"]] == [
|
||||
CARROCK
|
||||
]
|
||||
assert [i["username"] for i in public_orders(search="bob")["items"]] == ["bob"]
|
||||
assert [
|
||||
i["class_name"] for i in public_orders(search="iron")["items"]
|
||||
] == [IRON_ORE]
|
||||
|
||||
alice.patch(f"/api/v1/orders/{first['id']}", json={"status": "fulfilled"})
|
||||
assert [i["class_name"] for i in public_orders(status="open")["items"]] == [
|
||||
IRON_ORE,
|
||||
RIFLE,
|
||||
]
|
||||
assert [i["class_name"] for i in public_orders(status="fulfilled")["items"]] == [
|
||||
CARROCK
|
||||
]
|
||||
|
||||
|
||||
def test_listing_paginates_and_rejects_unknown_sort():
|
||||
alice = authed_client("alice")
|
||||
place(alice, CARROCK)
|
||||
place(alice, RIFLE)
|
||||
|
||||
body = public_orders(page=2, page_size=1)
|
||||
assert body["total"] == 2
|
||||
assert body["total_pages"] == 2
|
||||
assert len(body["items"]) == 1
|
||||
|
||||
assert public_orders(page_size=500)["page_size"] == 100
|
||||
assert TestClient(app).get("/api/v1/orders?sort=bogus").status_code == 422
|
||||
assert TestClient(app).get("/api/v1/orders?status=bogus").status_code == 422
|
||||
|
||||
|
||||
def test_only_the_owner_can_edit_or_remove_an_order():
|
||||
alice = authed_client("alice")
|
||||
bob = authed_client("bob")
|
||||
order = place(alice, IRON_ORE, quality=500).json()
|
||||
|
||||
assert (
|
||||
bob.patch(
|
||||
f"/api/v1/orders/{order['id']}", json={"quantity": 99}
|
||||
).status_code
|
||||
== 404
|
||||
)
|
||||
assert bob.delete(f"/api/v1/orders/{order['id']}").status_code == 404
|
||||
|
||||
updated = alice.patch(
|
||||
f"/api/v1/orders/{order['id']}",
|
||||
json={"quantity": 3, "notes": "Updated", "status": "fulfilled"},
|
||||
)
|
||||
assert updated.status_code == 200, updated.text
|
||||
body = updated.json()
|
||||
assert body["quantity"] == 3
|
||||
assert body["notes"] == "Updated"
|
||||
assert body["status"] == "fulfilled"
|
||||
assert body["quality"] == 500
|
||||
|
||||
cleared = alice.patch(
|
||||
f"/api/v1/orders/{order['id']}", json={"quality": None}
|
||||
)
|
||||
assert cleared.status_code == 200, cleared.text
|
||||
assert cleared.json()["quality"] is None
|
||||
|
||||
assert alice.delete(f"/api/v1/orders/{order['id']}").status_code == 204
|
||||
assert public_orders()["total"] == 0
|
||||
Reference in New Issue
Block a user