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