File: test_vts.py

package info (click to toggle)
python-gvm 26.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,132 kB
  • sloc: python: 44,662; makefile: 18
file content (95 lines) | stat: -rw-r--r-- 3,308 bytes parent folder | download
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
# SPDX-FileCopyrightText: 2025 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later

import unittest
from unittest.mock import MagicMock

import httpx

from gvm.protocols.http.openvasd._vts import VtsAPI


def _mock_response(status_code=200):
    mock_response = MagicMock(spec=httpx.Response)
    mock_response.status_code = status_code
    mock_response.raise_for_status = MagicMock()
    return mock_response


class TestVtsAPI(unittest.TestCase):
    def setUp(self):
        self.mock_client = MagicMock(spec=httpx.Client)

    def test_get_all_returns_response(self):
        mock_response = _mock_response(200)
        self.mock_client.get.return_value = mock_response

        api = VtsAPI(self.mock_client)
        response = api.get_all()
        self.mock_client.get.assert_called_once_with("/vts")
        mock_response.raise_for_status.assert_called_once()
        self.assertEqual(response, mock_response)

    def test_get_all_raises_httpx_error(self):
        self.mock_client.get.side_effect = httpx.HTTPStatusError(
            "Error", request=MagicMock(), response=MagicMock(status_code=500)
        )
        api = VtsAPI(self.mock_client)
        with self.assertRaises(httpx.HTTPStatusError):
            api.get_all()

    def test_get_all_returns_response_on_httpx_error_with_suppress_exceptions(
        self,
    ):
        mock_response = _mock_response()
        mock_http_error = httpx.HTTPStatusError(
            "Server failed",
            request=MagicMock(),
            response=MagicMock(status_code=500),
        )
        mock_response.raise_for_status.side_effect = mock_http_error
        self.mock_client.get.return_value = mock_response

        api = VtsAPI(self.mock_client, suppress_exceptions=True)
        result = api.get_all()

        self.mock_client.get.assert_called_once_with("/vts")
        self.assertEqual(result, mock_http_error.response)

    def test_get_by_oid_returns_response(self):
        mock_response = _mock_response(200)
        self.mock_client.get.return_value = mock_response

        oid = "1.3.6.1.4.1.25623.1.0.123456"
        api = VtsAPI(self.mock_client)
        response = api.get(oid)
        self.mock_client.get.assert_called_once_with(f"/vts/{oid}")
        mock_response.raise_for_status.assert_called_once()
        self.assertEqual(response, mock_response)

    def test_get_by_oid_raises_httpx_error(self):
        self.mock_client.get.side_effect = httpx.HTTPStatusError(
            "Not Found",
            request=MagicMock(),
            response=MagicMock(status_code=404),
        )

        api = VtsAPI(self.mock_client)
        with self.assertRaises(httpx.HTTPStatusError):
            api.get("nonexistent-oid")

    def test_get_by_oid_response_on_httpx_error_with_suppress_exceptions(self):
        mock_response = _mock_response()
        mock_http_error = httpx.HTTPStatusError(
            "Not Found",
            request=MagicMock(),
            response=MagicMock(status_code=404),
        )
        mock_response.raise_for_status.side_effect = mock_http_error
        self.mock_client.get.return_value = mock_response

        api = VtsAPI(self.mock_client, suppress_exceptions=True)
        response = api.get("nonexistent-oid")

        self.assertEqual(response, mock_http_error.response)