1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2019-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
|
|
|
import unittest |
20
|
|
|
|
21
|
|
|
from gvm.errors import ( |
22
|
|
|
InvalidArgument, |
23
|
|
|
InvalidArgumentType, |
24
|
|
|
RequiredArgument, |
25
|
|
|
GvmError, |
26
|
|
|
GvmServerError, |
27
|
|
|
GvmResponseError, |
28
|
|
|
) |
29
|
|
|
|
30
|
|
|
|
31
|
|
View Code Duplication |
class InvalidArgumentTestCase(unittest.TestCase): |
|
|
|
|
32
|
|
|
def test_raise_with_message(self): |
33
|
|
|
with self.assertRaisesRegex(InvalidArgument, '^foo bar$'): |
34
|
|
|
raise InvalidArgument('foo bar') |
35
|
|
|
|
36
|
|
|
def test_message_precedence(self): |
37
|
|
|
with self.assertRaisesRegex(InvalidArgument, '^foo bar$') as cm: |
38
|
|
|
raise InvalidArgument('foo bar', argument='foo', function='bar') |
39
|
|
|
|
40
|
|
|
ex = cm.exception |
41
|
|
|
self.assertEqual(ex.argument, 'foo') |
42
|
|
|
self.assertEqual(ex.function, 'bar') |
43
|
|
|
|
44
|
|
|
self.assertEqual(str(ex), 'foo bar') |
45
|
|
|
|
46
|
|
|
def test_raise_with_argument(self): |
47
|
|
|
with self.assertRaises(InvalidArgument) as cm: |
48
|
|
|
raise InvalidArgument(argument='foo') |
49
|
|
|
|
50
|
|
|
ex = cm.exception |
51
|
|
|
self.assertEqual(ex.argument, 'foo') |
52
|
|
|
self.assertIsNone(ex.function) |
53
|
|
|
|
54
|
|
|
def test_raise_with_function(self): |
55
|
|
|
with self.assertRaises(InvalidArgument) as cm: |
56
|
|
|
raise InvalidArgument(function='foo') |
57
|
|
|
|
58
|
|
|
ex = cm.exception |
59
|
|
|
self.assertEqual(ex.function, 'foo') |
60
|
|
|
self.assertIsNone(ex.argument) |
61
|
|
|
|
62
|
|
|
def test_raise_with_argument_and_function(self): |
63
|
|
|
with self.assertRaises(InvalidArgument) as cm: |
64
|
|
|
raise InvalidArgument(argument='foo', function='bar') |
65
|
|
|
|
66
|
|
|
ex = cm.exception |
67
|
|
|
self.assertEqual(ex.argument, 'foo') |
68
|
|
|
self.assertEqual(ex.function, 'bar') |
69
|
|
|
|
70
|
|
|
def test_string_conversion(self): |
71
|
|
|
with self.assertRaises(InvalidArgument) as cm: |
72
|
|
|
raise InvalidArgument('foo bar', argument='foo') |
73
|
|
|
|
74
|
|
|
ex = cm.exception |
75
|
|
|
self.assertEqual(str(ex), 'foo bar') |
76
|
|
|
|
77
|
|
|
with self.assertRaises(InvalidArgument) as cm: |
78
|
|
|
raise InvalidArgument(argument='foo') |
79
|
|
|
|
80
|
|
|
ex = cm.exception |
81
|
|
|
self.assertEqual(str(ex), 'Invalid argument foo') |
82
|
|
|
|
83
|
|
|
with self.assertRaises(InvalidArgument) as cm: |
84
|
|
|
raise InvalidArgument(function='foo') |
85
|
|
|
|
86
|
|
|
ex = cm.exception |
87
|
|
|
self.assertEqual(str(ex), 'Invalid argument for foo') |
88
|
|
|
|
89
|
|
|
with self.assertRaises(InvalidArgument) as cm: |
90
|
|
|
raise InvalidArgument(argument='foo', function='bar') |
91
|
|
|
|
92
|
|
|
ex = cm.exception |
93
|
|
|
self.assertEqual(str(ex), 'Invalid argument foo for bar') |
94
|
|
|
|
95
|
|
|
def test_is_gvm_error(self): |
96
|
|
|
with self.assertRaises(GvmError): |
97
|
|
|
raise InvalidArgument('foo bar') |
98
|
|
|
|
99
|
|
|
|
100
|
|
View Code Duplication |
class RequiredArgumentTestCase(unittest.TestCase): |
|
|
|
|
101
|
|
|
def test_raise_with_message(self): |
102
|
|
|
with self.assertRaisesRegex(RequiredArgument, '^foo bar$'): |
103
|
|
|
raise RequiredArgument('foo bar') |
104
|
|
|
|
105
|
|
|
def test_message_precedence(self): |
106
|
|
|
with self.assertRaisesRegex(RequiredArgument, '^foo bar$') as cm: |
107
|
|
|
raise RequiredArgument('foo bar', argument='foo', function='bar') |
108
|
|
|
|
109
|
|
|
ex = cm.exception |
110
|
|
|
self.assertEqual(ex.argument, 'foo') |
111
|
|
|
self.assertEqual(ex.function, 'bar') |
112
|
|
|
|
113
|
|
|
self.assertEqual(str(ex), 'foo bar') |
114
|
|
|
|
115
|
|
|
def test_raise_with_argument(self): |
116
|
|
|
with self.assertRaises(RequiredArgument) as cm: |
117
|
|
|
raise RequiredArgument(argument='foo') |
118
|
|
|
|
119
|
|
|
ex = cm.exception |
120
|
|
|
self.assertEqual(ex.argument, 'foo') |
121
|
|
|
self.assertIsNone(ex.function) |
122
|
|
|
|
123
|
|
|
def test_raise_with_function(self): |
124
|
|
|
with self.assertRaises(RequiredArgument) as cm: |
125
|
|
|
raise RequiredArgument(function='foo') |
126
|
|
|
|
127
|
|
|
ex = cm.exception |
128
|
|
|
self.assertEqual(ex.function, 'foo') |
129
|
|
|
self.assertIsNone(ex.argument) |
130
|
|
|
|
131
|
|
|
def test_raise_with_argument_and_function(self): |
132
|
|
|
with self.assertRaises(RequiredArgument) as cm: |
133
|
|
|
raise RequiredArgument(argument='foo', function='bar') |
134
|
|
|
|
135
|
|
|
ex = cm.exception |
136
|
|
|
self.assertEqual(ex.argument, 'foo') |
137
|
|
|
self.assertEqual(ex.function, 'bar') |
138
|
|
|
|
139
|
|
|
def test_string_conversion(self): |
140
|
|
|
with self.assertRaises(RequiredArgument) as cm: |
141
|
|
|
raise RequiredArgument('foo bar') |
142
|
|
|
|
143
|
|
|
ex = cm.exception |
144
|
|
|
self.assertEqual(str(ex), 'foo bar') |
145
|
|
|
|
146
|
|
|
with self.assertRaises(RequiredArgument) as cm: |
147
|
|
|
raise RequiredArgument(argument='foo') |
148
|
|
|
|
149
|
|
|
ex = cm.exception |
150
|
|
|
self.assertEqual(str(ex), 'Required argument foo') |
151
|
|
|
|
152
|
|
|
with self.assertRaises(RequiredArgument) as cm: |
153
|
|
|
raise RequiredArgument(function='foo') |
154
|
|
|
|
155
|
|
|
ex = cm.exception |
156
|
|
|
self.assertEqual(str(ex), 'Required argument missing for foo') |
157
|
|
|
|
158
|
|
|
with self.assertRaises(RequiredArgument) as cm: |
159
|
|
|
raise RequiredArgument(argument='foo', function='bar') |
160
|
|
|
|
161
|
|
|
ex = cm.exception |
162
|
|
|
self.assertEqual(str(ex), 'bar requires a foo argument') |
163
|
|
|
|
164
|
|
|
def test_is_gvm_error(self): |
165
|
|
|
with self.assertRaises(GvmError): |
166
|
|
|
raise RequiredArgument('foo bar') |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
class GvmServerErrorTestCase(unittest.TestCase): |
170
|
|
|
def test_raise_with_message_and_status(self): |
171
|
|
|
with self.assertRaisesRegex(GvmServerError, '^Server Error foo. bar$'): |
172
|
|
|
raise GvmServerError('foo', 'bar') |
173
|
|
|
|
174
|
|
|
def test_is_gvm_error(self): |
175
|
|
|
with self.assertRaises(GvmError): |
176
|
|
|
raise GvmServerError('foo', 'bar') |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
class GvmResponseErrorTestCase(unittest.TestCase): |
180
|
|
|
def test_raise_with_message_and_status(self): |
181
|
|
|
with self.assertRaisesRegex( |
182
|
|
|
GvmResponseError, '^Response Error foo. bar$' |
183
|
|
|
): |
184
|
|
|
raise GvmResponseError('foo', 'bar') |
185
|
|
|
|
186
|
|
|
def test_is_gvm_error(self): |
187
|
|
|
with self.assertRaises(GvmError): |
188
|
|
|
raise GvmResponseError('foo', 'bar') |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
class InvalidArgumentTypeTestCase(unittest.TestCase): |
192
|
|
|
def test_raise_with_argument_and_arg_type(self): |
193
|
|
|
with self.assertRaisesRegex( |
194
|
|
|
InvalidArgumentType, '^The argument foo must be of type bar.$' |
195
|
|
|
): |
196
|
|
|
raise InvalidArgumentType('foo', 'bar') |
197
|
|
|
|
198
|
|
|
def test_raise_with_function(self): |
199
|
|
|
with self.assertRaisesRegex( |
200
|
|
|
InvalidArgumentType, |
201
|
|
|
'^In baz the argument foo must be of type bar.$', |
202
|
|
|
): |
203
|
|
|
raise InvalidArgumentType('foo', 'bar', function='baz') |
204
|
|
|
|
205
|
|
|
def test_string_conversion(self): |
206
|
|
|
with self.assertRaises(InvalidArgumentType) as cm: |
207
|
|
|
raise InvalidArgumentType('foo', 'bar') |
208
|
|
|
|
209
|
|
|
ex = cm.exception |
210
|
|
|
self.assertEqual(str(ex), 'The argument foo must be of type bar.') |
211
|
|
|
self.assertIsNone(ex.function) |
212
|
|
|
|
213
|
|
|
with self.assertRaises(InvalidArgumentType) as cm: |
214
|
|
|
raise InvalidArgumentType('foo', 'bar', function='baz') |
215
|
|
|
|
216
|
|
|
ex = cm.exception |
217
|
|
|
self.assertEqual( |
218
|
|
|
str(ex), 'In baz the argument foo must be of type bar.' |
219
|
|
|
) |
220
|
|
|
|
221
|
|
|
def test_is_gvm_error(self): |
222
|
|
|
with self.assertRaises(GvmError): |
223
|
|
|
raise InvalidArgumentType('foo', 'bar') |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
if __name__ == '__main__': |
227
|
|
|
unittest.main() |
228
|
|
|
|