Passed
Pull Request — master (#50)
by
unknown
01:51
created

GmpCreateAlertTestCase.test_create_alert_with_comment()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
2
# -*- coding: utf-8 -*-
3
# Copyright (C) 2018 Greenbone Networks GmbH
4
#
5
# SPDX-License-Identifier: GPL-3.0-or-later
6
#
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
import unittest
21
22
from gvm.errors import RequiredArgument
23
from gvm.protocols.gmpv7 import Gmp
24
25
from .. import MockConnection
26
27
class GmpCreateAlertTestCase(unittest.TestCase):
28
29
    def setUp(self):
30
        self.connection = MockConnection()
31
        self.gmp = Gmp(self.connection)
32
33
    def test_missing_name(self):
34
        with self.assertRaises(RequiredArgument):
35
            self.gmp.create_alert(
36
                name='', condition='foo', event='bar', method='lorem')
37
38
        with self.assertRaises(RequiredArgument):
39
            self.gmp.create_alert(
40
                name=None, condition='foo', event='bar', method='lorem')
41
42
    def test_missing_condition(self):
43
        with self.assertRaises(RequiredArgument):
44
            self.gmp.create_alert(
45
                name='foo', condition='', event='bar', method='lorem')
46
47
        with self.assertRaises(RequiredArgument):
48
            self.gmp.create_alert(
49
                name='foo', condition=None, event='bar', method='lorem')
50
51
    def test_missing_event(self):
52
        with self.assertRaises(RequiredArgument):
53
            self.gmp.create_alert(
54
                name='foo', condition='bar', event='', method='lorem')
55
56
        with self.assertRaises(RequiredArgument):
57
            self.gmp.create_alert(
58
                name='foo', condition='bar', event=None, method='lorem')
59
60
    def test_missing_method(self):
61
        with self.assertRaises(RequiredArgument):
62
            self.gmp.create_alert(
63
                name='foo', condition='bar', event='lorem', method='')
64
65
        with self.assertRaises(RequiredArgument):
66
            self.gmp.create_alert(
67
                name='foo', condition='bar', event='lorem', method=None)
68
69
    def test_create_alert(self):
70
        self.gmp.create_alert(
71
            name='foo', condition='bar', event='lorem', method='ipsum')
72
73
        self.connection.send.has_been_called_with(
74
            '<create_alert>'
75
            '<name>foo</name>'
76
            '<condition>bar</condition>'
77
            '<event>lorem</event>'
78
            '<method>ipsum</method>'
79
            '</create_alert>'
80
        )
81
82
    def test_create_alert_with_filter_id(self):
83
        self.gmp.create_alert(
84
            name='foo', condition='bar', event='lorem', method='ipsum',
85
            filter_id='f1')
86
87
        self.connection.send.has_been_called_with(
88
            '<create_alert>'
89
            '<name>foo</name>'
90
            '<condition>bar</condition>'
91
            '<event>lorem</event>'
92
            '<method>ipsum</method>'
93
            '<filter id="f1"/>'
94
            '</create_alert>'
95
        )
96
97
    def test_create_alert_with_comment(self):
98
        self.gmp.create_alert(
99
            name='foo', condition='bar', event='lorem', method='ipsum',
100
            comment='hello')
101
102
        self.connection.send.has_been_called_with(
103
            '<create_alert>'
104
            '<name>foo</name>'
105
            '<condition>bar</condition>'
106
            '<event>lorem</event>'
107
            '<method>ipsum</method>'
108
            '<comment>hello</comment>'
109
            '</create_alert>'
110
        )
111
112
    def test_create_alert_with_condition_data(self):
113
        self.gmp.create_alert(
114
            name='foo', condition='bar', event='lorem', method='ipsum',
115
            condition_data={'foo': 'bar'})
116
117
        self.connection.send.has_been_called_with(
118
            '<create_alert>'
119
            '<name>foo</name>'
120
            '<condition>bar<data>bar<name>foo</name></data></condition>'
121
            '<event>lorem</event>'
122
            '<method>ipsum</method>'
123
            '</create_alert>'
124
        )
125
126
    def test_create_alert_with_event_data(self):
127
        self.gmp.create_alert(
128
            name='foo', condition='bar', event='lorem', method='ipsum',
129
            event_data={'foo': 'bar'})
130
131
        self.connection.send.has_been_called_with(
132
            '<create_alert>'
133
            '<name>foo</name>'
134
            '<condition>bar</condition>'
135
            '<event>lorem<data>bar<name>foo</name></data></event>'
136
            '<method>ipsum</method>'
137
            '</create_alert>'
138
        )
139
140
    def test_create_alert_with_method_data(self):
141
        self.gmp.create_alert(
142
            name='foo', condition='bar', event='lorem', method='ipsum',
143
            method_data={'foo': 'bar'})
144
145
        self.connection.send.has_been_called_with(
146
            '<create_alert>'
147
            '<name>foo</name>'
148
            '<condition>bar</condition>'
149
            '<event>lorem</event>'
150
            '<method>ipsum<data>bar<name>foo</name></data></method>'
151
            '</create_alert>'
152
        )
153
154
if __name__ == '__main__':
155
    unittest.main()
156