From 81c8a802ac1a234ee3ba05917ab88bb4f422c028 Mon Sep 17 00:00:00 2001 From: Samuel Smoker Date: Tue, 28 Feb 2017 19:39:24 -0800 Subject: Fix and test address methods --- syncthingmanager/__init__.py | 16 ++++++++++++---- syncthingmanager/tests/test_stman.py | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/syncthingmanager/__init__.py b/syncthingmanager/__init__.py index da8e219..0973446 100644 --- a/syncthingmanager/__init__.py +++ b/syncthingmanager/__init__.py @@ -193,15 +193,23 @@ class SyncthingManager(Syncthing): address(str): a tcp://address to add. """ info = self.device_info(devicestr) - base_addresses = self.system.config()['devices'][info['index']]['addresses'] - self.edit_device(devicestr, addresses, base_addresses.append(address)) + try: + addresses = self.system.config()['devices'][info['index']]['addresses'] + except TypeError: + raise SyncthingManagerError('Device not configured: ' + devicestr) + addresses.append(address) + self.edit_device(devicestr, 'addresses', addresses) def device_remove_address(self, devicestr, address): """The inverse of device_add_address.""" info = self.device_info(devicestr) - base_addresses = self.system.config()['devices'][info['index']]['addresses'] try: - self.edit_device(devicestr, 'addresses', base_addresses.remove('address')) + addresses = self.system.config()['devices'][info['index']]['addresses'] + except TypeError: + raise SyncthingManagerError('Device not configured: ' + devicestr) + try: + addresses.remove(address) + self.edit_device(devicestr, 'addresses', addresses) except ValueError: pass diff --git a/syncthingmanager/tests/test_stman.py b/syncthingmanager/tests/test_stman.py index 6d6d996..b2139f1 100644 --- a/syncthingmanager/tests/test_stman.py +++ b/syncthingmanager/tests/test_stman.py @@ -50,6 +50,24 @@ def test_edit_device(s): assert b['compression'] == 'always' assert b['addresses'] == address +def test_device_add_address(s): + cfg = s.system.config() + a = next(filter(lambda x: x['name'] == 'SyncthingManagerTestDevice1', cfg['devices'])) + s.device_add_address('SyncthingManagerTestDevice1', 'tcp://127.0.0.2:8384') + cfg = s.system.config() + b = next(filter(lambda x: x['name'] == 'SyncthingManagerTestDevice1', cfg['devices'])) + assert 'tcp://127.0.0.2:8384' not in a['addresses'] + assert 'tcp://127.0.0.2:8384' in b['addresses'] + +def test_device_remove_address(s): + cfg = s.system.config() + a = next(filter(lambda x: x['name'] == 'SyncthingManagerTestDevice1', cfg['devices'])) + s.device_remove_address('SyncthingManagerTestDevice1', 'localhost') + cfg = s.system.config() + b = next(filter(lambda x: x['name'] == 'SyncthingManagerTestDevice1', cfg['devices'])) + assert 'localhost' in a['addresses'] + assert 'localhost' not in b['addresses'] + def test_device_change_name(s): cfg = s.system.config() a = next(filter(lambda x: x['name'] == 'SyncthingManagerTestDevice1', cfg['devices'])) -- cgit v1.2.1