-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_other_http.py
161 lines (113 loc) · 6.14 KB
/
test_other_http.py
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
from unittest import TestCase
from unittest.mock import MagicMock, patch
from pytest_httpx import HTTPXMock
from httpx import ConnectError
from grafana_api.model import APIModel
from grafana_api.other_http import OtherHTTP
class OtherHTTPTestCase(TestCase):
@patch("grafana_api.api.Api.call_the_api")
def test_get_frontend_settings(self, call_the_api_mock):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
other_http: OtherHTTP = OtherHTTP(grafana_api_model=model)
call_the_api_mock.return_value = dict({"allowOrgCreate": True})
self.assertEqual(
dict({"allowOrgCreate": True}), other_http.get_frontend_settings()
)
@patch("grafana_api.api.Api.call_the_api")
def test_get_frontend_settings_no_valid_result(self, call_the_api_mock):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
other_http: OtherHTTP = OtherHTTP(grafana_api_model=model)
call_the_api_mock.return_value = dict()
with self.assertRaises(Exception):
other_http.get_frontend_settings()
@patch("grafana_api.api.Api.call_the_api")
def test_renew_login_session_based_on_remember_cookie(self, call_the_api_mock):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
other_http: OtherHTTP = OtherHTTP(grafana_api_model=model)
call_the_api_mock.return_value = dict({"message": "Logged in"})
self.assertEqual(
None, other_http.renew_login_session_based_on_remember_cookie()
)
@patch("grafana_api.api.Api.call_the_api")
def test_renew_login_session_based_on_remember_cookie_no_valid_result(
self, call_the_api_mock
):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
other_http: OtherHTTP = OtherHTTP(grafana_api_model=model)
call_the_api_mock.return_value = dict()
with self.assertRaises(Exception):
other_http.renew_login_session_based_on_remember_cookie()
@patch("httpx.Client")
def test_get_health_status(self, httpx_client_mock):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
other_http: OtherHTTP = OtherHTTP(grafana_api_model=model)
httpx_client_mock.return_value.request.return_value.text = (
'{"commit": "087143285"}'
)
self.assertEqual(dict({"commit": "087143285"}), other_http.get_health_status())
@patch("httpx.Client")
def test_get_health_status_no_valid_result(self, httpx_client_mock):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
other_http: OtherHTTP = OtherHTTP(grafana_api_model=model)
httpx_client_mock.return_value.request.return_value.text = "{}"
with self.assertRaises(Exception):
other_http.get_health_status()
@patch("httpx.Client")
def test_get_metrics(self, httpx_client_mock):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
other_http: OtherHTTP = OtherHTTP(grafana_api_model=model)
httpx_client_mock.return_value.request.return_value.text = "test"
self.assertEqual("test", other_http.get_metrics())
@patch("httpx.Client")
def test_get_metrics_basic_auth(self, httpx_client_mock):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
other_http: OtherHTTP = OtherHTTP(grafana_api_model=model)
httpx_client_mock.return_value.request.return_value.text = "test"
self.assertEqual("test", other_http.get_metrics("test", "test"))
@patch("httpx.Client")
def test_get_metrics_request_issue(self, httpx_client_mock):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
other_http: OtherHTTP = OtherHTTP(grafana_api_model=model)
httpx_client_mock.return_value.request.side_effect = ConnectError("Test")
with self.assertRaises(ConnectError):
other_http.get_metrics()
@patch("httpx.Client")
def test_get_metrics_no_valid_result(self, httpx_client_mock):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
other_http: OtherHTTP = OtherHTTP(grafana_api_model=model)
httpx_client_mock.return_value.request.return_value.text = ""
with self.assertRaises(Exception):
other_http.get_metrics()
@patch("httpx.Client")
def test_get_plugin_metrics(self, httpx_client_mock):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
other_http: OtherHTTP = OtherHTTP(grafana_api_model=model)
httpx_client_mock.return_value.request.return_value.text = "test"
self.assertEqual("test", other_http.get_plugin_metrics("test"))
@patch("httpx.Client")
def test_get_plugin_metrics_basic_auth(self, httpx_client_mock):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
other_http: OtherHTTP = OtherHTTP(grafana_api_model=model)
httpx_client_mock.return_value.request.return_value.text = "test"
self.assertEqual("test", other_http.get_plugin_metrics("test", "test", "test"))
@patch("httpx.Client")
def test_get_plugin_metrics_no_plugin_id(self, httpx_client_mock):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
other_http: OtherHTTP = OtherHTTP(grafana_api_model=model)
httpx_client_mock.return_value.request.return_value.text = ""
with self.assertRaises(ValueError):
other_http.get_plugin_metrics("")
@patch("httpx.Client")
def test_get_plugin_metrics_no_valid_result(self, httpx_client_mock):
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
other_http: OtherHTTP = OtherHTTP(grafana_api_model=model)
httpx_client_mock.return_value.request.return_value.text = ""
with self.assertRaises(Exception):
other_http.get_plugin_metrics("test")
def test_get_plugin_metrics_http2_support(httpx_mock: HTTPXMock):
httpx_mock.add_response(text="test")
model: APIModel = APIModel(
host="https://test.com", token="test", http2_support=True
)
other_http: OtherHTTP = OtherHTTP(grafana_api_model=model)
assert other_http.get_plugin_metrics("test") == "test"