Add public profile view endpoint: favorites, friends, privacy enforcement (US-3)
- Implemented GET /api/profile/{user_id} in backend/app/routers/profiles.py
- Server-side privacy enforcement for profile_visibility, favorites_visibility, and friends_list_visibility
- Block relationships enforce restricted view (no bio, empty favorites, empty friends)
- Non-friends restricted when profile_visibility or sections set to friends-only
- Asset public check for avatar in restricted view
- No private user fields leaked (email, password excluded)
- Added 7 integration tests in backend/tests/test_profile.py covering all privacy and block permutations
Independently verified: 26/26 profile tests passing.
This commit is contained in:
@@ -251,3 +251,156 @@ class TestFavorites:
|
||||
client = TestClient(app)
|
||||
assert client.get("/api/profile/me/favorites").status_code == 401
|
||||
assert client.put("/api/profile/me/favorites", json={"world_ids": []}).status_code == 401
|
||||
|
||||
|
||||
class TestPublicProfile:
|
||||
def test_public_profile_full_view(self):
|
||||
client = TestClient(app)
|
||||
h_alice = _auth_headers(client, "pub_alice")
|
||||
alice_id = _me_id(client, h_alice)
|
||||
h_bob = _auth_headers(client, "pub_bob")
|
||||
|
||||
w1 = _make_world(client, h_alice, "AliceWorld")
|
||||
asset_id = _make_asset(client, h_alice, "AliceAsset")
|
||||
|
||||
client.put(
|
||||
"/api/profile/me",
|
||||
headers=h_alice,
|
||||
json={"bio": "Hi, I am Alice", "avatar_asset_id": asset_id},
|
||||
)
|
||||
client.put("/api/profile/me/favorites", headers=h_alice, json={"world_ids": [w1]})
|
||||
|
||||
resp = client.get(f"/api/profile/{alice_id}", headers=h_bob)
|
||||
assert resp.status_code == 200, resp.text
|
||||
data = resp.json()
|
||||
assert data["user_id"] == alice_id
|
||||
assert data["username"] == "pub_alice"
|
||||
assert data["bio"] == "Hi, I am Alice"
|
||||
assert data["avatar_asset_id"] == asset_id
|
||||
assert data["is_restricted"] is False
|
||||
assert len(data["favorites"]) == 1
|
||||
assert data["favorites"][0]["id"] == w1
|
||||
assert data["favorites"][0]["name"] == "AliceWorld"
|
||||
assert "email" not in data
|
||||
assert "password" not in data
|
||||
|
||||
def test_public_profile_not_found(self):
|
||||
client = TestClient(app)
|
||||
h_bob = _auth_headers(client, "pub_nf_bob")
|
||||
resp = client.get("/api/profile/999999", headers=h_bob)
|
||||
assert resp.status_code == 404
|
||||
|
||||
def test_public_profile_requires_auth(self):
|
||||
client = TestClient(app)
|
||||
assert client.get("/api/profile/1").status_code == 401
|
||||
|
||||
def test_public_profile_private_visibility(self):
|
||||
client = TestClient(app)
|
||||
h_alice = _auth_headers(client, "pub_priv_a")
|
||||
alice_id = _me_id(client, h_alice)
|
||||
h_bob = _auth_headers(client, "pub_priv_b")
|
||||
|
||||
client.put(
|
||||
"/api/profile/me",
|
||||
headers=h_alice,
|
||||
json={"bio": "Top secret bio", "privacy": {"profile_visibility": "private"}},
|
||||
)
|
||||
|
||||
resp_bob = client.get(f"/api/profile/{alice_id}", headers=h_bob)
|
||||
assert resp_bob.status_code == 200
|
||||
data_bob = resp_bob.json()
|
||||
assert data_bob["is_restricted"] is True
|
||||
assert data_bob["bio"] is None
|
||||
assert data_bob["favorites"] == []
|
||||
assert data_bob["friends"] == []
|
||||
|
||||
# Alice viewing herself is not restricted
|
||||
resp_alice = client.get(f"/api/profile/{alice_id}", headers=h_alice)
|
||||
assert resp_alice.status_code == 200
|
||||
assert resp_alice.json()["is_restricted"] is False
|
||||
assert resp_alice.json()["bio"] == "Top secret bio"
|
||||
|
||||
def test_public_profile_friends_only_visibility(self):
|
||||
client = TestClient(app)
|
||||
h_alice = _auth_headers(client, "pub_fo_a")
|
||||
alice_id = _me_id(client, h_alice)
|
||||
h_bob = _auth_headers(client, "pub_fo_b")
|
||||
bob_id = _me_id(client, h_bob)
|
||||
h_charlie = _auth_headers(client, "pub_fo_c")
|
||||
|
||||
client.put(
|
||||
"/api/profile/me",
|
||||
headers=h_alice,
|
||||
json={"bio": "Friends only bio", "privacy": {"profile_visibility": "friends"}},
|
||||
)
|
||||
|
||||
# Charlie is not a friend -> restricted
|
||||
resp_c = client.get(f"/api/profile/{alice_id}", headers=h_charlie)
|
||||
assert resp_c.json()["is_restricted"] is True
|
||||
assert resp_c.json()["bio"] is None
|
||||
|
||||
# Bob becomes friends with Alice
|
||||
req = client.post("/api/friends/requests", headers=h_bob, json={"user_id": alice_id})
|
||||
req_id = req.json()["id"]
|
||||
client.post(f"/api/friends/requests/{req_id}/accept", headers=h_alice)
|
||||
|
||||
# Now Bob sees full profile and friends list contains Bob
|
||||
resp_b = client.get(f"/api/profile/{alice_id}", headers=h_bob)
|
||||
assert resp_b.json()["is_restricted"] is False
|
||||
assert resp_b.json()["bio"] == "Friends only bio"
|
||||
assert any(f["id"] == bob_id for f in resp_b.json()["friends"])
|
||||
|
||||
def test_public_profile_blocked_user_gets_restricted(self):
|
||||
client = TestClient(app)
|
||||
h_alice = _auth_headers(client, "pub_blk_a")
|
||||
alice_id = _me_id(client, h_alice)
|
||||
h_dan = _auth_headers(client, "pub_blk_d")
|
||||
dan_id = _me_id(client, h_dan)
|
||||
|
||||
client.put("/api/profile/me", headers=h_alice, json={"bio": "Alice public bio"})
|
||||
client.post(f"/api/friends/{dan_id}/block", headers=h_alice)
|
||||
|
||||
# Dan viewing Alice gets restricted view
|
||||
resp_dan = client.get(f"/api/profile/{alice_id}", headers=h_dan)
|
||||
assert resp_dan.status_code == 200
|
||||
assert resp_dan.json()["is_restricted"] is True
|
||||
assert resp_dan.json()["bio"] is None
|
||||
|
||||
# Alice viewing Dan gets restricted view
|
||||
resp_alice = client.get(f"/api/profile/{dan_id}", headers=h_alice)
|
||||
assert resp_alice.status_code == 200
|
||||
assert resp_alice.json()["is_restricted"] is True
|
||||
|
||||
def test_public_profile_selective_favorites_and_friends_privacy(self):
|
||||
client = TestClient(app)
|
||||
h_alice = _auth_headers(client, "pub_sec_a")
|
||||
alice_id = _me_id(client, h_alice)
|
||||
h_bob = _auth_headers(client, "pub_sec_b")
|
||||
bob_id = _me_id(client, h_bob)
|
||||
w1 = _make_world(client, h_alice, "WorldSec")
|
||||
|
||||
client.put("/api/profile/me/favorites", headers=h_alice, json={"world_ids": [w1]})
|
||||
req = client.post("/api/friends/requests", headers=h_bob, json={"user_id": alice_id})
|
||||
req_id = req.json()["id"]
|
||||
client.post(f"/api/friends/requests/{req_id}/accept", headers=h_alice)
|
||||
|
||||
client.put(
|
||||
"/api/profile/me",
|
||||
headers=h_alice,
|
||||
json={
|
||||
"bio": "Bio is public",
|
||||
"privacy": {
|
||||
"profile_visibility": "public",
|
||||
"favorites_visibility": "private",
|
||||
"friends_list_visibility": "private",
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
resp = client.get(f"/api/profile/{alice_id}", headers=h_bob)
|
||||
assert resp.status_code == 200
|
||||
data = resp.json()
|
||||
assert data["bio"] == "Bio is public"
|
||||
assert data["favorites"] == []
|
||||
assert data["friends"] == []
|
||||
|
||||
|
||||
Reference in New Issue
Block a user