async def test_setup_protocol(): client = Client(project='project', token='token', connection=MockedConnection) responses = [ json.loads( '{"requester_nodeid":"uuid","responder_nodeid":"uuid","result":{"protocol":"signalwire-proto-test"}}' ), json.loads( '{"protocol":"signalwire-proto-test","command":"add","subscribe_channels":["notifications"]}' ) ] client.execute = AsyncMock(side_effect=responses) new_protocol = await setup_protocol(client) assert new_protocol == 'signalwire-proto-test' assert client.execute.mock.call_count == 2 setup, subscription = client.execute.mock.call_args_list assert setup[0][0].params == { 'protocol': 'signalwire', 'method': 'setup', 'params': {} } assert subscription[0][0].params == { 'command': 'add', 'protocol': 'signalwire-proto-test', 'channels': ['notifications'] }
def relay_tasking(relay_client): relay_client.protocol = 'signalwire-proto-test' response = json.loads( '{"requester_nodeid":"uuid","responder_nodeid":"uuid","result":{"code":"200","message":"Receiving all inbound related to the requested relay contexts and available scopes"}}' ) relay_client.execute = AsyncMock(return_value=response) return relay_client.tasking
def relay_client_to_connect(relay_client): asyncio.sleep = AsyncMock() # Mock sleep relay_client.connection.responses = [ '{"jsonrpc":"2.0","id":"uuid","result":{"session_restored":false,"sessionid":"87cf6699-7a89-4491-b732-b51144155d46","nodeid":"uuid_node","master_nodeid":"00000000-0000-0000-0000-000000000000","authorization":{"project":"project","expires_at":null,"scopes":["calling"],"signature":"random_signature"},"routes":[],"protocols":[],"subscriptions":[],"authorities":[],"authorizations":[],"accesses":[],"protocols_uncertified":["signalwire"]}}', '{"jsonrpc":"2.0","id":"uuid","result":{"requester_nodeid":"uuid","responder_nodeid":"uuid","result":{"protocol":"signalwire_random_proto"}}}', '{"jsonrpc":"2.0","id":"uuid","result":{"protocol":"signalwire_random_proto","command":"add","subscribe_channels":["notifications"]}}' ] return relay_client
async def test_receive_contexts_already_present(): client = Client(project='project', token='token', connection=MockedConnection) client.contexts = ['already_there'] client.execute = AsyncMock() await receive_contexts(client, ['already_there']) assert client.contexts == ['already_there'] client.execute.mock.assert_not_called()
async def test_wait_for_ended(relay_call): relay_call.calling.client.execute = AsyncMock() payload = json.loads( '{"event_type":"calling.call.state","event_channel":"signalwire-proto-test","timestamp":1570204684.1133151,"project_id":"project-uuid","space_id":"space-uuid","params":{"call_state":"ended","direction":"outbound","device":{"type":"phone","params":{"from_number":"+12029999999","to_number":"+12028888888"}},"call_id":"call-id","node_id":"node-id","tag":"call-tag"}}' ) asyncio.create_task(_fire(relay_call.calling, payload)) result = await relay_call.wait_for_ended() assert result assert relay_call.state == 'ended' relay_call.calling.client.execute.mock.assert_not_called()
async def test_send_fail(relay_messaging): response = json.loads( '{"requester_nodeid":"uuid","responder_nodeid":"uuid","result":{"message":"Some error","code":"400"}}' ) relay_messaging.client.execute = AsyncMock(return_value=response) result = await relay_messaging.send( from_number='+12029999999', to_number='+12028888888', context='office', body='Hey There, Welcome at SignalWire!') assert isinstance(result, SendResult) assert not result.successful assert result.message_id == None relay_messaging.client.execute.mock.assert_called_once()
async def test_receive_contexts_with_mixed_contexts(): client = Client(project='project', token='token', connection=MockedConnection) client.protocol = 'signalwire-proto-test' client.contexts = ['already_there'] response = json.loads( '{"requester_nodeid":"uuid","responder_nodeid":"uuid","result":{"code":"200","message":"Receiving all inbound related to the requested relay contexts and available scopes"}}' ) client.execute = AsyncMock(return_value=response) await receive_contexts(client, ['another_one']) msg = client.execute.mock.call_args[0][0] assert msg.params.pop('params') == {'contexts': ['another_one']} client.contexts.sort() assert client.contexts == ['already_there', 'another_one'] client.execute.mock.assert_called_once()
async def test_send_success(relay_messaging): response = json.loads( '{"requester_nodeid":"uuid","responder_nodeid":"uuid","result":{"message":"Message accepted","code":"200","message_id":"2c0e265d-4597-470e-9d5d-00581e0874a2"}}' ) relay_messaging.client.execute = AsyncMock(return_value=response) result = await relay_messaging.send( from_number='+12029999999', to_number='+12028888888', context='office', body='Hey There, Welcome at SignalWire!') assert isinstance(result, SendResult) assert result.successful assert result.message_id == '2c0e265d-4597-470e-9d5d-00581e0874a2' msg = relay_messaging.client.execute.mock.call_args[0][0] params = msg.params.pop('params') assert msg.params.pop('protocol') == 'signalwire-proto-test' assert msg.params.pop('method') == 'messaging.send' assert params['from_number'] == '+12029999999' assert params['body'] == 'Hey There, Welcome at SignalWire!' relay_messaging.client.execute.mock.assert_called_once()
async def test_record_async_with_success(success_response, relay_call): with patch('signalwire.relay.calling.components.uuid4', mock_uuid): relay_response = json.loads( '{"requester_nodeid":"uuid","responder_nodeid":"uuid","result":{"code":"200","message":"Message","url":"record.mp3"}}' ) relay_call.calling.client.execute = AsyncMock( return_value=relay_response) # relay_call.calling.client.execute = success_response action = await relay_call.record_async(terminators='#') assert not action.completed assert action.url == 'record.mp3' # Complete the action now.. payload = json.loads( '{"event_type":"calling.call.record","params":{"state":"finished","record":{"audio":{"format":"mp3","direction":"speak","stereo":true}},"url":"record.mp3","control_id":"control-id","size":4096,"duration":4,"call_id":"call-id","node_id":"node-id"}}' ) await _fire(relay_call.calling, payload) assert action.completed msg = relay_call.calling.client.execute.mock.call_args[0][0] assert msg.params == json.loads( '{"protocol":"signalwire-proto-test","method":"calling.record","params":{"call_id":"call-id","node_id":"node-id","control_id":"control-id","record":{"audio":{"terminators":"#"}}}}' ) relay_call.calling.client.execute.mock.assert_called_once()
def tap_success_response(): response = json.loads( '{"requester_nodeid":"uuid","responder_nodeid":"uuid","result":{"code":"200","message":"Tapping","call_id":"call-id","control_id":"control-id","source_device":{"type":"rtp","params":{"addr":"10.10.10.10","port":30030,"codec":"PCMU","ptime":20,"rate":8000}}}}' ) return AsyncMock(return_value=response)
def fail_response(): # response = json.loads('{"requester_nodeid":"uuid","responder_nodeid":"uuid","result":{"code":"400","message":"Some error"}}') return AsyncMock(side_effect=Exception('Some error'))
def success_response(): response = json.loads( '{"requester_nodeid":"uuid","responder_nodeid":"uuid","result":{"code":"200","message":"Message"}}' ) return AsyncMock(return_value=response)