File: test_metadata.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 (129 lines) | stat: -rw-r--r-- 4,189 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
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
# 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._metadata import (
    Metadata,
    MetadataAPI,
    MetadataError,
)


def _mock_head_response(status_code=200, headers=None):
    response = MagicMock(spec=httpx.Response)
    response.status_code = status_code
    response.headers = headers or {}
    response.raise_for_status = MagicMock()
    return response


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

    def test_get_successful(self):
        headers = {
            "api-version": "1.0",
            "feed-version": "2025.01",
            "authentication": "API-KEY",
        }
        mock_response = _mock_head_response(200, headers)
        self.mock_client.head.return_value = mock_response

        api = MetadataAPI(self.mock_client)
        result = api.get()
        self.mock_client.head.assert_called_once_with("/")
        mock_response.raise_for_status.assert_called_once()
        self.assertEqual(
            result,
            Metadata(
                api_version="1.0",
                feed_version="2025.01",
                authentication="API-KEY",
            ),
        )

    def test_get_unauthorized_with_suppress_exceptions(self):
        mock_response = MagicMock(spec=httpx.Response)
        mock_response.status_code = 401

        self.mock_client.head.side_effect = httpx.HTTPStatusError(
            "Unauthorized", request=MagicMock(), response=mock_response
        )

        api = MetadataAPI(self.mock_client, suppress_exceptions=True)
        result = api.get()
        self.assertEqual(
            result, MetadataError(error="Unauthorized", status_code=401)
        )

    def test_get_failure_raises_httpx_error(self):
        mock_response = _mock_head_response(500, None)
        mock_response.raise_for_status.side_effect = httpx.HTTPStatusError(
            "Server failed",
            request=MagicMock(),
            response=MagicMock(status_code=500),
        )
        self.mock_client.head.return_value = mock_response

        api = MetadataAPI(self.mock_client)
        with self.assertRaises(httpx.HTTPStatusError):
            api.get()

        self.mock_client.head.assert_called_once_with("/")

    def test_get_scans_successful(self):
        headers = {
            "api-version": "1.0",
            "feed-version": "2025.01",
            "authentication": "API-KEY",
        }
        mock_response = _mock_head_response(200, headers)
        self.mock_client.head.return_value = mock_response

        api = MetadataAPI(self.mock_client)
        result = api.get_scans()
        self.mock_client.head.assert_called_once_with("/scans")
        mock_response.raise_for_status.assert_called_once()
        self.assertEqual(
            result,
            Metadata(
                api_version="1.0",
                feed_version="2025.01",
                authentication="API-KEY",
            ),
        )

    def test_get_scans_unauthorized_with_suppress_exceptions(self):
        mock_response = MagicMock(spec=httpx.Response)
        mock_response.status_code = 401

        self.mock_client.head.side_effect = httpx.HTTPStatusError(
            "Unauthorized", request=MagicMock(), response=mock_response
        )

        api = MetadataAPI(self.mock_client, suppress_exceptions=True)
        result = api.get_scans()
        self.assertEqual(
            result, MetadataError(error="Unauthorized", status_code=401)
        )

    def test_get_scans_failure_raises_httpx_error(self):
        mock_response = _mock_head_response(500, None)
        mock_response.raise_for_status.side_effect = httpx.HTTPStatusError(
            "Server failed",
            request=MagicMock(),
            response=MagicMock(status_code=500),
        )
        self.mock_client.head.return_value = mock_response

        api = MetadataAPI(self.mock_client)
        with self.assertRaises(httpx.HTTPStatusError):
            api.get_scans()

        self.mock_client.head.assert_called_once_with("/scans")