# Fakes shared by pass-broker-test.nix and pass-broker-gateway-test.nix: a # pass-cli that only logs in with the expected PAT (and refuses one on its # command line), and a Telegram API on 127.0.0.1:8081 whose button presses # the test script injects via /test/press. { pkgs }: { passCli = pkgs.writeShellScriptBin "pass-cli" '' set -eu case "$*" in *pst_*) echo "PAT on the command line" >&2; exit 99 ;; esac s="$PROTON_PASS_SESSION_DIR"; mkdir -p "$s" case "$1" in info) [ -f "$s/ok" ] ;; logout) rm -f "$s/ok" ;; login) [ "''${PROTON_PASS_PERSONAL_ACCESS_TOKEN:-}" = "pst_test::key" ] && touch "$s/ok" ;; item) [ -f "$s/ok" ] || { echo "not logged in" >&2; exit 1; } case "$2" in list) printf '%s\n' \ '- [id1]: darman@jupiter (state=Active)' \ '- [id2]: root@jupiter (state=Active)' \ '- [id4]: secret-note (state=Active)' \ '- [id5]: auto-item (state=Active)' ;; view) [ -n "''${PROTON_PASS_AGENT_REASON:-}" ] || { echo "reason required" >&2; exit 1; } echo "$PROTON_PASS_AGENT_REASON" >> "$HOME/reasons" case "$*" in *"--item-id id1 "*) echo hunter2 ;; *"--item-id id2 "*) echo rootpw ;; *"--item-id id5 "*) echo autopw ;; *) exit 1 ;; esac ;; esac ;; *) exit 1 ;; esac ''; telegram = pkgs.writers.writePython3Bin "fake-telegram" { flakeIgnore = [ "E501" "E701" "E704" ]; } '' import json import threading import time from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler from urllib.parse import urlparse, parse_qs PREFIX = "/bot123:test/" cv = threading.Condition() state = {"messages": [], "edits": [], "answers": []} updates = [] class H(BaseHTTPRequestHandler): def reply(self, obj, code=200): b = json.dumps(obj).encode() self.send_response(code) self.send_header("Content-Type", "application/json") self.send_header("Content-Length", str(len(b))) self.end_headers() self.wfile.write(b) def do_GET(self): with cv: self.reply(state) def do_POST(self): n = int(self.headers.get("Content-Length", 0)) body = json.loads(self.rfile.read(n) or b"{}") u = urlparse(self.path) if u.path == "/test/press": q = parse_qs(u.query) with cv: uid = len(updates) + 1 updates.append({"update_id": uid, "callback_query": { "id": "cq%d" % uid, "from": {"id": int(q["from"][0])}, "data": q["data"][0]}}) cv.notify_all() return self.reply({"ok": True}) if not u.path.startswith(PREFIX): return self.reply({"ok": False, "description": "Unauthorized"}, 401) method = u.path[len(PREFIX):] with cv: if method == "sendMessage": body["message_id"] = len(state["messages"]) + 1 state["messages"].append(body) return self.reply({"ok": True, "result": {"message_id": body["message_id"]}}) if method in ("editMessageText", "answerCallbackQuery"): state["edits" if method == "editMessageText" else "answers"].append(body) return self.reply({"ok": True, "result": True}) if method == "getUpdates": deadline = time.time() + min(body.get("timeout", 0), 5) while True: pending = [x for x in updates if x["update_id"] >= body.get("offset", 0)] if pending or time.time() >= deadline: return self.reply({"ok": True, "result": pending}) cv.wait(deadline - time.time()) self.reply({"ok": False, "description": "unknown method"}, 404) def log_message(self, *a): pass ThreadingHTTPServer(("127.0.0.1", 8081), H).serve_forever() ''; # try-get : result lands in /tmp/r/.{out,err,rc}. The tag # is in the reason so pkill can pick out one client. tryGet = pkgs.writeShellScriptBin "try-get" '' pass-broker get --item "$2" --reason "vm test $1" >/tmp/r/$1.out 2>/tmp/r/$1.err echo $? >/tmp/r/$1.rc ''; }