Skip to content
Prev Previous commit
Next Next commit
switch from async test to standard TestCase
  • Loading branch information
ccaruceru committed Jan 5, 2025
commit 598c9286d094746489c0dfa35b2170ccd1975417
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
from slack_sdk.oauth.installation_store.google_cloud_storage import GoogleCloudStorageInstallationStore


class TestGoogleInstallationStore(unittest.IsolatedAsyncioTestCase):
class TestGoogleInstallationStore(unittest.TestCase):
"""Tests for GoogleCloudStorageInstallationStore"""

async def asyncSetUp(self):
def setUp(self):
"""Setup test"""
self.blob = Mock(spec=Blob)
self.bucket = Mock(spec=Bucket)
Expand All @@ -43,9 +43,9 @@ def test_get_logger(self):
self.assertEqual(self.installation_store.logger, self.logger)

@patch("slack_sdk.oauth.installation_store.google_cloud_storage.GoogleCloudStorageInstallationStore._save_entity")
async def test_async_save_install(self, save_entity: Mock):
"""Test async_save method"""
await self.installation_store.async_save(self.entr_installation)
def test_save_install(self, save_entity: Mock):
"""Test save method"""
self.installation_store.save(self.entr_installation)
self.storage_client.bucket.assert_called_once_with(self.bucket_name)
save_entity.assert_has_calls(
[
Expand Down Expand Up @@ -74,9 +74,9 @@ async def test_async_save_install(self, save_entity: Mock):
)

@patch("slack_sdk.oauth.installation_store.google_cloud_storage.GoogleCloudStorageInstallationStore._save_entity")
async def test_async_save_bot(self, save_entity: Mock):
"""Test async_save_bot method"""
await self.installation_store.async_save_bot(bot=self.entr_installation.to_bot())
def test_save_bot(self, save_entity: Mock):
"""Test save_bot method"""
self.installation_store.save_bot(bot=self.entr_installation.to_bot())
save_entity.assert_called_once_with(
data_type="bot",
entity=json.dumps(self.entr_installation.to_bot().__dict__),
Expand Down Expand Up @@ -115,14 +115,14 @@ def test_save_entity_and_test_key(self):

self.bucket.reset_mock()

async def test_async_find_bot(self):
"""Test async_find_bot method"""
def test_find_bot(self):
"""Test find_bot method"""
self.blob.download_as_text.return_value = json.dumps(
{"bot_token": "xoxb-token", "bot_id": "bid", "bot_user_id": "buid", "installed_at": time.time()}
)
# test bot found enterprise installation + normal workspace
for install in [self.entr_installation, self.team_installation]:
bot = await self.installation_store.async_find_bot(
bot = self.installation_store.find_bot(
enterprise_id=install.enterprise_id,
team_id=install.team_id,
is_enterprise_install=install.is_enterprise_install,
Expand All @@ -140,20 +140,20 @@ async def test_async_find_bot(self):

# test bot not found
self.blob.download_as_text.side_effect = Exception()
bot = await self.installation_store.async_find_bot(
bot = self.installation_store.find_bot(
enterprise_id=self.entr_installation.enterprise_id,
team_id=self.entr_installation.team_id,
is_enterprise_install=self.entr_installation.is_enterprise_install,
)
self.blob.download_as_text.assert_called_once_with(encoding="utf-8")
self.assertIsNone(bot)

async def test_async_find_installation(self):
"""Test async_find_installation method"""
def test_find_installation(self):
"""Test find_installation method"""
self.blob.download_as_text.return_value = json.dumps({"user_id": self.entr_installation.user_id})
# test installation found on enterprise install + normal workspace
for expect_install in [self.entr_installation, self.team_installation]:
actual_install = await self.installation_store.async_find_installation(
actual_install = self.installation_store.find_installation(
enterprise_id=expect_install.enterprise_id,
team_id=expect_install.team_id,
user_id=expect_install.user_id,
Expand All @@ -173,7 +173,7 @@ async def test_async_find_installation(self):

# test installation not found
self.blob.download_as_text.side_effect = Exception()
actual_install = await self.installation_store.async_find_installation(
actual_install = self.installation_store.find_installation(
enterprise_id=self.entr_installation.enterprise_id,
team_id=self.entr_installation.team_id,
user_id=self.entr_installation.user_id,
Expand All @@ -182,12 +182,12 @@ async def test_async_find_installation(self):
self.blob.download_as_text.assert_called_once_with(encoding="utf-8")
self.assertIsNone(actual_install)

async def test_async_delete_installation_and_test_delete_entity(self):
"""Test async_delete_installation and test_delete_entity methods"""
def test_delete_installation_and_test_delete_entity(self):
"""Test delete_installation and test_delete_entity methods"""
self.blob.exists.return_value = True
# test delete enterprise install + normal workspace when blob exists
for install in [self.entr_installation, self.team_installation]:
await self.installation_store.async_delete_installation(
self.installation_store.delete_installation(
enterprise_id=install.enterprise_id, team_id=install.team_id, user_id=install.user_id
)
self.storage_client.bucket.assert_called_once_with(self.bucket_name)
Expand All @@ -203,7 +203,7 @@ async def test_async_delete_installation_and_test_delete_entity(self):

# test delete blob doesn't exist
self.blob.exists.return_value = False
await self.installation_store.async_delete_installation(
self.installation_store.delete_installation(
enterprise_id=self.entr_installation.enterprise_id,
team_id=self.entr_installation.team_id,
user_id=self.entr_installation.user_id,
Expand All @@ -212,11 +212,11 @@ async def test_async_delete_installation_and_test_delete_entity(self):
self.blob.delete.assert_not_called()

@patch("slack_sdk.oauth.installation_store.google_cloud_storage.GoogleCloudStorageInstallationStore._delete_entity")
async def test_async_delete_bot(self, delete_entity: Mock):
"""Test async_delete_bot method"""
def test_delete_bot(self, delete_entity: Mock):
"""Test delete_bot method"""
# test delete bot from enterprise install + normal workspace
for install in [self.entr_installation, self.team_installation]:
await self.installation_store.async_delete_bot(enterprise_id=install.enterprise_id, team_id=install.team_id)
self.installation_store.delete_bot(enterprise_id=install.enterprise_id, team_id=install.team_id)
delete_entity.assert_called_once_with(
data_type="bot", enterprise_id=install.enterprise_id, team_id=install.team_id, user_id=None
)
Expand Down
20 changes: 10 additions & 10 deletions tests/slack_sdk/oauth/state_store/test_google_cloud_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
from slack_sdk.oauth.state_store.google_cloud_storage import GoogleCloudStorageOAuthStateStore


class TestGoogleStateStore(unittest.IsolatedAsyncioTestCase):
class TestGoogleStateStore(unittest.TestCase):
"""Test GoogleCloudStorageOAuthStateStore class"""

async def asyncSetUp(self):
def setUp(self):
"""Setup tests"""
self.blob = Mock(spec=Blob)
self.blob.download_as_text.return_value = str(time.time())
Expand All @@ -39,20 +39,20 @@ def test_get_logger(self):
"""Test get_logger method"""
self.assertEqual(self.state_store.logger, self.logger)

async def test_async_issue(self):
"""Test async_issue method"""
state = await self.state_store.async_issue()
def test_issue(self):
"""Test issue method"""
state = self.state_store.issue()
self.storage_client.bucket.assert_called_once_with(self.bucket_name)
self.bucket.blob.assert_called_once()
self.assertEqual(self.bucket.blob.call_args.args[0], state)
self.blob.upload_from_string.assert_called_once()
self.assertRegex(self.blob.upload_from_string.call_args.args[0], r"\d{10,}.\d{5,}")

async def test_async_comsume(self):
"""Test async_comsume method"""
def test_consume(self):
"""Test consume method"""
state = "state"
# test consume returns valid
valid = await self.state_store.async_consume(state=state)
valid = self.state_store.consume(state=state)
self.storage_client.bucket.assert_called_once_with(self.bucket_name)
self.bucket.blob.assert_called_once_with(state)
self.blob.download_as_text.assert_called_once_with(encoding="utf-8")
Expand All @@ -64,14 +64,14 @@ async def test_async_comsume(self):

# test consume returns invalid
self.state_store.expiration_seconds = 0
valid = await self.state_store.async_consume(state=state)
valid = self.state_store.consume(state=state)
self.assertFalse(time.time() < float(self.blob.download_as_text.return_value) + self.state_store.expiration_seconds)
self.assertFalse(valid)

self.blob.reset_mock()

# test consume throw exception
self.blob.download_as_text.side_effect = Exception()
valid = await self.state_store.async_consume(state=state)
valid = self.state_store.consume(state=state)
self.blob.download_as_text.assert_called_once_with(encoding="utf-8")
self.assertFalse(valid)