28 lines
1.1 KiB
Python
28 lines
1.1 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' / 'modem_control.py'
|
|
spec = importlib.util.spec_from_file_location('modem_control', path)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
|
|
|
|
class ModemControlTests(TestCase):
|
|
def test_rejects_invalid_ids(self):
|
|
for value in ('-1', '1;touch /tmp/foo', '../../etc/passwd', '999999'):
|
|
self.assertFalse(module.control(value, 'enable')['ok'])
|
|
|
|
def test_rejects_arbitrary_actions(self):
|
|
self.assertFalse(module.control('0', 'reset')['ok'])
|
|
|
|
@patch.object(module.shutil, 'which', return_value='/usr/bin/mmcli')
|
|
def test_uses_argument_vector(self, _):
|
|
calls = []
|
|
def runner(args, **kwargs):
|
|
calls.append(args)
|
|
return type('Result', (), {'returncode': 0, 'stderr': ''})()
|
|
self.assertTrue(module.control('7', 'disable', runner)['ok'])
|
|
self.assertEqual(calls, [['mmcli', '-m', '7', '--disable']])
|