feat: add recommendation command and expand test coverage
This commit is contained in:
1 parent
702280a416
commit
f9267352cc
9 files changed
+684
-4
No files matched your search
@@ -0,0 +1,90 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
import requests
|
||||
|
||||
from protocols.fivesim import FiveSimClient
|
||||
from protocols.grizzly import GrizzlyClient
|
||||
from protocols.smsvn import SmsVnClient
|
||||
|
||||
|
||||
class DummyResp:
|
||||
def __init__(self, status_code=200, text="", headers=None, json_data=None, url="https://x.test/api"):
|
||||
self.status_code = status_code
|
||||
self.text = text
|
||||
self.headers = headers or {"content-type": "application/json"}
|
||||
self._json_data = json_data
|
||||
self.url = url
|
||||
|
||||
def json(self):
|
||||
if self._json_data is not None:
|
||||
return self._json_data
|
||||
return json.loads(self.text)
|
||||
|
||||
def raise_for_status(self):
|
||||
if self.status_code >= 400:
|
||||
e = requests.HTTPError(f"{self.status_code} error")
|
||||
req = requests.Request("GET", self.url).prepare()
|
||||
e.request = req
|
||||
e.response = self
|
||||
raise e
|
||||
|
||||
|
||||
def test_smsvn_get_number_string_response(monkeypatch):
|
||||
def fake_get(url, params=None, timeout=20):
|
||||
return DummyResp(status_code=200, text="ACCESS_NUMBER:123:15550001111", headers={"content-type": "text/plain"})
|
||||
|
||||
monkeypatch.setattr(requests, "get", fake_get)
|
||||
c = SmsVnClient(api_key="k")
|
||||
out = c.get_number(service="am", country=12, operator="any")
|
||||
assert out["activationId"] == "123"
|
||||
assert out["phoneNumber"] == "15550001111"
|
||||
|
||||
|
||||
def test_smsvn_request_error_redacts_query_key(monkeypatch):
|
||||
def fake_get(url, params=None, timeout=20):
|
||||
req = requests.Request("GET", "https://x.test/stubs/handler_api?api_key=SECRET123&action=getBalance").prepare()
|
||||
resp = DummyResp(status_code=500, text="boom", headers={"content-type": "text/plain"}, url=req.url)
|
||||
err = requests.HTTPError("500 error")
|
||||
err.request = req
|
||||
err.response = resp
|
||||
raise err
|
||||
|
||||
monkeypatch.setattr(requests, "get", fake_get)
|
||||
c = SmsVnClient(api_key="k")
|
||||
try:
|
||||
c.balance()
|
||||
assert False, "expected error"
|
||||
except Exception as e:
|
||||
assert "SECRET123" not in str(getattr(e, "payload", ""))
|
||||
|
||||
|
||||
def test_fivesim_check_extracts_sms_code(monkeypatch):
|
||||
class FakeSession:
|
||||
headers = {}
|
||||
|
||||
def get(self, url, params=None, timeout=25):
|
||||
payload = {
|
||||
"id": 1,
|
||||
"status": "RECEIVED",
|
||||
"sms": [{"text": "code 667788", "code": "667788"}],
|
||||
}
|
||||
return DummyResp(status_code=200, json_data=payload)
|
||||
|
||||
monkeypatch.setattr(requests, "Session", lambda: FakeSession())
|
||||
c = FiveSimClient(token="t")
|
||||
out = c.check("1")
|
||||
assert out["status"] == "code_received"
|
||||
assert out["sms_code"] == "667788"
|
||||
|
||||
|
||||
def test_grizzly_get_number_v1_string(monkeypatch):
|
||||
def fake_get(url, params=None, timeout=20):
|
||||
return DummyResp(status_code=200, text="ACCESS_NUMBER:9:13330000000", headers={"content-type": "text/plain"})
|
||||
|
||||
monkeypatch.setattr(requests, "get", fake_get)
|
||||
c = GrizzlyClient(api_key="k")
|
||||
out = c.get_number(service="tg", country=187, v2=False)
|
||||
assert out["activationId"] == "9"
|
||||
assert out["phoneNumber"] == "13330000000"
|
||||
Reference in new issue
Block a user