90 lines
2.4 KiB
Python
90 lines
2.4 KiB
Python
from __future__ import annotations
|
|
|
|
import io
|
|
import json
|
|
import sys
|
|
import contextlib
|
|
import importlib
|
|
|
|
|
|
def _run(mod, argv):
|
|
old = sys.argv[:]
|
|
try:
|
|
sys.argv = argv
|
|
buf = io.StringIO()
|
|
with contextlib.redirect_stdout(buf):
|
|
rc = mod.main()
|
|
return rc, json.loads(buf.getvalue())
|
|
finally:
|
|
sys.argv = old
|
|
|
|
|
|
def test_discover_smsvn_country_and_price_branch(monkeypatch):
|
|
mod = importlib.import_module("scripts.sms_activation")
|
|
|
|
class FakeClient:
|
|
def _call(self, action, **kwargs):
|
|
if action == "getPrices":
|
|
return {"ok": True, "branch": "prices"}
|
|
return {"ok": True}
|
|
|
|
def services(self, country):
|
|
return {"ok": True, "branch": "services", "country": country}
|
|
|
|
def countries(self):
|
|
return [{"id": 1}]
|
|
|
|
monkeypatch.setattr(mod, "get_client", lambda provider: FakeClient())
|
|
monkeypatch.setattr(mod, "read_state", lambda path: {})
|
|
|
|
rc, out = _run(mod, ["sms_activation.py", "--provider", "smsvn", "discover"])
|
|
assert rc == 0 and out[0]["id"] == 1
|
|
|
|
rc, out = _run(mod, ["sms_activation.py", "--provider", "smsvn", "discover", "--country", "12"])
|
|
assert rc == 0 and out["branch"] == "services"
|
|
|
|
rc, out = _run(
|
|
mod,
|
|
["sms_activation.py", "--provider", "smsvn", "discover", "--country", "12", "--service", "am"],
|
|
)
|
|
assert rc == 0 and out["branch"] == "prices"
|
|
|
|
|
|
def test_buy_writes_state(monkeypatch):
|
|
mod = importlib.import_module("scripts.sms_activation")
|
|
captured = {}
|
|
|
|
class FakeClient:
|
|
def get_number(self, **kwargs):
|
|
return {"activationId": "100", "phoneNumber": "15550000000"}
|
|
|
|
def fake_write(path, state):
|
|
captured["path"] = path
|
|
captured["state"] = state
|
|
|
|
monkeypatch.setattr(mod, "get_client", lambda provider: FakeClient())
|
|
monkeypatch.setattr(mod, "read_state", lambda path: {})
|
|
monkeypatch.setattr(mod, "write_state", fake_write)
|
|
|
|
rc, out = _run(
|
|
mod,
|
|
[
|
|
"sms_activation.py",
|
|
"--provider",
|
|
"smsvn",
|
|
"--state-file",
|
|
"x.json",
|
|
"buy",
|
|
"--country",
|
|
"12",
|
|
"--operator",
|
|
"any",
|
|
"--service",
|
|
"am",
|
|
],
|
|
)
|
|
assert rc == 0
|
|
assert out["activationId"] == "100"
|
|
assert captured["path"] == "x.json"
|
|
assert captured["state"]["smsvn"]["order_id"] == "100"
|