Skip to content

Commit

Permalink
Add: Add tests for current requests implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernricks authored and greenbonebot committed Jun 14, 2024
1 parent 70cef3b commit 5dae6d3
Show file tree
Hide file tree
Showing 5 changed files with 871 additions and 1 deletion.
9 changes: 8 additions & 1 deletion gvm/protocols/gmp/requests/_resource_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ class ResourceType(Enum):


class ResourceNames:
@staticmethod
@classmethod
def get_resource_names(
cls,
resource_type: Union[ResourceType, str],
*,
filter_string: Optional[str] = None,
Expand All @@ -61,6 +62,12 @@ def get_resource_names(
"""
cmd = XmlCommand("get_resource_names")

if not resource_type:
raise RequiredArgument(
function=cls.get_resource_names.__name__,
argument="resource_type",
)

if not isinstance(resource_type, ResourceType):
resource_type = ResourceType(resource_type)

Expand Down
92 changes: 92 additions & 0 deletions tests/protocols/gmp/requests/test_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# SPDX-FileCopyrightText: 2024 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later

import unittest

from gvm.errors import RequiredArgument
from gvm.protocols.core import Request
from gvm.protocols.gmp.requests import Authentication


class AuthenticationTestCase(unittest.TestCase):
def test_authenticate(self) -> None:
request = Authentication.authenticate("admin", "admin")

self.assertIsInstance(request, Request)
self.assertEqual(
bytes(request),
b"<authenticate><credentials><username>admin</username>"
b"<password>admin</password></credentials></authenticate>",
)

def test_authenticate_missing_username(self) -> None:
with self.assertRaises(RequiredArgument):
Authentication.authenticate(None, "foo") # type: ignore

with self.assertRaises(RequiredArgument):
Authentication.authenticate("", "foo")

def test_authenticate_missing_password(self) -> None:
with self.assertRaises(RequiredArgument):
Authentication.authenticate("bar", None) # type: ignore

with self.assertRaises(RequiredArgument):
Authentication.authenticate("bar", "")

def test_describe_auth(self) -> None:
request = Authentication.describe_auth()

self.assertIsInstance(request, Request)
self.assertEqual(bytes(request), b"<describe_auth/>")

def test_modify_auth(self) -> None:
request = Authentication.modify_auth(
"foo", dict([("foo", "bar"), ("lorem", "ipsum")])
)

self.assertIsInstance(request, Request)
self.assertEqual(
bytes(request),
b"<modify_auth>"
b'<group name="foo">'
b"<auth_conf_setting>"
b"<key>foo</key>"
b"<value>bar</value>"
b"</auth_conf_setting>"
b"<auth_conf_setting>"
b"<key>lorem</key>"
b"<value>ipsum</value>"
b"</auth_conf_setting>"
b"</group>"
b"</modify_auth>",
)

def test_modify_auth_missing_group_name(self) -> None:
with self.assertRaises(RequiredArgument):
Authentication.modify_auth(
group_name=None, auth_conf_settings={"foo": "bar"} # type: ignore
)

with self.assertRaises(RequiredArgument):
Authentication.modify_auth(
group_name="", auth_conf_settings={"foo": "bar"}
)

with self.assertRaises(RequiredArgument):
Authentication.modify_auth("", auth_conf_settings={"foo": "bar"})

def test_modify_auth_auth_conf_settings(self) -> None:
with self.assertRaises(RequiredArgument):
Authentication.modify_auth(
group_name="foo", auth_conf_settings=None # type: ignore
)

with self.assertRaises(RequiredArgument):
Authentication.modify_auth(group_name="foo", auth_conf_settings="") # type: ignore

with self.assertRaises(RequiredArgument):
Authentication.modify_auth("foo", "") # type: ignore

with self.assertRaises(RequiredArgument):
Authentication.modify_auth("foo", {})
Loading

0 comments on commit 5dae6d3

Please sign in to comment.