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