-
-
Notifications
You must be signed in to change notification settings - Fork 650
Clean PR: Slack member sync implementation #1629
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9b7edee
Clean PR: Slack member sync implementation
shdwcodr c18fe30
Update backend/apps/slack/management/commands/slack_sync_data.py
shdwcodr c2e6285
Update backend/apps/slack/tests/test_slack_sync_data.py
shdwcodr da366de
Update backend/apps/slack/tests/test_slack_sync_data.py
shdwcodr 27040ff
Add slack_sync_data command test with mock, fix bot token leak, handlβ¦
shdwcodr ec30f99
Update test_slack_sync_data.py
shdwcodr 4968692
Update slack_sync_data.py
shdwcodr 1c7157a
Merge branch 'main' into rebased-sync
shdwcodr b48fb55
Merge branch 'main' into rebased-sync
shdwcodr f6020c6
Merge branch 'main' into rebased-sync
shdwcodr 852d8af
Merge branch 'main' into rebased-sync
shdwcodr 8179199
Merge branch 'main' into rebased-sync
shdwcodr c1fb453
Merge branch 'main' into rebased-sync
shdwcodr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
backend/apps/slack/migrations/0014_remove_workspace_total_members_count.py
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the migration if no changes in the models? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Generated by Django 5.2.1 on 2025-06-11 08:36 | ||
|
|
||
| from django.db import migrations | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('slack', '0013_alter_conversation_total_members_count_and_more'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RemoveField( | ||
| model_name='workspace', | ||
| name='total_members_count', | ||
| ), | ||
| ] |
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use pytest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| from django.test import TestCase | ||
| from django.core.management import call_command | ||
| from apps.slack.models import Workspace | ||
| from unittest.mock import patch, MagicMock | ||
| import slack_bolt.app.app as slack_bolt_app | ||
| import slack_sdk.web.client as slack_web_client | ||
|
|
||
|
|
||
| class SlackSyncDataCommandTests(TestCase): | ||
| def setUp(self): | ||
| # Patch Slack App to prevent actual Slack startup | ||
| self.app_patcher = patch.object(slack_bolt_app, "App", autospec=True) | ||
| self.app_patcher.start() | ||
|
|
||
| # Patch Slack WebClient to prevent HTTP calls | ||
| self.web_client_patcher = patch.object(slack_web_client, "WebClient", autospec=True) | ||
| self.mock_web_client_class = self.web_client_patcher.start() | ||
| self.mock_web_client = MagicMock() | ||
| self.mock_web_client_class.return_value = self.mock_web_client | ||
|
|
||
| # Fake a valid Slack bot token check | ||
| self.mock_web_client.auth_test.return_value = {"ok": True, "user_id": "BOT123"} | ||
|
|
||
| # Create a fake workspace object in DB | ||
| self.workspace = Workspace.objects.create( | ||
| name="Test Workspace", | ||
| slack_workspace_id="TEST123" | ||
| ) | ||
| # Set up bot token in environment for the test | ||
| import os | ||
| os.environ["DJANGO_SLACK_BOT_TOKEN"] = "xoxb-test-token" | ||
| def tearDown(self): | ||
| self.app_patcher.stop() | ||
| self.web_client_patcher.stop() | ||
|
|
||
| def test_command_syncs_mocked_slack_data(self): | ||
| # Mock Slack's users_list response with 2 pages | ||
| self.mock_web_client.users_list.side_effect = [ | ||
| { | ||
| "ok": True, | ||
| "members": [{"id": "U1"}, {"id": "U2"}], | ||
| "response_metadata": {"next_cursor": "next-123"} | ||
| }, | ||
| { | ||
| "ok": True, | ||
| "members": [{"id": "U3"}], | ||
| "response_metadata": {"next_cursor": ""} | ||
| } | ||
| ] | ||
|
|
||
| # Mock Slack's conversations_list response | ||
| self.mock_web_client.conversations_list.return_value = { | ||
| "ok": True, | ||
| "channels": [{"id": "C1", "name": "general"}, {"id": "C2", "name": "random"}], | ||
| "response_metadata": {"next_cursor": ""} | ||
| } | ||
|
|
||
| # Mock conversations.info for member count fetching | ||
| self.mock_web_client.conversations_info.return_value = { | ||
| "ok": True, | ||
| "channel": {"num_members": 10} | ||
| } | ||
|
|
||
| # Call the Django management command | ||
| call_command("slack_sync_data", batch_size=100, delay=0) | ||
|
|
||
| # Refresh from DB in case command saves anything | ||
| self.workspace.refresh_from_db() | ||
|
|
||
| # Verify members were created | ||
| from apps.slack.models import Member, Conversation | ||
| self.assertEqual(Member.objects.count(), 3) | ||
| self.assertEqual(Conversation.objects.count(), 2) | ||
|
|
||
| # Verify API calls were made correctly | ||
| self.assertEqual(self.mock_web_client.users_list.call_count, 2) | ||
| self.assertEqual(self.mock_web_client.conversations_list.call_count, 1) | ||
| self.assertEqual(self.mock_web_client.conversations_info.call_count, 2) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.