"""Validate iOS workflows in a disposable environment with a native_account row for viewer.""" import concurrent.futures from urllib.parse import urlencode from smoke import RUN, await_operation, call, ready def verify(): ready() verify_profiles() space = call("POST", "/api/households", {"name": "见物体验 " + RUN}, status=201) root = "/api/households/" + space["id"] try: verify_search_and_voice(root) verify_direct_invitations(root) finally: call("DELETE", root, status=202) def verify_profiles(): path = "/api/me/profile" call("GET", path, user=None, status=401) owner = call("GET", path) viewer = call("GET", path, user="viewer") assert viewer["username"] == "viewer" fields = ("nickname", "signature", "avatar") try: update = {"nickname": " 小见 ", "signature": " 把常用物放回原处 ", "avatar": "LEAF"} result = call("PUT", path, update) assert result == {"username": owner["username"], "nickname": "小见", "signature": "把常用物放回原处", "avatar": "LEAF"} assert call("GET", path) == result assert call("GET", path, user="viewer") == viewer call("PUT", path, dict(update, avatar="UNKNOWN"), status=400) call("PUT", path, dict(update, signature="字" * 101), status=400) assert call("GET", path) == result finally: call("PUT", path, {field: owner[field] for field in fields}) print("PASS private editable profile, account display and input validation", flush=True) def verify_search_and_voice(root): def location(name, parent=None): return call("POST", root + "/locations", {"name": name, "parentId": parent}, status=201)["id"] room = location("书房") drawer = location("右抽屉", room) entry = location("玄关") duplicate = location("右抽屉", entry) def item(name, place, aliases=None, quantity=1): return {"name": name, "quantity": quantity, "locationId": place, "notes": "", "aliases": aliases or []} exact = call("POST", root + "/items", item("数据线", drawer), status=201)["id"] alias = call("POST", root + "/items", item("充电配件", drawer, ["数据线"]), status=201)["id"] prefix = call("POST", root + "/items", item("数据线收纳包", duplicate), status=201)["id"] contains = call("POST", root + "/items", item("旧数据线备用", drawer), status=201)["id"] percent = call("POST", root + "/items", item("标签%尺寸", entry), status=201)["id"] assert call("GET", root + "/items?favorites=true") == [] call("PUT", root + "/items/" + exact + "/preference", {"favorite": True}) assert [row["id"] for row in call("GET", root + "/items?favorites=true")] == [exact] def search(term): return [row["id"] for row in call("GET", root + "/items?" + urlencode({"q": term}))] assert search("数据线") == [exact, alias, prefix, contains] assert set(search("书房 / 右抽屉")) == {exact, alias, contains} assert search("%") == [percent] call("GET", root + "/items?" + urlencode({"q": "数据线"}), user="other", status=404) print("PASS scoped weighted name/alias/path search and literal wildcard", flush=True) def draft(text): result = call("POST", root + "/drafts", {"text": text, "source": "VOICE"}, status=201) assert result["status"] == "READY" and result["source"] == "VOICE" return result for text in ("我没把钥匙放在书房右抽屉", "钥匙好像放在书房右抽屉", "两根线一个转接头放在书房右抽屉"): assert draft(text)["candidates"] == [] note = draft("我刚把两根转接线放在书房右抽屉了。") assert note["candidates"] == [{"name": "转接线", "quantity": 2, "action": "RECORD", "location": "书房右抽屉了"}] confirm = root + "/drafts/" + note["id"] + "/confirm" payload = {"version": note["version"], "items": [item("转接线", duplicate, quantity=2)], "automatic": True} call("POST", confirm, payload, status=409) payload["items"][0]["locationId"] = drawer with concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool: operations = list(pool.map(lambda _: call("POST", confirm, payload, status=202), range(2))) assert operations[0]["id"] == operations[1]["id"] await_operation(root + "/operations/" + operations[0]["id"], "COMPLETED") assert len(search("转接线")) == 1 print("PASS voice rules, automatic location guard and one outbox delivery", flush=True) def verify_direct_invitations(root): sent = root + "/direct-invitations" inbox = "/api/households/direct-invitations" call("POST", sent, {"username": "unknown-" + RUN, "role": "VIEWER"}, status=202) assert call("GET", sent) == [] call("GET", root + "/items", user="viewer", status=404) call("POST", sent, {"username": "ViEwEr", "role": "VIEWER"}, status=202) call("POST", sent, {"username": "viewer", "role": "EDITOR"}, status=202) outgoing, = call("GET", sent) assert outgoing["username"] == "viewer" and outgoing["role"] == "VIEWER" received, = [row for row in call("GET", inbox, user="viewer") if row["id"] == outgoing["id"]] assert received["role"] == "VIEWER" assert not any(row["id"] == outgoing["id"] for row in call("GET", inbox, user="other")) accept = inbox + "/" + outgoing["id"] + "/accept" call("POST", accept, user="other", status=404) with concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool: results = list(pool.map(lambda _: call("POST", accept, user="viewer"), range(2))) assert results[0] == results[1] and results[0]["role"] == "VIEWER" call("GET", root + "/items", user="viewer") call("POST", root + "/drafts", {"text": "数据线放在书房右抽屉", "source": "VOICE"}, user="viewer", status=403) call("POST", sent, {"username": "other", "role": "EDITOR"}, user="viewer", status=403) assert call("GET", sent) == [] print("PASS addressed invitations, private inbox, atomic acceptance and role checks", flush=True) if __name__ == "__main__": verify()