Completed
Push — master ( f3cde9...b114e7 )
by
unknown
13s queued 11s
created

GmpModifyUserTestCase.test_modify_user_with_hosts()   A

Complexity

Conditions 1

Size

Total Lines 52
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 28
nop 1
dl 0
loc 52
rs 9.208
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
22
from gvm.protocols.gmpv7 import Gmp
23
24
from .. import MockConnection
25
26
27
class GmpModifyUserTestCase(unittest.TestCase):
28
29
    def setUp(self):
30
        self.connection = MockConnection()
31
        self.gmp = Gmp(self.connection)
32
33
    def test_modify_user(self):
34
        self.gmp.modify_user(
35
            user_id='u1',
36
        )
37
38
        self.connection.send.has_been_called_with(
39
            '<modify_user user_id="u1"/>'
40
        )
41
42
        self.gmp.modify_user(
43
            name='u1',
44
        )
45
46
        self.connection.send.has_been_called_with(
47
            '<modify_user>'
48
            '<name>u1</name>'
49
            '</modify_user>'
50
        )
51
52
    def test_modify_user_missing_user_id(self):
53
        with self.assertRaises(RequiredArgument):
54
            self.gmp.modify_user(
55
                user_id=None,
56
            )
57
58
        with self.assertRaises(RequiredArgument):
59
            self.gmp.modify_user(
60
                user_id='',
61
            )
62
63
    def test_modify_user_missing_name(self):
64
        with self.assertRaises(RequiredArgument):
65
            self.gmp.modify_user(
66
                name=None,
67
            )
68
69
        with self.assertRaises(RequiredArgument):
70
            self.gmp.modify_user(
71
                name='',
72
            )
73
74
    def test_modify_user_with_new_name(self):
75
        self.gmp.modify_user(
76
            user_id='u1',
77
            new_name='foo',
78
        )
79
80
        self.connection.send.has_been_called_with(
81
            '<modify_user user_id="u1">'
82
            '<new_name>foo</new_name>'
83
            '</modify_user>'
84
        )
85
86
    def test_modify_user_with_user_id_and_name(self):
87
        self.gmp.modify_user(
88
            user_id='u1',
89
            name='foo',
90
        )
91
92
        self.connection.send.has_been_called_with(
93
            '<modify_user user_id="u1"/>'
94
        )
95
96
    def test_modify_user_with_role_ids(self):
97
        self.gmp.modify_user(
98
            user_id='u1',
99
            role_ids=[],
100
        )
101
102
        self.connection.send.has_been_called_with(
103
            '<modify_user user_id="u1"/>'
104
        )
105
106
        self.gmp.modify_user(
107
            user_id='u1',
108
            role_ids=['r1'],
109
        )
110
111
        self.connection.send.has_been_called_with(
112
            '<modify_user user_id="u1">'
113
            '<role id="r1"/>'
114
            '</modify_user>'
115
        )
116
117
        self.gmp.modify_user(
118
            user_id='u1',
119
            role_ids=['r1', 'r2'],
120
        )
121
122
        self.connection.send.has_been_called_with(
123
            '<modify_user user_id="u1">'
124
            '<role id="r1"/>'
125
            '<role id="r2"/>'
126
            '</modify_user>'
127
        )
128
129
    def test_modify_user_with_password(self):
130
        self.gmp.modify_user(
131
            user_id='u1',
132
            password='foo',
133
        )
134
135
        self.connection.send.has_been_called_with(
136
            '<modify_user user_id="u1">'
137
            '<password>foo</password>'
138
            '</modify_user>'
139
        )
140
141
    def test_modify_user_with_hosts(self):
142
        self.gmp.modify_user(
143
            user_id='u1',
144
            hosts=[],
145
        )
146
147
        self.connection.send.has_been_called_with(
148
            '<modify_user user_id="u1"/>'
149
        )
150
151
        self.gmp.modify_user(
152
            user_id='u1',
153
            hosts=['foo'],
154
        )
155
156
        self.connection.send.has_been_called_with(
157
            '<modify_user user_id="u1">'
158
            '<hosts allow="0">foo</hosts>'
159
            '</modify_user>'
160
        )
161
162
        self.gmp.modify_user(
163
            user_id='u1',
164
            hosts=['foo', 'bar'],
165
        )
166
167
        self.connection.send.has_been_called_with(
168
            '<modify_user user_id="u1">'
169
            '<hosts allow="0">foo,bar</hosts>'
170
            '</modify_user>'
171
        )
172
173
        self.gmp.modify_user(
174
            user_id='u1',
175
            hosts=['foo', 'bar'],
176
            hosts_allow=False,
177
        )
178
179
        self.connection.send.has_been_called_with(
180
            '<modify_user user_id="u1">'
181
            '<hosts allow="0">foo,bar</hosts>'
182
            '</modify_user>'
183
        )
184
185
        self.gmp.modify_user(
186
            user_id='u1',
187
            hosts=['foo', 'bar'],
188
            hosts_allow=True,
189
        )
190
191
        self.connection.send.has_been_called_with(
192
            '<modify_user user_id="u1">'
193
            '<hosts allow="1">foo,bar</hosts>'
194
            '</modify_user>'
195
        )
196
197
    def test_modify_user_with_ifaces(self):
198
        self.gmp.modify_user(
199
            user_id='u1',
200
            ifaces=[],
201
        )
202
203
        self.connection.send.has_been_called_with(
204
            '<modify_user user_id="u1"/>'
205
        )
206
207
        self.gmp.modify_user(
208
            user_id='u1',
209
            ifaces=['foo'],
210
        )
211
212
        self.connection.send.has_been_called_with(
213
            '<modify_user user_id="u1">'
214
            '<ifaces allow="0">foo</ifaces>'
215
            '</modify_user>'
216
        )
217
218
        self.gmp.modify_user(
219
            user_id='u1',
220
            ifaces=['foo', 'bar'],
221
        )
222
223
        self.connection.send.has_been_called_with(
224
            '<modify_user user_id="u1">'
225
            '<ifaces allow="0">foo,bar</ifaces>'
226
            '</modify_user>'
227
        )
228
229
        self.gmp.modify_user(
230
            user_id='u1',
231
            ifaces=['foo', 'bar'],
232
            ifaces_allow=False,
233
        )
234
235
        self.connection.send.has_been_called_with(
236
            '<modify_user user_id="u1">'
237
            '<ifaces allow="0">foo,bar</ifaces>'
238
            '</modify_user>'
239
        )
240
241
        self.gmp.modify_user(
242
            user_id='u1',
243
            ifaces=['foo', 'bar'],
244
            ifaces_allow=True,
245
        )
246
247
        self.connection.send.has_been_called_with(
248
            '<modify_user user_id="u1">'
249
            '<ifaces allow="1">foo,bar</ifaces>'
250
            '</modify_user>'
251
        )
252
253
254
if __name__ == '__main__':
255
    unittest.main()
256