Passed
Pull Request — master (#443)
by Jaspar
01:31
created

GmpModifyTaskTestMixin.test_modify_task_with_comment()   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) 2018-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
from collections import OrderedDict
20
21
from gvm.errors import RequiredArgument, InvalidArgument, InvalidArgumentType
22
23
from gvm.protocols.gmpv208 import HostsOrdering
24
25
26
class GmpModifyTaskTestMixin:
27
    def test_modify_task(self):
28
        self.gmp.modify_task('t1')
29
30
        self.connection.send.has_been_called_with('<modify_task task_id="t1"/>')
31
32
    def test_modify_task_missing_task_id(self):
33
        with self.assertRaises(RequiredArgument):
34
            self.gmp.modify_task(None)
35
36
        with self.assertRaises(RequiredArgument):
37
            self.gmp.modify_task('')
38
39
        with self.assertRaises(RequiredArgument):
40
            self.gmp.modify_task(task_id='')
41
42
    def test_modify_task_with_name(self):
43
        self.gmp.modify_task(task_id='t1', name='foo')
44
45
        self.connection.send.has_been_called_with(
46
            '<modify_task task_id="t1">' '<name>foo</name>' '</modify_task>'
47
        )
48
49
    def test_modify_task_with_config_id(self):
50
        self.gmp.modify_task(task_id='t1', config_id='c1')
51
52
        self.connection.send.has_been_called_with(
53
            '<modify_task task_id="t1">' '<config id="c1"/>' '</modify_task>'
54
        )
55
56
    def test_modify_task_with_target_id(self):
57
        self.gmp.modify_task(task_id='t1', target_id='t1')
58
59
        self.connection.send.has_been_called_with(
60
            '<modify_task task_id="t1">' '<target id="t1"/>' '</modify_task>'
61
        )
62
63
    def test_modify_task_with_scanner_id(self):
64
        self.gmp.modify_task(task_id='t1', scanner_id='s1')
65
66
        self.connection.send.has_been_called_with(
67
            '<modify_task task_id="t1">' '<scanner id="s1"/>' '</modify_task>'
68
        )
69
70
    def test_modify_task_with_schedule_id(self):
71
        self.gmp.modify_task(task_id='t1', schedule_id='s1')
72
73
        self.connection.send.has_been_called_with(
74
            '<modify_task task_id="t1">' '<schedule id="s1"/>' '</modify_task>'
75
        )
76
77
    def test_modify_task_with_comment(self):
78
        self.gmp.modify_task(task_id='t1', comment='bar')
79
80
        self.connection.send.has_been_called_with(
81
            '<modify_task task_id="t1">'
82
            '<comment>bar</comment>'
83
            '</modify_task>'
84
        )
85
86
    def test_modify_task_with_alerts_ids(self):
87
        self.gmp.modify_task(task_id='t1', alert_ids=['a1', 'a2', 'a3'])
88
89
        self.connection.send.has_been_called_with(
90
            '<modify_task task_id="t1">'
91
            '<alert id="a1"/>'
92
            '<alert id="a2"/>'
93
            '<alert id="a3"/>'
94
            '</modify_task>'
95
        )
96
97
    def test_modify_task_invalid_alerts_ids(self):
98
        with self.assertRaises(InvalidArgumentType):
99
            self.gmp.modify_task(task_id='t1', alert_ids='')
100
101
        with self.assertRaises(InvalidArgumentType):
102
            self.gmp.modify_task(task_id='t1', alert_ids='a1')
103
104
    def test_modify_task_with_empty_alert_ids(self):
105
        self.gmp.modify_task(task_id='t1', alert_ids=[])
106
107
        self.connection.send.has_been_called_with(
108
            '<modify_task task_id="t1">' '<alert id="0"/>' '</modify_task>'
109
        )
110
111
    def test_modify_task_with_alterable(self):
112
        self.gmp.modify_task(task_id='t1', alterable=True)
113
114
        self.connection.send.has_been_called_with(
115
            '<modify_task task_id="t1">'
116
            '<alterable>1</alterable>'
117
            '</modify_task>'
118
        )
119
120
        self.gmp.modify_task(task_id='t1', alterable=False)
121
122
        self.connection.send.has_been_called_with(
123
            '<modify_task task_id="t1">'
124
            '<alterable>0</alterable>'
125
            '</modify_task>'
126
        )
127
128
    def test_modify_task_with_hosts_ordering(self):
129
        self.gmp.modify_task(task_id='t1', hosts_ordering=HostsOrdering.REVERSE)
130
131
        self.connection.send.has_been_called_with(
132
            '<modify_task task_id="t1">'
133
            '<hosts_ordering>reverse</hosts_ordering>'
134
            '</modify_task>'
135
        )
136
137
    def test_modify_task_invalid_hosts_ordering(self):
138
        with self.assertRaises(InvalidArgumentType):
139
            self.gmp.modify_task(task_id='t1', hosts_ordering='foo')
140
141
    def test_modify_task_with_schedule(self):
142
        self.gmp.modify_task(task_id='t1', schedule_id='s1')
143
144
        self.connection.send.has_been_called_with(
145
            '<modify_task task_id="t1">' '<schedule id="s1"/>' '</modify_task>'
146
        )
147
148
    def test_modify_task_with_schedule_periods(self):
149
        self.gmp.modify_task(task_id='t1', schedule_periods=0)
150
151
        self.connection.send.has_been_called_with(
152
            '<modify_task task_id="t1">'
153
            '<schedule_periods>0</schedule_periods>'
154
            '</modify_task>'
155
        )
156
157
        self.gmp.modify_task(task_id='t1', schedule_periods=5)
158
159
        self.connection.send.has_been_called_with(
160
            '<modify_task task_id="t1">'
161
            '<schedule_periods>5</schedule_periods>'
162
            '</modify_task>'
163
        )
164
165
    def test_modify_task_invalid_schedule_periods(self):
166
        with self.assertRaises(InvalidArgument):
167
            self.gmp.modify_task(task_id='t1', schedule_periods='foo')
168
169
        with self.assertRaises(InvalidArgument):
170
            self.gmp.modify_task(task_id='t1', schedule_periods=-1)
171
172
    def test_modify_task_with_observers(self):
173
        self.gmp.modify_task(task_id='t1', observers=['u1', 'u2'])
174
175
        self.connection.send.has_been_called_with(
176
            '<modify_task task_id="t1">'
177
            '<observers>u1,u2</observers>'
178
            '</modify_task>'
179
        )
180
181
    def test_modify_task_invalid_observers(self):
182
        with self.assertRaises(InvalidArgumentType):
183
            self.gmp.modify_task(task_id='t1', observers='')
184
185
        with self.assertRaises(InvalidArgumentType):
186
            self.gmp.modify_task(task_id='t1', observers='foo')
187
188
    def test_modify_task_with_preferences(self):
189
        self.gmp.modify_task(
190
            task_id='t1',
191
            preferences=OrderedDict([('foo', 'bar'), ('lorem', 'ipsum')]),
192
        )
193
194
        self.connection.send.has_been_called_with(
195
            '<modify_task task_id="t1">'
196
            '<preferences>'
197
            '<preference>'
198
            '<scanner_name>foo</scanner_name>'
199
            '<value>bar</value>'
200
            '</preference>'
201
            '<preference>'
202
            '<scanner_name>lorem</scanner_name>'
203
            '<value>ipsum</value>'
204
            '</preference>'
205
            '</preferences>'
206
            '</modify_task>'
207
        )
208
209
    def test_modify_task_invalid_preferences(self):
210
        with self.assertRaises(InvalidArgumentType):
211
            self.gmp.modify_task(task_id='t1', preferences='')
212
213
        with self.assertRaises(InvalidArgumentType):
214
            self.gmp.modify_task(task_id='t1', preferences=['foo', 'bar'])
215