Completed
Push — master ( f3cde9...b114e7 )
by
unknown
13s queued 11s
created

GmpCreateNoteTestCase.test_create_note_with_severity()   A

Complexity

Conditions 1

Size

Total Lines 25
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nop 1
dl 0
loc 25
rs 9.9
c 0
b 0
f 0
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 GmpCreateNoteTestCase(unittest.TestCase):
30
31
    def setUp(self):
32
        self.connection = MockConnection()
33
        self.gmp = Gmp(self.connection)
34
35
    def test_create_note(self):
36
        self.gmp.create_note('foo', nvt_oid='oid1')
37
38
        self.connection.send.has_been_called_with(
39
            '<create_note>'
40
            '<text>foo</text>'
41
            '<nvt oid="oid1"/>'
42
            '</create_note>'
43
        )
44
45
    def test_create_note_missing_text(self):
46
        with self.assertRaises(RequiredArgument):
47
            self.gmp.create_note(None, 'od1')
48
49
        with self.assertRaises(RequiredArgument):
50
            self.gmp.create_note('', 'oid1')
51
52
    def test_create_note_missing_nvt_iod(self):
53
        with self.assertRaises(RequiredArgument):
54
            self.gmp.create_note('foo', None)
55
56
        with self.assertRaises(RequiredArgument):
57
            self.gmp.create_note('foo', '')
58
59
    def test_create_note_with_hosts(self):
60
        self.gmp.create_note('foo', nvt_oid='oid1', hosts=[])
61
62
        self.connection.send.has_been_called_with(
63
            '<create_note>'
64
            '<text>foo</text>'
65
            '<nvt oid="oid1"/>'
66
            '</create_note>'
67
        )
68
69
        self.gmp.create_note('foo', nvt_oid='oid1', hosts=['h1', 'h2'])
70
71
        self.connection.send.has_been_called_with(
72
            '<create_note>'
73
            '<text>foo</text>'
74
            '<nvt oid="oid1"/>'
75
            '<hosts>h1,h2</hosts>'
76
            '</create_note>'
77
        )
78
79
    def test_create_note_with_port(self):
80
        self.gmp.create_note('foo', nvt_oid='oid1', port='666')
81
82
        self.connection.send.has_been_called_with(
83
            '<create_note>'
84
            '<text>foo</text>'
85
            '<nvt oid="oid1"/>'
86
            '<port>666</port>'
87
            '</create_note>'
88
        )
89
90
        self.gmp.create_note('foo', nvt_oid='oid1', port=666)
91
92
        self.connection.send.has_been_called_with(
93
            '<create_note>'
94
            '<text>foo</text>'
95
            '<nvt oid="oid1"/>'
96
            '<port>666</port>'
97
            '</create_note>'
98
        )
99
100
    def test_create_note_with_result_id(self):
101
        self.gmp.create_note('foo', nvt_oid='oid1', result_id='r1')
102
103
        self.connection.send.has_been_called_with(
104
            '<create_note>'
105
            '<text>foo</text>'
106
            '<nvt oid="oid1"/>'
107
            '<result id="r1"/>'
108
            '</create_note>'
109
        )
110
111
    def test_create_note_with_task_id(self):
112
        self.gmp.create_note('foo', nvt_oid='oid1', task_id='t1')
113
114
        self.connection.send.has_been_called_with(
115
            '<create_note>'
116
            '<text>foo</text>'
117
            '<nvt oid="oid1"/>'
118
            '<task id="t1"/>'
119
            '</create_note>'
120
        )
121
122
    def test_create_note_with_severity(self):
123
        self.gmp.create_note('foo', nvt_oid='oid1', severity='5.5')
124
125
        self.connection.send.has_been_called_with(
126
            '<create_note>'
127
            '<text>foo</text>'
128
            '<nvt oid="oid1"/>'
129
            '<severity>5.5</severity>'
130
            '</create_note>'
131
        )
132
133
        self.gmp.create_note('foo', nvt_oid='oid1', severity=5.5)
134
135
        self.connection.send.has_been_called_with(
136
            '<create_note>'
137
            '<text>foo</text>'
138
            '<nvt oid="oid1"/>'
139
            '<severity>5.5</severity>'
140
            '</create_note>'
141
        )
142
143
        self.gmp.create_note('foo', nvt_oid='oid1', severity=Decimal(5.5))
144
145
        self.connection.send.has_been_called_with(
146
            '<create_note>'
147
            '<text>foo</text>'
148
            '<nvt oid="oid1"/>'
149
            '<severity>5.5</severity>'
150
            '</create_note>'
151
        )
152
153
    def test_create_note_with_threat(self):
154
        self.gmp.create_note('foo', nvt_oid='oid1', threat='High')
155
156
        self.connection.send.has_been_called_with(
157
            '<create_note>'
158
            '<text>foo</text>'
159
            '<nvt oid="oid1"/>'
160
            '<threat>High</threat>'
161
            '</create_note>'
162
        )
163
164
    def test_create_note_invalid_threat(self):
165
        with self.assertRaises(InvalidArgument):
166
            self.gmp.create_note(
167
                'foo',
168
                nvt_oid='oid1',
169
                threat='',
170
            )
171
172
        with self.assertRaises(InvalidArgument):
173
            self.gmp.create_note(
174
                'foo',
175
                nvt_oid='oid1',
176
                threat='foo',
177
            )
178
179
    def test_create_note_with_seconds_active(self):
180
        self.gmp.create_note('foo', nvt_oid='oid1', seconds_active=0)
181
182
        self.connection.send.has_been_called_with(
183
            '<create_note>'
184
            '<text>foo</text>'
185
            '<nvt oid="oid1"/>'
186
            '<active>0</active>'
187
            '</create_note>'
188
        )
189
190
        self.gmp.create_note('foo', nvt_oid='oid1', seconds_active=-1)
191
192
        self.connection.send.has_been_called_with(
193
            '<create_note>'
194
            '<text>foo</text>'
195
            '<nvt oid="oid1"/>'
196
            '<active>-1</active>'
197
            '</create_note>'
198
        )
199
200
        self.gmp.create_note('foo', nvt_oid='oid1', seconds_active=3600)
201
202
        self.connection.send.has_been_called_with(
203
            '<create_note>'
204
            '<text>foo</text>'
205
            '<nvt oid="oid1"/>'
206
            '<active>3600</active>'
207
            '</create_note>'
208
        )
209
210
211
if __name__ == '__main__':
212
    unittest.main()
213