Skip to content

Commit afa63b2

Browse files
committed
test: add coverage for SSL, user agent, and async execution
Add tests for cafile, insecure, and ignore_hostname_verification SSL parameters, custom and default user agent headers, and actual async call execution with result verification.
1 parent b9fba37 commit afa63b2

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

tests/test_kanboard.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,55 @@ def test_async_call_generates_coro(self):
105105
method = self.client.my_method_async()
106106
self.assertIsInstance(method, types.CoroutineType)
107107

108+
def test_async_call_returns_result(self):
109+
body = b'{"jsonrpc": "2.0", "result": 42, "id": 123}'
110+
self.urlopen.return_value.read.return_value = body
111+
loop = self.client._event_loop
112+
result = loop.run_until_complete(self.client.my_method_async())
113+
self.assertEqual(42, result)
114+
115+
def test_custom_user_agent(self):
116+
client = kanboard.Client(self.url, "username", "password", user_agent="CustomAgent/1.0")
117+
body = b'{"jsonrpc": "2.0", "result": true, "id": 123}'
118+
self.urlopen.return_value.read.return_value = body
119+
client.remote_procedure()
120+
_, kwargs = self.request.call_args
121+
self.assertEqual("CustomAgent/1.0", kwargs["headers"]["User-Agent"])
122+
123+
def test_default_user_agent(self):
124+
body = b'{"jsonrpc": "2.0", "result": true, "id": 123}'
125+
self.urlopen.return_value.read.return_value = body
126+
self.client.remote_procedure()
127+
_, kwargs = self.request.call_args
128+
self.assertEqual("Kanboard Python API Client", kwargs["headers"]["User-Agent"])
129+
130+
@mock.patch("ssl.create_default_context")
131+
def test_insecure_disables_ssl_verification(self, mock_ssl_context):
132+
client = kanboard.Client(self.url, "username", "password", insecure=True)
133+
ctx = mock_ssl_context.return_value
134+
body = b'{"jsonrpc": "2.0", "result": true, "id": 123}'
135+
self.urlopen.return_value.read.return_value = body
136+
client.remote_procedure()
137+
self.assertFalse(ctx.check_hostname)
138+
self.assertEqual(ctx.verify_mode, __import__("ssl").CERT_NONE)
139+
140+
@mock.patch("ssl.create_default_context")
141+
def test_ignore_hostname_verification(self, mock_ssl_context):
142+
client = kanboard.Client(self.url, "username", "password", ignore_hostname_verification=True)
143+
ctx = mock_ssl_context.return_value
144+
body = b'{"jsonrpc": "2.0", "result": true, "id": 123}'
145+
self.urlopen.return_value.read.return_value = body
146+
client.remote_procedure()
147+
self.assertFalse(ctx.check_hostname)
148+
149+
@mock.patch("ssl.create_default_context")
150+
def test_cafile_passed_to_ssl_context(self, mock_ssl_context):
151+
client = kanboard.Client(self.url, "username", "password", cafile="/path/to/ca.pem")
152+
body = b'{"jsonrpc": "2.0", "result": true, "id": 123}'
153+
self.urlopen.return_value.read.return_value = body
154+
client.remote_procedure()
155+
mock_ssl_context.assert_called_once_with(cafile="/path/to/ca.pem")
156+
108157
@staticmethod
109158
def _create_mocks():
110159
request_patcher = mock.patch("urllib.request.Request")

0 commit comments

Comments
 (0)