130 lines
4.0 KiB
Python
130 lines
4.0 KiB
Python
import io
|
|
import json
|
|
import os
|
|
import sys
|
|
import contextlib
|
|
import importlib
|
|
from pathlib import Path
|
|
|
|
from protocols._common import extract_code, normalize_status, redact_url_secrets, write_state, read_state
|
|
|
|
|
|
def test_extract_code():
|
|
assert extract_code("Your code is 123456") == "123456"
|
|
assert extract_code("no code") is None
|
|
|
|
|
|
def test_normalize_smsvn():
|
|
assert normalize_status("smsvn", "STATUS_WAIT_CODE") == "waiting"
|
|
assert normalize_status("smsvn", "STATUS_OK:8888") == "code_received"
|
|
assert normalize_status("smsvn", "NO_CODE_YET") == "error"
|
|
|
|
|
|
def test_normalize_5sim():
|
|
assert normalize_status("5sim", "PENDING") == "waiting"
|
|
assert normalize_status("5sim", "RECEIVED") == "code_received"
|
|
|
|
|
|
def test_redact_url_secrets():
|
|
u = "https://x.test/stubs/handler_api.php?api_key=SECRET123&action=getBalance&token=ABC"
|
|
redacted = redact_url_secrets(u)
|
|
assert "SECRET123" not in redacted
|
|
assert "ABC" not in redacted
|
|
assert "api_key=%2A%2A%2A" in redacted
|
|
assert "token=%2A%2A%2A" in redacted
|
|
|
|
|
|
def test_write_state_supports_bare_filename(tmp_path):
|
|
prev = Path.cwd()
|
|
try:
|
|
os.chdir(tmp_path)
|
|
write_state("state.json", {"a": 1})
|
|
assert read_state("state.json") == {"a": 1}
|
|
write_state("nested/state.json", {"b": 2})
|
|
assert read_state("nested/state.json") == {"b": 2}
|
|
finally:
|
|
os.chdir(prev)
|
|
|
|
|
|
def test_cli_returns_nonzero_on_provider_error(monkeypatch):
|
|
mod = importlib.import_module("scripts.sms_activation")
|
|
|
|
class FakeClient:
|
|
def balance(self):
|
|
raise mod.ProviderError("smsvn", "BAD_KEY", "bad key", payload={"debug": "x"})
|
|
|
|
monkeypatch.setattr(mod, "get_client", lambda provider: FakeClient())
|
|
monkeypatch.setattr(mod, "read_state", lambda path: {})
|
|
monkeypatch.setattr(sys, "argv", ["sms_activation.py", "--provider", "smsvn", "balance"])
|
|
|
|
buf = io.StringIO()
|
|
with contextlib.redirect_stdout(buf):
|
|
rc = mod.main()
|
|
out = json.loads(buf.getvalue())
|
|
assert rc == 1
|
|
assert out["ok"] is False
|
|
assert out["error_code"] == "BAD_KEY"
|
|
assert "payload" not in out
|
|
|
|
|
|
def test_cli_debug_errors_includes_payload(monkeypatch):
|
|
mod = importlib.import_module("scripts.sms_activation")
|
|
|
|
class FakeClient:
|
|
def balance(self):
|
|
raise mod.ProviderError("smsvn", "HTTP_500", "req failed", payload={"url": "https://a.test?k=%2A%2A%2A"})
|
|
|
|
monkeypatch.setattr(mod, "get_client", lambda provider: FakeClient())
|
|
monkeypatch.setattr(mod, "read_state", lambda path: {})
|
|
monkeypatch.setattr(sys, "argv", ["sms_activation.py", "--provider", "smsvn", "--debug-errors", "balance"])
|
|
|
|
buf = io.StringIO()
|
|
with contextlib.redirect_stdout(buf):
|
|
rc = mod.main()
|
|
out = json.loads(buf.getvalue())
|
|
assert rc == 1
|
|
assert out["ok"] is False
|
|
assert out["payload"]["url"].startswith("https://")
|
|
|
|
|
|
def test_poll_stops_on_terminal_state(monkeypatch):
|
|
mod = importlib.import_module("scripts.sms_activation")
|
|
calls = {"n": 0}
|
|
|
|
class FakeClient:
|
|
def get_status(self, order_id):
|
|
calls["n"] += 1
|
|
if calls["n"] == 1:
|
|
return {"status": "waiting"}
|
|
return {"status": "code_received", "sms_code": "1234"}
|
|
|
|
monkeypatch.setattr(mod, "get_client", lambda provider: FakeClient())
|
|
monkeypatch.setattr(mod, "read_state", lambda path: {})
|
|
monkeypatch.setattr(mod, "sleep_with_backoff", lambda *a, **k: None)
|
|
monkeypatch.setattr(
|
|
sys,
|
|
"argv",
|
|
[
|
|
"sms_activation.py",
|
|
"--provider",
|
|
"smsvn",
|
|
"poll",
|
|
"--order-id",
|
|
"1",
|
|
"--attempts",
|
|
"10",
|
|
"--interval",
|
|
"0.01",
|
|
"--backoff",
|
|
"1.0",
|
|
],
|
|
)
|
|
|
|
buf = io.StringIO()
|
|
with contextlib.redirect_stdout(buf):
|
|
rc = mod.main()
|
|
out = json.loads(buf.getvalue())
|
|
assert rc == 0
|
|
assert calls["n"] == 2
|
|
assert out["status"] == "code_received"
|