eb8dbf66a1
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
91 lines
2.5 KiB
Python
91 lines
2.5 KiB
Python
"""Tests para hoppscotch_update_request.
|
|
|
|
Deterministas: monkeypatchean requests.post para no tocar la red. Verifican que
|
|
el POST GraphQL lleva la mutation updateRequest con requestID, el access_token
|
|
en la cookie, y que `request` es el json string de un HoppRESTRequest v:"2".
|
|
"""
|
|
|
|
import json
|
|
import sys
|
|
|
|
import infra.hoppscotch_update_request # noqa: F401
|
|
|
|
# El __init__ rebinds el nombre a la funcion; recuperamos el submodulo real.
|
|
mod = sys.modules["infra.hoppscotch_update_request"]
|
|
|
|
|
|
class _FakeResponse:
|
|
def __init__(self, status_code=200, json_data=None):
|
|
self.status_code = status_code
|
|
self._json = json_data
|
|
|
|
def json(self):
|
|
if self._json is None:
|
|
raise ValueError("no json")
|
|
return self._json
|
|
|
|
|
|
def test_golden_actualiza_request_y_devuelve_id(monkeypatch):
|
|
captured = {}
|
|
|
|
def fake_post(url, **kwargs):
|
|
captured["url"] = url
|
|
captured["kwargs"] = kwargs
|
|
return _FakeResponse(
|
|
200,
|
|
{
|
|
"data": {
|
|
"updateRequest": {"id": "req-7", "title": "New title"}
|
|
}
|
|
},
|
|
)
|
|
|
|
monkeypatch.setattr(mod.requests, "post", fake_post)
|
|
|
|
result = mod.hoppscotch_update_request(
|
|
"req-7",
|
|
"POST",
|
|
"https://api.example.com/x",
|
|
title="New title",
|
|
body="a=1&b=2",
|
|
body_type="form",
|
|
access_token="ACCESS-JWT",
|
|
)
|
|
|
|
assert result["status"] == "ok"
|
|
assert result["id"] == "req-7"
|
|
assert result["title"] == "New title"
|
|
|
|
assert captured["url"].endswith("/graphql")
|
|
assert captured["kwargs"]["cookies"] == {"access_token": "ACCESS-JWT"}
|
|
|
|
payload = captured["kwargs"]["json"]
|
|
assert "updateRequest" in payload["query"]
|
|
variables = payload["variables"]
|
|
assert variables["r"] == "req-7"
|
|
assert variables["d"]["title"] == "New title"
|
|
|
|
req = json.loads(variables["d"]["request"])
|
|
assert req["v"] == "2"
|
|
assert req["method"] == "POST"
|
|
assert req["body"] == {
|
|
"contentType": "application/x-www-form-urlencoded",
|
|
"body": "a=1&b=2",
|
|
}
|
|
|
|
|
|
def test_error_graphql_errors(monkeypatch):
|
|
def fake_post(url, **kwargs):
|
|
return _FakeResponse(
|
|
200, {"errors": [{"message": "team_req/not_found"}]}
|
|
)
|
|
|
|
monkeypatch.setattr(mod.requests, "post", fake_post)
|
|
|
|
result = mod.hoppscotch_update_request(
|
|
"missing", "GET", "https://x", access_token="A"
|
|
)
|
|
assert result["status"] == "error"
|
|
assert result["error"] == "graphql errors"
|
|
assert "errors" in result["data"]
|