|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import unittest |
| 4 | + |
| 5 | +from integration_tests.env_variable_names import ( |
| 6 | + SLACK_SDK_TEST_BOT_TOKEN, |
| 7 | + SLACK_SDK_TEST_WEB_TEST_CHANNEL_ID, |
| 8 | +) |
| 9 | +from slack_sdk.web import WebClient |
| 10 | +from slack_sdk.web.async_client import AsyncWebClient |
| 11 | + |
| 12 | + |
| 13 | +class TestBookmarks(unittest.TestCase): |
| 14 | + """Runs integration tests with real Slack API testing the bookmarks.* APIs""" |
| 15 | + |
| 16 | + def setUp(self): |
| 17 | + if not hasattr(self, "logger"): |
| 18 | + self.logger = logging.getLogger(__name__) |
| 19 | + self.bot_token = os.environ[SLACK_SDK_TEST_BOT_TOKEN] |
| 20 | + self.async_client: AsyncWebClient = AsyncWebClient(token=self.bot_token) |
| 21 | + self.sync_client: WebClient = WebClient(token=self.bot_token) |
| 22 | + self.channel_id = os.environ[SLACK_SDK_TEST_WEB_TEST_CHANNEL_ID] |
| 23 | + |
| 24 | + def tearDown(self): |
| 25 | + pass |
| 26 | + |
| 27 | + def test_adding_listing_editing_removing_bookmark(self): |
| 28 | + client = self.sync_client |
| 29 | + # create a new bookmark |
| 30 | + bookmark = client.bookmarks_add( |
| 31 | + channel_id=self.channel_id, |
| 32 | + title="slack!", |
| 33 | + type="link", |
| 34 | + link="https://slack.com", |
| 35 | + ) |
| 36 | + self.assertIsNotNone(bookmark) |
| 37 | + bookmark_id = bookmark["bookmark"]["id"] |
| 38 | + # make sure we find the bookmark we just added |
| 39 | + all_bookmarks = client.bookmarks_list(channel_id=self.channel_id) |
| 40 | + self.assertIsNotNone(all_bookmarks) |
| 41 | + self.assertIsNotNone( |
| 42 | + next( |
| 43 | + (b for b in all_bookmarks["bookmarks"] if b["id"] == bookmark_id), None |
| 44 | + ) |
| 45 | + ) |
| 46 | + # edit the bookmark |
| 47 | + bookmark = client.bookmarks_edit( |
| 48 | + bookmark_id=bookmark_id, |
| 49 | + channel_id=self.channel_id, |
| 50 | + title="slack api!", |
| 51 | + type="link", |
| 52 | + link="https://api.slack.com", |
| 53 | + ) |
| 54 | + self.assertIsNotNone(bookmark) |
| 55 | + # make sure we find the edited bookmark we just added |
| 56 | + all_bookmarks = client.bookmarks_list(channel_id=self.channel_id) |
| 57 | + self.assertIsNotNone(all_bookmarks) |
| 58 | + edited_bookmark = next( |
| 59 | + (b for b in all_bookmarks["bookmarks"] if b["id"] == bookmark_id), None |
| 60 | + ) |
| 61 | + self.assertIsNotNone(edited_bookmark) |
| 62 | + self.assertEqual(edited_bookmark["title"], "slack api!") |
| 63 | + # remove the bookmark |
| 64 | + removed_bookmark = client.bookmarks_remove( |
| 65 | + bookmark_id=bookmark_id, channel_id=self.channel_id |
| 66 | + ) |
| 67 | + self.assertIsNotNone(removed_bookmark) |
| 68 | + # make sure we cannot find the bookmark we just removed |
| 69 | + all_bookmarks = client.bookmarks_list(channel_id=self.channel_id) |
| 70 | + self.assertIsNotNone(all_bookmarks) |
| 71 | + self.assertIsNone( |
| 72 | + next((b for b in all_bookmarks if b["id"] == bookmark_id), None) |
| 73 | + ) |
0 commit comments