forked from meraki/dashboard-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dashboard_api_init.py
More file actions
498 lines (452 loc) · 20.4 KB
/
Copy pathtest_dashboard_api_init.py
File metadata and controls
498 lines (452 loc) · 20.4 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import logging
import os
from unittest.mock import MagicMock, patch
import pytest
import meraki
from meraki.exceptions import APIKeyError
class TestDashboardAPIInit:
@patch("meraki.session.base.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.session.base.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.session.base.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.session.base.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.session.base.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.session.base.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.session.base.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.session.base.check_python_version")
def test_use_iterator_default_false_propagates(self, mock_check):
# Regression for removed dead self-assignment
# `use_iterator_for_get_pages = use_iterator_for_get_pages`: param
# must still reach the session unchanged.
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
use_iterator_for_get_pages=False,
caller="TestApp TestVendor",
)
assert d._session.use_iterator_for_get_pages is False
@patch("meraki.session.base.check_python_version")
def test_inherit_logging_config_param_takes_effect(self, mock_check):
# Regression for removed dead self-assignment
# `inherit_logging_config = inherit_logging_config`: passing True must
# leave the logger at its inherited (non-DEBUG-forced) level.
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=False,
inherit_logging_config=True,
caller="TestApp TestVendor",
)
assert d._logger is not None
d._logger.handlers.clear()
@patch("meraki.session.base.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._client.headers["User-Agent"]
@patch("meraki.session.base.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
@patch("meraki.session.base.check_python_version")
def test_be_geo_id_from_env(self, mock_check):
with patch.dict(os.environ, {"BE_GEO_ID": "GeoApp V1"}):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
)
assert "GeoApp V1" in d._session._client.headers["User-Agent"]
@patch("meraki.session.base.check_python_version")
def test_batch_initialized(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
caller="TestApp TestVendor",
)
assert d.batch is not None
@patch("meraki.session.base.check_python_version")
def test_all_additional_sections(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
caller="TestApp TestVendor",
)
assert d.administered is not None
assert d.camera is not None
assert d.cellularGateway is not None
assert d.insight is not None
assert d.licensing is not None
assert d.sensor is not None
assert d.sm is not None
assert d.switch is not None
assert d.spaces is not None
assert d.wirelessController is not None
assert d.campusGateway is not None
class TestDashboardAPILogging:
@patch("meraki.session.base.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.session.base.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
d._logger.handlers.clear()
@patch("meraki.session.base.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.session.base.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.session.base.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.session.base.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()
class TestDashboardAPILoggingHandlers:
@patch("meraki.session.base.check_python_version")
def test_handlers_added_when_no_existing_handlers(self, mock_check, tmp_path):
with patch("meraki.__init__.logging.getLogger") as mock_get_logger:
mock_logger = MagicMock()
mock_logger.hasHandlers.return_value = False
mock_get_logger.return_value = mock_logger
meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=False,
inherit_logging_config=False,
output_log=True,
log_path=str(tmp_path),
log_file_prefix="t",
print_console=True,
caller="TestApp TestVendor",
)
assert mock_logger.addHandler.call_count == 2
@patch("meraki.session.base.check_python_version")
def test_no_handlers_added_when_already_has_handlers(self, mock_check, tmp_path):
with patch("meraki.__init__.logging.getLogger") as mock_get_logger:
mock_logger = MagicMock()
mock_logger.hasHandlers.return_value = True
mock_get_logger.return_value = mock_logger
meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=False,
inherit_logging_config=False,
output_log=True,
log_path=str(tmp_path),
log_file_prefix="t",
print_console=True,
caller="TestApp TestVendor",
)
assert mock_logger.addHandler.call_count == 0
@patch("meraki.session.base.check_python_version")
def test_console_only_handler_when_no_output_log(self, mock_check):
with patch("meraki.__init__.logging.getLogger") as mock_get_logger:
mock_logger = MagicMock()
mock_logger.hasHandlers.return_value = False
mock_get_logger.return_value = mock_logger
meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=False,
inherit_logging_config=False,
output_log=False,
print_console=True,
caller="TestApp TestVendor",
)
assert mock_logger.addHandler.call_count == 1
class TestDashboardAPISmartLimiting:
@patch("meraki.session.base.check_python_version")
def test_smart_flow_enabled_by_default(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
caller="TestApp TestVendor",
)
assert d._session._smart_flow is not None
@patch("meraki.session.base.check_python_version")
def test_smart_flow_disabled_explicitly(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
smart_flow_enabled=False,
caller="TestApp TestVendor",
)
assert d._session._smart_flow is None
@patch("meraki.session.base.check_python_version")
def test_smart_flow_creates_limiter(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
smart_flow_enabled=True,
smart_flow_cache_mode="lazy",
caller="TestApp TestVendor",
)
assert d._session._smart_flow is not None
@patch("meraki.session.base.check_python_version")
def test_smart_flow_with_cache_path(self, mock_check, tmp_path):
cache_file = str(tmp_path / "test_cache.json")
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
smart_flow_enabled=True,
smart_flow_cache_mode="lazy",
smart_flow_cache_path=cache_file,
caller="TestApp TestVendor",
)
assert d._session._smart_flow is not None
class TestDashboardAPIEagerLoad:
@patch("meraki.session.base.check_python_version")
def test_eager_load_populates_cache(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
smart_flow_enabled=True,
smart_flow_cache_mode="lazy",
caller="TestApp TestVendor",
)
mock_orgs = [{"id": "org_1"}, {"id": "org_2"}]
mock_networks = [{"id": "N_1"}, {"id": "N_2"}]
mock_devices = [{"serial": "QABC-1234-5678"}, {"serial": "QDEF-5678-9012"}]
with patch.object(d.organizations, "getOrganizations", return_value=mock_orgs):
with patch.object(d.organizations, "getOrganizationNetworks", return_value=mock_networks):
with patch.object(d.organizations, "getOrganizationInventoryDevices", return_value=mock_devices):
d._eager_load_rate_limit_cache()
limiter = d._session._smart_flow
assert limiter.resolve_org("/organizations/org_1/x") == "org_1"
assert limiter.resolve_org("/networks/N_1/x") is not None
assert limiter.resolve_org("/devices/QABC-1234-5678/x") is not None
@patch("meraki.session.base.check_python_version")
def test_eager_load_handles_org_failure(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
smart_flow_enabled=True,
smart_flow_cache_mode="lazy",
caller="TestApp TestVendor",
)
with patch.object(d.organizations, "getOrganizations", side_effect=Exception("API error")):
d._eager_load_rate_limit_cache()
@patch("meraki.session.base.check_python_version")
def test_eager_load_handles_network_failure(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
smart_flow_enabled=True,
smart_flow_cache_mode="lazy",
caller="TestApp TestVendor",
)
mock_orgs = [{"id": "org_1"}]
with patch.object(d.organizations, "getOrganizations", return_value=mock_orgs):
with patch.object(d.organizations, "getOrganizationNetworks", side_effect=Exception("net fail")):
with patch.object(d.organizations, "getOrganizationInventoryDevices", return_value=[]):
d._eager_load_rate_limit_cache()
assert d._session._smart_flow.resolve_org("/organizations/org_1/x") == "org_1"
@patch("meraki.session.base.check_python_version")
def test_eager_load_handles_device_failure(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
smart_flow_enabled=True,
smart_flow_cache_mode="lazy",
caller="TestApp TestVendor",
)
mock_orgs = [{"id": "org_1"}]
mock_networks = [{"id": "N_1"}]
with patch.object(d.organizations, "getOrganizations", return_value=mock_orgs):
with patch.object(d.organizations, "getOrganizationNetworks", return_value=mock_networks):
with patch.object(d.organizations, "getOrganizationInventoryDevices", side_effect=Exception("dev fail")):
d._eager_load_rate_limit_cache()
assert d._session._smart_flow.resolve_org("/networks/N_1/x") == "org_1"
@patch("meraki.session.base.check_python_version")
def test_eager_load_skips_devices_without_serial(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
smart_flow_enabled=True,
smart_flow_cache_mode="lazy",
caller="TestApp TestVendor",
)
mock_orgs = [{"id": "org_1"}]
mock_devices = [{"serial": "QABC-1234-5678"}, {"name": "no-serial-device"}]
with patch.object(d.organizations, "getOrganizations", return_value=mock_orgs):
with patch.object(d.organizations, "getOrganizationNetworks", return_value=[]):
with patch.object(d.organizations, "getOrganizationInventoryDevices", return_value=mock_devices):
d._eager_load_rate_limit_cache()
limiter = d._session._smart_flow
assert limiter.resolve_org("/devices/QABC-1234-5678/x") == "org_1"
@patch("meraki.session.base.check_python_version")
def test_eager_load_noop_without_limiter(self, mock_check):
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
smart_flow_enabled=False,
caller="TestApp TestVendor",
)
d._eager_load_rate_limit_cache()
@patch("meraki.session.base.check_python_version")
def test_eager_load_runs_during_init_when_cache_not_fresh(self, mock_check, tmp_path):
cache_file = str(tmp_path / "nonexistent_cache.json")
with patch("meraki.api.organizations.Organizations.getOrganizations", return_value=[]):
meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
smart_flow_enabled=True,
smart_flow_cache_mode="eager",
smart_flow_cache_path=cache_file,
caller="TestApp TestVendor",
)
@patch("meraki.session.base.check_python_version")
def test_eager_load_skipped_when_cache_fresh(self, mock_check, tmp_path):
import json
from datetime import datetime, timezone
cache_file = tmp_path / "cache.json"
cache_file.write_text(
json.dumps(
{
"saved_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"networks": [{"id": "N_cached", "organization": {"id": "org_cached"}}],
"devices": [],
}
)
)
d = meraki.DashboardAPI(
"test_key_1234567890123456789012345678901234567890",
suppress_logging=True,
smart_flow_enabled=True,
smart_flow_cache_mode="eager",
smart_flow_cache_path=str(cache_file),
caller="TestApp TestVendor",
)
assert d._session._smart_flow.resolve_org("/networks/N_cached/x") == "org_cached"