mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-20 10:34:30 +00:00
Update network tests, minor fixes to network.py.
Return UUID when adding a connection. Have get_connection raise an exception instead of returning None.
This commit is contained in:
parent
1055787be2
commit
b00a634647
@ -95,10 +95,14 @@ def get_connection(connection_uuid):
|
|||||||
"""
|
"""
|
||||||
client = nm.Client.new(None)
|
client = nm.Client.new(None)
|
||||||
try:
|
try:
|
||||||
return client.get_connection_by_uuid(connection_uuid)
|
connection = client.get_connection_by_uuid(connection_uuid)
|
||||||
|
if not connection:
|
||||||
|
raise ConnectionNotFound(connection_uuid)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise ConnectionNotFound(connection_uuid)
|
raise ConnectionNotFound(connection_uuid)
|
||||||
|
|
||||||
|
return connection
|
||||||
|
|
||||||
|
|
||||||
def get_active_connection(connection_uuid):
|
def get_active_connection(connection_uuid):
|
||||||
"""Returns active connection with matching uuid.
|
"""Returns active connection with matching uuid.
|
||||||
@ -170,11 +174,15 @@ def _update_ethernet_settings(connection, connection_uuid, name, zone,
|
|||||||
|
|
||||||
|
|
||||||
def add_ethernet_connection(name, zone, ipv4_method, ipv4_address):
|
def add_ethernet_connection(name, zone, ipv4_method, ipv4_address):
|
||||||
"""Add an automatic ethernet connection in network manager."""
|
"""Add an automatic ethernet connection in network manager.
|
||||||
|
Returns the UUID for the connection.
|
||||||
|
"""
|
||||||
|
connection_uuid = str(uuid.uuid4())
|
||||||
connection = _update_ethernet_settings(
|
connection = _update_ethernet_settings(
|
||||||
None, str(uuid.uuid4()), name, zone, ipv4_method, ipv4_address)
|
None, connection_uuid, name, zone, ipv4_method, ipv4_address)
|
||||||
client = nm.Client.new(None)
|
client = nm.Client.new(None)
|
||||||
client.add_connection_async(connection, True, None, _callback, None)
|
client.add_connection_async(connection, True, None, _callback, None)
|
||||||
|
return connection_uuid
|
||||||
|
|
||||||
|
|
||||||
def edit_ethernet_connection(connection, name, zone, ipv4_method,
|
def edit_ethernet_connection(connection, name, zone, ipv4_method,
|
||||||
@ -222,12 +230,16 @@ def _update_wifi_settings(connection, connection_uuid, name, zone, ssid, mode,
|
|||||||
|
|
||||||
def add_wifi_connection(name, zone, ssid, mode, auth_mode, passphrase,
|
def add_wifi_connection(name, zone, ssid, mode, auth_mode, passphrase,
|
||||||
ipv4_method, ipv4_address):
|
ipv4_method, ipv4_address):
|
||||||
"""Add an automatic Wi-Fi connection in network manager."""
|
"""Add an automatic Wi-Fi connection in network manager.
|
||||||
|
Return the UUID for the connection.
|
||||||
|
"""
|
||||||
|
connection_uuid = str(uuid.uuid4())
|
||||||
connection = _update_wifi_settings(
|
connection = _update_wifi_settings(
|
||||||
None, str(uuid.uuid4()), name, zone, ssid, mode, auth_mode, passphrase,
|
None, connection_uuid, name, zone, ssid, mode, auth_mode, passphrase,
|
||||||
ipv4_method, ipv4_address)
|
ipv4_method, ipv4_address)
|
||||||
client = nm.Client.new(None)
|
client = nm.Client.new(None)
|
||||||
client.add_connection_async(connection, True, None, _callback, None)
|
client.add_connection_async(connection, True, None, _callback, None)
|
||||||
|
return connection_uuid
|
||||||
|
|
||||||
|
|
||||||
def edit_wifi_connection(connection, name, zone,
|
def edit_wifi_connection(connection, name, zone,
|
||||||
|
|||||||
@ -33,15 +33,13 @@ class TestNetwork(unittest.TestCase):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUp(cls):
|
def setUp(cls):
|
||||||
connection = network.add_ethernet_connection(
|
cls.ethernet_uuid = network.add_ethernet_connection(
|
||||||
'plinth_test_eth', 'internal',
|
'plinth_test_eth', 'internal',
|
||||||
'auto', '')
|
'auto', '')
|
||||||
cls.ethernet_uuid = connection['connection']['uuid']
|
cls.wifi_uuid = network.add_wifi_connection(
|
||||||
connection = network.add_wifi_connection(
|
|
||||||
'plinth_test_wifi', 'external',
|
'plinth_test_wifi', 'external',
|
||||||
'plinthtestwifi', 'adhoc', 'open', '',
|
'plinthtestwifi', 'adhoc', 'open', '',
|
||||||
'auto', '')
|
'auto', '')
|
||||||
cls.wifi_uuid = connection['connection']['uuid']
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def tearDown(cls):
|
def tearDown(cls):
|
||||||
@ -61,11 +59,11 @@ class TestNetwork(unittest.TestCase):
|
|||||||
"""Check that we can get a connection by name."""
|
"""Check that we can get a connection by name."""
|
||||||
connection = network.get_connection(self.ethernet_uuid)
|
connection = network.get_connection(self.ethernet_uuid)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
connection.GetSettings()['connection']['id'], 'plinth_test_eth')
|
connection.get_id(), 'plinth_test_eth')
|
||||||
|
|
||||||
connection = network.get_connection(self.wifi_uuid)
|
connection = network.get_connection(self.wifi_uuid)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
connection.GetSettings()['connection']['id'], 'plinth_test_wifi')
|
connection.get_id(), 'plinth_test_wifi')
|
||||||
|
|
||||||
self.assertRaises(network.ConnectionNotFound, network.get_connection,
|
self.assertRaises(network.ConnectionNotFound, network.get_connection,
|
||||||
'x-invalid-network-id')
|
'x-invalid-network-id')
|
||||||
@ -79,12 +77,16 @@ class TestNetwork(unittest.TestCase):
|
|||||||
'169.254.0.1')
|
'169.254.0.1')
|
||||||
|
|
||||||
connection = network.get_connection(self.ethernet_uuid)
|
connection = network.get_connection(self.ethernet_uuid)
|
||||||
settings = connection.GetSettings()
|
self.assertEqual(connection.get_id(), 'plinth_test_eth_new')
|
||||||
self.assertEqual(settings['connection']['id'], 'plinth_test_eth_new')
|
|
||||||
self.assertEqual(settings['connection']['zone'], 'external')
|
settings_connection = connection.get_setting_connection()
|
||||||
self.assertEqual(settings['ipv4']['method'], 'manual')
|
self.assertEqual(settings_connection.get_zone(), 'external')
|
||||||
self.assertEqual(settings['ipv4']['addresses'],
|
|
||||||
[['169.254.0.1', 24, '0.0.0.0']])
|
settings_ipv4 = connection.get_setting_ip4_config()
|
||||||
|
self.assertEqual(settings_ipv4.get_method(), 'manual')
|
||||||
|
|
||||||
|
address = settings_ipv4.get_address(0)
|
||||||
|
self.assertEqual(address.get_address(), '169.254.0.1')
|
||||||
|
|
||||||
@unittest.skipUnless(euid == 0, 'Needs to be root')
|
@unittest.skipUnless(euid == 0, 'Needs to be root')
|
||||||
def test_edit_wifi_connection(self):
|
def test_edit_wifi_connection(self):
|
||||||
@ -96,16 +98,23 @@ class TestNetwork(unittest.TestCase):
|
|||||||
'auto', '')
|
'auto', '')
|
||||||
|
|
||||||
connection = network.get_connection(self.wifi_uuid)
|
connection = network.get_connection(self.wifi_uuid)
|
||||||
settings = connection.GetSettings()
|
|
||||||
self.assertEqual(settings['connection']['id'], 'plinth_test_wifi_new')
|
self.assertEqual(connection.get_id(), 'plinth_test_wifi_new')
|
||||||
self.assertEqual(settings['connection']['zone'], 'external')
|
|
||||||
self.assertEqual(settings['802-11-wireless']['ssid'],
|
settings_connection = connection.get_setting_connection()
|
||||||
'plinthtestwifi2')
|
self.assertEqual(settings_connection.get_zone(), 'external')
|
||||||
self.assertEqual(settings['802-11-wireless']['mode'], 'infrastructure')
|
|
||||||
self.assertEqual(settings['802-11-wireless-security']['key-mgmt'],
|
settings_wireless = connection.get_setting_wireless()
|
||||||
'wpa-psk')
|
self.assertEqual(settings_wireless.get_ssid().get_data(),
|
||||||
|
b'plinthtestwifi2')
|
||||||
|
self.assertEqual(settings_wireless.get_mode(), 'infrastructure')
|
||||||
|
|
||||||
|
wifi_sec = connection.get_setting_wireless_security()
|
||||||
|
self.assertEqual(wifi_sec.get_key_mgmt(), 'wpa-psk')
|
||||||
|
|
||||||
|
secrets = connection.get_secrets('802-11-wireless-security')
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
connection.GetSecrets()['802-11-wireless-security']['psk'],
|
secrets['802-11-wireless-security']['psk'],
|
||||||
'secretpassword')
|
'secretpassword')
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user