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