Passed
Pull Request — master (#87)
by
unknown
03:06
created

tests.protocols.gmpv7.test_modify_note   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 278
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 140
dl 0
loc 278
rs 10
c 0
b 0
f 0
wmc 20

12 Methods

Rating   Name   Duplication   Size   Complexity  
A GmpModifyNoteTestCase.test_modify_note_missing_note_id() 0 17 4
A GmpModifyNoteTestCase.test_modify_note_with_seconds_active() 0 35 1
A GmpModifyNoteTestCase.setUp() 0 3 1
A GmpModifyNoteTestCase.test_modify_note_invalid_threat() 0 13 3
A GmpModifyNoteTestCase.test_modify_note_missing_text() 0 17 4
A GmpModifyNoteTestCase.test_modify_note_with_hosts() 0 22 1
A GmpModifyNoteTestCase.test_modify_note_with_port() 0 22 1
A GmpModifyNoteTestCase.test_modify_note_with_severity() 0 35 1
A GmpModifyNoteTestCase.test_modify_note_with_task_id() 0 9 1
A GmpModifyNoteTestCase.test_modify_note_with_threat() 0 9 1
A GmpModifyNoteTestCase.test_modify_note() 0 8 1
A GmpModifyNoteTestCase.test_modify_note_with_result_id() 0 9 1
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 decimal import Decimal
22
23
from gvm.errors import RequiredArgument, InvalidArgument
24
from gvm.protocols.gmpv7 import Gmp
25
26
from .. import MockConnection
27
28
29
class GmpModifyNoteTestCase(unittest.TestCase):
30
31
    def setUp(self):
32
        self.connection = MockConnection()
33
        self.gmp = Gmp(self.connection)
34
35
    def test_modify_note(self):
36
        self.gmp.modify_note(
37
            note_id='n1',
38
            text='foo',
39
        )
40
41
        self.connection.send.has_been_called_with(
42
            '<modify_note note_id="n1">'
43
            '<text>foo</text>'
44
            '</modify_note>'
45
        )
46
47
    def test_modify_note_missing_note_id(self):
48
        with self.assertRaises(RequiredArgument):
49
            self.gmp.modify_note(
50
                note_id=None,
51
                text='foo'
52
            )
53
54
        with self.assertRaises(RequiredArgument):
55
            self.gmp.modify_note(
56
                note_id='',
57
                text='foo'
58
            )
59
60
        with self.assertRaises(RequiredArgument):
61
            self.gmp.modify_note(
62
                '',
63
                text='foo'
64
            )
65
66
    def test_modify_note_missing_text(self):
67
        with self.assertRaises(RequiredArgument):
68
            self.gmp.modify_note(
69
                note_id='n1',
70
                text=''
71
            )
72
73
        with self.assertRaises(RequiredArgument):
74
            self.gmp.modify_note(
75
                note_id='n1',
76
                text=None,
77
            )
78
79
        with self.assertRaises(RequiredArgument):
80
            self.gmp.modify_note(
81
                'n1',
82
                ''
83
            )
84
85
    def test_modify_note_with_seconds_active(self):
86
        self.gmp.modify_note(
87
            note_id='n1',
88
            text='foo',
89
            seconds_active=0,
90
        )
91
92
        self.connection.send.has_been_called_with(
93
            '<modify_note note_id="n1">'
94
            '<text>foo</text>'
95
            '<active>0</active>'
96
            '</modify_note>'
97
        )
98
99
        self.gmp.modify_note(
100
            note_id='n1',
101
            text='foo',
102
            seconds_active=-1,
103
        )
104
105
        self.connection.send.has_been_called_with(
106
            '<modify_note note_id="n1">'
107
            '<text>foo</text>'
108
            '<active>-1</active>'
109
            '</modify_note>'
110
        )
111
112
        self.gmp.modify_note(
113
            note_id='n1',
114
            text='foo',
115
            seconds_active=600,
116
        )
117
118
        self.connection.send.has_been_called_with(
119
            '<modify_note note_id="n1">'
120
            '<text>foo</text>'
121
            '<active>600</active>'
122
            '</modify_note>'
123
        )
124
    def test_modify_note_with_port(self):
125
        self.gmp.modify_note(
126
            note_id='n1',
127
            text='foo',
128
            port='123',
129
        )
130
131
        self.connection.send.has_been_called_with(
132
            '<modify_note note_id="n1">'
133
            '<text>foo</text>'
134
            '<port>123</port>'
135
            '</modify_note>'
136
        )
137
138
        self.gmp.modify_note(
139
            note_id='n1',
140
            text='foo',
141
            port=123,
142
        )
143
144
        self.connection.send.has_been_called_with(
145
            '<modify_note note_id="n1">'
146
            '<text>foo</text>'
147
            '<port>123</port>'
148
            '</modify_note>'
149
        )
150
151
    def test_modify_note_with_hosts(self):
152
        self.gmp.modify_note(
153
            note_id='n1',
154
            text='foo',
155
            hosts=['foo'],
156
        )
157
158
        self.connection.send.has_been_called_with(
159
            '<modify_note note_id="n1">'
160
            '<text>foo</text>'
161
            '<hosts>foo</hosts>'
162
            '</modify_note>'
163
        )
164
165
        self.gmp.modify_note(
166
            note_id='n1',
167
            text='foo',
168
            hosts=['foo', 'bar'],
169
        )
170
171
        self.connection.send.has_been_called_with(
172
            '<modify_note note_id="n1">'
173
            '<text>foo</text>'
174
            '<hosts>foo,bar</hosts>'
175
            '</modify_note>'
176
        )
177
178
    def test_modify_note_with_result_id(self):
179
        self.gmp.modify_note(
180
            note_id='n1',
181
            text='foo',
182
            result_id='r1',
183
        )
184
185
        self.connection.send.has_been_called_with(
186
            '<modify_note note_id="n1">'
187
            '<text>foo</text>'
188
            '<result id="r1"/>'
189
            '</modify_note>'
190
        )
191
192
    def test_modify_note_with_task_id(self):
193
        self.gmp.modify_note(
194
            note_id='n1',
195
            text='foo',
196
            task_id='r1',
197
        )
198
199
        self.connection.send.has_been_called_with(
200
            '<modify_note note_id="n1">'
201
            '<text>foo</text>'
202
            '<task id="r1"/>'
203
            '</modify_note>'
204
        )
205
206
    def test_modify_note_with_severity(self):
207
        self.gmp.modify_note(
208
            note_id='n1',
209
            text='foo',
210
            severity='5.5',
211
        )
212
213
        self.connection.send.has_been_called_with(
214
            '<modify_note note_id="n1">'
215
            '<text>foo</text>'
216
            '<severity>5.5</severity>'
217
            '</modify_note>'
218
        )
219
220
        self.gmp.modify_note(
221
            note_id='n1',
222
            text='foo',
223
            severity=5.5,
224
        )
225
226
        self.connection.send.has_been_called_with(
227
            '<modify_note note_id="n1">'
228
            '<text>foo</text>'
229
            '<severity>5.5</severity>'
230
            '</modify_note>'
231
        )
232
233
        self.gmp.modify_note(
234
            note_id='n1',
235
            text='foo',
236
            severity=Decimal(5.5),
237
        )
238
239
        self.connection.send.has_been_called_with(
240
            '<modify_note note_id="n1">'
241
            '<text>foo</text>'
242
            '<severity>5.5</severity>'
243
            '</modify_note>'
244
        )
245
246
    def test_modify_note_with_threat(self):
247
        self.gmp.modify_note(
248
            note_id='n1',
249
            text='foo',
250
            threat='High',
251
        )
252
253
        self.connection.send.has_been_called_with(
254
            '<modify_note note_id="n1">'
255
            '<text>foo</text>'
256
            '<threat>High</threat>'
257
            '</modify_note>'
258
        )
259
260
    def test_modify_note_invalid_threat(self):
261
        with self.assertRaises(InvalidArgument):
262
            self.gmp.modify_note(
263
                note_id='n1',
264
                text='foo',
265
                threat='',
266
            )
267
268
        with self.assertRaises(InvalidArgument):
269
            self.gmp.modify_note(
270
                note_id='n1',
271
                text='foo',
272
                threat='foo',
273
            )
274
275
276
if __name__ == '__main__':
277
    unittest.main()
278