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
|
import sys
from datetime import datetime
from unittest.mock import MagicMock
import pytest
pytestmark = pytest.mark.skipif(sys.platform != 'linux', reason="Linux only")
from vorta.network_status.abc import SystemWifiInfo # noqa: E402
from vorta.network_status.network_manager import ( # noqa: E402
ActiveConnectionInfo,
DBusException,
NetworkManagerDBusAdapter,
NetworkManagerMonitor,
NMMetered,
decode_ssid,
)
@pytest.fixture
def mock_adapter():
return MagicMock(spec_set=NetworkManagerDBusAdapter, wraps=UncallableNetworkManagerDBusAdapter())
@pytest.fixture
def nm_monitor(mock_adapter):
return NetworkManagerMonitor(nm_adapter=mock_adapter)
def test_is_network_status_available(nm_monitor):
assert nm_monitor.is_network_status_available() is True
@pytest.mark.parametrize(
'global_metered_status, expected',
[
(NMMetered.UNKNOWN, False),
(NMMetered.YES, True),
(NMMetered.NO, False),
(NMMetered.GUESS_YES, True),
(NMMetered.GUESS_NO, False),
],
)
def test_is_network_metered(global_metered_status, expected, nm_monitor):
nm_monitor._nm.get_global_metered_status.return_value = global_metered_status
result = nm_monitor.is_network_metered()
assert result == expected
@pytest.mark.parametrize(
'connection_path, connection_type, type_settings, expected',
[
(
'/org/freedesktop/NetworkManager/ActiveConnection/1',
'802-11-wireless',
{'ssid': bytes([84, 69, 83, 84])},
'TEST',
),
('/org/freedesktop/NetworkManager/ActiveConnection/2', '802-11-ethernet', {}, None),
],
)
def test_get_current_wifi(connection_path, connection_type, type_settings, expected, nm_monitor):
nm_monitor._nm.get_primary_connection_path.return_value = connection_path
nm_monitor._nm.get_active_connection_info.return_value = ActiveConnectionInfo(
connection='/org/freedesktop/NetworkManager/Settings/12', type=connection_type
)
nm_monitor._nm.get_settings.side_effect = [{connection_type: type_settings}]
result = nm_monitor.get_current_wifi()
assert result == expected
def test_get_current_wifi_with_no_connection(nm_monitor):
nm_monitor._nm.get_primary_connection_path.return_value = None
assert nm_monitor.get_current_wifi() is None
def test_get_known_wifis(nm_monitor):
nm_monitor._nm.get_connections_paths.return_value = ['/org/freedesktop/NetworkManager/Settings/12']
nm_monitor._nm.get_settings.return_value = {
'connection': {'timestamp': 1597303736},
'802-11-wireless': {'ssid': [84, 69, 83, 84]},
}
result = nm_monitor.get_known_wifis()
assert result == [
SystemWifiInfo(
ssid='TEST',
last_connected=datetime(2020, 8, 13, 7, 28, 56),
)
]
def test_get_known_wifis_with_never_used_connection(nm_monitor):
nm_monitor._nm.get_connections_paths.return_value = ['/org/freedesktop/NetworkManager/Settings/12']
nm_monitor._nm.get_settings.return_value = {
'connection': {},
'802-11-wireless': {'ssid': [84, 69, 83, 84]},
}
result = nm_monitor.get_known_wifis()
assert result == [
SystemWifiInfo(
ssid='TEST',
last_connected=None,
)
]
def test_get_known_wifis_partial_failure(nm_monitor):
nm_monitor._nm.get_connections_paths.return_value = [
'/org/freedesktop/NetworkManager/Settings/12',
'/org/freedesktop/NetworkManager/Settings/42',
]
nm_monitor._nm.get_settings.side_effect = [
DBusException("Test"),
{
'connection': {},
'802-11-wireless': {'ssid': [84, 69, 83, 84]},
},
]
result = nm_monitor.get_known_wifis()
assert result == [
SystemWifiInfo(
ssid='TEST',
last_connected=None,
)
]
def test_get_known_wifis_with_no_wifi_connections(nm_monitor):
nm_monitor._nm.get_connections_paths.return_value = ['/org/freedesktop/NetworkManager/Settings/12']
nm_monitor._nm.get_settings.return_value = {
'connection': {},
'802-11-ethernet': {},
}
result = nm_monitor.get_known_wifis()
assert result == []
@pytest.mark.parametrize(
'ssid_bytes, expected',
[
([84, 69, 83, 84], 'TEST'),
([240, 159, 150, 150], '🖖'),
([0, 1, 2, 10, 34, 39], '\\x00\\x01\\x02\\n"\''),
],
)
def test_decode_ssid(ssid_bytes, expected):
result = decode_ssid(ssid_bytes)
assert result == expected
class UncallableNetworkManagerDBusAdapter(NetworkManagerDBusAdapter):
def __init__(self):
# Skip parent setup, this way none of the DBus calls can happen in tests
super(NetworkManagerDBusAdapter, self).__init__(parent=None)
|