def test_data_sent_to_server(self):
     req_obj = KazooRequest(self.path, auth_required=False)
     with mock.patch('requests.get') as mock_get:
         data_dict = {"data1": "dataval1"}
         req_obj.execute("http://testserver",
                         param3="someval",
                         data=data_dict)
         self.assert_data(mock_get, data_dict)
 def test_data_sent_to_server(self):
     req_obj = KazooRequest(self.path, auth_required=False)
     with mock.patch('requests.get') as mock_get:
         data_dict = {
             "data1": "dataval1"
         }
         req_obj.execute("http://testserver", param3="someval",
                         data=data_dict)
         self.assert_data(mock_get, data_dict)
 def test_internal_server_error_unparseable(self):
     req_obj = KazooRequest("/somepath", auth_required=False)
     with mock.patch('requests.get') as mock_get:
         mock_response = mock.Mock()
         mock_response.status_code = 500
         mock_response.headers = {"X-Request-Id": "sdfaskldfjaosdf"}
         mock_response.json.return_value = None
         mock_get.return_value = mock_response
         with self.assertRaises(exceptions.KazooApiError) as cm:
             req_obj.execute("http://testserver")
             self.assertTrue("Request ID" in cm.exception)
 def test_invalid_data_displays_invalid_field_data(self):
     req_obj = KazooRequest("/somepath", auth_required=False)
     with mock.patch('requests.get') as mock_get:
         mock_response = mock.Mock()
         mock_response.status_code = 400
         mock_response.json.return_value = utils.load_fixture_as_dict(
             "invalid_data_response.json")
         mock_get.return_value = mock_response
         with self.assertRaises(exceptions.KazooApiBadDataError) as cm:
             req_obj.execute("http://testserver.com")
             self.assertTrue("realm" in cm.exception.field_errors)
 def test_invalid_data_displays_invalid_field_data(self):
     req_obj = KazooRequest("/somepath", auth_required=False)
     with mock.patch('requests.get') as mock_get:
         mock_response = mock.Mock()
         mock_response.status_code = 400
         mock_response.json = utils.load_fixture_as_dict(
             "invalid_data_response.json")
         mock_get.return_value = mock_response
         with self.assertRaises(exceptions.KazooApiBadDataError) as cm:
             req_obj.execute("http://testserver.com")
         self.assertTrue("realm" in cm.exception.field_errors)
 def test_internal_server_error_unparseable(self):
     req_obj = KazooRequest("/somepath", auth_required=False)
     with mock.patch('requests.get') as mock_get:
         mock_response = mock.Mock()
         mock_response.status_code = 500
         mock_response.headers = {"X-Request-Id": "sdfaskldfjaosdf"}
         mock_response.json = None
         mock_get.return_value = mock_response
         with self.assertRaises(exceptions.KazooApiError) as cm:
             req_obj.execute("http://testserver")
         self.assertTrue("Request ID" in cm.exception.message)
 def test_kazoo_api_error_raised_on_error_response(self):
     req_obj = KazooRequest("/somepath", auth_required=False)
     with mock.patch('requests.get') as mock_get:
         mock_response = mock.Mock()
         mock_response.json = self.error_response
         mock_get.return_value = mock_response
         with self.assertRaises(exceptions.KazooApiError) as cm:
             req_obj.execute("http://testserver")
         expected_errors = "the error was {0}".format(
             self.error_response["message"])
         self.assertTrue(expected_errors in cm.exception.message)
 def test_kazoo_api_error_raised_on_error_response(self):
     req_obj = KazooRequest("/somepath", auth_required=False)
     with mock.patch('requests.get') as mock_get:
         mock_response = mock.Mock()
         mock_response.json = self.error_response
         mock_get.return_value = mock_response
         with self.assertRaises(exceptions.KazooApiError) as cm:
             req_obj.execute("http://testserver")
         expected_errors = "the error was {0}".format(
                                self.error_response["message"])
         self.assertTrue(expected_errors in cm.exception.message)
Example #9
0
 def get_extra_view_request(self, viewname, **kwargs):
     view_desc = None
     for desc in self.extra_views:
         if desc["path"] == viewname:
             view_desc = desc
     if view_desc is None:
         raise ValueError("Unknown extra view name {0}".format(viewname))
     if view_desc["scope"] == "aggregate":
         return KazooRequest(self.path.format(**kwargs) + "/" + viewname,
                             method=view_desc["method"])
     return KazooRequest(self._get_full_url(kwargs) + "/" + viewname,
                         method=view_desc["method"])
 def test_internal_server_error_has_error_message_if_parseable(self):
     req_obj = KazooRequest("/somepath", auth_required=False)
     with mock.patch('requests.get') as mock_get:
         mock_response = mock.Mock()
         mock_response.status_code = 500
         mock_response.headers = {"X-Request-Id": "sdfaskldfjaosdf"}
         mock_response.json.return_value = utils.load_fixture_as_dict(
             "bad_billing_status_response.json")
         mock_get.return_value = mock_response
         with self.assertRaises(exceptions.KazooApiError) as cm:
             req_obj.execute("http://testserver")
             self.assertTrue(
                 "Unable to continue due to billing" in cm.exception)
 def test_internal_server_error_has_error_message_if_parseable(self):
     req_obj = KazooRequest("/somepath", auth_required=False)
     with mock.patch('requests.get') as mock_get:
         mock_response = mock.Mock()
         mock_response.status_code = 500
         mock_response.headers = {"X-Request-Id": "sdfaskldfjaosdf"}
         mock_response.json = utils.load_fixture_as_dict(
             "bad_billing_status_response.json")
         mock_get.return_value = mock_response
         with self.assertRaises(exceptions.KazooApiError) as cm:
             req_obj.execute("http://testserver")
         ex = cm.exception
         self.assertTrue("Unable to continue due to billing" in ex.message)
 def test_get_parameters_added_to_url(self):
     request = KazooRequest("/somepath", auth_required=False,
                            get_params={"one":1, "two":2})
     with mock.patch('requests.get') as mock_get:
         mock_response = mock.Mock()
         mock_response.status_code = 200
         mock_response.json = {"result":"fake", "status":"success"}
         mock_get.return_value = mock_response
         request.execute("http://testserver.com")
         mock_get.assert_called_with(
             "http://testserver.com/somepath?two=2&one=1",
             headers=mock.ANY
         )
 def test_data_sent_to_server_with_auth_if_required(self):
     req_obj = KazooRequest(self.path, auth_required=True)
     with mock.patch('requests.post') as mock_post:
         data_dict = {
             "data1": "dataval1"
         }
         token = "sdkjfhasdfa"
         expected_headers = {
             "Content-Type": "application/json",
             "X-Auth-Token": token,
         }
         req_obj.execute("http://testserver", param3="someval",
                         token=token, method="post")
         mock_post.assert_called_with(mock.ANY, headers=expected_headers)
 def test_data_sent_to_server_with_auth_if_required(self):
     req_obj = KazooRequest(self.path, auth_required=True)
     with mock.patch('requests.post') as mock_post:
         data_dict = {"data1": "dataval1"}
         token = "sdkjfhasdfa"
         expected_headers = {
             "Content-Type": "application/json",
             "X-Auth-Token": token,
         }
         req_obj.execute("http://testserver",
                         param3="someval",
                         token=token,
                         method="post")
         mock_post.assert_called_with(mock.ANY, headers=expected_headers)
 def test_get_parameters_added_to_url(self):
     request = KazooRequest("/somepath",
                            auth_required=False,
                            get_params={
                                "one": 1,
                                "two": 2
                            })
     with mock.patch('requests.get') as mock_get:
         mock_response = mock.Mock()
         mock_response.status_code = 200
         mock_response.json = {"result": "fake", "status": "success"}
         mock_get.return_value = mock_response
         request.execute("http://testserver.com")
         mock_get.assert_called_with(
             "http://testserver.com/somepath?two=2&one=1", headers=mock.ANY)
Example #16
0
 def upload_phone_number_file(self, acct_id, phone_number, filename,
                              file_obj):
     """Uploads a file like object as part of a phone numbers documents"""
     request = KazooRequest(
         "/accounts/{account_id}/phone_numbers/{phone_number}",
         method="post")
     return self._execute_request(request, files={filename: file_obj})
Example #17
0
 def create_phone_number(self, acct_id, phone_number):
     request = KazooRequest(
         "/accounts/{account_id}/phone_numbers/{phone_number}",
         method="put")
     return self._execute_request(request,
                                  account_id=acct_id,
                                  phone_number=phone_number)
Example #18
0
 def search_phone_numbers(self, prefix, quantity=10):
     request = KazooRequest("/phone_numbers",
                            get_params={
                                "prefix": prefix,
                                "quantity": quantity
                            })
     return self._execute_request(request)
Example #19
0
 def upload_media_file(self, acct_id, media_id, filename, file_obj):
     """Uploads a media file like object as part of a media document"""
     request = KazooRequest("/accounts/{account_id}/media/{media_id}/raw",
                            method="post")
     return self._execute_request(request, 
                                  account_id=acct_id, 
                                  media_id=media_id,
                                  rawfiles=({filename: file_obj}))
 def create_req_obj(self, url, auth_required=False, method='get'):
     return KazooRequest(url, auth_required=auth_required, method=method)
Example #21
0
 def get_object_request(self, **kwargs):
     return KazooRequest(self._get_full_url(kwargs))
Example #22
0
 def get_delete_object_request(self, **kwargs):
     return KazooRequest(self._get_full_url(kwargs), method='delete')
Example #23
0
 def get_list_request(self, **kwargs):
     relative_path = self.path.format(**kwargs)
     if kwargs['request_optional_args']:
         relative_path = relative_path + '?' + self.dict_to_string(
             kwargs['request_optional_args'])
     return KazooRequest(relative_path)
Example #24
0
    def list_devices_by_owner(self, accountId, ownerId):
        request = KazooRequest("/accounts/{account_id}/devices", get_params={"filter_owner_id": ownerId})
        request.auth_required = True

        return self._execute_request(request, account_id=accountId)
Example #25
0
 def get_partial_update_object_request(self, **kwargs):
     return KazooRequest(self._get_full_url(kwargs), method='patch')
Example #26
0
    def list_child_accounts(self, parentAccountId):
        request = KazooRequest("/accounts/{account_id}/children")
        request.auth_required = True

        return self._execute_request(request, account_id=parentAccountId)
Example #27
0
 def get_create_object_request(self, **kwargs):
     return KazooRequest(self.path.format(**kwargs), method='put')
Example #28
0
 def get_list_request(self, **kwargs):
     relative_path = self.path.format(**kwargs)
     return KazooRequest(relative_path)