forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_backend_webagg.py
More file actions
80 lines (65 loc) · 2.81 KB
/
test_backend_webagg.py
File metadata and controls
80 lines (65 loc) · 2.81 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
import os
import sys
from unittest.mock import MagicMock
import pytest
import matplotlib.backends.backend_webagg_core
from matplotlib.backends.backend_webagg_core import (
FigureCanvasWebAggCore, NavigationToolbar2WebAgg,
)
from matplotlib.testing import subprocess_run_for_testing
@pytest.mark.parametrize("backend", ["webagg", "nbagg"])
def test_webagg_fallback(backend):
pytest.importorskip("tornado")
if backend == "nbagg":
pytest.importorskip("IPython")
env = dict(os.environ)
if sys.platform != "win32":
env["DISPLAY"] = ""
env["MPLBACKEND"] = backend
test_code = (
"import os;"
+ f"assert os.environ['MPLBACKEND'] == '{backend}';"
+ "import matplotlib.pyplot as plt; "
+ "print(plt.get_backend());"
f"assert '{backend}' == plt.get_backend().lower();"
)
subprocess_run_for_testing([sys.executable, "-c", test_code], env=env, check=True)
def test_webagg_core_no_toolbar():
fm = matplotlib.backends.backend_webagg_core.FigureManagerWebAgg
assert fm._toolbar2_class is None
def test_toolbar_button_dispatch_allowlist():
"""Only declared toolbar items should be dispatched."""
fig = MagicMock()
canvas = FigureCanvasWebAggCore(fig)
canvas.toolbar = MagicMock(spec=NavigationToolbar2WebAgg)
canvas.toolbar.toolitems = NavigationToolbar2WebAgg.toolitems
# Valid toolbar action should be dispatched.
canvas.handle_toolbar_button({'name': 'home'})
canvas.toolbar.home.assert_called_once()
# Invalid names should be silently ignored.
canvas.toolbar.reset_mock()
canvas.handle_toolbar_button({'name': '__init__'})
canvas.handle_toolbar_button({'name': 'not_a_real_button'})
# No methods should have been called.
assert canvas.toolbar.method_calls == []
@pytest.mark.parametrize("host, origin, allowed", [
("localhost:8988", "http://localhost:8988", True),
("localhost:8988", "http://evil.com", False),
("localhost:8988", "http://127.0.0.1:8988", False),
("localhost:8988", "http://[::1]:8988", False),
("127.0.0.1:8988", "http://127.0.0.1:8988", True),
("127.0.0.1:8988", "http://localhost:8988", False),
("127.0.0.1:8988", "http://[::1]:8988", False),
("[::1]:8988", "http://[::1]:8988", True),
("[::1]:8988", "http://[::2]:8988", False),
("[::1]:8988", "http://localhost:8988", False),
("[::1]:8988", "http://evil.com", False),
])
def test_websocket_rejects_cross_origin(host, origin, allowed):
"""Verify Tornado's default check_origin rejects cross-origin requests."""
pytest.importorskip("tornado")
from matplotlib.backends.backend_webagg import WebAggApplication
ws = WebAggApplication.WebSocket.__new__(WebAggApplication.WebSocket)
ws.request = MagicMock()
ws.request.headers = {"Host": host}
assert ws.check_origin(origin) is allowed