from __future__ import annotations import json import requests from protocols.fivesim import FiveSimClient from protocols._common import parse_json_or_text 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_parse_json_or_text_fallback_text(): r = DummyResp(status_code=200, text="PLAIN_OK", headers={"content-type": "text/plain"}) assert parse_json_or_text(r) == "PLAIN_OK" def test_fivesim_auth_error(monkeypatch): class FakeSession: headers = {} def get(self, url, params=None, timeout=25): return DummyResp(status_code=401, text="unauthorized", headers={"content-type": "text/plain"}) monkeypatch.setattr(requests, "Session", lambda: FakeSession()) c = FiveSimClient(token="x") try: c.profile() assert False, "expected auth error" except Exception as e: assert "AUTH" in str(e) def test_fivesim_http_error(monkeypatch): class FakeSession: headers = {} def get(self, url, params=None, timeout=25): return DummyResp(status_code=500, text="server error", headers={"content-type": "text/plain"}) monkeypatch.setattr(requests, "Session", lambda: FakeSession()) c = FiveSimClient(token="x") try: c.profile() assert False, "expected http error" except Exception as e: assert "HTTP" in str(e)