Completed
Push — master ( 4f5a72...b4bcf9 )
by Jaspar
18s queued 15s
created

tests.protocols.gmpv214.testcmds.test_modify_user   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 205
Duplicated Lines 36.1 %

Importance

Changes 0
Metric Value
eloc 84
dl 74
loc 205
rs 10
c 0
b 0
f 0
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A GmpModifyUserTestCase.test_modify_user_with_new_name() 0 5 1
A GmpModifyUserTestCase.test_modify_user_with_auth_source() 0 7 1
A GmpModifyUserTestCase.test_modify_user_with_ifaces() 37 37 1
A GmpModifyUserTestCase.test_modify_user_with_group_ids() 0 17 1
A GmpModifyUserTestCase.test_modify_user_missing_user_id() 0 6 3
A GmpModifyUserTestCase.test_modify_user_with_role_ids() 0 15 1
A GmpModifyUserTestCase.test_modify_user() 0 4 1
A GmpModifyUserTestCase.test_modify_user_with_hosts() 37 37 1
A GmpModifyUserTestCase.test_modify_user_with_new_comment() 0 5 1
A GmpModifyUserTestCase.test_modify_user_with_password() 0 5 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 - 2020 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
22
from gvm.protocols.gmpv7 import UserAuthType
23
24
25
class GmpModifyUserTestCase:
26
    def test_modify_user(self):
27
        self.gmp.modify_user(user_id='u1')
28
29
        self.connection.send.has_been_called_with('<modify_user user_id="u1"/>')
30
31
    def test_modify_user_missing_user_id(self):
32
        with self.assertRaises(RequiredArgument):
33
            self.gmp.modify_user(user_id=None)
34
35
        with self.assertRaises(RequiredArgument):
36
            self.gmp.modify_user(user_id='')
37
38
    def test_modify_user_with_new_name(self):
39
        self.gmp.modify_user(user_id='u1', name='foo')
40
41
        self.connection.send.has_been_called_with(
42
            '<modify_user user_id="u1">'
43
            '<new_name>foo</new_name>'
44
            '</modify_user>'
45
        )
46
47
    def test_modify_user_with_new_comment(self):
48
        self.gmp.modify_user(user_id='u1', comment='foo')
49
50
        self.connection.send.has_been_called_with(
51
            '<modify_user user_id="u1">'
52
            '<comment>foo</comment>'
53
            '</modify_user>'
54
        )
55
56
    def test_modify_user_with_role_ids(self):
57
        self.gmp.modify_user(user_id='u1', role_ids=[])
58
59
        self.connection.send.has_been_called_with('<modify_user user_id="u1"/>')
60
61
        self.gmp.modify_user(user_id='u1', role_ids=['r1'])
62
63
        self.connection.send.has_been_called_with(
64
            '<modify_user user_id="u1">' '<role id="r1"/>' '</modify_user>'
65
        )
66
67
        self.gmp.modify_user(user_id='u1', role_ids=['r1', 'r2'])
68
69
        self.connection.send.has_been_called_with(
70
            '<modify_user user_id="u1">'
71
            '<role id="r1"/>'
72
            '<role id="r2"/>'
73
            '</modify_user>'
74
        )
75
76
    def test_modify_user_with_group_ids(self):
77
        self.gmp.modify_user(user_id='u1', role_ids=[])
78
79
        self.connection.send.has_been_called_with('<modify_user user_id="u1"/>')
80
81
        self.gmp.modify_user(user_id='u1', group_ids=['r1'])
82
83
        self.connection.send.has_been_called_with(
84
            '<modify_user user_id="u1">'
85
            '<groups><group id="r1"/></groups>'
86
            '</modify_user>'
87
        )
88
89
        self.gmp.modify_user(user_id='u1', group_ids=['r1', 'r2'])
90
91
        self.connection.send.has_been_called_with(
92
            '<modify_user user_id="u1">'
93
            '<groups>'
94
            '<group id="r1"/>'
95
            '<group id="r2"/>'
96
            '</groups>'
97
            '</modify_user>'
98
        )
99
100
    def test_modify_user_with_password(self):
101
        self.gmp.modify_user(user_id='u1', password='foo')
102
103
        self.connection.send.has_been_called_with(
104
            '<modify_user user_id="u1">'
105
            '<password>foo</password>'
106
            '</modify_user>'
107
        )
108
109
    def test_modify_user_with_auth_source(self):
110
        self.gmp.modify_user(
111
            user_id='u1', auth_source=UserAuthType.LDAP_CONNECT
112
        )
113
114
        self.connection.send.has_been_called_with(
115
            '<modify_user user_id="u1">'
116
            '<sources><source>ldap_connect</source></sources>'
117
            '</modify_user>'
118
        )
119
120 View Code Duplication
    def test_modify_user_with_hosts(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
121
        self.gmp.modify_user(user_id='u1', hosts=[])
122
123
        self.connection.send.has_been_called_with('<modify_user user_id="u1"/>')
124
125
        self.gmp.modify_user(user_id='u1', hosts=['foo'])
126
127
        self.connection.send.has_been_called_with(
128
            '<modify_user user_id="u1">'
129
            '<hosts allow="0">foo</hosts>'
130
            '</modify_user>'
131
        )
132
133
        self.gmp.modify_user(user_id='u1', hosts=['foo', 'bar'])
134
135
        self.connection.send.has_been_called_with(
136
            '<modify_user user_id="u1">'
137
            '<hosts allow="0">foo,bar</hosts>'
138
            '</modify_user>'
139
        )
140
141
        self.gmp.modify_user(
142
            user_id='u1', hosts=['foo', 'bar'], hosts_allow=False
143
        )
144
145
        self.connection.send.has_been_called_with(
146
            '<modify_user user_id="u1">'
147
            '<hosts allow="0">foo,bar</hosts>'
148
            '</modify_user>'
149
        )
150
151
        self.gmp.modify_user(
152
            user_id='u1', hosts=['foo', 'bar'], hosts_allow=True
153
        )
154
155
        self.connection.send.has_been_called_with(
156
            '<modify_user user_id="u1">'
157
            '<hosts allow="1">foo,bar</hosts>'
158
            '</modify_user>'
159
        )
160
161 View Code Duplication
    def test_modify_user_with_ifaces(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
162
        self.gmp.modify_user(user_id='u1', ifaces=[])
163
164
        self.connection.send.has_been_called_with('<modify_user user_id="u1"/>')
165
166
        self.gmp.modify_user(user_id='u1', ifaces=['foo'])
167
168
        self.connection.send.has_been_called_with(
169
            '<modify_user user_id="u1">'
170
            '<ifaces allow="0">foo</ifaces>'
171
            '</modify_user>'
172
        )
173
174
        self.gmp.modify_user(user_id='u1', ifaces=['foo', 'bar'])
175
176
        self.connection.send.has_been_called_with(
177
            '<modify_user user_id="u1">'
178
            '<ifaces allow="0">foo,bar</ifaces>'
179
            '</modify_user>'
180
        )
181
182
        self.gmp.modify_user(
183
            user_id='u1', ifaces=['foo', 'bar'], ifaces_allow=False
184
        )
185
186
        self.connection.send.has_been_called_with(
187
            '<modify_user user_id="u1">'
188
            '<ifaces allow="0">foo,bar</ifaces>'
189
            '</modify_user>'
190
        )
191
192
        self.gmp.modify_user(
193
            user_id='u1', ifaces=['foo', 'bar'], ifaces_allow=True
194
        )
195
196
        self.connection.send.has_been_called_with(
197
            '<modify_user user_id="u1">'
198
            '<ifaces allow="1">foo,bar</ifaces>'
199
            '</modify_user>'
200
        )
201
202
203
if __name__ == '__main__':
204
    unittest.main()
205