diff --git a/canvasapi/submission.py b/canvasapi/submission.py index f2d738c0..f54c1c96 100644 --- a/canvasapi/submission.py +++ b/canvasapi/submission.py @@ -153,7 +153,7 @@ def mark_unread(self, **kwargs): ) return response.status_code == 204 - def upload_comment(self, file: FileOrPathLike, **kwargs): + def upload_comment(self, file: FileOrPathLike, text_comment=None, **kwargs): """ Upload a file to attach to this submission as a comment. @@ -177,7 +177,10 @@ def upload_comment(self, file: FileOrPathLike, **kwargs): ).start() if response[0]: - self.edit(comment={"file_ids": [response[1]["id"]]}) + comment = {"file_ids": [response[1]["id"]]} + if text_comment: + comment["text_comment"] = text_comment + self.edit(comment=comment) return response diff --git a/tests/test_submission.py b/tests/test_submission.py index 84b7a2dd..ae36ea05 100644 --- a/tests/test_submission.py +++ b/tests/test_submission.py @@ -9,6 +9,7 @@ from canvasapi.submission import GroupedSubmission, Submission from tests import settings from tests.util import cleanup_file, register_uris +from unittest.mock import patch @requests_mock.Mocker() @@ -111,14 +112,24 @@ def test_upload_comment(self, m): ) filename = "testfile_submission_{}".format(uuid.uuid4().hex) + text_comment = "Here is my file comment" try: with open(filename, "w+") as file: - response = self.submission.upload_comment(file) + with patch.object(self.submission, 'edit') as mock_edit: + response = self.submission.upload_comment(file, text_comment=text_comment) + + self.assertTrue(response[0]) + self.assertIsInstance(response[1], dict) + self.assertIn("url", response[1]) + + # Verify if the edit method was called with the correct arguments + mock_edit.assert_called_once() + _, kwargs = mock_edit.call_args + self.assertIn("comment", kwargs) + self.assertIn("text_comment", kwargs["comment"]) + self.assertEqual(kwargs["comment"]["text_comment"], text_comment) - self.assertTrue(response[0]) - self.assertIsInstance(response[1], dict) - self.assertIn("url", response[1]) finally: cleanup_file(filename)