Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing get_credential and update_credential methods #45

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions confidant_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,64 @@ def update_blind_credential(
ret['result'] = True
return ret

def get_credential(self, id):
"""Retrieves a standard credential in Confidant by id"""
# Return a dict, always with an attribute that specifies whether or not
# the function was able to successfully get a result.
ret = {'result': False}

# Make a request to confidant with the provided url, to fetch the
# service providing the service name and base64 encoded
# token for authentication.

try:
response = self._execute_request(
'get',
'{0}/v1/credentials/{1}'.format(self.config['url'], id),
expected_return_codes=[200, 404]
)
except RequestExecutionError:
logging.exception('Error with executing request')
return ret
if response.status_code == 404:
logging.debug('Specified credential not found in confidant.')
ret['result'] = False
return ret
data = response.json()

ret['result'] = True
ret['credential'] = data
return ret

def update_credential(
self,
id,
credential_pairs):
"""Updates a standard credential in Confidant by id"""
# Return a dict, always with an attribute that specifies whether or not
# the function was able to successfully get a result.
ret = {'result': False}

try:
response = self._execute_request(
'put',
'{0}/v1/credentials/{1}'.format(self.config['url'], id),
json=json.dumps(credential_pairs),
expected_return_codes=[200, 404]
)
except RequestExecutionError:
logging.exception('Error with executing request')
return ret
if response.status_code == 404:
logging.debug('Speficified credential not found in confidant.')
ret['result'] = False
return ret
data = response.json()

ret['result'] = True
ret['credential'] = data
return ret

def list_blind_credentials(self):
"""Get a list of blind credentials."""
# Return a dict, always with an attribute that specifies whether or not
Expand Down
91 changes: 91 additions & 0 deletions tests/unit/confidant_client/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,94 @@ def test_update_blind_credential(self):
)
# TODO: test all arguments
# TODO: test request exceptions

# @patch(
# 'confidant_client.services.get_boto_client',
# MagicMock()
# )
# def test_get_credential(self):
# client = confidant_client.ConfidantClient(
# 'http://localhost/',
# 'alias/authnz-testing',
# {'from': 'confidant-unittest',
# 'to': 'test',
# 'user_type': 'service'},
# )
# client._get_token = MagicMock()
# # Test 404. Should return True with no service entry since the call
# # succeeded, but the credential didn't exist.
# client.request_session.request = mock_404
# self.maxDiff = None
# self.assertEqual(
# client.get_credential(
# '12345',
# False
# ),
# {'result': True}
# )
# # Test 200. Should return True with an empty dict, since that's how we
# # have the service mocked out.
# client.request_session.request = mock_200
# self.assertEqual(
# client.get_credential(
# '12345',
# False
# ),
# {'result': True, 'credential': {}}
# )
# # Test 500. Should return False as the request failed.
# client.request_session.request = mock_500
# self.assertEqual(
# client.get_credential(
# '12345',
# False
# ),
# {'result': False}
# )

# @patch(
# 'confidant_client.services.get_boto_client',
# MagicMock()
# )
# def test_update_credential(self):
# client = confidant_client.ConfidantClient(
# 'http://localhost/',
# 'alias/authnz-testing',
# {'from': 'confidant-unittest',
# 'to': 'test',
# 'user_type': 'service'},
# )
# client._get_token = MagicMock()
# # Test 404. Should return True with no service entry since the call
# # succeeded, but the credential didn't exist.
# client.request_session.request = mock_404
# self.maxDiff = None
# self.assertEqual(
# client.update_credential(
# '12345',
# {'us-east-1', 'test'},
# False
# ),
# {'result': True}
# )
# # Test 200. Should return True with an empty dict, since that's how we
# # have the service mocked out.
# client.request_session.request = mock_200
# self.assertEqual(
# client.update_credential(
# '12345',
# {'us-east-1', 'test'},
# False
# ),
# {'result': True, 'credential': {}}
# )
# # Test 500. Should return False as the request failed.
# client.request_session.request = mock_500
# self.assertEqual(
# client.update_credential(
# '12345',
# {'us-east-1', 'test'},
# False
# ),
# {'result': False}
# )