Ejemplo n.º 1
0
    def test_create(self, validate_create, device_dao_create,
                    notifier_created):
        expected_device = {
            'ip': '10.9.0.5',
            'mac': '00:11:22:33:44:55',
            'template_id': 'abcd1234',
            'plugin': 'superduperplugin',
            'vendor': 'Aastra',
            'model': '6531i',
        }
        device = Device(**expected_device)

        device_dao_create.return_value = Device(**expected_device)

        result = device_services.create(device)

        validate_create.assert_called_with(device)
        device_dao_create.assert_called_once_with(device)
        notifier_created.assert_called_once_with(result)
        self.assertEquals(result.mac, expected_device['mac'])
        self.assertEquals(result.ip, expected_device['ip'])
        self.assertEquals(result.template_id, expected_device['template_id'])
        self.assertEquals(result.plugin, expected_device['plugin'])
        self.assertEquals(result.vendor, expected_device['vendor'])
        self.assertEquals(result.model, expected_device['model'])
Ejemplo n.º 2
0
    def test_create_empty_device(self, notifier_created, device_dao_create):
        device = Device()
        created_device = Device(id='abcd')

        device_dao_create.return_value = created_device

        result = device_services.create(device)

        self.assertEquals(result.id, created_device.id)
        device_dao_create.assert_called_once_with(device)
        notifier_created.assert_called_once_with(result)
Ejemplo n.º 3
0
    def test_to_source_with_parameters(self):
        device_id = 'abcd1234'
        device_mac = 'AB:11:22:33:44:55'
        expected_mac = 'ab:11:22:33:44:55'

        device = Device(id=device_id,
                        ip='10.0.0.1',
                        mac=device_mac,
                        plugin='xivo-aastra-3.2.2-SP3',
                        vendor='Aastra',
                        model='6731i')

        expected_provd_device = {
            'id': device_id,
            'ip': device.ip,
            'mac': expected_mac,
            'plugin': device.plugin,
            'vendor': device.vendor,
            'model': device.model,
        }

        expected_provd_config = {
            'configdevice': 'defaultconfigdevice',
            'deletable': True,
            'id': device_id,
            'parent_ids': ['base', 'defaultconfigdevice'],
            'raw_config': {}
        }

        provd_device, provd_config = provd_converter.to_source(device)
        assert_that(provd_device, equal_to(expected_provd_device))
        assert_that(provd_config, equal_to(expected_provd_config))
Ejemplo n.º 4
0
    def test_build_edit_with_new_properties_and_no_config(self):
        device_id = 'abc123'

        device = Device(id=device_id,
                        ip='10.0.0.1',
                        mac='00:11:22:33:44:55',
                        plugin='aastraplugin',
                        model='6735i',
                        vendor='Aastra',
                        version='0.0.1')

        provd_device = {'configured': False, 'id': device_id}

        expected_provd_device = {
            'configured': False,
            'id': device_id,
            'ip': '10.0.0.1',
            'mac': '00:11:22:33:44:55',
            'plugin': 'aastraplugin',
            'model': '6735i',
            'vendor': 'Aastra',
            'version': '0.0.1'
        }

        result_device, result_config = provd_converter.build_edit(
            device, provd_device, None)

        assert_that(result_device, equal_to(expected_provd_device))
Ejemplo n.º 5
0
    def test_autoprov_with_error(self, device_manager, config_manager):
        device = Device(id=self.device_id)

        device_manager().update.side_effect = Exception('')

        self.assertRaises(ProvdError, device_services.reset_to_autoprov,
                          device)
Ejemplo n.º 6
0
    def test_edit_with_a_device_associated(self,
                                           line_dao_edit,
                                           line_notifier_edited,
                                           device_dao_find,
                                           device_services_rebuild_device_config):
        name = 'line'
        context = 'toto'
        secret = '1234'
        device_id = '2'
        device = Device(id=device_id)
        line = LineSIP(name=name,
                       context=context,
                       username=name,
                       secret=secret,
                       device_id=device_id,
                       device_slot=1)

        device_dao_find.return_value = device

        line_services.edit(line)

        line_dao_edit.assert_called_once_with(line)
        line_notifier_edited.assert_called_once_with(line)
        device_dao_find.assert_called_once_with(device_id)
        device_services_rebuild_device_config.assert_called_once_with(device)
Ejemplo n.º 7
0
    def test_remove_line_from_device(self, config_manager):
        config_dict = {
            "raw_config": {
                "protocol": "sip",
                "sip_lines": {
                    "1": {
                        "username": "******"
                    },
                    "2": {
                        "username": "******"
                    }
                }
            }
        }
        config_manager().get.return_value = config_dict
        line = LineSIP(device_slot=2)

        device = Device(id=self.device_id)

        expected_arg = {
            "raw_config": {
                "protocol": "sip",
                "sip_lines": {
                    "1": {
                        "username": "******"
                    }
                }
            }
        }

        device_services.remove_line_from_device(device, line)

        config_manager().get.assert_called_with(self.device_id)
        config_manager().update.assert_called_with(expected_arg)
        self.assertEquals(0, config_manager().autocreate.call_count)
Ejemplo n.º 8
0
    def test_remove_line_from_device_no_funckeys(self, device_manager,
                                                 config_manager, reset_config,
                                                 reset_to_autoprov):
        autoprovid = "autoprov1234"
        config_dict = {
            "raw_config": {
                "protocol": "sip",
                "sip_lines": {
                    "1": {
                        "username": "******"
                    }
                }
            }
        }
        device_dict = {
            "ip": "10.60.0.109",
            "version": "3.2.2.1136",
            "config": self.device_id,
            "id": self.device_id
        }
        line = LineSIP(device_slot=1)

        device = Device(id=self.device_id)

        config_manager().get.return_value = config_dict
        device_manager().get.return_value = device_dict
        config_manager().autocreate.return_value = autoprovid

        try:
            device_services.remove_line_from_device(device, line)
        except:
            self.fail("An exception was raised whereas it should not")

        reset_config.assert_called_once_with(config_dict)
        reset_to_autoprov.assert_called_once_with(device)
Ejemplo n.º 9
0
    def test_validate_edit(self, dao_find, template_id_exists, plugin_exists,
                           mac_exists):
        device_found = Mock(Device)
        device_found.mac = '00:11:22:33:44:54'

        dao_find.return_value = device_found
        template_id_exists.return_value = True
        plugin_exists.return_value = True
        mac_exists.return_value = False

        device_id = '123abc'

        device = {
            'id': device_id,
            'mac': '00:11:22:33:44:55',
            'ip': '10.0.0.1',
            'template_id': 'defaultconfigdevice',
            'plugin': 'null',
        }

        device = Device(**device)

        validator.validate_edit(device)

        dao_find.assert_called_once_with(device_id)
        template_id_exists.assert_called_once_with(device.template_id)
        plugin_exists.assert_called_once_with(device.plugin)
        mac_exists.assert_called_once_with(device.mac)
Ejemplo n.º 10
0
    def test_synchronize_with_error(self, device_manager):
        device = Device(id=self.device_id)

        device_manager().synchronize.side_effect = Exception('')

        self.assertRaises(ProvdError, device_services.synchronize, device)
        device_manager().synchronize.assert_called_with(self.device_id)
Ejemplo n.º 11
0
    def test_delete_with_error(self, ine_dao_reset, device_dao_delete,
                               device_notifier_deleted):
        device = Device(id=self.device_id, ip='10.0.0.1')
        device_dao_delete.side_effect = ElementDeletionError(
            'Device', 'Not Exist')

        self.assertRaises(ElementDeletionError, device_services.delete, device)
        self.assertEquals(device_notifier_deleted.call_count, 0)
Ejemplo n.º 12
0
    def test_rebuild_device_config_no_result(self, line_find_all_by_device_id,
                                             build_line_for_device):
        device = Device(id=self.device_id)
        line_find_all_by_device_id.return_value = []

        device_services.rebuild_device_config(device)

        self.assertEquals(build_line_for_device.call_count, 0)
Ejemplo n.º 13
0
def to_model(device, config=None):
    filtered_device = dict((key, value) for key, value in device.iteritems() if key in PROVD_DEVICE_KEYS)
    new_model = Device(**filtered_device)

    if config:
        if 'configdevice' in config:
            new_model.template_id = config['configdevice']

        if device['configured'] is True:
            if device['config'].startswith('autoprov'):
                new_model.status = 'autoprov'
            else:
                new_model.status = 'configured'
        else:
            new_model.status = 'not_configured'

    return new_model
Ejemplo n.º 14
0
    def test_build_line_for_device_with_a_sip_line_with_proxy_backup(
            self, config_manager, find_by_line_id, extension_dao_get):
        username = '******'
        secret = 'password'
        exten = '1250'
        context = 'default'
        display_name = 'Francis Dagobert'
        callerid = '"%s" <%s>' % (display_name, exten)
        proxy_ip = '10.39.5.1'
        registrar_ip = proxy_ip
        proxy_backup = '10.39.5.2'
        configregistrar = 'default'

        line = LineSIP(id=1,
                       device_slot=1,
                       context=context,
                       username=username,
                       name=username,
                       secret=secret,
                       callerid=callerid,
                       configregistrar=configregistrar)
        device = Device(id=self.device_id)

        provd_base_config = {"raw_config": {}}

        config_registrar_dict = self._give_me_a_provd_configregistrar(
            proxy_ip, proxy_backup)
        config_manager().get.side_effect = (provd_base_config,
                                            config_registrar_dict)
        find_by_line_id.return_value = LineExtension(line_id=line.id,
                                                     extension_id=3)
        extension_dao_get.return_value = Extension(exten=exten,
                                                   context=context)

        expected_arg = {
            "raw_config": {
                "sip_lines": {
                    "1": {
                        'username': username,
                        'auth_username': username,
                        'display_name': display_name,
                        'number': exten,
                        'password': secret,
                        'proxy_ip': proxy_ip,
                        'registrar_ip': registrar_ip,
                        'backup_registrar_ip': proxy_backup,
                        'backup_proxy_ip': proxy_backup
                    }
                }
            }
        }

        device_services.build_line_for_device(device, line)

        config_manager().get.assert_any_call(self.device_id)
        config_manager().get.assert_any_call(configregistrar)
        config_manager().update.assert_called_with(expected_arg)
Ejemplo n.º 15
0
    def test_rebuild_device_config(self, line_find_all_by_device_id,
                                   build_line_for_device):
        device = Device(id=self.device_id)
        line1 = LineSIP(device_id=self.device_id)
        line_find_all_by_device_id.return_value = [line1]

        device_services.rebuild_device_config(device)

        build_line_for_device.assert_called_once_with(device, line1)
Ejemplo n.º 16
0
    def test_create_with_dao_error(self, validate_create, device_dao_create,
                                   notifier_created):
        device = Device()

        device_dao_create.side_effect = ElementCreationError('Device', '')

        self.assertRaises(ElementCreationError, device_services.create, device)
        self.assertEquals(notifier_created.call_count, 0)
        validate_create.assert_called_once_with(device)
Ejemplo n.º 17
0
    def test_validate_create_invalid_mac(self):
        device = {
            'id': '02aff2a361004aaf8a8a686a48dc980d',
            'mac': 'ZA:22:33:44:55:66'
        }
        device = Device(**device)

        self.assertRaises(InvalidParametersError, validator.validate_create,
                          device)
Ejemplo n.º 18
0
    def test_validate_create_invalid_ip(self):
        device = {
            'id': '02aff2a361004aaf8a8a686a48dc980d',
            'ip': '10.9.0.5156'
        }
        device = Device(**device)

        self.assertRaises(InvalidParametersError, validator.validate_create,
                          device)
Ejemplo n.º 19
0
    def test_delete(self):
        device_id = 'abc1234'
        device = Device(id=device_id)

        with self.provd_managers() as (device_manager, config_manager, _):
            device_dao.delete(device)

            device_manager.remove.assert_called_once_with(device_id)
            config_manager.remove.assert_called_once_with(device_id)
Ejemplo n.º 20
0
    def test_delete_with_error(self):
        device = Device(id='abcd')

        with self.provd_managers() as (device_manager, config_manager, _):
            device_manager.remove.side_effect = Exception

            self.assertRaises(ElementDeletionError, device_dao.delete, device)
            device_manager.remove.assert_called_once_with(device.id)
            self.assertEquals(config_manager.remove.call_count, 0)
Ejemplo n.º 21
0
    def test_validate_create_mac_already_exists(self, dao_mac_exists):
        device = Device(mac='00:11:22:33:44:55')

        dao_mac_exists.return_value = True

        self.assertRaises(ElementAlreadyExistsError, validator.validate_create,
                          device)

        dao_mac_exists.assert_called_once_with(device.mac)
Ejemplo n.º 22
0
    def test_delete_unassociated(self, device_dao_delete,
                                 line_dao_reset_device,
                                 device_notifier_deleted):
        device = Device(id=self.device_id, ip='10.0.0.1')

        device_services.delete(device)

        device_dao_delete.assert_called_once_with(device)
        line_dao_reset_device.assert_called_once_with(device.id)
        device_notifier_deleted.assert_called_once_with(device)
Ejemplo n.º 23
0
    def test_validate_create_plugin_does_not_exist(self, dao_plugin_exists):
        device = {
            'plugin': 'superduperplugin',
        }
        device = Device(**device)

        dao_plugin_exists.return_value = False

        self.assertRaises(NonexistentParametersError,
                          validator.validate_create, device)
Ejemplo n.º 24
0
    def test_delete_not_exist(self):
        device = Device(id='abcd')

        with self.provd_managers() as (device_manager, config_manager, _):
            device_manager.remove.side_effect = HTTPError(
                '', 404, '', '', StringIO(''))

            self.assertRaises(ElementNotExistsError, device_dao.delete, device)
            device_manager.remove.assert_called_once_with(device.id)
            self.assertEquals(config_manager.remove.call_count, 0)
Ejemplo n.º 25
0
    def test_associate_line_to_device(self, line_dao_edit, link_device_config,
                                      rebuild_device_config):
        device = Device(id=self.device_id)
        line = LineSIP()

        device_services.associate_line_to_device(device, line)

        line_dao_edit.assert_called_once_with(line)
        link_device_config.assert_called_once_with(device)
        rebuild_device_config.assert_called_once_with(device)
        self.assertEquals(line.device_id, self.device_id)
Ejemplo n.º 26
0
    def test_answer(self):
        device = Device(ip='127.0.0.1')
        snom_controller = SnomController(self._ami_class)
        answerer = Mock(_SnomAnswerer)
        snom_controller._get_answerer = Mock(return_value=answerer)

        snom_controller.answer(device)

        snom_controller._get_answerer.assert_called_once_with(
            device.ip, 'guest', 'guest')
        self._assert_answerer_called_once(answerer)
Ejemplo n.º 27
0
    def test_validate_create_template_does_not_exist(self,
                                                     dao_template_id_exists):
        device = {
            'template_id': 'mysuperdupertemplate',
        }
        device = Device(**device)

        dao_template_id_exists.return_value = False

        self.assertRaises(NonexistentParametersError,
                          validator.validate_create, device)
Ejemplo n.º 28
0
    def test_delete_associated(self, device_validate, device_dao_delete,
                               line_dao_reset_device, device_notifier_deleted):
        device = Device(id=self.device_id, ip='10.0.0.1')
        device_validate.side_effect = ElementDeletionError(
            'Device', 'device is still linked')

        self.assertRaises(ElementDeletionError, device_services.delete, device)

        assert_that(device_dao_delete.call_count, equal_to(0))
        assert_that(line_dao_reset_device.call_count, equal_to(0))
        assert_that(device_notifier_deleted.call_count, equal_to(0))
Ejemplo n.º 29
0
    def test_edit_with_error_on_device_update(self, provd_build_edit):
        device_id = 'abc1234'
        device = Device(id=device_id)

        provd_build_edit.return_value = ({}, {})

        with self.provd_managers() as (device_manager, config_manager, _):
            device_manager.get.return_value = {}
            config_manager.get.return_value = {}
            device_manager.update.side_effect = Exception

            self.assertRaises(ElementEditionError, device_dao.edit, device)
Ejemplo n.º 30
0
    def test_list_devices_with_two_devices(self, device_search):
        device1 = Device(id='abcdefghijklmnopqrstuvwxyz123456',
                         ip='10.0.0.1',
                         mac='00:11:22:33:44:55')
        device2 = Device(id='1234567890abcdefghij1234567890abc',
                         ip='10.0.0.2',
                         mac='00:11:22:33:44:56')

        device_search.return_value = SearchResult(total=2,
                                                  items=[device1, device2])

        expected_result = {
            'total': 2,
            'items': [self.build_item(device1),
                      self.build_item(device2)]
        }

        result = self.app.get(BASE_URL)

        self.assert_response_for_list(result, expected_result)
        device_search.assert_called_once_with()
Ejemplo n.º 31
0
    def test_rebuild_device_config_provd_error(self,
                                               line_find_all_by_device_id,
                                               build_line_for_device):
        device = Device(id=self.device_id)
        line1 = LineSIP(device_id=self.device_id)
        line_find_all_by_device_id.return_value = [line1]
        build_line_for_device.side_effect = URLError('urlerror')

        self.assertRaises(ProvdError, device_services.rebuild_device_config,
                          device)

        build_line_for_device.assert_called_once_with(device, line1)
Ejemplo n.º 32
0
    def test_is_switchboard_plugin_name(self):
        device = Device()
        device.plugin = 'xivo-aastra-switchboard'

        assert_that(device.is_switchboard())
Ejemplo n.º 33
0
    def test_is_switchboard_no_plugin(self):
        device = Device()

        assert_that(is_not(device.is_switchboard()))
Ejemplo n.º 34
0
    def test_is_switchboard_option_true(self):
        device = Device()
        device.options = {'switchboard': True}

        assert_that(device.is_switchboard())
Ejemplo n.º 35
0
    def test_is_switchboard_option_false(self):
        device = Device()
        device.options = {'switchboard': False}

        assert_that(is_not(device.is_switchboard()))
Ejemplo n.º 36
0
    def test_is_switchboard_no_option(self):
        device = Device()
        device.options = {}

        assert_that(is_not(device.is_switchboard()))
Ejemplo n.º 37
0
    def test_validate_create_invalid_options_type(self):
        device = Device()
        device.options = 'foobar'

        self.assertRaises(InvalidParametersError, validator.validate_create, device)
Ejemplo n.º 38
0
    def test_validate_create_invalid_switchboard_option_type(self):
        device = Device()
        device.options = {'switchboard': 42}

        self.assertRaises(InvalidParametersError, validator.validate_create, device)