Completed
Push — master ( e81db1...a7ad2f )
by Björn
17s queued 13s
created

GmpCreateNoteTestCase.test_create_note_with_threat()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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