|
| 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({}) |
0 commit comments