50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
import json
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from cloud import CreateVPCRequest, VPCAPIError, VPCNetworkManager
|
|
|
|
|
|
class Response:
|
|
def __init__(self, payload: dict) -> None:
|
|
self.payload = payload
|
|
|
|
def read(self) -> bytes:
|
|
return json.dumps(self.payload).encode()
|
|
|
|
def __enter__(self) -> "Response":
|
|
return self
|
|
|
|
def __exit__(self, *args: object) -> None:
|
|
return None
|
|
|
|
|
|
@patch("cloud.vpc_network_manager.urlopen")
|
|
def test_create_vpc_sends_bearer_request(urlopen) -> None:
|
|
urlopen.return_value = Response({"id": "operation-id"})
|
|
manager = VPCNetworkManager("token", base_url="https://api.example")
|
|
|
|
operation = manager.create_vpc(
|
|
CreateVPCRequest(projectId="project-id", name="game-vpc")
|
|
)
|
|
assert operation.id == "operation-id"
|
|
|
|
request = urlopen.call_args.args[0]
|
|
assert request.full_url == "https://api.example/v1/vpcs"
|
|
assert request.method == "POST"
|
|
assert request.get_header("Authorization") == "Bearer token"
|
|
assert json.loads(request.data) == {"projectId": "project-id", "name": "game-vpc"}
|
|
|
|
|
|
def test_manager_rejects_empty_token() -> None:
|
|
with pytest.raises(ValueError, match="token must not be empty"):
|
|
VPCNetworkManager("")
|
|
|
|
|
|
def test_api_error_keeps_status_and_details() -> None:
|
|
error = VPCAPIError(403, "forbidden", [{"reason": "denied"}])
|
|
|
|
assert error.status == 403
|
|
assert error.details == [{"reason": "denied"}]
|