|
| 1 | +"""Tests for Turn API client.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from src.turn_client import ( |
| 6 | + TurnAPIClient, |
| 7 | + TurnAPIClientError, |
| 8 | + TurnAPIError, |
| 9 | + TurnAPIServerError, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture(autouse=True) |
| 14 | +def setup_turn_env(monkeypatch): |
| 15 | + """Set up Turn API environment variables for all tests.""" |
| 16 | + monkeypatch.setenv("TURN_API_BASE_URL", "https://api.turn.io") |
| 17 | + monkeypatch.setenv("TURN_API_TOKEN", "test-token") |
| 18 | + |
| 19 | + |
| 20 | +class TestTurnAPIClient: |
| 21 | + """Tests for TurnAPIClient class.""" |
| 22 | + |
| 23 | + def test_client_initialization_with_env_vars(self): |
| 24 | + """Test client initialization with environment variables.""" |
| 25 | + client = TurnAPIClient() |
| 26 | + assert client.base_url == "https://api.turn.io" |
| 27 | + assert client.api_token == "test-token" # noqa: S105 |
| 28 | + |
| 29 | + def test_client_initialization_strips_trailing_slash(self, monkeypatch): |
| 30 | + """Test that trailing slash is removed from base URL.""" |
| 31 | + monkeypatch.setenv("TURN_API_BASE_URL", "https://api.turn.io/") |
| 32 | + monkeypatch.setenv("TURN_API_TOKEN", "test-token") |
| 33 | + |
| 34 | + client = TurnAPIClient() |
| 35 | + assert client.base_url == "https://api.turn.io" |
| 36 | + |
| 37 | + def test_client_initialization_missing_base_url(self, monkeypatch): |
| 38 | + """Test that ValueError is raised if base URL is missing.""" |
| 39 | + monkeypatch.delenv("TURN_API_BASE_URL", raising=False) |
| 40 | + monkeypatch.setenv("TURN_API_TOKEN", "test-token") |
| 41 | + |
| 42 | + with pytest.raises(ValueError, match="TURN_API_BASE_URL must be set"): |
| 43 | + TurnAPIClient() |
| 44 | + |
| 45 | + def test_client_initialization_missing_token(self, monkeypatch): |
| 46 | + """Test that ValueError is raised if token is missing.""" |
| 47 | + monkeypatch.setenv("TURN_API_BASE_URL", "https://api.turn.io") |
| 48 | + monkeypatch.delenv("TURN_API_TOKEN", raising=False) |
| 49 | + |
| 50 | + with pytest.raises(ValueError, match="TURN_API_TOKEN must be set"): |
| 51 | + TurnAPIClient() |
| 52 | + |
| 53 | + def test_update_message_label_success(self, mocker): |
| 54 | + """Test successful label update.""" |
| 55 | + client = TurnAPIClient() |
| 56 | + |
| 57 | + # Mock successful response |
| 58 | + mock_response = mocker.Mock() |
| 59 | + mock_response.status_code = 200 |
| 60 | + mock_response.text = '{"status": "success", "message_id": "msg-123"}' |
| 61 | + mock_response.json.return_value = {"status": "success", "message_id": "msg-123"} |
| 62 | + mock_post = mocker.patch.object( |
| 63 | + client.session, "post", return_value=mock_response |
| 64 | + ) |
| 65 | + |
| 66 | + result = client.update_message_label("msg-123", "compliment") |
| 67 | + |
| 68 | + # Verify request |
| 69 | + mock_post.assert_called_once_with( |
| 70 | + "https://api.turn.io/v1/messages/msg-123/labels", |
| 71 | + json={"labels": ["compliment"]}, |
| 72 | + ) |
| 73 | + |
| 74 | + # Verify response |
| 75 | + assert result == {"status": "success", "message_id": "msg-123"} |
| 76 | + |
| 77 | + def test_update_message_label_client_error(self, mocker): |
| 78 | + """Test 4xx client error handling.""" |
| 79 | + client = TurnAPIClient() |
| 80 | + |
| 81 | + # Mock 400 Bad Request |
| 82 | + mock_response = mocker.Mock() |
| 83 | + mock_response.status_code = 400 |
| 84 | + mock_response.text = "Invalid label format" |
| 85 | + mocker.patch.object(client.session, "post", return_value=mock_response) |
| 86 | + |
| 87 | + with pytest.raises( |
| 88 | + TurnAPIClientError, match="Turn API client error \\(HTTP 400\\)" |
| 89 | + ): |
| 90 | + client.update_message_label("msg-123", "invalid-label") |
| 91 | + |
| 92 | + def test_update_message_label_server_error(self, mocker): |
| 93 | + """Test 5xx server error handling.""" |
| 94 | + client = TurnAPIClient() |
| 95 | + |
| 96 | + # Mock 500 Internal Server Error |
| 97 | + mock_response = mocker.Mock() |
| 98 | + mock_response.status_code = 500 |
| 99 | + mock_response.text = "Internal server error" |
| 100 | + mocker.patch.object(client.session, "post", return_value=mock_response) |
| 101 | + |
| 102 | + with pytest.raises( |
| 103 | + TurnAPIServerError, match="Turn API server error \\(HTTP 500\\)" |
| 104 | + ): |
| 105 | + client.update_message_label("msg-123", "label") |
| 106 | + |
| 107 | + def test_update_message_label_request_error(self, mocker): |
| 108 | + """Test general request error handling.""" |
| 109 | + import requests |
| 110 | + |
| 111 | + client = TurnAPIClient() |
| 112 | + |
| 113 | + # Mock request exception |
| 114 | + mocker.patch.object( |
| 115 | + client.session, |
| 116 | + "post", |
| 117 | + side_effect=requests.RequestException("Connection failed"), |
| 118 | + ) |
| 119 | + |
| 120 | + with pytest.raises( |
| 121 | + TurnAPIError, match="Turn API request error for message msg-123" |
| 122 | + ): |
| 123 | + client.update_message_label("msg-123", "label") |
| 124 | + |
| 125 | + def test_client_headers(self, monkeypatch): |
| 126 | + """Test that client has correct headers.""" |
| 127 | + monkeypatch.setenv("TURN_API_BASE_URL", "https://api.turn.io") |
| 128 | + monkeypatch.setenv("TURN_API_TOKEN", "test-token-123") |
| 129 | + |
| 130 | + client = TurnAPIClient() |
| 131 | + |
| 132 | + headers = client.session.headers |
| 133 | + assert headers["Authorization"] == "Bearer test-token-123" |
| 134 | + assert headers["Accept"] == "application/vnd.v1+json" |
| 135 | + assert headers["Content-Type"] == "application/json" |
0 commit comments