-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sandbox.py
More file actions
109 lines (81 loc) · 4.21 KB
/
test_sandbox.py
File metadata and controls
109 lines (81 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
"""
Tests for sandbox functionality in the Devo Global Communications Python SDK.
"""
from unittest.mock import Mock, patch
import pytest
from devo_global_comms_python.client import DevoClient
from devo_global_comms_python.exceptions import DevoException
class TestSandboxFunctionality:
"""Test sandbox API key switching functionality."""
def test_client_initialization_with_sandbox_api_key(self):
"""Test that client can be initialized with sandbox API key."""
client = DevoClient(api_key="test-api-key", sandbox_api_key="sandbox-api-key")
assert client.api_key == "test-api-key"
assert client.sandbox_api_key == "sandbox-api-key"
def test_client_initialization_without_sandbox_api_key(self):
"""Test that client can be initialized without sandbox API key."""
client = DevoClient(api_key="test-api-key")
assert client.api_key == "test-api-key"
assert client.sandbox_api_key is None
def test_sandbox_request_without_sandbox_api_key_raises_error(self):
"""Test that sandbox request without sandbox API key raises appropriate error."""
client = DevoClient(api_key="test-api-key")
with pytest.raises(DevoException, match="Sandbox API key required when sandbox=True"):
client.get("test-endpoint", sandbox=True)
@patch("devo_global_comms_python.client.requests.Session.request")
def test_sandbox_request_uses_sandbox_api_key(self, mock_request):
"""Test that sandbox request uses sandbox API key for authentication."""
# Mock successful response
mock_response = Mock()
mock_response.ok = True
mock_response.json.return_value = {"success": True}
mock_request.return_value = mock_response
client = DevoClient(api_key="production-api-key", sandbox_api_key="sandbox-api-key")
# Make sandbox request
client.get("test-endpoint", sandbox=True)
# Verify the request was made with sandbox API key
mock_request.assert_called_once()
call_args = mock_request.call_args
headers = call_args[1]["headers"]
# Check that X-API-Key header contains sandbox API key
assert "X-API-Key" in headers
assert "sandbox-api-key" in headers["X-API-Key"]
@patch("devo_global_comms_python.client.requests.Session.request")
def test_regular_request_uses_production_api_key(self, mock_request):
"""Test that regular request uses production API key for authentication."""
# Mock successful response
mock_response = Mock()
mock_response.ok = True
mock_response.json.return_value = {"success": True}
mock_request.return_value = mock_response
client = DevoClient(api_key="production-api-key", sandbox_api_key="sandbox-api-key")
# Make regular request (sandbox=False by default)
client.get("test-endpoint")
# Verify the request was made with production API key
mock_request.assert_called_once()
call_args = mock_request.call_args
headers = call_args[1]["headers"]
# Check that X-API-Key header contains production API key
assert "X-API-Key" in headers
assert "production-api-key" in headers["X-API-Key"]
@patch("devo_global_comms_python.client.requests.Session.request")
def test_sms_resource_sandbox_parameter(self, mock_request):
"""Test that SMS resource functions correctly pass sandbox parameter."""
# Mock successful response
mock_response = Mock()
mock_response.ok = True
mock_response.json.return_value = {"senders": []}
mock_request.return_value = mock_response
client = DevoClient(api_key="production-api-key", sandbox_api_key="sandbox-api-key")
# Call SMS function with sandbox=True
client.sms.get_senders(sandbox=True)
# Verify the request was made with sandbox API key
mock_request.assert_called_once()
call_args = mock_request.call_args
headers = call_args[1]["headers"]
# Check that X-API-Key header contains sandbox API key
assert "X-API-Key" in headers
assert "sandbox-api-key" in headers["X-API-Key"]
if __name__ == "__main__":
pytest.main([__file__, "-v"])