-
Notifications
You must be signed in to change notification settings - Fork 58
Enabled slack connector to ingest files and support OAuth #707
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3ceea10
File ingestion fixes to slack connector
mateuszkuprowski ce7bac0
feat(slack): accept OAuth refresh tokens
af2c593
Merge branch 'main' into fs-1968-slack-connector-oauth
mateuszkuprowski 32e0604
Bot review fixes and version bump
16e89a5
Code review improvements
9a21653
Merge origin/main into fs-1968-slack-connector-oauth
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
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,166 @@ | ||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
|
|
||
| from unstructured_ingest.data_types.file_data import ( | ||
| FileData, | ||
| FileDataSourceMetadata, | ||
| SourceIdentifiers, | ||
| ) | ||
| from unstructured_ingest.error import ValueError as IngestValueError | ||
| from unstructured_ingest.processes.connectors.slack import ( | ||
| PRIVATE_FILE_DOWNLOAD_TIMEOUT_SECONDS, | ||
| SlackAccessConfig, | ||
| SlackDownloader, | ||
| SlackIndexer, | ||
| SlackIndexerConfig, | ||
| _NoRedirectHandler, | ||
| ) | ||
|
|
||
|
|
||
| def test_slack_access_config_accepts_refresh_token(): | ||
| config = SlackAccessConfig(token="xoxb-slack-token", refresh_token="xoxe-slack-refresh-token") | ||
|
|
||
| assert config.token == "xoxb-slack-token" | ||
| assert config.refresh_token == "xoxe-slack-refresh-token" | ||
|
|
||
|
|
||
| def test_slack_indexer_emits_file_data_for_message_files(): | ||
| client = Mock() | ||
| client.conversations_history.return_value = [ | ||
| { | ||
| "messages": [ | ||
| { | ||
| "ts": "1710000000.000100", | ||
| "text": "Here is the report", | ||
| "files": [ | ||
| { | ||
| "id": "F123", | ||
| "name": "report.pdf", | ||
| "url_private_download": "https://files.slack.com/report.pdf", | ||
| } | ||
| ], | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| connection_config = Mock() | ||
| connection_config.get_client.return_value = client | ||
| indexer = SlackIndexer( | ||
| index_config=SlackIndexerConfig(channels=["C123"]), | ||
| connection_config=connection_config, | ||
| ) | ||
|
|
||
| file_data = list(indexer.run()) | ||
|
|
||
| assert len(file_data) == 2 | ||
| assert file_data[0].source_identifiers.filename.endswith(".xml") | ||
| slack_file = file_data[1] | ||
| assert slack_file.source_identifiers.filename == "F123-report.pdf" | ||
| assert slack_file.metadata.record_locator == { | ||
| "type": "file", | ||
| "channel": "C123", | ||
| "message_ts": "1710000000.000100", | ||
| "file_id": "F123", | ||
| } | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_slack_downloader_downloads_file_attachment(tmp_path, mocker): | ||
| async_client = Mock() | ||
| async_client.files_info = mocker.AsyncMock( | ||
| return_value={ | ||
| "ok": True, | ||
| "file": { | ||
| "id": "F123", | ||
| "name": "report.pdf", | ||
| "url_private_download": "https://files.slack.com/report.pdf", | ||
| }, | ||
| } | ||
| ) | ||
| connection_config = Mock() | ||
| connection_config.get_async_client.return_value = async_client | ||
| connection_config.access_config.get_secret_value.return_value.token = "xoxb-slack-token" | ||
| downloader = SlackDownloader(connection_config=connection_config) | ||
| file_data = FileData( | ||
| identifier="F123", | ||
| connector_type="slack", | ||
| source_identifiers=SourceIdentifiers( | ||
| filename="F123-report.pdf", | ||
| fullpath="F123-report.pdf", | ||
| ), | ||
| metadata=FileDataSourceMetadata( | ||
| record_locator={ | ||
| "type": "file", | ||
| "channel": "C123", | ||
| "message_ts": "1710000000.000100", | ||
| "file_id": "F123", | ||
| } | ||
| ), | ||
| ) | ||
| response = Mock() | ||
| response.__enter__ = Mock(return_value=response) | ||
| response.__exit__ = Mock(return_value=None) | ||
| response.read.side_effect = [b"pdf ", b"bytes", b""] | ||
| opener = Mock() | ||
| opener.open.return_value = response | ||
| build_opener = mocker.patch("urllib.request.build_opener", return_value=opener) | ||
|
|
||
| await downloader._download_file(file_data, tmp_path / "F123-report.pdf") | ||
|
|
||
| assert (tmp_path / "F123-report.pdf").read_bytes() == b"pdf bytes" | ||
| build_opener.assert_called_once_with(_NoRedirectHandler) | ||
| opener.open.assert_called_once() | ||
| request = opener.open.call_args.args[0] | ||
| assert request.full_url == "https://files.slack.com/report.pdf" | ||
| assert request.headers["Authorization"] == "Bearer xoxb-slack-token" | ||
| assert opener.open.call_args.kwargs["timeout"] == PRIVATE_FILE_DOWNLOAD_TIMEOUT_SECONDS | ||
| assert response.read.call_count > 1 | ||
|
|
||
|
|
||
| def test_slack_private_file_download_rejects_redirects(): | ||
| handler = _NoRedirectHandler() | ||
|
|
||
| with pytest.raises(IngestValueError, match="redirected"): | ||
| handler.redirect_request(None, None, 302, "Found", {}, "https://example.com/report.pdf") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_slack_downloader_rejects_non_slack_private_download_url(tmp_path, mocker): | ||
| async_client = Mock() | ||
| async_client.files_info = mocker.AsyncMock( | ||
| return_value={ | ||
| "ok": True, | ||
| "file": { | ||
| "id": "F123", | ||
| "name": "report.pdf", | ||
| "url_private_download": "https://example.com/report.pdf", | ||
| }, | ||
| } | ||
| ) | ||
| connection_config = Mock() | ||
| connection_config.get_async_client.return_value = async_client | ||
| connection_config.access_config.get_secret_value.return_value.token = "xoxb-slack-token" | ||
| downloader = SlackDownloader(connection_config=connection_config) | ||
| file_data = FileData( | ||
| identifier="F123", | ||
| connector_type="slack", | ||
| source_identifiers=SourceIdentifiers( | ||
| filename="F123-report.pdf", | ||
| fullpath="F123-report.pdf", | ||
| ), | ||
| metadata=FileDataSourceMetadata( | ||
| record_locator={ | ||
| "type": "file", | ||
| "channel": "C123", | ||
| "message_ts": "1710000000.000100", | ||
| "file_id": "F123", | ||
| } | ||
| ), | ||
| ) | ||
| build_opener = mocker.patch("urllib.request.build_opener") | ||
|
|
||
| with pytest.raises(IngestValueError, match="files.slack.com"): | ||
| await downloader._download_file(file_data, tmp_path / "F123-report.pdf") | ||
|
|
||
| build_opener.assert_not_called() |
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| __version__ = "1.6.3" # pragma: no cover | ||
| __version__ = "1.6.4" # pragma: no cover |
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
Oops, something went wrong.
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.