Skip to content
This repository has been archived by the owner on Sep 30, 2020. It is now read-only.

Commit

Permalink
__future__.unicode_literals solved the problem
Browse files Browse the repository at this point in the history
  • Loading branch information
karolyi committed Jan 20, 2016
1 parent f590bfe commit 38f2cfa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
15 changes: 9 additions & 6 deletions src/main/python/afp_cli/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, absolute_import, division
from __future__ import (
print_function, absolute_import, division, unicode_literals)

import requests
import json
Expand All @@ -14,8 +15,8 @@ class AWSFederationClientCmd(object):
"""Class for a command line client which uses the afp api"""

def __init__(self, *args, **kwargs):
self.username = kwargs.get('username', None)
self._password = kwargs.get('password', None)
self.username = kwargs.get('username', '')
self.password = kwargs.get('password', '')
self.api_url = kwargs.get('api_url', None)
self.ssl_verify = kwargs.get('ssl_verify', True)

Expand All @@ -27,12 +28,14 @@ def call_api(self, url_suffix):
headers=headers,
verify=self.ssl_verify,
auth=HTTPBasicAuth(self.username,
self._password))
self.password))
if api_result.status_code != 200:
if api_result.status_code == 401:
# Need to treat 401 specially since it is directly send from webserver and body has different format.
# Need to treat 401 specially since it is directly send
# from webserver and body has different format.
raise APICallError("API call to AWS (%s/%s) failed: %s %s" % (
self.api_url, url_suffix, api_result.status_code, api_result.reason))
self.api_url, url_suffix, api_result.status_code,
api_result.reason))
else:
raise APICallError("API call to AWS (%s/%s) failed: %s" % (
self.api_url, url_suffix, api_result.json()['message']))
Expand Down
31 changes: 17 additions & 14 deletions src/unittest/python/aws_federation_client_cmd_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,25 @@ def test_get_correct_account_and_role_list(self, mock_get):
reason="Ok")
mock_get.return_value = expected_result
result = self.api_client.get_account_and_role_list()
self.assertEqual(result, {"testaccount": ["testrole"]}, msg='Should be the same')
self.assertEqual(
result, {"testaccount": ["testrole"]}, msg='Should be the same')

@patch("afp_cli.client.requests.get")
def test_get_correct_aws_credentials(self, mock_get):
expected_result = Mock(text='{"Code": "Success", '
'"AccessKeyId": "testAccessKey", '
'"SecretAccessKey": "testSecretAccessKey", '
'"Token": "testToken", '
'"Expiration": "2015-01-01T12:34:56Z"}',
status_code=200,
reason="Ok")
expected_result = Mock(
text='{"Code": "Success", '
'"AccessKeyId": "testAccessKey", '
'"SecretAccessKey": "testSecretAccessKey", '
'"Token": "testToken", '
'"Expiration": "2015-01-01T12:34:56Z"}',
status_code=200,
reason="Ok")
mock_get.return_value = expected_result
result = self.api_client.get_aws_credentials("testaccount", "testrole")
self.assertEqual(result, {"AWS_ACCESS_KEY_ID": "testAccessKey",
"AWS_SECRET_ACCESS_KEY": "testSecretAccessKey",
"AWS_SESSION_TOKEN": "testToken",
"AWS_SECURITY_TOKEN": "testToken",
"AWS_EXPIRATION_DATE": "2015-01-01T12:34:56Z"},
msg='Should be the same')
self.assertEqual(result, {
"AWS_ACCESS_KEY_ID": "testAccessKey",
"AWS_SECRET_ACCESS_KEY": "testSecretAccessKey",
"AWS_SESSION_TOKEN": "testToken",
"AWS_SECURITY_TOKEN": "testToken",
"AWS_EXPIRATION_DATE": "2015-01-01T12:34:56Z"},
msg='Should be the same')

0 comments on commit 38f2cfa

Please sign in to comment.