Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: settings to allow new networks and show notif when a network is disallowed #1658

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/vorta/assets/UI/scheduletab.ui
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@
<property name="verticalSpacing">
<number>12</number>
</property>
<item row="1" column="0">
<item row="2" column="0">
<layout class="QVBoxLayout" name="verticalLayout_8">
<property name="spacing">
<number>4</number>
Expand All @@ -557,6 +557,13 @@
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="allowNewNetworksCheckBox">
<property name="text">
<string>Allow new networks by default</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="meteredNetworksCheckBox">
<property name="text">
Expand Down
5 changes: 5 additions & 0 deletions src/vorta/store/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ def run_migrations(current_schema, db_connection):
'name',
pw.CharField(default=''),
),
migrator.add_column(
BackupProfileModel._meta.table_name,
'allow_new_networks',
pw.BooleanField(default=True),
),
)


Expand Down
1 change: 1 addition & 0 deletions src/vorta/store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class BackupProfileModel(BaseModel):
pre_backup_cmd = pw.CharField(default='')
post_backup_cmd = pw.CharField(default='')
dont_run_on_metered_networks = pw.BooleanField(default=True)
allow_new_networks = pw.BooleanField(default=False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it defaults to False, but in migrations it's True? Suggest to use True everywhere, which is the current behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think yf and I discussed this before (been long so idr exactly where, I tried searching). The new behaviour should not surprise existing users, so the migration changes nothing. But for new users, having this value as false is more beneficial.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

found it - #1658 (comment)


def refresh(self):
return type(self).get(self._pk_expr())
Expand Down
3 changes: 2 additions & 1 deletion src/vorta/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,13 @@ def get_sorted_wifis(profile):
# Pull networks known to OS and all other backup profiles
system_wifis = get_network_status_monitor().get_known_wifis()
from_other_profiles = WifiSettingModel.select().where(WifiSettingModel.profile != profile.id).execute()
allow_new_networks = profile.allow_new_networks

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},
defaults={'last_connected': wifi.last_connected, 'allowed': allow_new_networks},
)

# Update last connected time
Expand Down
4 changes: 4 additions & 0 deletions src/vorta/views/schedule_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def __init__(self, parent=None):
self.meteredNetworksCheckBox.stateChanged.connect(
lambda new_val, attr='dont_run_on_metered_networks': self.save_profile_attr(attr, not new_val)
)
self.allowNewNetworksCheckBox.stateChanged.connect(
lambda new_val, attr='allow_new_networks': self.save_profile_attr(attr, new_val)
)
self.postBackupCmdLineEdit.textEdited.connect(
lambda new_val, attr='post_backup_cmd': self.save_profile_attr(attr, new_val)
)
Expand Down Expand Up @@ -161,6 +164,7 @@ def populate_from_profile(self):
self.missedBackupsCheckBox.setCheckState(
QtCore.Qt.CheckState.Checked if profile.schedule_make_up_missed else QtCore.Qt.CheckState.Unchecked
)
self.allowNewNetworksCheckBox.setChecked(profile.allow_new_networks)
self.meteredNetworksCheckBox.setChecked(False if profile.dont_run_on_metered_networks else True)

self.preBackupCmdLineEdit.setText(profile.pre_backup_cmd)
Expand Down
105 changes: 105 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
import os
import sys
from datetime import datetime as dt
from unittest.mock import MagicMock

import pytest
import vorta
import vorta.application
import vorta.borg.jobs_manager
from peewee import SqliteDatabase
from vorta.store.models import (
ArchiveModel,
BackupProfileModel,
EventLogModel,
RepoModel,
RepoPassword,
SchemaVersion,
SettingsModel,
SourceFileModel,
WifiSettingModel,
)
from vorta.views.main_window import MainWindow

models = [
RepoModel,
RepoPassword,
BackupProfileModel,
SourceFileModel,
SettingsModel,
ArchiveModel,
WifiSettingModel,
EventLogModel,
SchemaVersion,
]


def pytest_configure(config):
Expand All @@ -29,8 +55,87 @@ def qapp(tmpdir_factory):

from vorta.application import VortaApp

VortaApp.set_borg_details_action = MagicMock() # Can't use pytest-mock in session scope
VortaApp.scheduler = MagicMock()

qapp = VortaApp([]) # Only init QApplication once to avoid segfaults while testing.

yield qapp
mock_db.close()
qapp.quit()


@pytest.fixture(scope='function', autouse=True)
def init_db(qapp, qtbot, tmpdir_factory):
tmp_db = tmpdir_factory.mktemp('Vorta').join('settings.sqlite')
mock_db = SqliteDatabase(
str(tmp_db),
pragmas={
'journal_mode': 'wal',
},
)
vorta.store.connection.init_db(mock_db)

default_profile = BackupProfileModel(name='Default')
default_profile.save()

new_repo = RepoModel(url='i0fi93@i593.repo.borgbase.com:repo')
new_repo.encryption = 'none'
new_repo.save()

default_profile.repo = new_repo.id
default_profile.dont_run_on_metered_networks = False
default_profile.allow_new_networks = True
default_profile.validation_on = False
default_profile.save()

test_archive = ArchiveModel(snapshot_id='99999', name='test-archive', time=dt(2000, 1, 1, 0, 0), repo=1)
test_archive.save()

test_archive1 = ArchiveModel(snapshot_id='99998', name='test-archive1', time=dt(2000, 1, 1, 0, 0), repo=1)
test_archive1.save()

source_dir = SourceFileModel(dir='/tmp/another', repo=new_repo, dir_size=100, dir_files_count=18, path_isdir=True)
source_dir.save()

qapp.main_window.deleteLater()
del qapp.main_window
qapp.main_window = MainWindow(qapp) # Re-open main window to apply mock data in UI

yield

qapp.jobs_manager.cancel_all_jobs()
qapp.backup_finished_event.disconnect()
qapp.scheduler.schedule_changed.disconnect()
qtbot.waitUntil(lambda: not qapp.jobs_manager.is_worker_running(), **pytest._wait_defaults)
mock_db.close()


@pytest.fixture
def choose_file_dialog(*args):
class MockFileDialog:
def __init__(self, *args, **kwargs):
pass

def open(self, func):
func()

def selectedFiles(self):
return ['/tmp']

return MockFileDialog


@pytest.fixture
def borg_json_output():
def _read_json(subcommand):
stdout = open(f'tests/borg_json_output/{subcommand}_stdout.json')
stderr = open(f'tests/borg_json_output/{subcommand}_stderr.json')
return stdout, stderr

return _read_json


@pytest.fixture
def rootdir():
return os.path.dirname(os.path.abspath(__file__))
1 change: 1 addition & 0 deletions tests/unit/profile_exports/valid.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"password": "Tr0ub4dor&3",
"post_backup_cmd": "",
"dont_run_on_metered_networks": true,
"allow_new_networks": true,
"SourceFileModel": [
{
"dir": "/this/is/a/test/file",
Expand Down
Loading