diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9c171636..b5030e1e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -32,7 +32,7 @@ jobs: brew install openssl readline xz borgbackup - name: Install Vorta run: | - pip install . + pip install -e . pip install -r requirements.d/dev.txt # - name: Setup tmate session # uses: mxschmitt/action-tmate@v1 @@ -40,11 +40,11 @@ jobs: if: runner.os == 'Linux' run: | xvfb-run --server-args="-screen 0 1024x768x24+32" \ - -a dbus-run-session -- coverage run -m pytest + -a dbus-run-session -- pytest --cov=vorta - name: Test with pytest (macOS) if: runner.os == 'macOS' run: | - coverage run -m pytest + pytest --cov=vorta - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 env: diff --git a/requirements.d/dev.txt b/requirements.d/dev.txt index e3be1394..72ff7001 100644 --- a/requirements.d/dev.txt +++ b/requirements.d/dev.txt @@ -3,6 +3,7 @@ flake8 pyinstaller pylint pytest +pytest-cov pytest-faulthandler pytest-mock pytest-qt diff --git a/src/vorta/network_status/abc.py b/src/vorta/network_status/abc.py index 453a7d8d..a96c1c28 100644 --- a/src/vorta/network_status/abc.py +++ b/src/vorta/network_status/abc.py @@ -28,7 +28,7 @@ def get_current_wifi(self) -> Optional[str]: """Get current SSID or None if Wifi is off.""" raise NotImplementedError() - def get_known_wifis(self) -> Optional[List['SystemWifiInfo']]: + def get_known_wifis(self) -> List['SystemWifiInfo']: """Get WiFi networks known to system.""" raise NotImplementedError() @@ -50,5 +50,5 @@ def is_network_metered(self) -> bool: def get_current_wifi(self) -> Optional[str]: pass - def get_known_wifis(self) -> Optional[List['SystemWifiInfo']]: - pass + def get_known_wifis(self) -> List['SystemWifiInfo']: + return [] diff --git a/src/vorta/network_status/darwin.py b/src/vorta/network_status/darwin.py index 09fdcf36..070095d3 100644 --- a/src/vorta/network_status/darwin.py +++ b/src/vorta/network_status/darwin.py @@ -1,9 +1,6 @@ -import plistlib import subprocess -import xml from typing import Optional, Iterator - -from peewee import format_date_time +from datetime import datetime as dt from vorta.log import logger from vorta.network_status.abc import NetworkStatusMonitor, SystemWifiInfo @@ -29,30 +26,16 @@ def get_current_wifi(self) -> Optional[str]: return split_line[1].strip() def get_known_wifis(self): - from vorta.models import WifiSettingModel - plist_path = '/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist' + """ + Listing all known Wifi networks isn't possible any more from macOS 11. Instead we + just return the current Wifi. + """ + wifis = [] + current_wifi = self.get_current_wifi() + if current_wifi is not None: + wifis.append(SystemWifiInfo(ssid=current_wifi, last_connected=dt.now())) - try: - plist_file = open(plist_path, 'rb') - wifis = plistlib.load(plist_file).get('KnownNetworks') - except xml.parsers.expat.ExpatError: - logger.error('Unable to parse list of Wifi networks.') - return None - - result = [] - if wifis is not None: - for wifi in wifis.values(): - ssid = wifi.get('SSIDString', None) - if ssid is None: - continue - - last_connected = wifi.get('LastConnected', None) or wifi.get('LastAutoJoinAt', None) - if isinstance(last_connected, str): # TODO: Maybe not needed any more? - last_connected = format_date_time(last_connected, WifiSettingModel.last_connected.formats) - - result.append(SystemWifiInfo(ssid=ssid, last_connected=last_connected)) - - return result + return wifis def get_network_devices() -> Iterator[str]: diff --git a/src/vorta/network_status/network_manager.py b/src/vorta/network_status/network_manager.py index 2a263c1e..663ae338 100644 --- a/src/vorta/network_status/network_manager.py +++ b/src/vorta/network_status/network_manager.py @@ -39,14 +39,14 @@ def get_current_wifi(self) -> Optional[str]: logger.exception("Failed to get currently connected WiFi network, assuming none") return None - def get_known_wifis(self) -> Optional[List[SystemWifiInfo]]: + def get_known_wifis(self) -> List[SystemWifiInfo]: + wifis = [] try: connections_paths = self._nm.get_connections_paths() except DBusException: logger.exception("Failed to list connections") - return None + return wifis - wifis = [] for connection_path in connections_paths: try: settings = self._nm.get_settings(connection_path) diff --git a/src/vorta/utils.py b/src/vorta/utils.py index d17c49eb..0ff7c24f 100644 --- a/src/vorta/utils.py +++ b/src/vorta/utils.py @@ -199,33 +199,31 @@ def get_asset(path): def get_sorted_wifis(profile): - """Get SSIDs from OS and merge with settings in DB.""" + """ + Get Wifi networks known to the OS (only current one on macOS) and + merge with networks from other profiles. Update last connected time. + """ from vorta.models import WifiSettingModel + # Pull networks known to OS and all other backup profiles system_wifis = get_network_status_monitor().get_known_wifis() - if system_wifis is None: - # Don't show any networks if we can't get the current list - return [] + from_other_profiles = WifiSettingModel.select() \ + .where(WifiSettingModel.profile != profile.id).execute() - for wifi in system_wifis: + for wifi in list(from_other_profiles) + system_wifis: db_wifi, created = WifiSettingModel.get_or_create( ssid=wifi.ssid, profile=profile.id, defaults={'last_connected': wifi.last_connected, 'allowed': True} ) - # update last connected time + # Update last connected time if not created and db_wifi.last_connected != wifi.last_connected: db_wifi.last_connected = wifi.last_connected db_wifi.save() - # remove Wifis that were deleted in the system. - deleted_wifis = WifiSettingModel.select() \ - .where(WifiSettingModel.ssid.not_in([wifi.ssid for wifi in system_wifis])) - for wifi in deleted_wifis: - wifi.delete_instance() - + # Finally return list of networks and settings for that profile return WifiSettingModel.select() \ .where(WifiSettingModel.profile == profile.id).order_by(-WifiSettingModel.last_connected)