|
1 | 1 | import os |
| 2 | +import tempfile |
2 | 3 | import unittest |
3 | 4 |
|
4 | 5 | from slack_sdk.oauth.installation_store import Installation |
5 | 6 | from slack_sdk.oauth.installation_store.async_cacheable_installation_store import ( |
6 | 7 | AsyncCacheableInstallationStore, |
7 | 8 | ) |
| 9 | +from slack_sdk.oauth.installation_store.file import FileInstallationStore |
8 | 10 | from slack_sdk.oauth.installation_store.sqlite3 import SQLite3InstallationStore |
9 | 11 | from tests.helpers import async_test |
10 | 12 |
|
11 | 13 |
|
| 14 | +def build_installation(*, user_id: str, bot_token: str) -> Installation: |
| 15 | + return Installation( |
| 16 | + app_id="A111", |
| 17 | + enterprise_id="E111", |
| 18 | + team_id="T111", |
| 19 | + user_id=user_id, |
| 20 | + bot_id="B111", |
| 21 | + bot_token=bot_token, |
| 22 | + bot_scopes=["chat:write"], |
| 23 | + bot_user_id="UB111", |
| 24 | + ) |
| 25 | + |
| 26 | + |
12 | 27 | class TestCacheable(unittest.TestCase): |
13 | 28 | @async_test |
14 | 29 | async def test_save_and_find(self): |
@@ -86,3 +101,24 @@ async def test_save_and_find_token_rotation(self): |
86 | 101 | self.assertIsNone(bot) |
87 | 102 | bot = await store.async_find_bot(enterprise_id="E111", team_id="T111") |
88 | 103 | self.assertIsNotNone(bot) |
| 104 | + |
| 105 | + @async_test |
| 106 | + async def test_save_evicts_stale_installations(self): |
| 107 | + base_dir = tempfile.TemporaryDirectory() |
| 108 | + self.addCleanup(base_dir.cleanup) |
| 109 | + file_store = FileInstallationStore(base_dir=base_dir.name, client_id="111.222") |
| 110 | + store = AsyncCacheableInstallationStore(file_store) |
| 111 | + |
| 112 | + await store.async_save(build_installation(user_id="U111", bot_token="xoxb-initial")) |
| 113 | + await store.async_find_installation(enterprise_id="E111", team_id="T111") |
| 114 | + await store.async_find_installation(enterprise_id="E111", team_id="T111", user_id="U111") |
| 115 | + |
| 116 | + await store.async_save(build_installation(user_id="U111", bot_token="xoxb-refreshed")) |
| 117 | + i = await store.async_find_installation(enterprise_id="E111", team_id="T111") |
| 118 | + self.assertEqual(i.bot_token, "xoxb-refreshed") |
| 119 | + i = await store.async_find_installation(enterprise_id="E111", team_id="T111", user_id="U111") |
| 120 | + self.assertEqual(i.bot_token, "xoxb-refreshed") |
| 121 | + |
| 122 | + await store.async_save(build_installation(user_id="U222", bot_token="xoxb-u222")) |
| 123 | + i = await store.async_find_installation(enterprise_id="E111", team_id="T111", user_id="U111") |
| 124 | + self.assertEqual(i.bot_token, "xoxb-u222") |
0 commit comments