forked from mtusi2026/mtusi2026lab2
Lab2 grade 4: PostgreSQL, Redis sessions, Compose, student tests and evidence.
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
"""Grade 3/4 API checks: auth, conflict, X-User-Id ignored, logout."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import time
|
||||
import uuid
|
||||
|
||||
from helpers import call, register
|
||||
|
||||
|
||||
def test_username_conflict():
|
||||
a = register("conflict")
|
||||
body = dict(a["registration"])
|
||||
call("POST", "/api/v1/users", 409, body=body)
|
||||
|
||||
|
||||
def test_wrong_password():
|
||||
a = register("wrongpw")
|
||||
bad = base64.b64encode(
|
||||
(a["registration"]["username"] + ":wrong-password-xx").encode()
|
||||
).decode()
|
||||
resp = call(
|
||||
"POST",
|
||||
"/api/v1/auth/sessions",
|
||||
401,
|
||||
headers={"Authorization": "Basic " + bad},
|
||||
)
|
||||
assert resp["headers"].get("WWW-Authenticate", "").lower().startswith("basic")
|
||||
|
||||
|
||||
def test_x_user_id_ignored():
|
||||
a = register("xu")
|
||||
b = register("xv")
|
||||
me = call(
|
||||
"GET",
|
||||
"/api/v1/users/me",
|
||||
200,
|
||||
token=a["token"],
|
||||
headers={"X-User-Id": b["id"]},
|
||||
)["json"]
|
||||
assert me["id"] == a["id"]
|
||||
|
||||
|
||||
def test_basic_on_business_rejected():
|
||||
a = register("basicbiz")
|
||||
cred = base64.b64encode(
|
||||
(a["registration"]["username"] + ":" + a["registration"]["password"]).encode()
|
||||
).decode()
|
||||
call(
|
||||
"GET",
|
||||
"/api/v1/users/me",
|
||||
401,
|
||||
headers={"Authorization": "Basic " + cred},
|
||||
)
|
||||
|
||||
|
||||
def test_logout_revokes():
|
||||
a = register("logout")
|
||||
call("DELETE", "/api/v1/auth/sessions/current", 204, token=a["token"])
|
||||
call("GET", "/api/v1/users/me", 401, token=a["token"])
|
||||
|
||||
|
||||
def test_chat_requires_caller_member():
|
||||
a = register("owner")
|
||||
b = register("peer")
|
||||
call(
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
400,
|
||||
token=a["token"],
|
||||
body={"title": "no me", "member_ids": [b["id"]]},
|
||||
)
|
||||
|
||||
|
||||
def test_unknown_member_rolls_back():
|
||||
a = register("atom")
|
||||
fake = str(uuid.uuid4())
|
||||
call(
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
400,
|
||||
token=a["token"],
|
||||
body={"title": "bad", "member_ids": [a["id"], fake]},
|
||||
)
|
||||
|
||||
|
||||
def test_whitespace_only_password_rejected():
|
||||
# Maintainer remark: required strings must not be whitespace-only.
|
||||
body = {
|
||||
"username": "ws_" + uuid.uuid4().hex[:10],
|
||||
"display_name": "Spaces",
|
||||
"password": " " * 12,
|
||||
}
|
||||
call("POST", "/api/v1/users", 400, body=body)
|
||||
|
||||
|
||||
def test_nul_in_text_rejected():
|
||||
a = register("nul")
|
||||
peer = register("nulb")
|
||||
chat = call(
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
201,
|
||||
token=a["token"],
|
||||
body={"title": "nul-chat", "member_ids": [a["id"], peer["id"]]},
|
||||
)["json"]
|
||||
call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
400,
|
||||
token=a["token"],
|
||||
body={"text": "hello\u0000world"},
|
||||
)
|
||||
|
||||
|
||||
def test_login_rejects_json_body():
|
||||
a = register("bodylogin")
|
||||
cred = base64.b64encode(
|
||||
(a["registration"]["username"] + ":" + a["registration"]["password"]).encode()
|
||||
).decode()
|
||||
call(
|
||||
"POST",
|
||||
"/api/v1/auth/sessions",
|
||||
400,
|
||||
body={"unexpected": True},
|
||||
headers={"Authorization": "Basic " + cred},
|
||||
)
|
||||
|
||||
|
||||
def test_invalid_basic_encoding_is_401():
|
||||
# Maintainer remark: malformed Basic must be 401, not 500.
|
||||
call(
|
||||
"POST",
|
||||
"/api/v1/auth/sessions",
|
||||
401,
|
||||
headers={"Authorization": "Basic " + base64.b64encode("é".encode()).decode()},
|
||||
)
|
||||
|
||||
|
||||
def test_unsupported_media_type():
|
||||
a = register("ctype")
|
||||
peer = register("ctypeb")
|
||||
chat = call(
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
201,
|
||||
token=a["token"],
|
||||
body={"title": "ctype", "member_ids": [a["id"], peer["id"]]},
|
||||
)["json"]
|
||||
raw = b'{"text":"x"}'
|
||||
call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
415,
|
||||
token=a["token"],
|
||||
raw=raw,
|
||||
headers={"Content-Type": "text/plain"},
|
||||
)
|
||||
Reference in New Issue
Block a user