WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 00c501b

Browse files
committed
Add Template.filter() API method
Add ``Template.create()`` API method
1 parent 5e17864 commit 00c501b

File tree

6 files changed

+145
-1
lines changed

6 files changed

+145
-1
lines changed

docs/source/modules/tcms.rpc.api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Submodules
3434
tcms.rpc.api.priority
3535
tcms.rpc.api.product
3636
tcms.rpc.api.tag
37+
tcms.rpc.api.template
3738
tcms.rpc.api.testcase
3839
tcms.rpc.api.testcasestatus
3940
tcms.rpc.api.testexecution
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tcms.rpc.api.template module
2+
============================
3+
4+
.. automodule:: tcms.rpc.api.template
5+
:members:
6+
:show-inheritance:
7+
:undoc-members:

tcms/rpc/api/forms/testcase.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from tcms.core.forms.fields import UserField
44
from tcms.rpc.api.forms import UpdateModelFormMixin
5-
from tcms.testcases.models import Category, TestCase, TestCaseStatus
5+
from tcms.testcases.models import Category, Template, TestCase, TestCaseStatus
66

77

88
class NewForm(forms.ModelForm):
@@ -32,6 +32,12 @@ class Meta:
3232
fields = "__all__"
3333

3434

35+
class TemplateForm(forms.ModelForm):
36+
class Meta:
37+
model = Template
38+
fields = "__all__"
39+
40+
3541
class TestCaseStatusForm(forms.ModelForm):
3642
class Meta:
3743
model = TestCaseStatus

tcms/rpc/api/template.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright (c) 2025 Alexander Todorov <[email protected]>
2+
#
3+
# Licensed under the GPL 2.0: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
4+
5+
from django.forms.models import model_to_dict
6+
from modernrpc.core import rpc_method
7+
8+
from tcms.rpc.api.forms.testcase import TemplateForm
9+
from tcms.rpc.decorators import permissions_required
10+
from tcms.testcases.models import Template
11+
12+
13+
@permissions_required("testcases.view_template")
14+
@rpc_method(name="Template.filter")
15+
def filter(query): # pylint: disable=redefined-builtin
16+
"""
17+
.. function:: RPC Template.filter(query)
18+
19+
Perform a search and return the resulting list of templates.
20+
21+
:param query: Field lookups for :class:`tcms.testcases.models.Template`
22+
:type query: dict
23+
:return: Serialized list of :class:`tcms.testcases.models.Template` objects
24+
:rtype: dict
25+
:raises PermissionDenied: if missing *testcases.view_template* permission
26+
"""
27+
return list(
28+
Template.objects.filter(**query).values("id", "name", "text").distinct()
29+
)
30+
31+
32+
@permissions_required("testcases.add_template")
33+
@rpc_method(name="Template.create")
34+
def create(values):
35+
"""
36+
.. function:: RPC Template.create(values)
37+
38+
Create a new Template object and store it in the database.
39+
40+
:param values: Field values for :class:`tcms.testcases.models.Template`
41+
:type values: dict
42+
:return: Serialized :class:`tcms.testcases.models.Template` object
43+
:rtype: dict
44+
:raises ValueError: if input values don't validate
45+
:raises PermissionDenied: if missing *testcases.add_template* permission
46+
"""
47+
form = TemplateForm(values)
48+
49+
if form.is_valid():
50+
template = form.save()
51+
return model_to_dict(template)
52+
53+
raise ValueError(list(form.errors.items()))

tcms/rpc/tests/test_template.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -*- coding: utf-8 -*-
2+
# pylint: disable=attribute-defined-outside-init, invalid-name, objects-update-used
3+
#
4+
# Copyright (c) 2025 Alexander Todorov <[email protected]>
5+
#
6+
# Licensed under the GPL 2.0: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7+
8+
9+
from tcms.rpc.tests.utils import APIPermissionsTestCase
10+
from tcms.testcases.models import Template
11+
from tcms.xmlrpc_wrapper import XmlRPCFault
12+
13+
14+
class TemplateCreate(APIPermissionsTestCase):
15+
permission_label = "testcases.add_template"
16+
17+
def verify_api_with_permission(self):
18+
result = self.rpc_client.Template.create(
19+
{
20+
"name": "API test",
21+
"text": """
22+
- Method name:
23+
- Method URL:
24+
- Input data:
25+
- Expected result:""",
26+
}
27+
)
28+
29+
# verify the serialized result
30+
self.assertIn("id", result)
31+
self.assertEqual(result["name"], "API test")
32+
self.assertIn("Method URL", result["text"])
33+
34+
# verify the object from the DB
35+
template = Template.objects.get(pk=result["id"])
36+
self.assertEqual(template.name, result["name"])
37+
self.assertEqual(template.text, result["text"])
38+
39+
def verify_api_without_permission(self):
40+
with self.assertRaisesRegex(
41+
XmlRPCFault, 'Authentication failed when calling "Template.create"'
42+
):
43+
self.rpc_client.Template.create(
44+
{
45+
"name": "1-2-3",
46+
"text": """
47+
1) Do:
48+
2) Do:
49+
3) Result should be:""",
50+
}
51+
)
52+
53+
54+
class TestTemplateFilter(APIPermissionsTestCase):
55+
permission_label = "testcases.view_template"
56+
57+
@classmethod
58+
def _fixture_setup(cls):
59+
super()._fixture_setup()
60+
61+
def verify_api_with_permission(self):
62+
result = self.rpc_client.Template.filter(
63+
{
64+
"name": "Gherkin syntax",
65+
}
66+
)[0]
67+
68+
self.assertIn("id", result)
69+
self.assertEqual(result["name"], "Gherkin syntax")
70+
self.assertIn("what behavior will be tested", result["text"])
71+
72+
def verify_api_without_permission(self):
73+
with self.assertRaisesRegex(
74+
XmlRPCFault, 'Authentication failed when calling "Template.filter"'
75+
):
76+
self.rpc_client.Template.filter({})

tcms/settings/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@
498498
"tcms.rpc.api.priority",
499499
"tcms.rpc.api.product",
500500
"tcms.rpc.api.tag",
501+
"tcms.rpc.api.template",
501502
"tcms.rpc.api.testcase",
502503
"tcms.rpc.api.testexecution",
503504
"tcms.rpc.api.testexecutionstatus",

0 commit comments

Comments
 (0)