"""Authentication and registration tests (lab2 auth, unchanged in lab3).""" from __future__ import annotations import base64 import os def _assert_error(response, status, code, challenge_prefix=None): assert response.status == status, (response.status, response.body) assert response.content_type == "application/json" payload = response.json assert payload["error"]["code"] == code assert response.request_id, "missing X-Request-Id" assert payload["error"]["request_id"] == response.request_id if challenge_prefix: header = response.headers.get("WWW-Authenticate", "") assert header.lower().startswith(challenge_prefix.lower()), header def test_registration_requires_password(client): response = client.request( "POST", "/api/v1/users", body={"username": "no_pw_" + os.urandom(3).hex(), "display_name": "X"} ) _assert_error(response, 400, "invalid_request") def test_short_password_is_400(client): response = client.register("shortpw_" + os.urandom(3).hex(), "too-short") _assert_error(response, 400, "invalid_request") def test_extra_field_is_400(client): response = client.request( "POST", "/api/v1/users", body={ "username": "extra_" + os.urandom(3).hex(), "display_name": "X", "password": "T3st-" + os.urandom(8).hex(), "role": "admin", }, ) _assert_error(response, 400, "invalid_request") def test_malformed_json_is_400(client): response = client.request("POST", "/api/v1/users", raw=b"{not json") _assert_error(response, 400, "invalid_request") def test_duplicate_username_is_409(client): username = "dup_" + os.urandom(4).hex() password = "T3st-" + os.urandom(8).hex() assert client.register(username, password).status == 201 _assert_error(client.register(username, password), 409, "conflict") def test_registration_never_returns_password(client): response = client.register("pw_hidden_" + os.urandom(3).hex(), "T3st-" + os.urandom(8).hex()) assert response.status == 201, response.body assert "password" not in response.json def test_wrong_password_is_401_basic(client, alice): _assert_error( client.login(alice["username"], "definitely-wrong-password"), 401, "unauthorized", challenge_prefix="Basic", ) def test_unknown_user_is_401_basic(client): _assert_error( client.login("ghost_" + os.urandom(4).hex(), "T3st-" + os.urandom(8).hex()), 401, "unauthorized", challenge_prefix="Basic", ) def test_basic_on_business_endpoint_is_401_bearer(client, alice): credential = base64.b64encode(f"{alice['username']}:{alice['password']}".encode()).decode() response = client.request("GET", "/api/v1/users/me", basic=credential) _assert_error(response, 401, "unauthorized", challenge_prefix="Bearer") def test_missing_auth_is_401_bearer(client): _assert_error(client.request("GET", "/api/v1/users/me"), 401, "unauthorized", challenge_prefix="Bearer") def test_unknown_bearer_is_401(client): _assert_error( client.me("not-a-real-token-" + os.urandom(8).hex()), 401, "unauthorized", challenge_prefix="Bearer", ) def test_x_user_id_is_ignored(client, alice, bob): only_header = client.request("GET", "/api/v1/users/me", headers={"X-User-Id": alice["id"]}) _assert_error(only_header, 401, "unauthorized", challenge_prefix="Bearer") response = client.request( "GET", "/api/v1/users/me", token=alice["token"], headers={"X-User-Id": bob["id"]} ) assert response.status == 200, response.body assert response.json["id"] == alice["id"] def test_logout_revokes_session(client): suffix = os.urandom(4).hex() username = f"s_out_{suffix}" password = "T3st-" + os.urandom(8).hex() assert client.register(username, password).status == 201 token = client.login(username, password).json["token"] assert client.me(token).status == 200 logout = client.logout(token) assert logout.status == 204, logout.body assert not logout.body _assert_error(client.me(token), 401, "unauthorized", challenge_prefix="Bearer") _assert_error(client.logout(token), 401, "unauthorized", challenge_prefix="Bearer")