|
| 1 | +import unittest |
| 2 | + |
| 3 | +import boto3 |
| 4 | +from moto import mock_s3 |
| 5 | +from slack_sdk.oauth.installation_store import Installation |
| 6 | +from slack_sdk.oauth.installation_store.amazon_s3 import AmazonS3InstallationStore |
| 7 | + |
| 8 | + |
| 9 | +class TestAmazonS3(unittest.TestCase): |
| 10 | + mock_s3 = mock_s3() |
| 11 | + bucket_name = "test-bucket" |
| 12 | + |
| 13 | + def setUp(self): |
| 14 | + self.mock_s3.start() |
| 15 | + s3 = boto3.resource("s3") |
| 16 | + bucket = s3.Bucket(self.bucket_name) |
| 17 | + bucket.create(CreateBucketConfiguration={"LocationConstraint": "af-south-1"}) |
| 18 | + |
| 19 | + def tearDown(self): |
| 20 | + self.mock_s3.stop() |
| 21 | + |
| 22 | + def build_store(self) -> AmazonS3InstallationStore: |
| 23 | + return AmazonS3InstallationStore( |
| 24 | + s3_client=boto3.client("s3"), |
| 25 | + bucket_name=self.bucket_name, |
| 26 | + client_id="111.222", |
| 27 | + ) |
| 28 | + |
| 29 | + def test_instance(self): |
| 30 | + store = self.build_store() |
| 31 | + self.assertIsNotNone(store) |
| 32 | + |
| 33 | + def test_save_and_find(self): |
| 34 | + store = self.build_store() |
| 35 | + installation = Installation( |
| 36 | + app_id="A111", |
| 37 | + enterprise_id="E111", |
| 38 | + team_id="T111", |
| 39 | + user_id="U111", |
| 40 | + bot_id="B111", |
| 41 | + bot_token="xoxb-111", |
| 42 | + bot_scopes=["chat:write"], |
| 43 | + bot_user_id="U222", |
| 44 | + ) |
| 45 | + store.save(installation) |
| 46 | + |
| 47 | + # find bots |
| 48 | + bot = store.find_bot(enterprise_id="E111", team_id="T111") |
| 49 | + self.assertIsNotNone(bot) |
| 50 | + bot = store.find_bot(enterprise_id="E111", team_id="T222") |
| 51 | + self.assertIsNone(bot) |
| 52 | + bot = store.find_bot(enterprise_id=None, team_id="T111") |
| 53 | + self.assertIsNone(bot) |
| 54 | + |
| 55 | + # delete bots |
| 56 | + store.delete_bot(enterprise_id="E111", team_id="T222") |
| 57 | + bot = store.find_bot(enterprise_id="E111", team_id="T222") |
| 58 | + self.assertIsNone(bot) |
| 59 | + |
| 60 | + # find installations |
| 61 | + i = store.find_installation(enterprise_id="E111", team_id="T111") |
| 62 | + self.assertIsNotNone(i) |
| 63 | + i = store.find_installation(enterprise_id="E111", team_id="T222") |
| 64 | + self.assertIsNone(i) |
| 65 | + i = store.find_installation(enterprise_id=None, team_id="T111") |
| 66 | + self.assertIsNone(i) |
| 67 | + |
| 68 | + i = store.find_installation( |
| 69 | + enterprise_id="E111", team_id="T111", user_id="U111" |
| 70 | + ) |
| 71 | + self.assertIsNotNone(i) |
| 72 | + i = store.find_installation( |
| 73 | + enterprise_id="E111", team_id="T111", user_id="U222" |
| 74 | + ) |
| 75 | + self.assertIsNone(i) |
| 76 | + i = store.find_installation( |
| 77 | + enterprise_id="E111", team_id="T222", user_id="U111" |
| 78 | + ) |
| 79 | + self.assertIsNone(i) |
| 80 | + |
| 81 | + # delete installations |
| 82 | + store.delete_installation(enterprise_id="E111", team_id="T111", user_id="U111") |
| 83 | + i = store.find_installation( |
| 84 | + enterprise_id="E111", team_id="T111", user_id="U111" |
| 85 | + ) |
| 86 | + self.assertIsNone(i) |
| 87 | + i = store.find_installation(enterprise_id="E111", team_id="T111") |
| 88 | + self.assertIsNone(i) |
| 89 | + |
| 90 | + # delete all |
| 91 | + store.save(installation) |
| 92 | + store.delete_all(enterprise_id="E111", team_id="T111") |
| 93 | + |
| 94 | + i = store.find_installation(enterprise_id="E111", team_id="T111") |
| 95 | + self.assertIsNone(i) |
| 96 | + i = store.find_installation( |
| 97 | + enterprise_id="E111", team_id="T111", user_id="U111" |
| 98 | + ) |
| 99 | + self.assertIsNone(i) |
| 100 | + bot = store.find_bot(enterprise_id="E111", team_id="T222") |
| 101 | + self.assertIsNone(bot) |
| 102 | + |
| 103 | + def test_org_installation(self): |
| 104 | + store = self.build_store() |
| 105 | + installation = Installation( |
| 106 | + app_id="AO111", |
| 107 | + enterprise_id="EO111", |
| 108 | + user_id="UO111", |
| 109 | + bot_id="BO111", |
| 110 | + bot_token="xoxb-O111", |
| 111 | + bot_scopes=["chat:write"], |
| 112 | + bot_user_id="UO222", |
| 113 | + is_enterprise_install=True, |
| 114 | + ) |
| 115 | + store.save(installation) |
| 116 | + |
| 117 | + # find bots |
| 118 | + bot = store.find_bot(enterprise_id="EO111", team_id=None) |
| 119 | + self.assertIsNotNone(bot) |
| 120 | + bot = store.find_bot( |
| 121 | + enterprise_id="EO111", team_id="TO222", is_enterprise_install=True |
| 122 | + ) |
| 123 | + self.assertIsNotNone(bot) |
| 124 | + bot = store.find_bot(enterprise_id="EO111", team_id="TO222") |
| 125 | + self.assertIsNone(bot) |
| 126 | + bot = store.find_bot(enterprise_id=None, team_id="TO111") |
| 127 | + self.assertIsNone(bot) |
| 128 | + |
| 129 | + # delete bots |
| 130 | + store.delete_bot(enterprise_id="EO111", team_id="TO222") |
| 131 | + bot = store.find_bot(enterprise_id="EO111", team_id=None) |
| 132 | + self.assertIsNotNone(bot) |
| 133 | + |
| 134 | + store.delete_bot(enterprise_id="EO111", team_id=None) |
| 135 | + bot = store.find_bot(enterprise_id="EO111", team_id=None) |
| 136 | + self.assertIsNone(bot) |
| 137 | + |
| 138 | + # find installations |
| 139 | + i = store.find_installation(enterprise_id="EO111", team_id=None) |
| 140 | + self.assertIsNotNone(i) |
| 141 | + i = store.find_installation( |
| 142 | + enterprise_id="EO111", team_id="T111", is_enterprise_install=True |
| 143 | + ) |
| 144 | + self.assertIsNotNone(i) |
| 145 | + i = store.find_installation(enterprise_id="EO111", team_id="T222") |
| 146 | + self.assertIsNone(i) |
| 147 | + i = store.find_installation(enterprise_id=None, team_id="T111") |
| 148 | + self.assertIsNone(i) |
| 149 | + |
| 150 | + i = store.find_installation( |
| 151 | + enterprise_id="EO111", team_id=None, user_id="UO111" |
| 152 | + ) |
| 153 | + self.assertIsNotNone(i) |
| 154 | + i = store.find_installation( |
| 155 | + enterprise_id="E111", |
| 156 | + team_id="T111", |
| 157 | + is_enterprise_install=True, |
| 158 | + user_id="U222", |
| 159 | + ) |
| 160 | + self.assertIsNone(i) |
| 161 | + i = store.find_installation(enterprise_id=None, team_id="T222", user_id="U111") |
| 162 | + self.assertIsNone(i) |
| 163 | + |
| 164 | + # delete installations |
| 165 | + store.delete_installation(enterprise_id="E111", team_id=None) |
| 166 | + i = store.find_installation(enterprise_id="E111", team_id=None) |
| 167 | + self.assertIsNone(i) |
| 168 | + |
| 169 | + # delete all |
| 170 | + store.save(installation) |
| 171 | + store.delete_all(enterprise_id="E111", team_id=None) |
| 172 | + |
| 173 | + i = store.find_installation(enterprise_id="E111", team_id=None) |
| 174 | + self.assertIsNone(i) |
| 175 | + i = store.find_installation(enterprise_id="E111", team_id=None, user_id="U111") |
| 176 | + self.assertIsNone(i) |
| 177 | + bot = store.find_bot(enterprise_id=None, team_id="T222") |
| 178 | + self.assertIsNone(bot) |
| 179 | + |
| 180 | + def test_save_and_find_token_rotation(self): |
| 181 | + store = self.build_store() |
| 182 | + installation = Installation( |
| 183 | + app_id="A111", |
| 184 | + enterprise_id="E111", |
| 185 | + team_id="T111", |
| 186 | + user_id="U111", |
| 187 | + bot_id="B111", |
| 188 | + bot_token="xoxe.xoxp-1-initial", |
| 189 | + bot_scopes=["chat:write"], |
| 190 | + bot_user_id="U222", |
| 191 | + bot_refresh_token="xoxe-1-initial", |
| 192 | + bot_token_expires_in=43200, |
| 193 | + ) |
| 194 | + store.save(installation) |
| 195 | + |
| 196 | + bot = store.find_bot(enterprise_id="E111", team_id="T111") |
| 197 | + self.assertIsNotNone(bot) |
| 198 | + self.assertEqual(bot.bot_refresh_token, "xoxe-1-initial") |
| 199 | + |
| 200 | + # Update the existing data |
| 201 | + refreshed_installation = Installation( |
| 202 | + app_id="A111", |
| 203 | + enterprise_id="E111", |
| 204 | + team_id="T111", |
| 205 | + user_id="U111", |
| 206 | + bot_id="B111", |
| 207 | + bot_token="xoxe.xoxp-1-refreshed", |
| 208 | + bot_scopes=["chat:write"], |
| 209 | + bot_user_id="U222", |
| 210 | + bot_refresh_token="xoxe-1-refreshed", |
| 211 | + bot_token_expires_in=43200, |
| 212 | + ) |
| 213 | + store.save(refreshed_installation) |
| 214 | + |
| 215 | + # find bots |
| 216 | + bot = store.find_bot(enterprise_id="E111", team_id="T111") |
| 217 | + self.assertIsNotNone(bot) |
| 218 | + self.assertEqual(bot.bot_refresh_token, "xoxe-1-refreshed") |
| 219 | + bot = store.find_bot(enterprise_id="E111", team_id="T222") |
| 220 | + self.assertIsNone(bot) |
| 221 | + bot = store.find_bot(enterprise_id=None, team_id="T111") |
| 222 | + self.assertIsNone(bot) |
| 223 | + |
| 224 | + # delete bots |
| 225 | + store.delete_bot(enterprise_id="E111", team_id="T222") |
| 226 | + bot = store.find_bot(enterprise_id="E111", team_id="T222") |
| 227 | + self.assertIsNone(bot) |
| 228 | + |
| 229 | + # find installations |
| 230 | + i = store.find_installation(enterprise_id="E111", team_id="T111") |
| 231 | + self.assertIsNotNone(i) |
| 232 | + i = store.find_installation(enterprise_id="E111", team_id="T222") |
| 233 | + self.assertIsNone(i) |
| 234 | + i = store.find_installation(enterprise_id=None, team_id="T111") |
| 235 | + self.assertIsNone(i) |
| 236 | + |
| 237 | + i = store.find_installation( |
| 238 | + enterprise_id="E111", team_id="T111", user_id="U111" |
| 239 | + ) |
| 240 | + self.assertIsNotNone(i) |
| 241 | + i = store.find_installation( |
| 242 | + enterprise_id="E111", team_id="T111", user_id="U222" |
| 243 | + ) |
| 244 | + self.assertIsNone(i) |
| 245 | + i = store.find_installation( |
| 246 | + enterprise_id="E111", team_id="T222", user_id="U111" |
| 247 | + ) |
| 248 | + self.assertIsNone(i) |
| 249 | + |
| 250 | + # delete installations |
| 251 | + store.delete_installation(enterprise_id="E111", team_id="T111", user_id="U111") |
| 252 | + i = store.find_installation( |
| 253 | + enterprise_id="E111", team_id="T111", user_id="U111" |
| 254 | + ) |
| 255 | + self.assertIsNone(i) |
| 256 | + i = store.find_installation(enterprise_id="E111", team_id="T111") |
| 257 | + self.assertIsNone(i) |
| 258 | + |
| 259 | + # delete all |
| 260 | + store.save(installation) |
| 261 | + store.delete_all(enterprise_id="E111", team_id="T111") |
| 262 | + |
| 263 | + i = store.find_installation(enterprise_id="E111", team_id="T111") |
| 264 | + self.assertIsNone(i) |
| 265 | + i = store.find_installation( |
| 266 | + enterprise_id="E111", team_id="T111", user_id="U111" |
| 267 | + ) |
| 268 | + self.assertIsNone(i) |
| 269 | + bot = store.find_bot(enterprise_id="E111", team_id="T222") |
| 270 | + self.assertIsNone(bot) |
0 commit comments