Skip to content

Commit

Permalink
Enable uploading text comments and file in one line
Browse files Browse the repository at this point in the history
  • Loading branch information
StellaYHXXX committed Nov 28, 2023
1 parent 02d42cb commit b6ad694
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
7 changes: 5 additions & 2 deletions canvasapi/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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


Expand Down
19 changes: 15 additions & 4 deletions tests/test_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit b6ad694

Please sign in to comment.