Beispiel #1
0
    def test_create_with_empty_attributes(self, line_dao_create, make_provisioning_id):
        line1 = LineSIP(context='',
                        device_slot=1)

        line2 = LineSIP(context='default',
                        device_slot='')

        self.assertRaises(InvalidParametersError, line_services.create, line1)
        self.assertRaises(InvalidParametersError, line_services.create, line2)
        self.assertEquals(make_provisioning_id.call_count, 0)
Beispiel #2
0
    def test_rebuild_device_config_2_lines_same_device(
            self, line_find_all_by_device_id, build_line_for_device):
        device = Device(id=self.device_id)
        line1 = LineSIP(device_id=self.device_id)
        line2 = LineSIP(device_id=self.device_id)
        line_find_all_by_device_id.return_value = [line1, line2]

        device_services.rebuild_device_config(device)

        build_line_for_device.assert_called_with(device, line1)
        build_line_for_device.assert_called_with(device, line2)
    def test_to_user_data(self):
        line_sip = LineSIP()
        line_sip.private_value = 'private_value'
        provisioning_extension = line_sip.provisioning_extension = '192837'
        username = line_sip.username = '******'

        line_sip_dict = line_sip.to_user_data()

        assert_that(line_sip_dict, has_entries({
            'provisioning_extension': provisioning_extension,
            'username': username,
        }))
        assert_that(line_sip_dict, all_of(is_not(has_key('private_value')),  # not mapped
                                          is_not(has_key('secret'))))  # not set
Beispiel #4
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)
Beispiel #5
0
    def test_edit_with_unknown_line(self):
        line_sip = self.add_usersip()
        line = LineSIP(id=123,
                       username='******',
                       protocolid=line_sip.id)

        self.assertRaises(ElementNotExistsError, line_dao.edit, line)
Beispiel #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)
Beispiel #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)
Beispiel #8
0
    def test_create_with_inexisting_context(self, line_dao_create, make_provisioning_id, find_context_by_name):
        line = LineSIP(context='superdupercontext',
                       device_slot=1)
        find_context_by_name.return_value = None

        self.assertRaises(InvalidParametersError, line_services.create, line)
        self.assertEquals(make_provisioning_id.call_count, 0)
Beispiel #9
0
    def test_create_sip_line_with_no_extension(self):
        line = LineSIP(protocol='sip',
                       context='default',
                       provisioning_extension="123456")

        line_created = line_dao.create(line)

        assert_that(line_created, is_not(has_property('number')))
Beispiel #10
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)
Beispiel #11
0
    def test_to_user_data(self):
        line_sip = LineSIP()
        line_sip.private_value = 'private_value'
        provisioning_extension = line_sip.provisioning_extension = '192837'
        username = line_sip.username = '******'

        line_sip_dict = line_sip.to_user_data()

        assert_that(
            line_sip_dict,
            has_entries({
                'provisioning_extension': provisioning_extension,
                'username': username,
            }))
        assert_that(
            line_sip_dict,
            all_of(
                is_not(has_key('private_value')),  # not mapped
                is_not(has_key('secret'))))  # not set
Beispiel #12
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)
Beispiel #13
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)
Beispiel #14
0
    def test_edit_with_empty_attributes(self,
                                        line_dao_edit,
                                        line_notifier_edited,
                                        device_services_get,
                                        device_services_rebuild_device_config):
        line = LineSIP(context='',
                       device_slot=1)

        self.assertRaises(InvalidParametersError, line_services.edit, line)
        self.assertEquals(line_dao_edit.call_count, 0)
        self.assertEquals(device_services_get.call_count, 0)
        self.assertEquals(device_services_rebuild_device_config.call_count, 0)
        self.assertEquals(line_notifier_edited.call_count, 0)
Beispiel #15
0
    def test_remove_line_from_device_provd_error(self, device_manager,
                                                 config_manager):
        config_manager().get.side_effect = URLError('urlerror')
        line = LineSIP(device_slot=2)
        device = Device(id=self.device_id)

        self.assertRaises(ProvdError, device_services.remove_line_from_device,
                          device, line)

        config_manager().get.assert_called_once_with(self.device_id)
        self.assertEquals(config_manager.update.call_count, 0)
        self.assertEquals(config_manager.autocreate.call_count, 0)
        self.assertEquals(device_manager.call_count, 0)
Beispiel #16
0
    def test_edit_with_database_error(self, Session):
        session = Mock()
        session.commit.side_effect = SQLAlchemyError()
        Session.return_value = session

        line_sip = self.add_usersip()
        line = LineSIP(id=123,
                       username='******',
                       secret='kiki',
                       protocolid=line_sip.id)

        self.assertRaises(ElementEditionError, line_dao.edit, line)
        session.begin.assert_called_once_with()
        session.rollback.assert_called_once_with()
Beispiel #17
0
    def test_update_callerid(self, line_dao_find_by_user_id, line_services_edit):
        expected_caller_id = 'titi'
        user = User(id=1,
                    firstname='titi',
                    caller_id=expected_caller_id)
        line = LineSIP(callerid=expected_caller_id,
                       number='1000',
                       name='toto')

        line_dao_find_by_user_id.return_value = line

        line_services.update_callerid(user)

        line_dao_find_by_user_id.assert_called_once_with(user.id)
        line_services_edit.assert_called_once_with(line)
Beispiel #18
0
    def test_create_database_error(self, Session):
        session = Mock()
        session.commit.side_effect = SQLAlchemyError()
        Session.return_value = session

        name = 'line'
        context = 'toto'
        secret = '1234'

        line = LineSIP(name=name,
                       context=context,
                       username=name,
                       secret=secret)

        self.assertRaises(ElementCreationError, line_dao.create, line)
Beispiel #19
0
    def test_create_with_error_from_dao(self, line_dao_create, make_provisioning_id):
        name = 'line'
        context = 'toto'
        secret = '1234'

        line = LineSIP(name=name,
                       context=context,
                       username=name,
                       secret=secret,
                       device_slot=1)

        error = Exception("message")
        line_dao_create.side_effect = ElementCreationError(error, '')

        self.assertRaises(ElementCreationError, line_services.create, line)
        make_provisioning_id.assert_called_with()
Beispiel #20
0
    def test_delete(self,
                    line_dao_delete,
                    line_notifier_deleted,
                    device_dao_find,
                    remove_line_from_device):
        line_id = 1
        username = '******'
        secret = 'toto'

        line = LineSIP(id=line_id, username=username, secret=secret)

        line_services.delete(line)

        line_dao_delete.assert_called_once_with(line)
        line_notifier_deleted.assert_called_once_with(line)
        self.assertEquals(device_dao_find.call_count, 0)
        self.assertEquals(remove_line_from_device.call_count, 0)
Beispiel #21
0
    def test_create_sip_line_with_error_from_dao(self, Session):
        session = Mock()
        session.commit.side_effect = SQLAlchemyError()
        Session.return_value = session

        name = 'line'
        context = 'toto'
        secret = '1234'

        line = LineSIP(name=name,
                       context=context,
                       username=name,
                       secret=secret)

        self.assertRaises(ElementCreationError, line_dao.create, line)
        session.begin.assert_called_once_with()
        session.rollback.assert_called_once_with()
Beispiel #22
0
    def test_remove_line_from_device_no_sip_lines(self, config_manager,
                                                  reset_to_autoprov):
        config_dict = {
            "raw_config": {
                "protocol": "sip",
            }
        }
        config_manager().get.return_value = config_dict
        line = LineSIP(device_slot=2)

        device = Device(id=self.device_id)

        device_services.remove_line_from_device(device, line)

        config_manager().get.assert_called_with(self.device_id)
        self.assertEquals(config_manager().update.call_count, 0)
        self.assertEquals(reset_to_autoprov.update.call_count, 0)
Beispiel #23
0
    def test_create(self, line_dao_create, line_notifier_created, make_provisioning_id):
        name = 'line'
        context = 'toto'
        secret = '1234'

        line = LineSIP(name=name,
                       context=context,
                       username=name,
                       secret=secret,
                       device_slot=1)

        line_dao_create.return_value = line

        result = line_services.create(line)

        line_dao_create.assert_called_once_with(line)
        line_notifier_created.assert_called_once_with(line)
        make_provisioning_id.assert_called_with()
        self.assertEquals(type(result), LineSIP)
Beispiel #24
0
    def test_edit(self,
                  line_dao_edit,
                  line_notifier_edited,
                  device_dao_find,
                  device_services_rebuild_device_config):
        name = 'line'
        context = 'toto'
        secret = '1234'
        line = LineSIP(name=name,
                       context=context,
                       username=name,
                       secret=secret,
                       device_slot=1)

        line_services.edit(line)

        line_dao_edit.assert_called_once_with(line)
        self.assertEquals(device_dao_find.call_count, 0)
        self.assertEquals(device_services_rebuild_device_config.call_count, 0)
        line_notifier_edited.assert_called_once_with(line)
Beispiel #25
0
    def test_delete_with_device(self,
                                line_dao_delete,
                                line_notifier_deleted,
                                device_dao_find,
                                remove_line_from_device):
        line_id = 1
        username = '******'
        secret = 'toto'
        device_id = 15
        device_slot = 1

        line = LineSIP(id=line_id, username=username, secret=secret, device_id=device_id, device_slot=device_slot)
        device = device_dao_find.return_value = Mock()

        line_services.delete(line)

        line_dao_delete.assert_called_once_with(line)
        device_dao_find.assert_called_once_with(line.device_id)
        remove_line_from_device.assert_called_once_with(device, line)
        line_notifier_deleted.assert_called_once_with(line)
Beispiel #26
0
    def test_delete_with_device_not_found(self,
                                          line_dao_delete,
                                          line_notifier_deleted,
                                          device_dao_find,
                                          remove_line_from_device):
        line_id = 1
        username = '******'
        secret = 'toto'
        device_id = 15
        device_slot = 1

        line = LineSIP(id=line_id, username=username, secret=secret, device_id=device_id, device_slot=device_slot)
        device_dao_find.return_value = None

        line_services.delete(line)

        line_dao_delete.assert_called_once_with(line)
        device_dao_find.assert_called_once_with(line.device_id)
        self.assertEquals(remove_line_from_device.call_count, 0)
        line_notifier_deleted.assert_called_once_with(line)
Beispiel #27
0
    def test_create_sip_line(self):
        line = LineSIP(context='default',
                       provisioning_extension="123456")

        line_created = line_dao.create(line)

        result_protocol = (self.session.query(UserSIPSchema)
                           .filter(UserSIPSchema.id == line_created.protocolid)
                           .first())
        result_line = (self.session.query(LineSchema)
                       .filter(LineSchema.id == line_created.id)
                       .first())

        assert_that(result_line, all_of(
            has_property('protocol', 'sip'),
            has_property('protocolid', result_protocol.id),
            has_property('context', 'default'),
            has_property('provisioningid', int(line.provisioning_extension))
        ))

        assert_that(result_protocol, has_property('type', 'friend'))
Beispiel #28
0
    def test_edit_with_error_from_dao(self,
                                      line_dao_edit,
                                      line_notifier_edited,
                                      device_dao_find,
                                      device_services_rebuild_device_config):
        name = 'line'
        context = 'toto'
        secret = '1234'

        line = LineSIP(name=name,
                       context=context,
                       username=name,
                       secret=secret,
                       device_slot=1)

        error = Exception("message")
        line_dao_edit.side_effect = ElementEditionError(error, '')

        self.assertRaises(ElementEditionError, line_services.edit, line)
        self.assertEquals(device_dao_find.call_count, 0)
        self.assertEquals(device_services_rebuild_device_config.call_count, 0)
        self.assertEquals(line_notifier_edited.call_count, 0)
Beispiel #29
0
    def test_remove_line_from_device_sip_autoprov(self, config_manager,
                                                  reset_to_autoprov):
        autoprovid = "autoprov1234"
        config_dict = {
            "raw_config": {
                "protocol": "sip",
                "sip_lines": {
                    "1": {
                        "username": "******"
                    }
                },
                'funckeys': {
                    '1': {
                        'label': 'bob',
                        'line': 1,
                        'type': 'blf',
                        'value': '1001'
                    }
                }
            }
        }

        config_manager().get.return_value = config_dict
        config_manager().autocreate.return_value = autoprovid
        line = LineSIP(device_slot=1)

        device = Device(id=self.device_id)

        expected_arg_config = {
            "raw_config": {
                "protocol": "sip",
            }
        }

        device_services.remove_line_from_device(device, line)

        config_manager().get.assert_called_with(self.device_id)
        reset_to_autoprov.assert_called_with(device)
        config_manager().update.assert_called_with(expected_arg_config)
Beispiel #30
0
    def build_line(self, **kwargs):
        params = {
            'id': None,
            'name': None,
            'number': None,
            'context': None,
            'protocol': None,
            'protocolid': None,
            'callerid': None,
            'device_id': None,
            'provisioning_extension': None,
            'configregistrar': None,
            'device_slot': None,
            'username': None,
            'secret': None,
        }
        params.update(kwargs)

        line = LineSIP()
        for name, value in params.iteritems():
            setattr(line, name, value)

        return line
Beispiel #31
0
    def test_create(self, mock_line_services_create):
        mock_line_services_create.return_value = self.line

        expected_result = self.build_item(self.line)

        created_line = LineSIP(
            context=self.line.context,
            name=self.line.username,
            provisioning_extension=self.line.provisioning_extension,
            device_slot=self.line.device_slot,
            username=self.line.username)

        data = {
            'context': self.line.context,
            'provisioning_extension': self.line.provisioning_extension,
            'device_slot': self.line.device_slot,
            'username': self.line.username,
        }

        result = self.app.post(BASE_URL, data=self._serialize_encode(data))

        self.assert_response_for_create(result, expected_result)
        mock_line_services_create.assert_called_once_with(created_line)