Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: ensure consistent behavior around url format #1600

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio
import os
import sys
from types import ModuleType


def async_test(coro):
Expand Down Expand Up @@ -43,3 +45,19 @@ def get_mock_server_mode() -> str:

def is_ci_unstable_test_skip_enabled() -> bool:
return os.environ.get("CI_UNSTABLE_TESTS_SKIP_ENABLED") == "1"


def reload_module(root_module: ModuleType):
package_name = root_module.__name__
loaded_package_modules = {
key: value for key, value in sys.modules.items() if key.startswith(package_name) and isinstance(value, ModuleType)
}

for key in loaded_package_modules:
del sys.modules[key]

for key in loaded_package_modules:
new_module = __import__(key)
old_module = loaded_package_modules[key]
old_module.__dict__.clear()
old_module.__dict__.update(new_module.__dict__)
64 changes: 64 additions & 0 deletions tests/slack_sdk/web/test_web_client_url_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from unittest import TestCase
from unittest.mock import patch
from urllib import request
from urllib.request import Request, urlopen

import slack_sdk.web
from tests.helpers import reload_module
from tests.slack_sdk.web.mock_web_api_server import (
setup_mock_web_api_server,
cleanup_mock_web_api_server,
)


def build_spy_urlopen(test: TestCase):
def spy_urlopen(*args, **kwargs):
test.urlopen_spy_args = args
test.urlopen_spy_kwargs = kwargs
return urlopen(*args, **kwargs)

return spy_urlopen


class TestWebClientUrlFormat(TestCase):
def setUp(self):
setup_mock_web_api_server(self)
with patch.object(request, "urlopen") as mock_urlopen:
mock_urlopen.side_effect = build_spy_urlopen(self)
reload_module(slack_sdk.web)
self.client = slack_sdk.web.WebClient(token="xoxb-api_test", base_url="http://localhost:8888")
self.client_base_url_slash = slack_sdk.web.WebClient(token="xoxb-api_test", base_url="http://localhost:8888/")

def tearDown(self):
cleanup_mock_web_api_server(self)
self.urlopen_spy_args = None
self.urlopen_spy_kwargs = None

@classmethod
def tearDownClass(cls):
reload_module(slack_sdk.web)

def test_base_url_without_slash_api_method_without_slash(self):
self.client.api_call("api.test")
self.assertIsInstance(self.urlopen_spy_args[0], Request)
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test")

def test_base_url_without_slash_api_method_with_slash(self):
self.client.api_call("/api.test")
self.assertIsInstance(self.urlopen_spy_args[0], Request)
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test")

def test_base_url_with_slash_api_method_without_slash(self):
self.client_base_url_slash.api_call("api.test")
self.assertIsInstance(self.urlopen_spy_args[0], Request)
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test")

def test_base_url_with_slash_api_method_with_slash(self):
self.client_base_url_slash.api_call("/api.test")
self.assertIsInstance(self.urlopen_spy_args[0], Request)
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test")

def test_base_url_without_slash_api_method_with_slash_and_trailing_slash(self):
self.client.api_call("/api.test/")
self.assertIsInstance(self.urlopen_spy_args[0], Request)
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test/")
68 changes: 68 additions & 0 deletions tests/slack_sdk/web/test_web_legacy_client_url_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from unittest import TestCase
from unittest.mock import patch
from urllib import request
from urllib.request import Request, urlopen

import slack_sdk.web.legacy_base_client
from tests.helpers import reload_module
from tests.slack_sdk.web.mock_web_api_server import (
setup_mock_web_api_server,
cleanup_mock_web_api_server,
)


def build_spy_urlopen(test: TestCase):
def spy_urlopen(*args, **kwargs):
test.urlopen_spy_args = args
test.urlopen_spy_kwargs = kwargs
return urlopen(*args, **kwargs)

return spy_urlopen


class TestLegacyWebClientUrlFormat(TestCase):
def setUp(self):
setup_mock_web_api_server(self)
with patch.object(request, "urlopen") as mock_urlopen:
mock_urlopen.side_effect = build_spy_urlopen(self)
reload_module(slack_sdk.web.legacy_base_client)
self.client = slack_sdk.web.legacy_base_client.LegacyBaseClient(
token="xoxb-api_test", base_url="http://localhost:8888"
)
self.client_base_url_slash = slack_sdk.web.legacy_base_client.LegacyBaseClient(
token="xoxb-api_test", base_url="http://localhost:8888/"
)

def tearDown(self):
cleanup_mock_web_api_server(self)
self.urlopen_spy_args = None
self.urlopen_spy_kwargs = None

@classmethod
def tearDownClass(cls):
reload_module(slack_sdk.web.legacy_base_client)

def test_base_url_without_slash_api_method_without_slash(self):
self.client.api_call("api.test")
self.assertIsInstance(self.urlopen_spy_args[0], Request)
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test")

def test_base_url_without_slash_api_method_with_slash(self):
self.client.api_call("/api.test")
self.assertIsInstance(self.urlopen_spy_args[0], Request)
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test")

def test_base_url_with_slash_api_method_without_slash(self):
self.client_base_url_slash.api_call("api.test")
self.assertIsInstance(self.urlopen_spy_args[0], Request)
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test")

def test_base_url_with_slash_api_method_with_slash(self):
self.client_base_url_slash.api_call("/api.test")
self.assertIsInstance(self.urlopen_spy_args[0], Request)
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test")

def test_base_url_without_slash_api_method_with_slash_and_trailing_slash(self):
self.client.api_call("/api.test/")
self.assertIsInstance(self.urlopen_spy_args[0], Request)
self.assertEqual(self.urlopen_spy_args[0].full_url, "http://localhost:8888/api.test/")
71 changes: 71 additions & 0 deletions tests/slack_sdk_async/web/test_web_client_url_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import unittest
from unittest.mock import Mock
from aiohttp import ClientSession

from slack_sdk.web.async_client import AsyncWebClient
from tests.slack_sdk_async.helpers import async_test
from tests.slack_sdk.web.mock_web_api_server import (
setup_mock_web_api_server,
cleanup_mock_web_api_server,
)


class TestAsyncWebClientUrlFormat(unittest.TestCase):
def setUp(self):
setup_mock_web_api_server(self)

self.session = ClientSession()

def spy_session_request(*args, **kwargs):
self.session_request_spy_args = args
self.session_request_spy_kwargs = kwargs
return self.session.request(*args, **kwargs)

self.spy_session = ClientSession()
self.spy_session.request = Mock(side_effect=spy_session_request)
self.client = AsyncWebClient(token="xoxb-api_test", base_url="http://localhost:8888", session=self.spy_session)
self.client_base_url_slash = AsyncWebClient(
token="xoxb-api_test", base_url="http://localhost:8888/", session=self.spy_session
)

def tearDown(self):
cleanup_mock_web_api_server(self)

async def close_sessions(self):
await self.session.close()
await self.spy_session.close()

@async_test
async def test_base_url_without_slash_api_method_without_slash(self):
await self.client.api_call("api.test")
await self.close_sessions()
self.assertIsInstance(self.session_request_spy_args[1], str)
self.assertEqual(self.session_request_spy_args[1], "http://localhost:8888/api.test")

@async_test
async def test_base_url_without_slash_api_method_with_slash(self):
await self.client.api_call("/api.test")
await self.close_sessions()
self.assertIsInstance(self.session_request_spy_args[1], str)
self.assertEqual(self.session_request_spy_args[1], "http://localhost:8888/api.test")

@async_test
async def test_base_url_with_slash_api_method_without_slash(self):
await self.client_base_url_slash.api_call("api.test")
await self.close_sessions()
self.assertIsInstance(self.session_request_spy_args[1], str)
self.assertEqual(self.session_request_spy_args[1], "http://localhost:8888/api.test")

@async_test
async def test_base_url_with_slash_api_method_with_slash(self):
await self.client_base_url_slash.api_call("/api.test")
await self.close_sessions()
self.assertIsInstance(self.session_request_spy_args[1], str)
self.assertEqual(self.session_request_spy_args[1], "http://localhost:8888/api.test")

@async_test
async def test_base_url_without_slash_api_method_with_slash_and_trailing_slash(self):
await self.client.api_call("/api.test/")
await self.close_sessions()
self.assertIsInstance(self.session_request_spy_args[1], str)
self.assertEqual(self.session_request_spy_args[1], "http://localhost:8888/api.test/")
Loading