-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathtest_dashboard_api_init.py
More file actions
204 lines (184 loc) · 7.53 KB
/
Copy pathtest_dashboard_api_init.py
File metadata and controls
204 lines (184 loc) · 7.53 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import logging
import os
from unittest.mock import patch
import pytest
import meraki
from meraki.exceptions import APIKeyError
class TestDashboardAPIInit:
@patch("meraki.rest_session.check_python_version")
def test_api_key_from_param(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
caller="TestApp TestVendor",
)
assert (
d._session._api_key == "test_key_1234567890123456789012345678901234567890"
)
@patch("meraki.rest_session.check_python_version")
def test_api_key_from_env(self, mock_check):
with patch.dict(
os.environ,
{
"MERAKI_DASHBOARD_API_KEY": "env_key_12345678901234567890123456789012345678"
},
):
d = meraki.DashboardAPI(
suppress_logging=True,
caller="TestApp TestVendor",
)
assert d._session._api_key == "env_key_12345678901234567890123456789012345678"
def test_missing_api_key_raises(self):
with patch.dict(os.environ, {}, clear=True):
os.environ.pop("MERAKI_DASHBOARD_API_KEY", None)
with pytest.raises(APIKeyError):
meraki.DashboardAPI(suppress_logging=True, caller="TestApp TestVendor")
@patch("meraki.rest_session.check_python_version")
def test_suppress_logging_sets_none(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
caller="TestApp TestVendor",
)
assert d._logger is None
@patch("meraki.rest_session.check_python_version")
def test_simulate_mode_propagates(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
simulate=True,
caller="TestApp TestVendor",
)
assert d._session._simulate is True
@patch("meraki.rest_session.check_python_version")
def test_custom_base_url(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
base_url="https://api.meraki.cn/api/v1",
suppress_logging=True,
caller="TestApp TestVendor",
)
assert d._session._base_url == "https://api.meraki.cn/api/v1"
@patch("meraki.rest_session.check_python_version")
def test_maximum_retries_propagates(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
maximum_retries=10,
caller="TestApp TestVendor",
)
assert d._session._maximum_retries == 10
@patch("meraki.rest_session.check_python_version")
def test_use_iterator_propagates(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
use_iterator_for_get_pages=True,
caller="TestApp TestVendor",
)
assert d._session.use_iterator_for_get_pages is True
@patch("meraki.rest_session.check_python_version")
def test_caller_from_env(self, mock_check):
with patch.dict(os.environ, {"MERAKI_PYTHON_SDK_CALLER": "EnvApp EnvVendor"}):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
)
assert "EnvApp EnvVendor" in d._session._req_session.headers["User-Agent"]
@patch("meraki.rest_session.check_python_version")
def test_all_api_sections_initialized(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
caller="TestApp TestVendor",
)
assert d.organizations is not None
assert d.networks is not None
assert d.devices is not None
assert d.appliance is not None
assert d.wireless is not None
class TestDashboardAPILogging:
@patch("meraki.rest_session.check_python_version")
def test_inherit_logging_config(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=False,
inherit_logging_config=True,
caller="TestApp TestVendor",
)
assert d._logger is not None
assert d._logger.name == "meraki"
@patch("meraki.rest_session.check_python_version")
def test_output_log_with_print_console(self, mock_check, tmp_path):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=False,
inherit_logging_config=False,
output_log=True,
log_path=str(tmp_path),
log_file_prefix="test",
print_console=True,
caller="TestApp TestVendor",
)
assert d._logger is not None
assert d._logger.level == logging.DEBUG
assert hasattr(d, "_log_file")
assert "test_log__" in d._log_file
# Clean up handlers to avoid pollution
d._logger.handlers.clear()
@patch("meraki.rest_session.check_python_version")
def test_output_log_path_without_trailing_slash(self, mock_check, tmp_path):
log_path = str(tmp_path).rstrip("/").rstrip("\\")
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=False,
inherit_logging_config=False,
output_log=True,
log_path=log_path,
log_file_prefix="pfx",
print_console=False,
caller="TestApp TestVendor",
)
assert "/" in d._log_file or "\\" in d._log_file
assert "pfx_log__" in d._log_file
d._logger.handlers.clear()
@patch("meraki.rest_session.check_python_version")
def test_output_log_path_with_trailing_slash(self, mock_check, tmp_path):
log_path = str(tmp_path) + "/"
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=False,
inherit_logging_config=False,
output_log=True,
log_path=log_path,
log_file_prefix="pfx",
print_console=False,
caller="TestApp TestVendor",
)
assert "//" not in d._log_file.replace("\\", "/").replace("//", "/", 1)
d._logger.handlers.clear()
@patch("meraki.rest_session.check_python_version")
def test_no_output_log_with_print_console(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=False,
inherit_logging_config=False,
output_log=False,
print_console=True,
caller="TestApp TestVendor",
)
assert d._logger is not None
assert d._logger.level == logging.DEBUG
d._logger.handlers.clear()
@patch("meraki.rest_session.check_python_version")
def test_no_output_log_no_print_console(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=False,
inherit_logging_config=False,
output_log=False,
print_console=False,
caller="TestApp TestVendor",
)
assert d._logger is not None
d._logger.handlers.clear()