def test_list_calls(self): """ list_calls() should return calls """ estimated_resquest = { 'bridgeId': None, 'conferenceId': None, 'from': None, 'to': None, 'size': None, 'sortOrder': None } estimated_json = """ [{ "id": "callId" }] """ with patch('requests.request', return_value=create_response(200, estimated_json)) as p: client = get_client() data = list(client.list_calls()) p.assert_called_with( 'get', 'https://api.catapult.inetwork.com/v1/users/userId/calls', auth=AUTH, headers=headers, params=estimated_resquest) self.assertEqual('callId', data[0]['id'])
def test_get_transcription(self): """ get_transcription() should return a transcription """ estimated_json = """ { "chargeableDuration": 60, "id": "{transcription-id}", "state": "completed", "time": "2014-10-09T12:09:16Z", "text": "{transcription-text}", "textSize": 3627, "textUrl": "{url-to-full-text}" } """ with patch('requests.request', return_value=create_response(200, estimated_json)) as p: client = get_client() data = client.get_transcription('recId', 'transcriptionId') p.assert_called_with( 'get', 'https://api.catapult.inetwork.com/v1/users/userId/recordings/recId/transcriptions/transcriptionId', headers=headers, auth=AUTH) self.assertEqual('{transcription-id}', data['id'])
def test_list_recordings(self): """ list_recordings() should return recordings """ estimated_json = """ [{ "endTime": "2013-02-08T13:17:12.181Z", "id": "{recordingId1}", "media": "https://.../v1/users/.../media/{callId1}-1.wav", "call": "https://.../v1/users/.../calls/{callId1}", "startTime": "2013-02-08T13:15:47.587Z", "state": "complete" }] """ with patch('requests.request', return_value=create_response(200, estimated_json)) as p: client = get_client() data = list(client.list_recordings()) p.assert_called_with( 'get', 'https://api.catapult.inetwork.com/v1/users/userId/recordings', auth=AUTH, headers=headers, params={'size': None}) self.assertEqual('{recordingId1}', data[0]['id']) self.assertEqual('{callId1}-1.wav', data[0]['media_name'])
def test_create_call(self): """ create_call() should create a call and return id """ estimated_resquest = { 'from': '+1234567890', 'to': '+1234567891', 'callTimeout': None, 'callbackUrl': None, 'callbackTimeout': None, 'callbackHttpMethod': None, 'fallbackUrl': None, 'bridgeId': None, 'conferenceId': None, 'recordingEnabled': False, 'recordingFileFormat': None, 'recordingMaxDuration': None, 'transcriptionEnabled': False, 'tag': None, 'sipHeaders': None } estimated_response = create_response(201) estimated_response.headers['Location'] = 'http://localhost/callId' with patch('requests.request', return_value=estimated_response) as p: client = get_client() from_ = '+1234567890' to = '+1234567891' id = client.create_call(from_, to) p.assert_called_with( 'post', 'https://api.catapult.inetwork.com/v1/users/userId/calls', auth=AUTH, headers=headers, json=estimated_resquest) self.assertEqual('callId', id)
def test_get_messages(self): """ list_messages() should return messages """ estimated_json = """ [{ "id": "messageId" }] """ estimated_request = { 'from': None, 'to': None, 'fromDateTime': None, 'toDateTime': None, 'direction': None, 'state': None, 'deliveryState': None, 'sortOrder': None, 'size': None } with patch('requests.request', return_value=create_response(200, estimated_json)) as p: client = get_client() data = list(client.list_messages()) p.assert_called_with( 'get', 'https://api.catapult.inetwork.com/v1/users/userId/messages', auth=AUTH, headers=headers, params=estimated_request) self.assertEqual('messageId', data[0]['id'])
def test_list_applications(self): """ get_applications() should return applications """ estimated_json = """ [{ "id": "a-111", "name": "MyFirstApp", "incomingCallUrl": "http://example.com/calls.php", "incomingMessageUrl": "http://example.com/messages.php", "incomingMessageUrlCallbackTimeout": 1000, "incomingCallUrlCallbackTimeout": 1000, "incomingCallFallbackUrl" : "http://fallback.com/", "incomingMessageFallbackUrl": "http://fallback.com/", "callbackHttpMethod": "GET", "autoAnswer": true }] """ with patch('requests.request', return_value=create_response(200, estimated_json)) as p: client = get_client() data = list(client.list_applications()) p.assert_called_with( 'get', 'https://api.catapult.inetwork.com/v1/users/userId/applications', auth=AUTH, headers=headers, params={"size": None}) self.assertEqual( json.loads(estimated_json)[0]['id'], data[0]['id'])
def test_create_domain_endpoint(self): """ create_domain_endpoint() should create an endpoint and return id """ estimated_response = create_response(201) estimated_response.headers['Location'] = 'http://localhost/endpointId' estimated_request = { 'name': 'mysip', 'description': None, 'applicationId': None, 'enabled': True, 'credentials': { 'password': '******' } } with patch('requests.request', return_value=estimated_response) as p: client = get_client() data = {'name': 'mysip', 'password': '******'} id = client.create_domain_endpoint('domainId', **data) p.assert_called_with( 'post', 'https://api.catapult.inetwork.com/v1/users/userId/domains/domainId/endpoints', auth=AUTH, headers=headers, json=estimated_request) self.assertEqual('endpointId', id)
def test_update_application(self): """ update_application() should update an application """ estimated_json_request = { 'name': 'MyUpdatedApplication', 'incomingCallUrl': None, 'incomingCallUrlCallbackTimeout': None, 'incomingCallFallbackUrl': None, 'incomingMessageUrl': None, 'incomingMessageUrlCallbackTimeout': None, 'incomingMessageFallbackUrl': None, 'callbackHttpMethod': None, 'autoAnswer': None } estimated_response = create_response(201) estimated_response.headers[ 'Location'] = 'http://localhost/applicationId' with patch('requests.request', return_value=estimated_response) as p: client = get_client() id = client.update_application(app_id='a-123', name='MyUpdatedApplication') p.assert_called_with( 'post', 'https://api.catapult.inetwork.com/v1/users/userId/applications/a-123', auth=AUTH, headers=headers, json=estimated_json_request)
def test_get_phone_number(self): """ get_phone_number() should return a phone number """ estimated_json = """ { "id": "{numberId1}", "application": "https://catapult.inetwork.com/v1/users/users/u-ly123/applications/a-j321", "number":"{number1}", "nationalNumber":"{national_number1}", "name": "home phone", "createdTime": "2013-02-13T17:46:08.374Z", "state": "NC", "price": "0.60", "numberState": "enabled" } """ with patch('requests.request', return_value=create_response(200, estimated_json)) as p: client = get_client() data = client.get_phone_number('numberId') p.assert_called_with( 'get', 'https://api.catapult.inetwork.com/v1/users/userId/phoneNumbers/numberId', headers=headers, auth=AUTH) self.assertEqual('{numberId1}', data['id'])
def test_send_message(self): """ send_message() should create an message and return id """ estimated_response = create_response(201) estimated_response.headers['Location'] = 'http://localhost/messageId' estimated_request = { 'from': 'num1', 'to': 'num2', 'text': 'text', 'media': None, 'receiptRequested': None, 'callbackUrl': None, 'callbackHttpMethod': None, 'callbackTimeout': None, 'fallbackUrl': None, 'tag': None } with patch('requests.request', return_value=estimated_response) as p: client = get_client() messageID = client.send_message(from_='num1', to='num2', text='text') p.assert_called_with( 'post', 'https://api.catapult.inetwork.com/v1/users/userId/messages', auth=AUTH, headers=headers, json=estimated_request) self.assertEqual('messageId', messageID)
def test_search_and_order_local_numbers(self): """ search_and_order_available_numbers() should search, order and return available numbers """ estimated_json_request = { 'city': None, 'state': None, 'zip': '27606', 'areaCode': None, 'localNumber': None, 'inLocalCallingArea': None, 'quantity': 1 } estimated_json = """ [{ "number": "{number1}", "nationalNumber": "{national_number1}", "price": "0.60", "location": "https://.../v1/users/.../phoneNumbers/{numberId1}" }] """ with patch('requests.request', return_value=create_response(200, estimated_json)) as p: client = get_client() data = client.search_and_order_local_numbers(zip_code='27606', quantity=1) p.assert_called_with( 'post', 'https://api.catapult.inetwork.com/v1/availableNumbers/local', auth=AUTH, headers=headers, params=estimated_json_request) self.assertEqual('{national_number1}', data[0]['national_number']) self.assertEqual('{numberId1}', data[0]['id'])
def test_list_transcriptions(self): """ list_transcriptions() should return transcriptions """ estimated_json = """ [{ "chargeableDuration": 60, "id": "{transcription-id}", "state": "completed", "time": "2014-10-09T12:09:16Z", "text": "{transcription-text}", "textSize": 3627, "textUrl": "{url-to-full-text}" }] """ with patch('requests.request', return_value=create_response(200, estimated_json)) as p: client = get_client() data = list(client.list_transcriptions('recordingId')) p.assert_called_with( 'get', 'https://api.catapult.inetwork.com/v1/users/userId/recordings/recordingId/transcriptions', auth=AUTH, headers=headers, params={'size': None}) self.assertEqual('{transcription-id}', data[0]['id'])
def test_create_conference(self): """ create_conference() should create a conference and return id """ estimated_request = { 'callbackUrl': None, 'from': '+1234567980', 'callbackTimeout': None, 'callbackHttpMethod': 'GET', 'fallbackUrl': None, 'tag': 'my_tag' } estimated_response = create_response(201) estimated_response.headers[ 'Location'] = 'http://localhost/conferenceId' with patch('requests.request', return_value=estimated_response) as p: client = get_client() id = client.create_conference(from_='+1234567980', callbackUrl='http://abc.com', tag='my_tag', callback_http_method='GET') p.assert_called_with( 'post', 'https://api.catapult.inetwork.com/v1/users/userId/conferences', auth=AUTH, headers=headers, json=estimated_request) self.assertEqual('conferenceId', id)
def test_search_available_toll_free_numbers(self): """ search_available_numbers() should return available numbers """ estimated_json_request = {'quantity': 1, 'pattern': '*456'} estimated_json = """ [{ "nationalNumber": "(844) 489-0456", "number": "+18444890456", "patternMatch": " 456", "price": "0.75" }] """ with patch('requests.request', return_value=create_response(200, estimated_json)) as p: client = get_client() data = client.search_available_toll_free_numbers(quantity=1, pattern='*456') p.assert_called_with( 'get', 'https://api.catapult.inetwork.com/v1/availableNumbers/tollFree', auth=AUTH, headers=headers, params=estimated_json_request) self.assertEqual('+18444890456', data[0]['number'])
def test_list_bridge_calls(self): """ list_bridge_calls() should return calls of a bridge """ estimated_json = """ [{ "activeTime": "2013-05-22T19:49:39Z", "direction": "out", "from": "{fromNumber}", "id": "{callId1}", "bridgeId": "{bridgeId}", "startTime": "2013-05-22T19:49:35Z", "state": "active", "to": "{toNumber1}", "recordingEnabled": false, "events": "https://api.catapult.inetwork.com/v1/users/{userId}/calls/{callId1}/events", "bridge": "https://api.catapult.inetwork.com/v1/users/{userId}/bridges/{bridgeId}" }] """ with patch('requests.request', return_value=create_response(200, estimated_json)) as p: client = get_client() calls = list(client.list_bridge_calls('bridgeId')) p.assert_called_with( 'get', 'https://api.catapult.inetwork.com/v1/users/userId/bridges/bridgeId/calls', headers=headers, auth=AUTH) self.assertEqual('{callId1}', calls[0]['id'])
def test_list_bridges(self): """ list_bridges() should return bridges """ estimated_json = """ [{ "id": "bridgeId", "state": "completed", "bridgeAudio": "true", "calls":"https://.../v1/users/{userId}/bridges/{bridgeId}/calls", "createdTime": "2013-04-22T13:55:30.279Z", "activatedTime": "2013-04-22T13:55:30.280Z", "completedTime": "2013-04-22T13:59:30.122Z" }] """ with patch('requests.request', return_value=create_response(200, estimated_json)) as p: client = get_client() data = list(client.list_bridges()) p.assert_called_with( 'get', 'https://api.catapult.inetwork.com/v1/users/userId/bridges', auth=AUTH, headers=headers, params={ 'size': None}) self.assertEqual('bridgeId', data[0]['id'])
def test_request_with_absolute_url(self): """ _request() should make authorized request to absolute url """ with patch('requests.request', return_value=create_response()) as p: client = get_client() response = client._request('get', 'http://localhost') p.assert_called_with('get', 'http://localhost', headers=headers, auth=('apiToken', 'apiSecret'))
def test_request_with_relative_url(self): """ _request() should make authorized request to relative url """ with patch('requests.request', return_value=create_response()) as p: client = get_client() response = client._request('get', '/path') p.assert_called_with('get', 'https://api.catapult.inetwork.com/v1/path', headers=headers, auth=('apiToken', 'apiSecret'))
def test_get_lazy_enumerator_with_several_requests(self): """ get_lazy_enumerator() should request new portion of data on demand """ estimated_json1 = '[1, 2, 3]' estimated_json2 = '[4, 5, 6, 7]' response1 = create_response(200, estimated_json1) response1.headers['link'] = '<transactions?page=0&size=25>; rel="first", ' \ '<transactions?page=1&size=25>; rel="next"' response2 = create_response(200, estimated_json2) client = get_client() with patch('requests.request', return_value=response2) as p: results = get_lazy_enumerator(client, lambda: ([1, 2, 3], response1, None)) self.assertEqual([1, 2, 3, 4, 5, 6, 7], list(results)) p.assert_called_with( 'get', 'transactions?page=1&size=25', headers=headers, auth=AUTH)
def test_delete_domain(self): """ delete_domain() should remove an domain """ with patch('requests.request', return_value=create_response(200)) as p: client = get_client() client.delete_domain('domainId') p.assert_called_with( 'delete', 'https://api.catapult.inetwork.com/v1/users/userId/domains/domainId', headers=headers, auth=AUTH)
def test_make_request_with_json(self): """ _make_request() should make request, check response and extract json data """ estimated_response = create_response(200, '{"data": "data"}') with patch('requests.request', return_value=estimated_response) as p: client = get_client() data, response, _ = client._make_request('get', '/path') p.assert_called_with('get', 'https://api.catapult.inetwork.com/v1/path', headers=headers, auth=('apiToken', 'apiSecret')) self.assertIs(estimated_response, response) self.assertDictEqual({'data': 'data'}, data)
def test_delete_media_file(self): """ delete_media_file() should remove a media file """ with patch('requests.request', return_value=create_response(200)) as p: client = get_client() client.delete_media_file('file1') p.assert_called_with( 'delete', 'https://api.catapult.inetwork.com/v1/users/userId/media/file1', headers=headers, auth=AUTH)
def test_send_dtmf_to_call(self): """ send_dtmf_to_call() should send dtmf data to a call """ with patch('requests.request', return_value=create_response(200)) as p: client = get_client() client.send_dtmf_to_call('callId', 12) p.assert_called_with( 'post', 'https://api.catapult.inetwork.com/v1/users/userId/calls/callId/dtmf', auth=AUTH, headers=headers, json={'dtmfOut': 12})
def test_check_response_with_plain_text_error(self): """ _check_response() should extract error data from plain text """ response = create_response(400, 'This is error', 'text/html') client = get_client() with self.assertRaises(BandwidthAccountAPIException) as r: client._check_response(response) err = r.exception self.assertEqual('400', err.code) self.assertEqual('This is error', err.message) self.assertEqual(400, err.status_code) self.assertEqual('Error 400: This is error', str(err))
def test_check_response_with_json_error_without_code(self): """ _check_response() should extract error data from json (without code) """ response = create_response(400, '{"message": "This is error"}') client = get_client() with self.assertRaises(BandwidthAccountAPIException) as r: client._check_response(response) err = r.exception self.assertEqual('400', err.code) self.assertEqual('This is error', err.message) self.assertEqual(400, err.status_code) self.assertEqual('Error 400: This is error', str(err))
def test_make_request_with_location_header(self): """ _make_request() should make request, check response and extract id from location header """ estimated_response = create_response(201, '', 'text/html') estimated_response.headers['location'] = 'http://localhost/path/id' with patch('requests.request', return_value=estimated_response) as p: client = get_client() _, response, id = client._make_request('get', '/path') p.assert_called_with('get', 'https://api.catapult.inetwork.com/v1/path', headers=headers, auth=('apiToken', 'apiSecret')) self.assertIs(estimated_response, response) self.assertEqual('id', id)
def test_check_response_with_json_error(self): """ _check_response() should extract error data from json """ response = create_response(400, '{"message": "This is error", "code": "invalid-request"}') client = get_client() with self.assertRaises(BandwidthVoiceAPIException) as r: client._check_response(response) err = r.exception self.assertEqual('invalid-request', err.code) self.assertEqual('This is error', err.message) self.assertEqual(400, err.status_code) self.assertEqual('Error invalid-request: This is error', str(err))
def test_make_request_with_headers(self): """ _request() should add the user agent to header """ with patch('requests.request', return_value=create_response()) as p: client = get_client() req_headers = {'hello': 'world'} res_headers = copy.deepcopy(headers) res_headers['hello'] = 'world' response = client._request('get', '/path', headers=req_headers) p.assert_called_with('get', 'https://api.catapult.inetwork.com/v1/path', headers=res_headers, auth=('apiToken', 'apiSecret'))
def test_update_bridge(self): """ update_bridge() should update a bridge """ with patch('requests.request', return_value=create_response(200)) as p: client = get_client() data = {'bridgeAudio': False, 'callIds': None} client.update_bridge('bridgeId', bridge_audio=False) p.assert_called_with( 'post', 'https://api.catapult.inetwork.com/v1/users/userId/bridges/bridgeId', auth=AUTH, headers=headers, json=data)
def test_update_call_gather(self): """ update_call_gather() should update a gather """ with patch('requests.request', return_value=create_response(200)) as p: client = get_client() data = {'state': 'completed'} client.update_call_gather('callId', 'gatherId', state='completed') p.assert_called_with( 'post', 'https://api.catapult.inetwork.com/v1/users/userId/calls/callId/gather/gatherId', headers=headers, auth=AUTH, json=data)