Skip to content

Commit

Permalink
Add TestRun.add_attachment rpc.
Browse files Browse the repository at this point in the history
  • Loading branch information
carboxylman authored and atodorov committed Feb 17, 2023
1 parent 70d6790 commit 9344e28
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tcms/rpc/api/testrun.py
Expand Up @@ -9,6 +9,7 @@
from tcms.testcases.models import TestCase
from tcms.testruns.forms import NewRunForm
from tcms.testruns.models import Property, TestExecution, TestRun
from tcms.rpc import utils

__all__ = (
"create",
Expand All @@ -22,6 +23,7 @@
"add_cc",
"remove_cc",
"properties",
"add_attachment"
)


Expand Down Expand Up @@ -394,3 +396,29 @@ def properties(query=None):
.order_by("run", "name", "value")
.distinct()
)


@permissions_required("attachments.add_attachment")
@rpc_method(name="TestRun.add_attachment")
def add_attachment(run_id, filename, b64content, **kwargs):
"""
.. function:: RPC TestRun.add_attachment(run_id, filename, b64content)
Add attachment to the given TestRun.
:param run_id: PK of TestRun
:type run_id: int
:param filename: File name of attachment, e.g. 'logs.txt'
:type filename: str
:param b64content: Base64 encoded content
:type b64content: str
:param \\**kwargs: Dict providing access to the current request, protocol,
entry point name and handler instance from the rpc method
"""
utils.add_attachment(
run_id,
"testruns.TestRun",
kwargs.get(REQUEST_KEY).user,
filename,
b64content,
)
22 changes: 22 additions & 0 deletions tcms/rpc/tests/test_testrun.py
Expand Up @@ -591,3 +591,25 @@ def verify_api_with_permission(self):
def verify_api_without_permission(self):
with self.assertRaisesRegex(ProtocolError, "403 Forbidden"):
self.rpc_client.TestRun.update(self.test_run.pk, self.update_fields)


class TestAddAttachmentPermissions(APIPermissionsTestCase):
permission_label = "attachments.add_attachment"

def _fixture_setup(self):
super()._fixture_setup()

self.run = TestRunFactory()

def verify_api_with_permission(self):
self.rpc_client.TestRun.add_attachment(
self.run.pk, "test.txt", "a2l3aXRjbXM="
)
attachments = Attachment.objects.attachments_for_object(self.run)
self.assertEqual(len(attachments), 1)
self.assertEqual(attachments[0].object_id, str(self.run.pk))

def verify_api_without_permission(self):
with self.assertRaisesRegex(ProtocolError, "403 Forbidden"):
self.rpc_client.TestRun.add_attachment(
self.run.pk, "test.txt", "a2l3aXRjbXM="

0 comments on commit 9344e28

Please sign in to comment.