40 lines
1.9 KiB
Python
40 lines
1.9 KiB
Python
import importlib.util
|
|
from pathlib import Path
|
|
from unittest import TestCase
|
|
from unittest.mock import patch
|
|
|
|
path = Path(__file__).resolve().parents[1] / 'backend' / 'mobile_connections.py'
|
|
spec = importlib.util.spec_from_file_location('mobile_connections', path)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
|
|
UID = '12345678-1234-1234-1234-123456789abc'
|
|
|
|
class MobileConnectionsTests(TestCase):
|
|
@patch.object(module.shutil, 'which', return_value='/usr/bin/nmcli')
|
|
def test_create_argument_vector(self, _):
|
|
calls = []
|
|
def runner(args, **kwargs):
|
|
calls.append(args)
|
|
return type('R', (), {'returncode': 0, 'stdout': '', 'stderr': ''})()
|
|
self.assertTrue(module.manage('create', 'Mobile internet', 'internet', runner)['ok'])
|
|
self.assertEqual(calls[0][-9:], ['connection', 'add', 'type', 'gsm', 'con-name', 'Mobile internet', 'apn', 'internet', 'connection.autoconnect', 'no'][-9:])
|
|
|
|
@patch.object(module.shutil, 'which', return_value='/usr/bin/nmcli')
|
|
def test_rejects_injection(self, _):
|
|
for apn in ('bad;echo', '--help', 'foo bar', ''):
|
|
self.assertFalse(module.manage('create', 'internet', apn)['ok'])
|
|
self.assertFalse(module.manage('delete', '--all')['ok'])
|
|
|
|
@patch.object(module.shutil, 'which', return_value='/usr/bin/nmcli')
|
|
def test_only_mobile_uuid_allowed(self, _):
|
|
def runner(args, **kwargs):
|
|
return type('R', (), {'returncode': 0, 'stdout': UID + ':802-3-ethernet:Wired\n', 'stderr': ''})()
|
|
self.assertFalse(module.manage('delete', UID, runner=runner)['ok'])
|
|
|
|
@patch.object(module.shutil, 'which', return_value='/usr/bin/nmcli')
|
|
def test_lists_mobile_only(self, _):
|
|
def runner(args, **kwargs):
|
|
return type('R', (), {'returncode': 0, 'stdout': UID + ':gsm:Mobile\n' + UID + ':wifi:Wifi\n', 'stderr': ''})()
|
|
self.assertEqual(len(module.list_profiles(runner)['profiles']), 1)
|