def test__canonicalize_kwargs(self): api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, CONSUMER_KEY) _run(api.init()) self.assertEqual({}, api._canonicalize_kwargs({})) self.assertEqual({'from': 'value'}, api._canonicalize_kwargs({'from': 'value'})) self.assertEqual({'_to': 'value'}, api._canonicalize_kwargs({'_to': 'value'})) self.assertEqual({'from': 'value'}, api._canonicalize_kwargs({'_from': 'value'}))
def test_init_from_custom_config(self): # custom config file api = Client() _run(api.init(config_file=M_CUSTOM_CONFIG_PATH)) self.assertEqual('https://ca.api.ovh.com/1.0', api._endpoint) self.assertEqual('This is a fake custom application key', api._application_key) self.assertEqual('This is a *real* custom application key', api._application_secret) self.assertEqual('I am customingly kidding', api._consumer_key)
def test_init_from_config(self): with mock.patch.dict('os.environ', M_ENVIRON): api = Client() _run(api.init()) self.assertEqual('https://ca.api.soyoustart.com/1.0', api._endpoint) self.assertEqual(M_ENVIRON['OVH_APPLICATION_KEY'], api._application_key) self.assertEqual(M_ENVIRON['OVH_APPLICATION_SECRET'], api._application_secret) self.assertEqual(M_ENVIRON['OVH_CONSUMER_KEY'], api._consumer_key)
def test_init(self): # nominal api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, CONSUMER_KEY) _run(api.init()) self.assertEqual(APPLICATION_KEY, api._application_key) self.assertEqual(APPLICATION_SECRET, api._application_secret) self.assertEqual(CONSUMER_KEY, api._consumer_key) self.assertTrue(api._time_delta is None) self.assertEqual(TIMEOUT, api._timeout) # override default timeout timeout = (1, 1) api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, CONSUMER_KEY, timeout=timeout) self.assertEqual(timeout, api._timeout) # invalid region api = Client(ENDPOINT_BAD, '', '', '') self.assertRaises(InvalidRegion, _run, api.init())
def test_put(self, m_call): PAYLOAD = { 'arg1': object(), 'arg2': object(), 'arg3': object(), 'arg4': False, } api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, CONSUMER_KEY) _run(api.init()) self.assertEqual(m_call.return_value, _run(api.put(FAKE_URL, **PAYLOAD))) m_call.assert_called_once_with('PUT', FAKE_URL, PAYLOAD, True)
def test_call_query_id(self, m_req): m_res = m_req.return_value m_json = m_res.json.return_value m_res.headers = {"X-OVH-QUERYID": "FR.test1"} api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET) _run(api.init()) m_res.status = 99 self.assertRaises(APIError, _run, api.call(FAKE_METHOD, FAKE_PATH, None, False)) try: _run(api.call(FAKE_METHOD, FAKE_PATH, None, False)) self.assertEqual(0, 1) # should fail as method have to raise APIError except APIError as e: self.assertEqual(e.query_id, "FR.test1")
def test_request_consumerkey(self, m_call): api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, CONSUMER_KEY) _run(api.init()) # nominal FAKE_RULES = object() FAKE_CK = object() RET = {'consumerKey': FAKE_CK} m_call.return_value = RET ret = _run(api.request_consumerkey(FAKE_RULES, FAKE_URL)) self.assertEqual(RET, ret) m_call.assert_called_once_with('POST', '/auth/credential', { 'redirection': FAKE_URL, 'accessRules': FAKE_RULES, }, False)
def test_time_delta(self, m_call): api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, CONSUMER_KEY) _run(api.init()) m_call.return_value = 1404395895 api._time_delta = None # nominal time_delta = _run(api.get_time_delta()) m_call.assert_called_once_with('GET', '/auth/time', None, False, None) self.assertEqual(time_delta, 6) self.assertEqual(api._time_delta, 6) # ensure cache m_call.return_value = 0 m_call.reset_mock() self.assertEqual(_run(api.get_time_delta()), 6) self.assertFalse(m_call.called)
def test_get(self, m_call): # basic test api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, CONSUMER_KEY) _run(api.init()) self.assertEqual(m_call.return_value, _run(api.get(FAKE_URL))) m_call.assert_called_once_with('GET', FAKE_URL, None, True, None) # append query string m_call.reset_mock() api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, CONSUMER_KEY) _run(api.init()) self.assertEqual(m_call.return_value, _run(api.get(FAKE_URL, param="test"))) m_call.assert_called_once_with('GET', FAKE_URL+'?param=test', None, True, None) # append to existing query string m_call.reset_mock() api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, CONSUMER_KEY) _run(api.init()) self.assertEqual(m_call.return_value, _run(api.get(FAKE_URL+'?query=string', param="test"))) m_call.assert_called_once_with('GET', FAKE_URL+'?query=string¶m=test', None, True, None) # boolean arguments m_call.reset_mock() api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, CONSUMER_KEY) _run(api.init()) self.assertEqual(m_call.return_value, _run(api.get(FAKE_URL+'?query=string', checkbox=True))) m_call.assert_called_once_with('GET', FAKE_URL+'?query=string&checkbox=true', None, True, None) # keyword calling convention m_call.reset_mock() api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, CONSUMER_KEY) _run(api.init()) self.assertEqual(m_call.return_value, _run(api.get(FAKE_URL, _from="start", to="end"))) try: m_call.assert_called_once_with('GET', FAKE_URL+'?to=end&from=start', None, True, None) except: m_call.assert_called_once_with('GET', FAKE_URL+'?from=start&to=end', None, True, None) # batch call m_call.reset_mock() api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, CONSUMER_KEY) _run(api.init()) self.assertEqual(m_call.return_value, _run(api.get(FAKE_URL, _batch=','))) m_call.assert_called_once_with('GET', FAKE_URL, None, True, ',')
def test_raw_call_batch(self, m_req): api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET) _run(api.init()) r = _run(api.raw_call(FAKE_METHOD, FAKE_PATH_BATCH, None, False, ',')) self.assertEqual(r, ["first item", "second item"])
def test_raw_call(self, m_req): api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET) _run(api.init()) r = _run(api.raw_call(FAKE_METHOD, FAKE_PATH, None, False)) self.assertEqual(r, "Let's assume requests will return this")
def test_call_signature(self, m_time_delta, m_req): m_res = m_req.return_value m_json = m_res.json.return_value m_time_delta.return_value = 42 api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, CONSUMER_KEY) _run(api.init()) # nominal m_res.status = 200 self.assertEqual(m_json, _run(api.call(FAKE_METHOD, FAKE_PATH, None, True))) m_time_delta.assert_called_once_with() m_req.assert_called_once_with( FAKE_METHOD, BASE_URL+'/unit/test', headers={ 'X-Ovh-Consumer': CONSUMER_KEY, 'X-Ovh-Application': APPLICATION_KEY, 'X-Ovh-Signature': '$1$16ae5ba8c63841b1951575be905867991d5f49dc', 'X-Ovh-Timestamp': '1404395931', }, data='', timeout=TIMEOUT ) m_time_delta.reset_mock() m_req.reset_mock() # data, nominal data = OrderedDict([('some', 'random'), ('data', 42)]) m_res.status = 200 self.assertEqual(m_json, _run(api.call(FAKE_METHOD, FAKE_PATH, data, True))) m_time_delta.assert_called_once_with() m_req.assert_called_once_with( FAKE_METHOD, BASE_URL+'/unit/test', headers={ 'X-Ovh-Consumer': CONSUMER_KEY, 'X-Ovh-Application': APPLICATION_KEY, 'Content-type': 'application/json', 'X-Ovh-Timestamp': '1404395931', 'X-Ovh-Signature': '$1$9acb1ac0120006d16261a635aed788e83ab172d2', }, data=json.dumps(data), timeout=TIMEOUT ) m_time_delta.reset_mock() m_req.reset_mock() # Overwrite configuration to avoid interfering with any local config from asyncovh.client import config try: from ConfigParser import RawConfigParser except ImportError: # Python 3 from configparser import RawConfigParser self._orig_config = config.config config.config = RawConfigParser() # errors try: api = Client(ENDPOINT, APPLICATION_KEY, None, CONSUMER_KEY) _run(api.init()) self.assertRaises(InvalidKey, _run, api.call(FAKE_METHOD, FAKE_PATH, None, True)) api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, None) _run(api.init()) self.assertRaises(InvalidKey, _run, api.call(FAKE_METHOD, FAKE_PATH, None, True)) finally: # Restore configuration config.config = self._orig_config
def test_call_no_sign(self, m_req): m_res = m_req.return_value m_json = m_res.json.return_value api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET) _run(api.init()) # nominal m_res.status = 200 self.assertEqual(m_json, _run(api.call(FAKE_METHOD, FAKE_PATH, None, False))) m_req.assert_called_once_with( FAKE_METHOD, BASE_URL+'/unit/test', headers={'X-Ovh-Application': APPLICATION_KEY}, data='', timeout=TIMEOUT ) m_req.reset_mock() # data, nominal m_res.status = 200 data = {'key': 'value'} j_data = json.dumps(data) self.assertEqual(m_json, _run(api.call(FAKE_METHOD, FAKE_PATH, data, False))) m_req.assert_called_once_with( FAKE_METHOD, BASE_URL+'/unit/test', headers={ 'X-Ovh-Application': APPLICATION_KEY, 'Content-type': 'application/json', }, data=j_data, timeout=TIMEOUT ) m_req.reset_mock() # request fails, somehow m_req.side_effect = ClientError self.assertRaises(HTTPError, _run, api.call(FAKE_METHOD, FAKE_PATH, None, False)) m_req.side_effect = None # response decoding fails m_res.json.side_effect = ValueError self.assertRaises(InvalidResponse, _run, api.call(FAKE_METHOD, FAKE_PATH, None, False)) m_res.json.side_effect = None # HTTP errors m_res.status = 404 self.assertRaises(ResourceNotFoundError, _run, api.call(FAKE_METHOD, FAKE_PATH, None, False)) m_res.status = 403 m_res.json.return_value = {'errorCode': "NOT_GRANTED_CALL"} self.assertRaises(NotGrantedCall, _run, api.call(FAKE_METHOD, FAKE_PATH, None, False)) m_res.status = 403 m_res.json.return_value = {'errorCode': "NOT_CREDENTIAL"} self.assertRaises(NotCredential, _run, api.call(FAKE_METHOD, FAKE_PATH, None, False)) m_res.status = 403 m_res.json.return_value = {'errorCode': "INVALID_KEY"} self.assertRaises(InvalidKey, _run, api.call(FAKE_METHOD, FAKE_PATH, None, False)) m_res.status = 403 m_res.json.return_value = {'errorCode': "INVALID_CREDENTIAL"} self.assertRaises(InvalidCredential, _run, api.call(FAKE_METHOD, FAKE_PATH, None, False)) m_res.status = 403 m_res.json.return_value = {'errorCode': "FORBIDDEN"} self.assertRaises(Forbidden, _run, api.call(FAKE_METHOD, FAKE_PATH, None, False)) m_res.status = 400 self.assertRaises(BadParametersError, _run, api.call(FAKE_METHOD, FAKE_PATH, None, False)) m_res.status = 409 self.assertRaises(ResourceConflictError, _run, api.call(FAKE_METHOD, FAKE_PATH, None, False)) m_res.status = 460 self.assertRaises(ResourceExpiredError, _run, api.call(FAKE_METHOD, FAKE_PATH, None, False)) m_res.status = 0 self.assertRaises(NetworkError, _run, api.call(FAKE_METHOD, FAKE_PATH, None, False)) m_res.status = 99 self.assertRaises(APIError, _run, api.call(FAKE_METHOD, FAKE_PATH, None, False)) m_res.status = 306 self.assertRaises(APIError, _run, api.call(FAKE_METHOD, FAKE_PATH, None, False))
def test_delete(self, m_call): api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, CONSUMER_KEY) _run(api.init()) self.assertEqual(m_call.return_value, _run(api.delete(FAKE_URL))) m_call.assert_called_once_with('DELETE', FAKE_URL, None, True)
def test_new_consumer_key_request(self): api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, CONSUMER_KEY) _run(api.init()) ck = api.new_consumer_key_request() self.assertEqual(ck._client, api)