Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_returns_client(self):
file_path = os.path.join(os.path.dirname(__file__), 'ox3rctest')
ox = ox3apiclient.client_from_file(file_path=file_path)
self.assertTrue(isinstance(ox, ox3apiclient.Client))
def test_loads_alternate_env(self):
file_path = os.path.join(os.path.dirname(__file__), 'ox3rctest')
ox = ox3apiclient.client_from_file(file_path=file_path, env='dev')
test_values = [
'domain_dev',
'consumer_secret_dev',
'consumer_key_dev']
loaded_values = [
ox.domain,
ox.consumer_key,
ox.consumer_secret]
test_values.sort()
loaded_values.sort()
self.assertEqual(loaded_values, test_values)
def test_missing_required_option_raises_error(self):
file_path = os.path.join(os.path.dirname(__file__), 'ox3rctest')
self.assertRaises(
Exception,
ox3apiclient.client_from_file,
file_path,
'missing-required-option')
def test_loads_default_env(self):
file_path = os.path.join(os.path.dirname(__file__), 'ox3rctest')
ox = ox3apiclient.client_from_file(file_path=file_path)
test_values = [
'domain',
'consumer_secret',
'consumer_key']
loaded_values = [
ox.domain,
ox.consumer_key,
ox.consumer_secret]
test_values.sort()
loaded_values.sort()
self.assertEqual(loaded_values, test_values)
def test_loads_optional_options(self):
file_path = os.path.join(os.path.dirname(__file__), 'ox3rctest')
ox = ox3apiclient.client_from_file(
file_path=file_path,
env='optional-options')
test_values = [
'domain',
'consumer_secret',
'consumer_key',
'callback_url',
'scheme',
'request_token_url',
'access_token_url',
'authorization_url',
'api_path',
'email',
'password']
def _get_client(self, get_return=None, put_return=None, post_return=None, options_return=None, delete_return=None):
ex_resp = self._build_mock_response()
with patch('ox3apiclient.requests.Session') as mock_requests_session:
with patch('ox3apiclient.Client.log_request') as mock_client_log_request:
mock_requests_session.return_value.get.return_value = get_return or ex_resp
mock_requests_session.return_value.post.return_value = post_return or ex_resp
mock_requests_session.return_value.put.return_value = put_return or ex_resp
mock_requests_session.return_value.options.return_value = options_return or ex_resp
mock_requests_session.return_value.delete.return_value = delete_return or ex_resp
mock_client_log_request.return_value = None
return ox3apiclient.Client(
email=self.email,
password=self.password,
domain=self.domain,
realm=self.realm,
consumer_key=self.consumer_key,
consumer_secret=self.consumer_secret,
request_token_url=self.request_token_url,
access_token_url=self.access_token_url,
authorization_url=self.authorization_url)
def test_returns_client(self):
file_path = os.path.join(os.path.dirname(__file__), 'ox3rctest')
ox = ox3apiclient.client_from_file(file_path=file_path)
self.assertTrue(isinstance(ox, ox3apiclient.Client))
def test_logoff(self):
client = self._get_client()
ret_val = client.logoff()
self.assertTrue(isinstance(ret_val, ox3apiclient.Client))
def test_authorize_token(self):
# mock the post response, and do some setup
r = self._build_mock_response()
r.text = 'oauth_verifier=verifier'
r.return_value = {'key': 'key', 'secret': 'secret'}
client = self._get_client(post_return=r)
client._token = {'key': 'key', 'secret': 'secret'}
# Unauthorized Case
r.status_code = 401
with self.assertRaises(ox3apiclient.OAuthException):
client.authorize_token()
# Authorized Case
r.status_code = 200
client.authorize_token()
self.assertEqual(client._token['verifier'], 'verifier')
def fetch_access_token(self):
"""Helper method to fetch and set access token.
Returns token string.
"""
oauth = OAuth1(client_key=self.consumer_key,
client_secret=self.consumer_secret,
resource_owner_key=self._token['key'],
resource_owner_secret=self._token['secret'],
verifier=self._token['verifier'],
callback_uri=self.callback_url,
signature_type='auth_header')
response = self._session.post(url=self.access_token_url, auth=oauth, timeout=self.timeout)
self.log_request(response)
if response.status_code != 200:
raise OAuthException("OAuth token verification failed (%s) %s" % (response.status_code, response.text))
self._token = parse_qs(response.text)['oauth_token'][0]
return self._token