Completed
Push — master ( a7ad2f...bd2755 )
by Jaspar
22s queued 14s
created

tests.protocols.gmpv9.testcmds.test_modify_policy_set_family_selection   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 190
Duplicated Lines 80 %

Importance

Changes 0
Metric Value
eloc 56
dl 152
loc 190
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A GmpModifyPolicySetFamilySelectionTestCase.test_modify_config_set_family_selection_with_auto_add_new_nvts() 24 24 1
A GmpModifyPolicySetFamilySelectionTestCase.test_modify_config_set_family_selection() 46 46 1
A GmpModifyPolicySetFamilySelectionTestCase.test_modify_config_set_family_selection_invalid_families() 13 13 4
A GmpModifyPolicySetFamilySelectionTestCase.test_modify_config_set_family_selection_missing_config_id() 13 13 4
A GmpModifyPolicySetFamilySelectionTestCase.test_modify_config_set_family_selection_with_auto_add_new_families() 26 26 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
import unittest
20
21
from gvm.errors import RequiredArgument, InvalidArgumentType
22
23
24 View Code Duplication
class GmpModifyPolicySetFamilySelectionTestCase:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
25
    def test_modify_config_set_family_selection(self):
26
        self.gmp.modify_policy_set_family_selection(
27
            policy_id='c1', families=['foo']
28
        )
29
30
        self.connection.send.has_been_called_with(
31
            '<modify_config config_id="c1">'
32
            '<family_selection>'
33
            '<growing>1</growing>'
34
            '<family>'
35
            '<name>foo</name>'
36
            '<all>1</all>'
37
            '<growing>1</growing>'
38
            '</family>'
39
            '</family_selection>'
40
            '</modify_config>'
41
        )
42
43
        self.gmp.modify_policy_set_family_selection(
44
            policy_id='c1', families=['foo', 'bar']
45
        )
46
47
        self.connection.send.has_been_called_with(
48
            '<modify_config config_id="c1">'
49
            '<family_selection>'
50
            '<growing>1</growing>'
51
            '<family>'
52
            '<name>foo</name>'
53
            '<all>1</all>'
54
            '<growing>1</growing>'
55
            '</family>'
56
            '<family>'
57
            '<name>bar</name>'
58
            '<all>1</all>'
59
            '<growing>1</growing>'
60
            '</family>'
61
            '</family_selection>'
62
            '</modify_config>'
63
        )
64
65
        self.gmp.modify_policy_set_family_selection(
66
            policy_id='c1', families=('foo', 'bar')
67
        )
68
69
        self.connection.send.has_been_called_with(
70
            '<modify_config config_id="c1">'
71
            '<family_selection>'
72
            '<growing>1</growing>'
73
            '<family>'
74
            '<name>foo</name>'
75
            '<all>1</all>'
76
            '<growing>1</growing>'
77
            '</family>'
78
            '<family>'
79
            '<name>bar</name>'
80
            '<all>1</all>'
81
            '<growing>1</growing>'
82
            '</family>'
83
            '</family_selection>'
84
            '</modify_config>'
85
        )
86
87
    def test_modify_config_set_family_selection_missing_config_id(self):
88
        with self.assertRaises(RequiredArgument):
89
            self.gmp.modify_policy_set_family_selection(
90
                policy_id=None, families=['foo']
91
            )
92
93
        with self.assertRaises(RequiredArgument):
94
            self.gmp.modify_policy_set_family_selection(
95
                policy_id='', families=['foo']
96
            )
97
98
        with self.assertRaises(RequiredArgument):
99
            self.gmp.modify_policy_set_family_selection('', ['foo'])
100
101
    def test_modify_config_set_family_selection_invalid_families(self):
102
        with self.assertRaises(InvalidArgumentType):
103
            self.gmp.modify_policy_set_family_selection(
104
                policy_id='c1', families=None
105
            )
106
107
        with self.assertRaises(InvalidArgumentType):
108
            self.gmp.modify_policy_set_family_selection(
109
                policy_id='c1', families=''
110
            )
111
112
        with self.assertRaises(InvalidArgumentType):
113
            self.gmp.modify_policy_set_family_selection('c1', '')
114
115
    def test_modify_config_set_family_selection_with_auto_add_new_families(
116
        self,
117
    ):
118
        self.gmp.modify_policy_set_family_selection(
119
            policy_id='c1', families=['foo'], auto_add_new_families=True
120
        )
121
122
        self.connection.send.has_been_called_with(
123
            '<modify_config config_id="c1">'
124
            '<family_selection>'
125
            '<growing>1</growing>'
126
            '<family>'
127
            '<name>foo</name>'
128
            '<all>1</all>'
129
            '<growing>1</growing>'
130
            '</family>'
131
            '</family_selection>'
132
            '</modify_config>'
133
        )
134
135
        self.gmp.modify_policy_set_family_selection(
136
            policy_id='c1', families=['foo'], auto_add_new_families=False
137
        )
138
139
        self.connection.send.has_been_called_with(
140
            '<modify_config config_id="c1">'
141
            '<family_selection>'
142
            '<growing>0</growing>'
143
            '<family>'
144
            '<name>foo</name>'
145
            '<all>1</all>'
146
            '<growing>1</growing>'
147
            '</family>'
148
            '</family_selection>'
149
            '</modify_config>'
150
        )
151
152
    def test_modify_config_set_family_selection_with_auto_add_new_nvts(self):
153
        self.gmp.modify_policy_set_family_selection(
154
            policy_id='c1', families=['foo'], auto_add_new_nvts=True
155
        )
156
157
        self.connection.send.has_been_called_with(
158
            '<modify_config config_id="c1">'
159
            '<family_selection>'
160
            '<growing>1</growing>'
161
            '<family>'
162
            '<name>foo</name>'
163
            '<all>1</all>'
164
            '<growing>1</growing>'
165
            '</family>'
166
            '</family_selection>'
167
            '</modify_config>'
168
        )
169
170
        self.gmp.modify_policy_set_family_selection(
171
            policy_id='c1', families=['foo'], auto_add_new_nvts=False
172
        )
173
174
        self.connection.send.has_been_called_with(
175
            '<modify_config config_id="c1">'
176
            '<family_selection>'
177
            '<growing>1</growing>'
178
            '<family>'
179
            '<name>foo</name>'
180
            '<all>1</all>'
181
            '<growing>0</growing>'
182
            '</family>'
183
            '</family_selection>'
184
            '</modify_config>'
185
        )
186
187
188
if __name__ == '__main__':
189
    unittest.main()
190