"""Negative, pagination and auth tests for lab 2.""" import uuid from urllib.parse import quote from client import Client PASSWORD = "Example-only-not-a-real-secret-123" def register(c: Client, username=None): username = username or ("u_" + uuid.uuid4().hex[:10]) user = c.call( "POST", "/api/v1/users", 201, body={"username": username, "display_name": "Test User", "password": PASSWORD}, schema="User", )["json"] return user def login(c: Client, username, password=PASSWORD): import base64 cred = base64.b64encode((username + ":" + password).encode()).decode() session = c.call( "POST", "/api/v1/auth/sessions", 201, headers={"Authorization": "Basic " + cred}, schema="Session", )["json"] return {"id": session["user"]["id"], "user": session["user"], "token": session["token"], "registration": {"username": username, "password": password}} def actor_of(c: Client, username=None): user = register(c, username) return login(c, user["username"]) def test_malformed_json(live: Client): response = live.call( "POST", "/api/v1/users", 400, raw=b"{not-json", headers={"Content-Type": "application/json"}, ) assert response["json"]["error"]["code"] == "invalid_request" assert response["headers"].get("X-Request-Id") def test_duplicate_username(live: Client): user = register(live, username="dup_" + uuid.uuid4().hex[:8]) again = live.call( "POST", "/api/v1/users", 409, body={"username": user["username"], "display_name": "Other", "password": PASSWORD}, ) assert again["json"]["error"]["code"] == "conflict" def test_empty_message_rejected(live: Client): alice = actor_of(live) bob = actor_of(live) chat = live.call( "POST", "/api/v1/chats", 201, actor=alice, body={"title": "t", "member_ids": [alice["id"], bob["id"]]}, schema="Chat", )["json"] response = live.call( "POST", "/api/v1/chats/" + chat["id"] + "/messages", 400, actor=alice, body={"text": " "}, ) assert response["status"] == 400 def test_outsider_chat_is_404(live: Client): alice = actor_of(live) bob = actor_of(live) eve = actor_of(live) chat = live.call( "POST", "/api/v1/chats", 201, actor=alice, body={"title": "secret", "member_ids": [alice["id"], bob["id"]]}, schema="Chat", )["json"] response = live.call("GET", "/api/v1/chats/" + chat["id"], 404, actor=eve) assert response["json"]["error"]["code"] == "not_found" def test_message_pagination_multiple_pages(live: Client): alice = actor_of(live) bob = actor_of(live) chat = live.call( "POST", "/api/v1/chats", 201, actor=alice, body={"title": "pages", "member_ids": [alice["id"], bob["id"]]}, schema="Chat", )["json"] created = [] for idx in range(5): created.append(live.message(alice, chat, "m%s" % idx)["json"]) collected = [] cursor = None for _ in range(10): path = "/api/v1/chats/%s/messages?limit=2" % chat["id"] if cursor: path += "&cursor=" + quote(cursor, safe="") page = live.call("GET", path, actor=bob, schema="MessagePage")["json"] collected.extend(page["items"]) cursor = page["next_cursor"] if cursor is None: break assert len(collected) == 5 ordered = sorted(created, key=lambda x: (x["created_at"], x["id"])) assert collected == ordered def test_cursor_from_other_chat_rejected(live: Client): alice = actor_of(live) bob = actor_of(live) chat_a = live.call( "POST", "/api/v1/chats", 201, actor=alice, body={"title": "a", "member_ids": [alice["id"], bob["id"]]}, schema="Chat", )["json"] chat_b = live.call( "POST", "/api/v1/chats", 201, actor=alice, body={"title": "b", "member_ids": [alice["id"], bob["id"]]}, schema="Chat", )["json"] msg = live.message(alice, chat_a, "only in a")["json"] page = live.call( "GET", "/api/v1/chats/%s/messages?limit=1" % chat_a["id"], actor=alice, schema="MessagePage", )["json"] assert page["items"][0]["id"] == msg["id"] cursor = page["next_cursor"] # If history is a single item, next_cursor is null; encode a foreign cursor via first page of A after a dummy extra message. if cursor is None: live.message(alice, chat_a, "second") page = live.call( "GET", "/api/v1/chats/%s/messages?limit=1" % chat_a["id"], actor=alice, schema="MessagePage", )["json"] cursor = page["next_cursor"] response = live.call( "GET", "/api/v1/chats/%s/messages?limit=10&cursor=%s" % (chat_b["id"], quote(cursor, safe="")), 400, actor=alice, ) assert response["status"] == 400 def test_x_user_id_does_not_authenticate(live: Client): alice = actor_of(live) live.call("GET", "/api/v1/users/me", 401, headers={"X-User-Id": alice["id"]}) def test_wrong_password(live: Client): user = register(live) import base64 bad = base64.b64encode((user["username"] + ":wrong-password-xx").encode()).decode() live.call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": "Basic " + bad}) def test_logout(live: Client): alice = actor_of(live) live.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=alice) live.call("GET", "/api/v1/users/me", 401, actor=alice)