tcms.rpc.tests.test_component   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 71
dl 0
loc 100
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A TestFilterComponents._fixture_setup() 0 9 1
A TestFilterComponents.test_filter_non_existing() 0 3 1
A TestFilterComponents.test_filter_by_name() 0 4 1
A TestFilterComponents.test_filter_by_product_id() 0 10 1
A TestUpdateComponent._fixture_setup() 0 9 1
A TestCreateComponent.test_add_component() 0 15 1
A TestUpdateComponent.test_update_component() 0 4 1
A TestCreateComponent.test_add_component_with_no_perms() 0 4 2
A TestCreateComponent._fixture_setup() 0 4 1
A TestUpdateComponent.test_update_component_with_non_exist() 0 5 2
A TestUpdateComponent.test_update_component_with_no_perms() 0 4 2
1
# -*- coding: utf-8 -*-
2
# pylint: disable=attribute-defined-outside-init, invalid-name
3
4
from xmlrpc.client import Fault as XmlRPCFault
5
from xmlrpc.client import ProtocolError
6
7
from tcms.rpc.tests.utils import APITestCase
8
from tcms.tests.factories import ComponentFactory, ProductFactory
9
10
11
class TestFilterComponents(APITestCase):
12
    def _fixture_setup(self):
13
        super()._fixture_setup()
14
15
        self.product = ProductFactory(name="StarCraft")
16
        self.component = ComponentFactory(
17
            name="application",
18
            product=self.product,
19
            initial_owner=None,
20
            initial_qa_contact=None,
21
        )
22
23
    def test_filter_by_product_id(self):
24
        result = self.rpc_client.Component.filter({"product": self.product.pk})[0]
25
        self.assertIsNotNone(result)
26
27
        self.assertEqual(result["id"], self.component.pk)
28
        self.assertEqual(result["name"], "application")
29
        self.assertEqual(result["product"], self.product.pk)
30
        self.assertIn("description", result)
31
        self.assertIn("initial_owner", result)
32
        self.assertIn("initial_qa_contact", result)
33
34
    def test_filter_by_name(self):
35
        com = self.rpc_client.Component.filter({"name": "application"})
36
        self.assertIsNotNone(com)
37
        self.assertEqual(com[0]["name"], "application")
38
39
    def test_filter_non_existing(self):
40
        found = self.rpc_client.Component.filter({"name": "documentation"})
41
        self.assertEqual(0, len(found))
42
43
44
class TestCreateComponent(APITestCase):
45
    def _fixture_setup(self):
46
        super()._fixture_setup()
47
48
        self.product = ProductFactory()
49
50
    def test_add_component(self):
51
        result = self.rpc_client.Component.create(
52
            {
53
                "name": "application",
54
                "product": self.product.pk,
55
            }
56
        )
57
        self.assertIsNotNone(result)
58
59
        self.assertIn("id", result)
60
        self.assertEqual(result["name"], "application")
61
        self.assertEqual(result["product"], self.product.pk)
62
        self.assertEqual(result["initial_owner"], self.api_user.pk)
63
        self.assertEqual(result["initial_qa_contact"], self.api_user.pk)
64
        self.assertEqual(result["description"], "Created via API")
65
66
    def test_add_component_with_no_perms(self):
67
        self.rpc_client.Auth.logout()
68
        with self.assertRaisesRegex(ProtocolError, "403 Forbidden"):
69
            self.rpc_client.Component.create(self.product.pk, "MyComponent")
70
71
72
class TestUpdateComponent(APITestCase):
73
    # pylint: disable=objects-update-used
74
    def _fixture_setup(self):
75
        super()._fixture_setup()
76
77
        self.product = ProductFactory(name="StarCraft")
78
        self.component = ComponentFactory(
79
            name="application",
80
            product=self.product,
81
            initial_owner=None,
82
            initial_qa_contact=None,
83
        )
84
85
    def test_update_component(self):
86
        values = {"name": "Updated"}
87
        com = self.rpc_client.Component.update(self.component.pk, values)
88
        self.assertEqual(com["name"], "Updated")
89
90
    def test_update_component_with_non_exist(self):
91
        with self.assertRaisesRegex(
92
            XmlRPCFault, "Component matching query does not exist"
93
        ):
94
            self.rpc_client.Component.update(-99, {"name": "new name"})
95
96
    def test_update_component_with_no_perms(self):
97
        self.rpc_client.Auth.logout()
98
        with self.assertRaisesRegex(ProtocolError, "403 Forbidden"):
99
            self.rpc_client.Component.update(self.component.pk, {})
100