from test_main import * def test_add_friend(): login_response = client.post("/login", data={"username": "testuser", "password": "testpassword"}) token = login_response.json()["access_token"] # Register another user that will be used as a friend response = client.post("/register", json={"username": "testfriend", "password": "testpassword"}) user_id = response.json()["user_id"] friend_data = {"friend_user_id": user_id} response = client.post("/friend/add", json=friend_data, headers={"Authorization": f"Bearer {token}"}) assert response.status_code == 200 data = response.json() assert "id" in data friend_data = {"friend_user_id": str(db["users"].find_one({"username": "testuser"})["_id"])} response = client.post("/friend/add", json=friend_data, headers={"Authorization": f"Bearer {token}"}) assert response.status_code == 409 # Cannot add yourself as a friend def test_list_friends(): login_response = client.post("/login", data={"username": "testuser", "password": "testpassword"}) token = login_response.json()["access_token"] response = client.get("/friends", headers={"Authorization": f"Bearer {token}"}) assert response.status_code == 200 data = response.json() assert isinstance(data, dict) def test_get_friend(): login_response = client.post("/login", data={"username": "testuser", "password": "testpassword"}) token = login_response.json()["access_token"] friend_id = str(db["friends"].find_one({})["_id"]) response = client.get(f"/friend/{friend_id}", headers={"Authorization": f"Bearer {token}"}) assert response.status_code == 200 data = response.json() assert "id" in data assert "user_id" in data assert "friend_user_id" in data assert "status" in data # Test wrong format response = client.get(f"/friend/randomIdThatDoesntExists", headers={"Authorization": f"Bearer {token}"}) assert response.status_code == 422 # Test inexistant id response = client.get(f"/friend/123456789987654321abcdef", headers={"Authorization": f"Bearer {token}"}) assert response.status_code == 404