1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
# SPDX-FileCopyrightText: 2018-2024 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
from gvm.errors import InvalidArgument, RequiredArgument
from gvm.protocols.gmp.requests.v224 import (
AlertCondition,
AlertEvent,
AlertMethod,
)
class GmpModifyAlertTestMixin:
def test_modify_alert(self):
self.gmp.modify_alert(alert_id="a1")
self.connection.send.has_been_called_with(
b'<modify_alert alert_id="a1"/>'
)
def test_modify_alert_without_alert_id(self):
with self.assertRaises(RequiredArgument):
self.gmp.modify_alert(alert_id=None)
with self.assertRaises(RequiredArgument):
self.gmp.modify_alert(alert_id="")
with self.assertRaises(RequiredArgument):
self.gmp.modify_alert("")
def test_modify_alert_with_comment(self):
self.gmp.modify_alert(alert_id="a1", comment="lorem")
self.connection.send.has_been_called_with(
b'<modify_alert alert_id="a1">'
b"<comment>lorem</comment>"
b"</modify_alert>"
)
def test_modify_alert_with_name(self):
self.gmp.modify_alert(alert_id="a1", name="lorem")
self.connection.send.has_been_called_with(
b'<modify_alert alert_id="a1">'
b"<name>lorem</name>"
b"</modify_alert>"
)
def test_modify_alert_with_filter_id(self):
self.gmp.modify_alert(alert_id="a1", filter_id="f1")
self.connection.send.has_been_called_with(
b'<modify_alert alert_id="a1"><filter id="f1"/></modify_alert>'
)
def test_modify_alert_invalid_condition(self):
with self.assertRaises(InvalidArgument):
self.gmp.modify_alert(
alert_id="a1",
condition="bar",
event=AlertEvent.TASK_RUN_STATUS_CHANGED,
method=AlertMethod.SCP,
)
def test_modify_alert_invalid_event(self):
with self.assertRaises(InvalidArgument):
self.gmp.modify_alert(
alert_id="a1",
condition=AlertCondition.ALWAYS,
event="lorem",
method=AlertMethod.SCP,
)
def test_modify_alert_invalid_method(self):
with self.assertRaises(InvalidArgument):
self.gmp.modify_alert(
alert_id="a1",
condition=AlertCondition.ALWAYS,
event=AlertEvent.TASK_RUN_STATUS_CHANGED,
method="ipsum",
)
def test_modify_alert_with_event_missing_method(self):
with self.assertRaisesRegex(RequiredArgument, "method is required"):
self.gmp.modify_alert(
alert_id="a1",
event=AlertEvent.TASK_RUN_STATUS_CHANGED,
condition=AlertCondition.ALWAYS,
)
with self.assertRaisesRegex(RequiredArgument, "method is required"):
self.gmp.modify_alert(
alert_id="a1",
event=AlertEvent.NEW_SECINFO_ARRIVED,
condition=AlertCondition.ALWAYS,
)
with self.assertRaisesRegex(RequiredArgument, "method is required"):
self.gmp.modify_alert(
alert_id="a1",
event=AlertEvent.UPDATED_SECINFO_ARRIVED,
condition=AlertCondition.ALWAYS,
)
def test_modify_alert_with_event_missing_condition(self):
with self.assertRaisesRegex(RequiredArgument, "condition is required"):
self.gmp.modify_alert(
alert_id="a1",
event=AlertEvent.TASK_RUN_STATUS_CHANGED,
method=AlertMethod.SCP,
)
with self.assertRaisesRegex(RequiredArgument, "condition is required"):
self.gmp.modify_alert(
alert_id="a1",
event=AlertEvent.NEW_SECINFO_ARRIVED,
method=AlertMethod.SCP,
)
with self.assertRaisesRegex(RequiredArgument, "condition is required"):
self.gmp.modify_alert(
alert_id="a1",
event=AlertEvent.UPDATED_SECINFO_ARRIVED,
method=AlertMethod.SCP,
)
def test_modify_alert_invalid_condition_for_secinfo(self):
with self.assertRaises(InvalidArgument):
self.gmp.modify_alert(
alert_id="a1",
condition="Severity at least",
event="Updated SecInfo arrived",
method="Email",
)
def test_modify_alert_invalid_method_for_secinfo(self):
with self.assertRaises(InvalidArgument):
self.gmp.modify_alert(
alert_id="a1",
condition="Always",
event="Updated SecInfo arrived",
method="HTTP Get",
)
def test_modify_alert_with_event_data(self):
self.gmp.modify_alert(
alert_id="a1",
condition=AlertCondition.ALWAYS,
event=AlertEvent.TASK_RUN_STATUS_CHANGED,
method=AlertMethod.EMAIL,
event_data={"foo": "bar"},
)
self.connection.send.has_been_called_with(
b'<modify_alert alert_id="a1">'
b"<condition>Always</condition>"
b"<method>Email</method>"
b"<event>Task run status changed"
b"<data>bar<name>foo</name></data>"
b"</event>"
b"</modify_alert>"
)
def test_modify_alert_with_condition_data(self):
self.gmp.modify_alert(
alert_id="a1",
condition=AlertCondition.ALWAYS,
event=AlertEvent.TASK_RUN_STATUS_CHANGED,
method=AlertMethod.EMAIL,
condition_data={"foo": "bar"},
)
self.connection.send.has_been_called_with(
b'<modify_alert alert_id="a1">'
b"<condition>Always<data>bar<name>foo</name></data></condition>"
b"<method>Email</method>"
b"<event>Task run status changed</event>"
b"</modify_alert>"
)
def test_modify_alert_with_method_data(self):
self.gmp.modify_alert(
alert_id="a1",
condition=AlertCondition.ALWAYS,
event=AlertEvent.TASK_RUN_STATUS_CHANGED,
method=AlertMethod.EMAIL,
method_data={"foo": "bar"},
)
self.connection.send.has_been_called_with(
b'<modify_alert alert_id="a1">'
b"<condition>Always</condition>"
b"<method>Email<data>bar<name>foo</name></data></method>"
b"<event>Task run status changed</event>"
b"</modify_alert>"
)
def test_modify_missing_method_for_ticket_received(self):
with self.assertRaises(RequiredArgument):
self.gmp.modify_alert(
alert_id="a1",
condition=AlertCondition.ALWAYS,
event=AlertEvent.TICKET_RECEIVED,
method=None,
)
def test_modify_missing_condition_for_ticket_received(self):
with self.assertRaises(RequiredArgument):
self.gmp.modify_alert(
alert_id="a1",
condition=None,
event=AlertEvent.TICKET_RECEIVED,
method=AlertMethod.EMAIL,
)
|