Skip to content

Commit

Permalink
Improve create_audit/task, increase their coverage, filter->filter_st…
Browse files Browse the repository at this point in the history
…ring
  • Loading branch information
y0urself committed May 20, 2021
1 parent d7e7a6d commit 96c7c01
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 15 deletions.
18 changes: 12 additions & 6 deletions gvm/protocols/gmpv208/entities/audits.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,15 @@ def create_audit(
)
cmd.add_element("hosts_ordering", hosts_ordering.value)

if alert_ids:
if is_list_like(alert_ids):
# parse all given alert id's
if alert_ids is not None:
if not is_list_like(alert_ids):
raise InvalidArgumentType(
function=self.modify_task.__name__,
argument='alert_ids',
arg_type='list',
)

if not len(alert_ids) == 0:
for alert in alert_ids:
cmd.add_element("alert", attrs={"id": str(alert)})

Expand Down Expand Up @@ -204,7 +210,7 @@ def delete_audit(
def get_audits(
self,
*,
filter: Optional[str] = None,
filter_string: Optional[str] = None,
filter_id: Optional[str] = None,
trash: Optional[bool] = None,
details: Optional[bool] = None,
Expand All @@ -213,7 +219,7 @@ def get_audits(
"""Request a list of audits
Arguments:
filter: Filter term to use for the query
filter_string: Filter term to use for the query
filter_id: UUID of an existing filter to use for the query
trash: Whether to get the trashcan audits instead
details: Whether to include full audit details
Expand All @@ -226,7 +232,7 @@ def get_audits(
cmd = XmlCommand("get_tasks")
cmd.set_attribute("usage_type", "audit")

add_filter(cmd, filter, filter_id)
add_filter(cmd, filter_string, filter_id)

if trash is not None:
cmd.set_attribute("trash", to_bool(trash))
Expand Down
18 changes: 12 additions & 6 deletions gvm/protocols/gmpv208/entities/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,15 @@ def create_task(
)
cmd.add_element("hosts_ordering", hosts_ordering.value)

if alert_ids:
if is_list_like(alert_ids):
# parse all given alert id's
if alert_ids is not None:
if not is_list_like(alert_ids):
raise InvalidArgumentType(
function=self.modify_task.__name__,
argument='alert_ids',
arg_type='list',
)

if not len(alert_ids) == 0:
for alert in alert_ids:
cmd.add_element("alert", attrs={"id": str(alert)})

Expand Down Expand Up @@ -212,7 +218,7 @@ def delete_task(
def get_tasks(
self,
*,
filter: Optional[str] = None,
filter_string: Optional[str] = None,
filter_id: Optional[str] = None,
trash: Optional[bool] = None,
details: Optional[bool] = None,
Expand All @@ -221,7 +227,7 @@ def get_tasks(
"""Request a list of tasks
Arguments:
filter: Filter term to use for the query
filter_string: Filter term to use for the query
filter_id: UUID of an existing filter to use for the query
trash: Whether to get the trashcan tasks instead
details: Whether to include full task details
Expand All @@ -234,7 +240,7 @@ def get_tasks(
cmd = XmlCommand("get_tasks")
cmd.set_attribute("usage_type", "scan")

add_filter(cmd, filter, filter_id)
add_filter(cmd, filter_string, filter_id)

if trash is not None:
cmd.set_attribute("trash", to_bool(trash))
Expand Down
29 changes: 29 additions & 0 deletions tests/protocols/gmpv208/entities/audits/test_create_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,35 @@ def test_create_audit_multiple_alerts(self):
'</create_task>'
)

def test_create_audit_invalid_alerts(self):
with self.assertRaises(InvalidArgumentType):
self.gmp.create_audit(
name='foo',
policy_id='c1',
target_id='t1',
scanner_id='s1',
alert_ids='invalid',
)

def test_create_audit_with_empty_alert_ids(self):
self.gmp.create_audit(
name='foo',
policy_id='c1',
target_id='t1',
scanner_id='s1',
alert_ids=[],
)

self.connection.send.has_been_called_with(
'<create_task>'
'<name>foo</name>'
'<usage_type>audit</usage_type>'
'<config id="c1"/>'
'<target id="t1"/>'
'<scanner id="s1"/>'
'</create_task>'
)

def test_create_audit_with_alterable(self):
self.gmp.create_audit(
name='foo',
Expand Down
2 changes: 1 addition & 1 deletion tests/protocols/gmpv208/entities/audits/test_get_audits.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_get_audits_simple(self):
)

def test_get_audits_with_filter(self):
self.gmp.get_audits(filter='name=foo')
self.gmp.get_audits(filter_string='name=foo')

self.connection.send.has_been_called_with(
'<get_tasks usage_type="audit" filter="name=foo"/>'
Expand Down
29 changes: 29 additions & 0 deletions tests/protocols/gmpv208/entities/tasks/test_create_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,35 @@ def test_create_task_multiple_alerts(self):
'</create_task>'
)

def test_create_task_invalid_alerts(self):
with self.assertRaises(InvalidArgumentType):
self.gmp.create_task(
name='foo',
config_id='c1',
target_id='t1',
scanner_id='s1',
alert_ids='invalid',
)

def test_create_task_with_empty_alert_ids(self):
self.gmp.create_task(
name='foo',
config_id='c1',
target_id='t1',
scanner_id='s1',
alert_ids=[],
)

self.connection.send.has_been_called_with(
'<create_task>'
'<name>foo</name>'
'<usage_type>scan</usage_type>'
'<config id="c1"/>'
'<target id="t1"/>'
'<scanner id="s1"/>'
'</create_task>'
)

def test_create_task_with_alterable(self):
self.gmp.create_task(
name='foo',
Expand Down
4 changes: 2 additions & 2 deletions tests/protocols/gmpv208/entities/tasks/test_get_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def test_get_tasks_simple(self):
'<get_tasks usage_type="scan"/>'
)

def test_get_tasks_with_filter(self):
self.gmp.get_tasks(filter='name=foo')
def test_get_tasks_with_filter_string(self):
self.gmp.get_tasks(filter_string='name=foo')

self.connection.send.has_been_called_with(
'<get_tasks usage_type="scan" filter="name=foo"/>'
Expand Down

0 comments on commit 96c7c01

Please sign in to comment.