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_signature():
KEY = 'api_key'
SECRET = 'api_secret'
PATH = '/test/resource/show'
request_params = {
'a': 1,
'b': 'two',
'c3': 'Param 3',
u'❄': u'⛄',
't1': True,
'n0': None
}
jwp_client = jwplatform.Client(KEY, SECRET)
url, params = jwp_client._build_request(PATH, request_params)
assert url == 'https://api.jwplatform.com/v1{}'.format(PATH)
assert 'api_nonce' in params
assert 'api_timestamp' in params
assert 'api_key' in params
assert 'api_format' in params
assert 'api_kit' in params
assert 'api_signature' in params
request_params['api_nonce'] = params['api_nonce']
request_params['api_timestamp'] = params['api_timestamp']
request_params['api_key'] = params['api_key']
request_params['api_format'] = params['api_format']
request_params['api_kit'] = params['api_kit']
def test_supported_errors_parsing(test_case):
url_expr = re.compile(r'https?://api\.test\.tst/v1/error\?.*')
responses.add(
responses.GET, url_expr,
status=test_case['http_status'],
content_type='application/json',
body=json.dumps(test_case['response']))
jwp_client = jwplatform.Client('api_key', 'api_secret', host='api.test.tst')
with pytest.raises(test_case['expected_exception']) as err:
jwp_client.error()
assert err.value.message == test_case['response']['message']
def test_post_existing_resource():
url_expr = re.compile(r'https?://api\.test\.tst/v1/a/b/c/d')
responses.add(
responses.POST, url_expr,
status=200,
content_type='application/json',
body='{"status": "ok"}')
jwp_client = jwplatform.Client('api_key', 'api_secret', host='api.test.tst')
resp = jwp_client.a.b.c.d(http_method='POST', abcde=123)
assert resp['status'] == 'ok'
def test_post_parameters_in_url():
url_expr = re.compile(r'https?://api\.test\.tst/v1/a/b/c/d\?.*')
responses.add(
responses.POST, url_expr,
status=200,
content_type='application/json',
body='{"status": "ok"}')
jwp_client = jwplatform.Client('api_key', 'api_secret', host='api.test.tst')
resp = jwp_client.a.b.c.d(http_method='POST', use_body=False, _post='true', _body='false')
assert resp['status'] == 'ok'
def test_post_parameters_in_body():
url_expr = re.compile(r'https?://api\.test\.tst/v1/a/b/c/d\?.*')
responses.add(
responses.POST, url_expr,
status=200,
content_type='application/json',
body='{"status": "ok"}')
jwp_client = jwplatform.Client('api_key', 'api_secret', host='api.test.tst')
# ConnectionError is expected as request parameters are included in the
# request body for POST request by default.
with pytest.raises(ConnectionError):
resp = jwp_client.a.b.c.d(http_method='POST', post='true', _body='none')
def test_custom_initialization():
KEY = '_key_'
SECRET = '_secret_'
SCHEME = 'http'
HOST = 'api.host.domain'
PORT = 8080
API_VERSION = 'v7'
AGENT = 'test_agent'
jwp_client = jwplatform.Client(
KEY, SECRET,
scheme=SCHEME,
host=HOST,
port=PORT,
version=API_VERSION,
agent=AGENT)
assert jwp_client._Client__key == KEY
assert jwp_client._Client__secret == SECRET
assert jwp_client._scheme == SCHEME
assert jwp_client._host == HOST
assert jwp_client._port == PORT
assert jwp_client._api_version == API_VERSION
assert jwp_client._agent == AGENT
assert 'User-Agent' in jwp_client._connection.headers
assert jwp_client._connection.headers['User-Agent'] == \
def test_empty_response_parsing():
url_expr = re.compile(r'https?://api\.test\.tst/v1/error\?.*')
responses.add(
responses.GET, url_expr,
status=500,
content_type='application/json',
body='')
jwp_client = jwplatform.Client('api_key', 'api_secret', host='api.test.tst')
with pytest.raises(jwplatform.errors.JWPlatformUnknownError) as err:
jwp_client.error()
assert err.value.message == 'Not a valid JSON string: '
def test_long_resource():
url_expr = re.compile(r'https?://api\.test\.tst/v1/a/b/c/d/f/e\?.*'
'abcde=.*')
responses.add(
responses.GET, url_expr,
status=200,
content_type='application/json',
body='{"status": "ok"}')
jwp_client = jwplatform.Client('api_key', 'api_secret', host='api.test.tst')
resp = jwp_client.a.b.c.d.f.e(abcde='')
assert resp['status'] == 'ok'
'status': 'error',
'code': 'NotFound',
'title': 'Not Found',
'message': 'Item not found'
},
'expected_exception': jwplatform.errors.JWPlatformNotFoundError
},
{
'http_status': 400,
'response': {
'status': 'error',
'code': 'NoMethod',
'title': 'No Method Specified',
'message': ''
},
'expected_exception': jwplatform.errors.JWPlatformNoMethodError
},
{
'http_status': 501,
'response': {
'status': 'error',
'code': 'NotImplemented',
'title': 'Method Not Implemented',
'message': ''
},
'expected_exception': jwplatform.errors.JWPlatformNotImplementedError
},
{
'http_status': 405,
'response': {
'status': 'error',
'code': 'NotSupported',
def test_long_resource():
url_expr = re.compile(r'https?://api\.test\.tst/v1/json/error\?.*')
responses.add(
responses.GET, url_expr,
status=200,
content_type='application/json',
body='({"json": "error"})')
jwp_client = jwplatform.Client('api_key', 'api_secret', host='api.test.tst')
with pytest.raises(jwplatform.errors.JWPlatformUnknownError) as err:
jwp_client.json.error()
assert err.value.message == 'Not a valid JSON string: ({"json": "error"})'