-
Notifications
You must be signed in to change notification settings - Fork 76
update warning #1184
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
+54
−5
Merged
update warning #1184
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Deferred the missing python-magic warning in pysnow to only emit when attachment upload is attempted, rather than at import time. |
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,45 @@ | ||
| """Unit tests for the pysnow module.""" | ||
|
|
||
| import warnings | ||
| from unittest.mock import MagicMock, mock_open, patch | ||
|
|
||
| from nautobot.core.testing import TestCase | ||
|
|
||
| from nautobot_ssot.integrations.servicenow.third_party.pysnow.attachment import Attachment | ||
|
|
||
|
|
||
| class TestAttachmentUploadContentType(TestCase): | ||
| """Test that the upload method sets Content-Type based on HAS_MAGIC.""" | ||
|
|
||
| def setUp(self): | ||
| """Set up a minimal Attachment instance with a mocked resource.""" | ||
| self.resource = MagicMock() | ||
| self.attachment = Attachment(resource=self.resource, table_name="incident") | ||
|
|
||
| @patch("nautobot_ssot.integrations.servicenow.third_party.pysnow.attachment.HAS_MAGIC", True) | ||
| @patch("nautobot_ssot.integrations.servicenow.third_party.pysnow.attachment.magic", create=True) | ||
| @patch("builtins.open", mock_open(read_data=b"file content")) | ||
| def test_upload_uses_magic_when_available(self, mock_magic): | ||
| """When python-magic is available, Content-Type should be determined by magic.""" | ||
| file_path = "/tmp/test.pdf" | ||
| mock_magic.from_file.return_value = "application/pdf" | ||
|
|
||
| self.attachment.upload(sys_id="abc123", file_path=file_path) | ||
|
|
||
| mock_magic.from_file.assert_called_once_with(file_path, mime=True) | ||
| call_kwargs = self.resource.request.call_args[1] | ||
| self.assertEqual(call_kwargs["headers"]["Content-Type"], "application/pdf") | ||
|
|
||
| @patch("nautobot_ssot.integrations.servicenow.third_party.pysnow.attachment.HAS_MAGIC", False) | ||
| @patch("builtins.open", mock_open(read_data=b"file content")) | ||
| def test_upload_falls_back_to_text_plain_without_magic(self): | ||
| """When python-magic is missing, Content-Type should fall back to text/plain with a warning.""" | ||
| with warnings.catch_warnings(record=True) as caught_warnings: | ||
| warnings.simplefilter("always") | ||
| self.attachment.upload(sys_id="abc123", file_path="/tmp/test.txt") | ||
|
|
||
| self.assertEqual(len(caught_warnings), 1) | ||
| self.assertIn("python-magic", str(caught_warnings[0].message)) | ||
|
|
||
| call_kwargs = self.resource.request.call_args[1] | ||
| self.assertEqual(call_kwargs["headers"]["Content-Type"], "text/plain") |
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.