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, InvalidArgumentType |
20
|
|
|
|
21
|
|
|
from gvm.protocols.gmpv208 import PermissionSubjectType, EntityType |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class GmpModifyPermissionTestMixin: |
25
|
|
|
def test_modify_permission(self): |
26
|
|
|
self.gmp.modify_permission(permission_id='p1') |
27
|
|
|
|
28
|
|
|
self.connection.send.has_been_called_with( |
29
|
|
|
'<modify_permission permission_id="p1"/>' |
30
|
|
|
) |
31
|
|
|
|
32
|
|
|
def test_modify_permission_missing_permission_id(self): |
33
|
|
|
with self.assertRaises(RequiredArgument): |
34
|
|
|
self.gmp.modify_permission(permission_id=None) |
35
|
|
|
|
36
|
|
|
with self.assertRaises(RequiredArgument): |
37
|
|
|
self.gmp.modify_permission(permission_id='') |
38
|
|
|
|
39
|
|
|
with self.assertRaises(RequiredArgument): |
40
|
|
|
self.gmp.modify_permission('') |
41
|
|
|
|
42
|
|
|
def test_modify_permission_with_comment(self): |
43
|
|
|
self.gmp.modify_permission(permission_id='p1', comment='foo') |
44
|
|
|
|
45
|
|
|
self.connection.send.has_been_called_with( |
46
|
|
|
'<modify_permission permission_id="p1">' |
47
|
|
|
'<comment>foo</comment>' |
48
|
|
|
'</modify_permission>' |
49
|
|
|
) |
50
|
|
|
|
51
|
|
|
def test_modify_permission_with_resource_id_and_type(self): |
52
|
|
|
self.gmp.modify_permission( |
53
|
|
|
permission_id='p1', resource_id='r1', resource_type=EntityType.TASK |
54
|
|
|
) |
55
|
|
|
|
56
|
|
|
self.connection.send.has_been_called_with( |
57
|
|
|
'<modify_permission permission_id="p1">' |
58
|
|
|
'<resource id="r1">' |
59
|
|
|
'<type>task</type>' |
60
|
|
|
'</resource>' |
61
|
|
|
'</modify_permission>' |
62
|
|
|
) |
63
|
|
|
|
64
|
|
|
def test_modify_permission_with_resource_id_and_type_audit(self): |
65
|
|
|
self.gmp.modify_permission( |
66
|
|
|
permission_id='p1', resource_id='r1', resource_type=EntityType.AUDIT |
67
|
|
|
) |
68
|
|
|
|
69
|
|
|
self.connection.send.has_been_called_with( |
70
|
|
|
'<modify_permission permission_id="p1">' |
71
|
|
|
'<resource id="r1">' |
72
|
|
|
'<type>task</type>' |
73
|
|
|
'</resource>' |
74
|
|
|
'</modify_permission>' |
75
|
|
|
) |
76
|
|
|
|
77
|
|
|
def test_modify_permission_with_resource_id_and_type_policy(self): |
78
|
|
|
self.gmp.modify_permission( |
79
|
|
|
permission_id='p1', |
80
|
|
|
resource_id='r1', |
81
|
|
|
resource_type=EntityType.POLICY, |
82
|
|
|
) |
83
|
|
|
|
84
|
|
|
self.connection.send.has_been_called_with( |
85
|
|
|
'<modify_permission permission_id="p1">' |
86
|
|
|
'<resource id="r1">' |
87
|
|
|
'<type>config</type>' |
88
|
|
|
'</resource>' |
89
|
|
|
'</modify_permission>' |
90
|
|
|
) |
91
|
|
|
|
92
|
|
|
def test_modify_permission_with_missing_resource_id(self): |
93
|
|
|
with self.assertRaises(RequiredArgument): |
94
|
|
|
self.gmp.modify_permission( |
95
|
|
|
permission_id='p1', |
96
|
|
|
resource_id='', |
97
|
|
|
resource_type=EntityType.TASK, |
98
|
|
|
) |
99
|
|
|
|
100
|
|
|
with self.assertRaises(RequiredArgument): |
101
|
|
|
self.gmp.modify_permission( |
102
|
|
|
permission_id='p1', resource_type=EntityType.TASK |
103
|
|
|
) |
104
|
|
|
|
105
|
|
|
with self.assertRaises(RequiredArgument): |
106
|
|
|
self.gmp.modify_permission( |
107
|
|
|
permission_id='p1', |
108
|
|
|
resource_id=None, |
109
|
|
|
resource_type=EntityType.TASK, |
110
|
|
|
) |
111
|
|
|
|
112
|
|
|
def test_modify_permission_with_missing_resource_type(self): |
113
|
|
|
with self.assertRaises(RequiredArgument): |
114
|
|
|
self.gmp.modify_permission( |
115
|
|
|
permission_id='p1', resource_id='r1', resource_type='' |
116
|
|
|
) |
117
|
|
|
|
118
|
|
|
with self.assertRaises(RequiredArgument): |
119
|
|
|
self.gmp.modify_permission(permission_id='p1', resource_id='r1') |
120
|
|
|
|
121
|
|
|
with self.assertRaises(RequiredArgument): |
122
|
|
|
self.gmp.modify_permission( |
123
|
|
|
permission_id='p1', resource_id='r1', resource_type=None |
124
|
|
|
) |
125
|
|
|
|
126
|
|
|
def test_modify_permission_with_invalid_resource_type(self): |
127
|
|
|
with self.assertRaises(InvalidArgumentType): |
128
|
|
|
self.gmp.modify_permission( |
129
|
|
|
permission_id='p1', resource_id='r1', resource_type='blah' |
130
|
|
|
) |
131
|
|
|
|
132
|
|
|
def test_modify_permission_with_subject_id_and_type(self): |
133
|
|
|
self.gmp.modify_permission( |
134
|
|
|
permission_id='p1', |
135
|
|
|
subject_id='s1', |
136
|
|
|
subject_type=PermissionSubjectType.ROLE, |
137
|
|
|
) |
138
|
|
|
|
139
|
|
|
self.connection.send.has_been_called_with( |
140
|
|
|
'<modify_permission permission_id="p1">' |
141
|
|
|
'<subject id="s1">' |
142
|
|
|
'<type>role</type>' |
143
|
|
|
'</subject>' |
144
|
|
|
'</modify_permission>' |
145
|
|
|
) |
146
|
|
|
|
147
|
|
|
self.gmp.modify_permission( |
148
|
|
|
permission_id='p1', |
149
|
|
|
subject_id='s1', |
150
|
|
|
subject_type=PermissionSubjectType.USER, |
151
|
|
|
) |
152
|
|
|
|
153
|
|
|
self.connection.send.has_been_called_with( |
154
|
|
|
'<modify_permission permission_id="p1">' |
155
|
|
|
'<subject id="s1">' |
156
|
|
|
'<type>user</type>' |
157
|
|
|
'</subject>' |
158
|
|
|
'</modify_permission>' |
159
|
|
|
) |
160
|
|
|
|
161
|
|
|
self.gmp.modify_permission( |
162
|
|
|
permission_id='p1', |
163
|
|
|
subject_id='s1', |
164
|
|
|
subject_type=PermissionSubjectType.GROUP, |
165
|
|
|
) |
166
|
|
|
|
167
|
|
|
self.connection.send.has_been_called_with( |
168
|
|
|
'<modify_permission permission_id="p1">' |
169
|
|
|
'<subject id="s1">' |
170
|
|
|
'<type>group</type>' |
171
|
|
|
'</subject>' |
172
|
|
|
'</modify_permission>' |
173
|
|
|
) |
174
|
|
|
|
175
|
|
|
def test_modify_permission_missing_subject_id(self): |
176
|
|
|
with self.assertRaises(RequiredArgument): |
177
|
|
|
self.gmp.modify_permission( |
178
|
|
|
permission_id='p1', subject_type=PermissionSubjectType.ROLE |
179
|
|
|
) |
180
|
|
|
|
181
|
|
|
with self.assertRaises(RequiredArgument): |
182
|
|
|
self.gmp.modify_permission( |
183
|
|
|
permission_id='p1', |
184
|
|
|
subject_type=PermissionSubjectType.ROLE, |
185
|
|
|
subject_id='', |
186
|
|
|
) |
187
|
|
|
|
188
|
|
|
with self.assertRaises(RequiredArgument): |
189
|
|
|
self.gmp.modify_permission( |
190
|
|
|
permission_id='p1', |
191
|
|
|
subject_type=PermissionSubjectType.ROLE, |
192
|
|
|
subject_id=None, |
193
|
|
|
) |
194
|
|
|
|
195
|
|
|
def test_modify_permission_invalid_subject_type(self): |
196
|
|
|
with self.assertRaises(InvalidArgumentType): |
197
|
|
|
self.gmp.modify_permission( |
198
|
|
|
permission_id='p1', subject_id='s1', subject_type='foo' |
199
|
|
|
) |
200
|
|
|
|
201
|
|
|
with self.assertRaises(InvalidArgumentType): |
202
|
|
|
self.gmp.modify_permission( |
203
|
|
|
permission_id='p1', subject_id='s1', subject_type='' |
204
|
|
|
) |
205
|
|
|
|
206
|
|
|
with self.assertRaises(InvalidArgumentType): |
207
|
|
|
self.gmp.modify_permission( |
208
|
|
|
permission_id='p1', subject_id='s1', subject_type=None |
209
|
|
|
) |
210
|
|
|
|
211
|
|
|
def test_modify_permission_with_name(self): |
212
|
|
|
self.gmp.modify_permission(permission_id='p1', name='foo') |
213
|
|
|
|
214
|
|
|
self.connection.send.has_been_called_with( |
215
|
|
|
'<modify_permission permission_id="p1">' |
216
|
|
|
'<name>foo</name>' |
217
|
|
|
'</modify_permission>' |
218
|
|
|
) |
219
|
|
|
|