def test_initialize_options(self): """ HTTP client and API options are set with `initialize` method. """ initialize(api_key=API_KEY, app_key=APP_KEY, api_host=API_HOST, proxies=FAKE_PROXY, cacert=False) # Make a simple API call MyCreatable.create() _, options = self.request_mock.call_args() # Assert `requests` parameters self.assertIn('proxies', options) self.assertEqual(options['proxies'], FAKE_PROXY) self.assertIn('verify', options) self.assertEqual(options['verify'], False) # Arm the `requests` to raise self.arm_requests_to_raise() # No exception should be raised (mute=True by default) MyCreatable.create() # Repeat with mute to False initialize(api_key=API_KEY, mute=False) self.assertRaises(ApiError, MyCreatable.create)
def test_creatable(self): MyCreatable.create(mydata="val") self.request_called_with('POST', "host/api/v1/creatables", data={'mydata': "val"}) MyCreatable.create(mydata="val", attach_host_name=True) self.request_called_with('POST', "host/api/v1/creatables", data={'mydata': "val", 'host': api._host_name})
def test_initialize_options(self): """ HTTP client and API options are set with `initialize` method. """ initialize(api_key=API_KEY, app_key=APP_KEY, api_host=API_HOST, proxies=FAKE_PROXY, cacert=False) # Make a simple API call MyCreatable.create() _, options = self.request_mock.request.call_args # Assert `requests` parameters self.assertIn('proxies', options) self.assertEquals(options['proxies'], FAKE_PROXY) self.assertIn('verify', options) self.assertEquals(options['verify'], False) # Arm the `requests` to raise self.arm_requests_to_raise() # No exception should be raised (mute=True by default) MyCreatable.create() # Repeat with mute to False initialize(api_key=API_KEY, mute=False) self.assertRaises(ApiError, MyCreatable.create)
def test_creatable(self): """ Creatable resource logic. """ MyCreatable.create(mydata="val") self.request_called_with('POST', API_HOST + "/api/v1/creatables", data={'mydata': "val"}) MyCreatable.create(mydata="val", attach_host_name=True) self.request_called_with('POST', API_HOST + "/api/v1/creatables", data={'mydata': "val", 'host': api._host_name})
def test_no_initialization_fails(self, test='sisi'): assert_raises(ApiNotInitialized, MyCreatable.create) # No API key => only stats in statsd mode should work initialize() api._api_key = None assert_raises(ApiNotInitialized, MyCreatable.create) # Finally, initialize with an API key initialize(api_key=API_KEY, api_host=API_HOST) MyCreatable.create() assert self.request_mock.request.call_count == 1
def test_no_initialization_fails(self): """ Raise ApiNotInitialized exception when `initialize` has not ran or no API key was set. """ self.assertRaises(ApiNotInitialized, MyCreatable.create) # No API key => only stats in statsd mode should work initialize() api._api_key = None self.assertRaises(ApiNotInitialized, MyCreatable.create) # Finally, initialize with an API key initialize(api_key=API_KEY, api_host=API_HOST) MyCreatable.create() self.assertEqual(self.request_mock.call_count(), 1)
def test_no_initialization_fails(self): """ Raise ApiNotInitialized exception when `initialize` has not ran or no API key was set. """ self.assertRaises(ApiNotInitialized, MyCreatable.create) # No API key => only stats in statsd mode should work initialize() api._api_key = None self.assertRaises(ApiNotInitialized, MyCreatable.create) # Finally, initialize with an API key initialize(api_key=API_KEY, api_host=API_HOST) MyCreatable.create() self.assertEquals(self.request_mock.request.call_count, 1)
def test_request_parameters(self): # Test API, application keys, API host and proxies initialize(api_key=API_KEY, app_key=APP_KEY, api_host=API_HOST, proxies=FAKE_PROXY) # Make a simple API call MyCreatable.create() _, options = self.request_mock.request.call_args assert 'params' in options assert 'api_key' in options['params'] assert options['params']['api_key'] == API_KEY assert 'application_key' in options['params'] assert options['params']['application_key'] == APP_KEY assert 'proxies' in options assert options['proxies'] == FAKE_PROXY assert 'headers' in options assert options['headers'] == {'Content-Type': 'application/json'}
def test_request_parameters(self): """ API parameters are set with `initialize` method. """ # Test API, application keys, API host, and some HTTP client options initialize(api_key=API_KEY, app_key=APP_KEY, api_host=API_HOST) # Make a simple API call MyCreatable.create() _, options = self.request_mock.call_args() # Assert `requests` parameters self.assertIn('params', options) self.assertIn('headers', options) self.assertEqual(options['headers']['Content-Type'], 'application/json') self.assertEqual(options['headers']['DD-API-KEY'], API_KEY) self.assertEqual(options['headers']['DD-APPLICATION-KEY'], APP_KEY) assert "api_key" not in options['params'] assert "application_key" not in options['params']
def test_errors_suppressed(self): """ API `errors` field ApiError suppressed when specified """ # Test API, application keys, API host, and some HTTP client options initialize(api_key=API_KEY, app_key=APP_KEY, api_host=API_HOST) # Make a simple API call self.load_request_response(response_body='{"data": {}, "errors": ["foo error"]}') resp = MyCreatable.create(params={"suppress_response_errors_on_codes": [200]}) self.assertNotIsInstance(resp, ApiError) self.assertDictEqual({"data": {}, "errors": ["foo error"]}, resp)
def test_request_parameters(self): """ API parameters are set with `initialize` method. """ # Test API, application keys, API host, and some HTTP client options initialize(api_key=API_KEY, app_key=APP_KEY, api_host=API_HOST) # Make a simple API call MyCreatable.create() _, options = self.request_mock.request.call_args # Assert `requests` parameters self.assertIn('params', options) self.assertIn('api_key', options['params']) self.assertEquals(options['params']['api_key'], API_KEY) self.assertIn('application_key', options['params']) self.assertEquals(options['params']['application_key'], APP_KEY) self.assertIn('headers', options) self.assertEquals(options['headers'], {'Content-Type': 'application/json'})