Skip to content

Commit 4bb7359

Browse files
tests: add tests that aim to ensure consistent behavior around url formats
1 parent a5b9db6 commit 4bb7359

File tree

4 files changed

+221
-0
lines changed

4 files changed

+221
-0
lines changed

tests/helpers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import asyncio
22
import os
3+
import sys
4+
from types import ModuleType
35

46

57
def async_test(coro):
@@ -43,3 +45,19 @@ def get_mock_server_mode() -> str:
4345

4446
def is_ci_unstable_test_skip_enabled() -> bool:
4547
return os.environ.get("CI_UNSTABLE_TESTS_SKIP_ENABLED") == "1"
48+
49+
50+
def reload_module(root_module: ModuleType):
51+
package_name = root_module.__name__
52+
loaded_package_modules = {
53+
key: value for key, value in sys.modules.items() if key.startswith(package_name) and isinstance(value, ModuleType)
54+
}
55+
56+
for key in loaded_package_modules:
57+
del sys.modules[key]
58+
59+
for key in loaded_package_modules:
60+
new_module = __import__(key)
61+
old_module = loaded_package_modules[key]
62+
old_module.__dict__.clear()
63+
old_module.__dict__.update(new_module.__dict__)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from unittest import TestCase
2+
from unittest.mock import patch
3+
from urllib import request
4+
from urllib.request import Request, urlopen
5+
6+
import slack_sdk.web
7+
from tests.helpers import reload_module
8+
from tests.slack_sdk.web.mock_web_api_server import (
9+
setup_mock_web_api_server,
10+
cleanup_mock_web_api_server,
11+
)
12+
13+
14+
def build_spy_urlopen(test: TestCase):
15+
def spy_urlopen(*args, **kwargs):
16+
test.urlopen_spy_args = args
17+
test.urlopen_spy_kwargs = kwargs
18+
return urlopen(*args, **kwargs)
19+
20+
return spy_urlopen
21+
22+
23+
class TestWebClientUrlFormat(TestCase):
24+
def setUp(self):
25+
setup_mock_web_api_server(self)
26+
with patch.object(request, "urlopen") as mock_urlopen:
27+
mock_urlopen.side_effect = build_spy_urlopen(self)
28+
reload_module(slack_sdk.web)
29+
self.client = slack_sdk.web.WebClient(token="xoxb-api_test", base_url="http://localhost:8888")
30+
self.client_base_url_slash = slack_sdk.web.WebClient(token="xoxb-api_test", base_url="http://localhost:8888/")
31+
32+
def tearDown(self):
33+
cleanup_mock_web_api_server(self)
34+
self.urlopen_spy_args = None
35+
self.urlopen_spy_kwargs = None
36+
37+
@classmethod
38+
def tearDownClass(cls):
39+
reload_module(slack_sdk.web)
40+
41+
def test_base_url_without_slash_api_method_without_slash(self):
42+
self.client.api_call("api.test")
43+
self.assertIsInstance(self.urlopen_spy_args[0], Request)
44+
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test")
45+
46+
def test_base_url_without_slash_api_method_with_slash(self):
47+
self.client.api_call("/api.test")
48+
self.assertIsInstance(self.urlopen_spy_args[0], Request)
49+
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test")
50+
51+
def test_base_url_with_slash_api_method_without_slash(self):
52+
self.client_base_url_slash.api_call("api.test")
53+
self.assertIsInstance(self.urlopen_spy_args[0], Request)
54+
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test")
55+
56+
def test_base_url_with_slash_api_method_with_slash(self):
57+
self.client_base_url_slash.api_call("/api.test")
58+
self.assertIsInstance(self.urlopen_spy_args[0], Request)
59+
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test")
60+
61+
def test_base_url_without_slash_api_method_with_slash_and_trailing_slash(self):
62+
self.client.api_call("/api.test/")
63+
self.assertIsInstance(self.urlopen_spy_args[0], Request)
64+
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test/")
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from unittest import TestCase
2+
from unittest.mock import patch
3+
from urllib import request
4+
from urllib.request import Request, urlopen
5+
6+
import slack_sdk.web.legacy_base_client
7+
from tests.helpers import reload_module
8+
from tests.slack_sdk.web.mock_web_api_server import (
9+
setup_mock_web_api_server,
10+
cleanup_mock_web_api_server,
11+
)
12+
13+
14+
def build_spy_urlopen(test: TestCase):
15+
def spy_urlopen(*args, **kwargs):
16+
test.urlopen_spy_args = args
17+
test.urlopen_spy_kwargs = kwargs
18+
return urlopen(*args, **kwargs)
19+
20+
return spy_urlopen
21+
22+
23+
class TestLegacyWebClientUrlFormat(TestCase):
24+
def setUp(self):
25+
setup_mock_web_api_server(self)
26+
with patch.object(request, "urlopen") as mock_urlopen:
27+
mock_urlopen.side_effect = build_spy_urlopen(self)
28+
reload_module(slack_sdk.web.legacy_base_client)
29+
self.client = slack_sdk.web.legacy_base_client.LegacyBaseClient(
30+
token="xoxb-api_test", base_url="http://localhost:8888"
31+
)
32+
self.client_base_url_slash = slack_sdk.web.legacy_base_client.LegacyBaseClient(
33+
token="xoxb-api_test", base_url="http://localhost:8888/"
34+
)
35+
36+
def tearDown(self):
37+
cleanup_mock_web_api_server(self)
38+
self.urlopen_spy_args = None
39+
self.urlopen_spy_kwargs = None
40+
41+
@classmethod
42+
def tearDownClass(cls):
43+
reload_module(slack_sdk.web.legacy_base_client)
44+
45+
def test_base_url_without_slash_api_method_without_slash(self):
46+
self.client.api_call("api.test")
47+
self.assertIsInstance(self.urlopen_spy_args[0], Request)
48+
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test")
49+
50+
def test_base_url_without_slash_api_method_with_slash(self):
51+
self.client.api_call("/api.test")
52+
self.assertIsInstance(self.urlopen_spy_args[0], Request)
53+
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test")
54+
55+
def test_base_url_with_slash_api_method_without_slash(self):
56+
self.client_base_url_slash.api_call("api.test")
57+
self.assertIsInstance(self.urlopen_spy_args[0], Request)
58+
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test")
59+
60+
def test_base_url_with_slash_api_method_with_slash(self):
61+
self.client_base_url_slash.api_call("/api.test")
62+
self.assertIsInstance(self.urlopen_spy_args[0], Request)
63+
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test")
64+
65+
def test_base_url_without_slash_api_method_with_slash_and_trailing_slash(self):
66+
self.client.api_call("/api.test/")
67+
self.assertIsInstance(self.urlopen_spy_args[0], Request)
68+
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test/")
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import unittest
2+
from unittest.mock import Mock
3+
from aiohttp import ClientSession
4+
5+
from slack_sdk.web.async_client import AsyncWebClient
6+
from tests.slack_sdk_async.helpers import async_test
7+
from tests.slack_sdk.web.mock_web_api_server import (
8+
setup_mock_web_api_server,
9+
cleanup_mock_web_api_server,
10+
)
11+
12+
13+
class TestWebClientUrlFormat(unittest.TestCase):
14+
def setUp(self):
15+
setup_mock_web_api_server(self)
16+
17+
self.session = ClientSession()
18+
19+
def spy_session_request(*args, **kwargs):
20+
self.session_request_spy_args = args
21+
self.session_request_spy_kwargs = kwargs
22+
return self.session.request(*args, **kwargs)
23+
24+
self.spy_session = ClientSession()
25+
self.spy_session.request = Mock(side_effect=spy_session_request)
26+
self.client = AsyncWebClient(token="xoxb-api_test", base_url="http://localhost:8888", session=self.spy_session)
27+
self.client_base_url_slash = AsyncWebClient(
28+
token="xoxb-api_test", base_url="http://localhost:8888/", session=self.spy_session
29+
)
30+
31+
def tearDown(self):
32+
cleanup_mock_web_api_server(self)
33+
34+
async def close_sessions(self):
35+
await self.session.close()
36+
await self.spy_session.close()
37+
38+
@async_test
39+
async def test_base_url_without_slash_api_method_without_slash(self):
40+
await self.client.api_call("api.test")
41+
await self.close_sessions()
42+
self.assertIsInstance(self.session_request_spy_args[1], str)
43+
self.assertEqual(self.session_request_spy_args[1], "http://localhost:8888/api.test")
44+
45+
@async_test
46+
async def test_base_url_without_slash_api_method_with_slash(self):
47+
await self.client.api_call("/api.test")
48+
await self.close_sessions()
49+
self.assertIsInstance(self.session_request_spy_args[1], str)
50+
self.assertEqual(self.session_request_spy_args[1], "http://localhost:8888/api.test")
51+
52+
@async_test
53+
async def test_base_url_with_slash_api_method_without_slash(self):
54+
await self.client_base_url_slash.api_call("api.test")
55+
await self.close_sessions()
56+
self.assertIsInstance(self.session_request_spy_args[1], str)
57+
self.assertEqual(self.session_request_spy_args[1], "http://localhost:8888/api.test")
58+
59+
@async_test
60+
async def test_base_url_with_slash_api_method_with_slash(self):
61+
await self.client_base_url_slash.api_call("/api.test")
62+
await self.close_sessions()
63+
self.assertIsInstance(self.session_request_spy_args[1], str)
64+
self.assertEqual(self.session_request_spy_args[1], "http://localhost:8888/api.test")
65+
66+
@async_test
67+
async def test_base_url_without_slash_api_method_with_slash_and_trailing_slash(self):
68+
await self.client.api_call("/api.test/")
69+
await self.close_sessions()
70+
self.assertIsInstance(self.session_request_spy_args[1], str)
71+
self.assertEqual(self.session_request_spy_args[1], "http://localhost:8888/api.test/")

0 commit comments

Comments
 (0)