def test_same_extension_in_different_context(self): first_extension = Extension(number='1234', context='my_context', is_internal=True) second_extension = Extension(number='1234', context='other_context', is_internal=True) first_extension_calls = [ self._create_call(sentinel.uid1, source_exten=first_extension) ] second_extension_calls = [ self._create_call(sentinel.uid2, source_exten=second_extension) ] first_expected_event = EndpointEvent(first_extension, RINGING, first_extension_calls) second_expected_event = EndpointEvent(second_extension, RINGING, second_extension_calls) self.storage.update_endpoint_status(first_extension, RINGING) self.storage.update_endpoint_status(second_extension, RINGING) assert_that( self.endpoint_notifier.notify.call_args_list, contains_inanyorder(call(first_expected_event), call(second_expected_event)))
def test_subscribe_all_logged_agents(self, get_extension_from_agent_id, get_logged_agent_ids): agent_id_1 = 13 agent_id_2 = 72 status_1 = EndpointStatus.talking status_2 = EndpointStatus.available agent_extension_1 = Extension('3543', 'my_context', is_internal=True) agent_extension_2 = Extension('6353', 'my_context', is_internal=True) calls_1 = [Mock(Call)] calls_2 = [Mock(Call), Mock(Call)] event_1 = EndpointEvent(agent_extension_1, status_1, calls_1) event_2 = EndpointEvent(agent_extension_2, status_2, calls_2) get_logged_agent_ids.return_value = [agent_id_1, agent_id_2] self.call_storage.get_status_for_extension.side_effect = [ status_1, status_2 ] self.call_storage.find_all_calls_for_extension.side_effect = [ calls_1, calls_2 ] get_extension_from_agent_id.side_effect = [ (agent_extension_1.number, agent_extension_1.context), (agent_extension_2.number, agent_extension_2.context) ] self.adapter.subscribe_all_logged_agents() self.assertEquals( self.endpoint_notifier.subscribe_to_status_changes.call_count, 2) self.endpoint_notifier.subscribe_to_status_changes.assert_any_call( agent_extension_1, self.adapter.handle_endpoint_event) self.endpoint_notifier.subscribe_to_status_changes.assert_any_call( agent_extension_2, self.adapter.handle_endpoint_event) self.assertEquals(self.router.route.call_count, 2) self.router.route.assert_any_call(agent_id_1, event_1) self.router.route.assert_any_call(agent_id_2, event_2)
def test_get_extension_from_local_channel(self): channel = 'Local/id-5@agentcallback-38974358734;2' expected_extension = Extension('', '', False) result = helper.get_extension_from_channel(channel) self.assertEquals(expected_extension, result)
def test_find_all_calls_for_extension_when_calls_do_not_concern_extension( self): extension = Extension('1234', 'ze_context', is_internal=False) result = self.storage.find_all_calls_for_extension(extension) assert_that(result, contains())
def test_get_extension_from_channel_no_extension(self, dao_get_extension): channel = 'SIP/asdlkfj-532486' dao_get_extension.side_effect = LookupError expected_result = Extension(number='', context='', is_internal=False) result = helper.get_extension_from_channel(channel) self.assertEquals(expected_result, result)
def test_unsubscribe_from_agent_events(self): extension = Extension(NUMBER, CONTEXT, is_internal=True) self._subscribe_to_event_for_agent(AGENT_ID, extension) self.adapter.unsubscribe_from_agent_events(AGENT_ID) self.endpoint_notifier.unsubscribe_from_status_changes.assert_called_once_with( extension, self.adapter.handle_endpoint_event)
def get_extension_from_channel(channel): protocol_interface = protocol_interface_from_channel(channel) try: extension = line_dao.get_extension_from_protocol_interface( protocol_interface.protocol, protocol_interface.interface) except (LookupError, ValueError): extension = Extension(number='', context='', is_internal=False) return extension
def test_handle_new_channel(self, get_extension_from_channel): get_extension_from_channel.return_value = sentinel.source_exten event = self._mk_new_channel_event() self.call_receiver.handle_new_channel(event) self.call_storage.new_call.assert_called_once_with( UNIQUEID, '', _Channel(sentinel.source_exten, sentinel.channel), _Channel(Extension('', '', True), ''))
def subscribe_to_agent_events(self, agent_id): try: with session_scope(): number, context = agent_status_dao.get_extension_from_agent_id(agent_id) except LookupError: logger.debug('agent with id %s is not logged', agent_id) else: extension = Extension(number, context, is_internal=True) self._new_subscription(extension, agent_id)
def test_when_channel_1_is_local(self): self.storage._calls = { u'1395685236.26': Call( _Channel(Extension('1009', 'default', True), 'SIP/1uzh6d-0000000e'), _Channel(Extension('', '', True), 'Local/102@default-00000006;1'), ), u'1395685237.28': Call( _Channel(Extension('', '', True), 'Local/102@default-00000006;2'), _Channel(Extension('1002', 'default', True), 'SIP/8o5zja-0000000f'), ), } self.storage.merge_local_channels('Local/102@default-00000006;') expected = { u'1395685237.28': Call( _Channel(Extension('1009', 'default', True), 'SIP/1uzh6d-0000000e'), _Channel(Extension('1002', 'default', True), 'SIP/8o5zja-0000000f'), ), } assert_that(self.storage._calls, equal_to(expected))
def test_when_there_is_no_call_on_the_extension(self, mock_user_dao): mock_user_dao.get_line.return_value = { 'number': sentinel.number, 'context': sentinel.context } self.call_storage.find_all_calls_for_extension.return_value = [] self.manager.hangup(sentinel.userid) mock_user_dao.get_line.assert_called_once_with(sentinel.userid) assert_that(self.call_manager.hangup.call_count, equal_to(0)) self.call_storage.find_all_calls_for_extension.assert_called_once_with( Extension(sentinel.number, sentinel.context, True))
def handle_new_channel(self, event): try: channel = event['Channel'] unique_id = event['Uniqueid'] source_exten = helper.get_extension_from_channel(channel) except (InvalidChannelError, KeyError): logger.debug('ignoring %s', event) return self._call_storage.new_call( unique_id, '', _Channel(source_exten, channel), _Channel(Extension('', '', True), ''), )
def test_get_extension_from_protocol_interface_sccp(self): protocol = 'SCCP' name = LINE_NUMBER context = 'default' expected_extension = Extension(number=LINE_NUMBER, context=context, is_internal=True) self.add_line(context=context, name=name, protocol=protocol.lower(), number=LINE_NUMBER) extension = line_dao.get_extension_from_protocol_interface(protocol, name) self.assertEquals(extension, expected_extension)
def _get_active_call(self, user_id): try: line_dict = dao.user.get_line(user_id) except NoSuchLineException: raise NoSuchCallException('user has no line') for fieldname in ['number', 'context']: if fieldname not in line_dict: raise NoSuchCallException('line with no %s' % fieldname) extension = Extension(line_dict['number'], line_dict['context'], True) for call in self._call_storage.find_all_calls_for_extension(extension): return call raise NoSuchCallException('No call on {0}'.format(extension))
def test_subscribe_to_agent_events(self, get_extension_from_agent_id): status = EndpointStatus.talking calls = [Mock(Call)] extension = Extension(NUMBER, CONTEXT, is_internal=True) get_extension_from_agent_id.return_value = (NUMBER, CONTEXT) self.call_storage.get_status_for_extension.return_value = status self.call_storage.find_all_calls_for_extension.return_value = calls expected_event = EndpointEvent(extension, status, calls) self.adapter.subscribe_to_agent_events(AGENT_ID) get_extension_from_agent_id.assert_called_once_with(AGENT_ID) self.endpoint_notifier.subscribe_to_status_changes.assert_called_once_with( extension, self.adapter.handle_endpoint_event) self.router.route.assert_called_once_with(AGENT_ID, expected_event)
def test_subscribe_all_logged_agents_with_one_agent_then_unsubscribe(self, get_extension_from_agent_id, get_logged_agent_ids): status = EndpointStatus.talking calls = [Mock(Call)] agent_extension = Extension('3543', 'my_context', is_internal=True) get_logged_agent_ids.return_value = [AGENT_ID] self.call_storage.get_status_for_extension.return_value = status self.call_storage.find_all_calls_for_extension.return_value = calls event = EndpointEvent(agent_extension, status, calls) get_extension_from_agent_id.return_value = (agent_extension.number, agent_extension.context) self.adapter.subscribe_all_logged_agents() self.adapter.unsubscribe_from_agent_events(AGENT_ID) self.endpoint_notifier.subscribe_to_status_changes.assert_called_once_with(agent_extension, self.adapter.handle_endpoint_event) self.endpoint_notifier.unsubscribe_from_status_changes.assert_called_once_with(agent_extension, self.adapter.handle_endpoint_event) self.router.route.assert_called_once_with(AGENT_ID, event)
def test_when_everything_works(self, mock_user_dao): active_call = Mock(Call) mock_user_dao.get_line.return_value = { 'number': sentinel.number, 'context': sentinel.context } self.call_storage.find_all_calls_for_extension.return_value = [ active_call ] self.manager.hangup(sentinel.userid) mock_user_dao.get_line.assert_called_once_with(sentinel.userid) self.call_manager.hangup.assert_called_once_with(active_call) self.call_storage.find_all_calls_for_extension.assert_called_once_with( Extension(sentinel.number, sentinel.context, True))
def get_extension_from_protocol_interface(session, protocol, interface): try: line_row = (session.query( LineFeatures.number, LineFeatures.context).filter( LineFeatures.protocol == protocol.lower()).filter( LineFeatures.name == interface).first()) except DataError as e: raise ValueError(e) if not line_row: message = 'no line with interface %s' % interface raise LookupError(message) extension = Extension(number=line_row[0], context=line_row[1], is_internal=True) return extension
def test_route_device_in_use_outgoing_external(self): is_internal = False extension = Extension(number=NUMBER, context=CONTEXT, is_internal=is_internal) expected_is_internal = is_internal expected_direction = CallDirection.outgoing status = EndpointStatus.talking source_channel = _Channel(extension, sentinel.source_channel) destination_channel = _Channel(Mock(Extension), sentinel.destination_channel) calls = [Call(source_channel, destination_channel)] event = EndpointEvent(extension, status, calls) self.router.route(AGENT_ID, event) self.status_manager.device_in_use.assert_called_once_with( AGENT_ID, expected_direction, expected_is_internal)
from mock import Mock from mock import sentinel, call from xivo.asterisk.extension import Extension from xivo_cti.model.endpoint_event import EndpointEvent from xivo_cti.model.endpoint_status import EndpointStatus from xivo_cti.model.call_event import CallEvent from xivo_cti.model.call_status import CallStatus from xivo_cti.services.call.call_notifier import CallNotifier from xivo_cti.services.call.endpoint_notifier import EndpointNotifier from xivo_cti.services.call.storage import Call from xivo_cti.services.call.call import _Channel from xivo_cti.services.call.storage import CallStorage NUMBER = '1234' CONTEXT = 'ze_context' EXTENSION = Extension(NUMBER, CONTEXT, is_internal=True) SOURCE = _Channel(Extension('2398', 'ze_context', is_internal=True), sentinel.source_channel) DESTINATION = _Channel(Extension('3297', 'ze_context', is_internal=True), sentinel.destination) UNIQUEID = '8976549874.84' DEST_UNIQUEID = '6666549874.84' AVAILABLE = EndpointStatus.available RINGING = EndpointStatus.ringing class _BaseTestCase(unittest.TestCase): def setUp(self): self.endpoint_notifier = Mock(EndpointNotifier) self.call_notifier = Mock(CallNotifier)
import unittest from functools import partial from hamcrest import assert_that, equal_to from mock import patch, Mock, sentinel from xivo.asterisk.extension import Extension from xivo.asterisk.protocol_interface import InvalidChannelError from xivo_cti.services.call.call import _Channel from xivo_cti.services.call.receiver import CallReceiver from xivo_cti.services.call.storage import CallStorage from xivo_cti.model.endpoint_status import EndpointStatus NUMBER = '3573' CONTEXT = 'my_context' EXTENSION = Extension(number=NUMBER, context=CONTEXT, is_internal=True) EMPTY_EXTENSION = Extension(number='', context='', is_internal=False) CHANNEL = 'SIP/abcd-00001' UNIQUEID = '5765887387.56' DEST_UNIQUEID = '123456789.56' RINGING = EndpointStatus.ringing AVAILABLE = EndpointStatus.available patch_get_extension_from_channel = partial( patch, 'xivo_cti.services.call.helper.get_extension_from_channel', ) patch_channel_state_status = partial( patch,
def test_repr(self): e = Extension('1001', 'default', True) assert_that(repr(_Channel(e, 'mychannel')), equal_to('<_Channel 1001@default>'))
def setUp(self): super(TestEndCall, self).setUp() self.source_exten = Extension('3283', 'context_y', is_internal=True) self.destination_exten = Extension('3258', 'context_y', is_internal=True)