Skip to content

Commit

Permalink
Add list method to Adapter Class (#235)
Browse files Browse the repository at this point in the history
* Update test case method name to better match purpose

* assertEquals is deprecated, using assertEqual instead

* add list method
  • Loading branch information
jeffwecan committed Jul 30, 2018
1 parent b80f003 commit f57f38d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
13 changes: 13 additions & 0 deletions hvac/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ def delete(self, url, **kwargs):
"""
return self.request('delete', url, **kwargs)

def list(self, url, **kwargs):
"""Performs a LIST request.
:param url: Partial URL path to send the request to. This will be joined to the end of the instance's base_uri
attribute.
:type url: str | unicode
:param kwargs: Additional keyword arguments to include in the requests call.
:type kwargs: dict
:return: The response of the request.
:rtype: requests.Response
"""
return self.request('list', url, **kwargs)

def auth(self, url, use_token=True, **kwargs):
"""Performs a request (typically to a path prefixed with "/v1/auth") and optionaly stores the client token sent
in the resulting Vault response for use by the :py:meth:`hvac.adapters.Adapter` instance under the _adapater
Expand Down
41 changes: 39 additions & 2 deletions hvac/tests/test_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TestRequest(TestCase):
("Vault address with route", 'https://example.com/vault'),
])
@requests_mock.Mocker()
def test___request(self, test_label, test_url, requests_mocker):
def test_get(self, test_label, test_url, requests_mocker):
test_path = 'v1/sys/health'
expected_status_code = 200
mock_url = '{0}/{1}'.format(test_url, test_path)
Expand All @@ -26,7 +26,44 @@ def test___request(self, test_label, test_url, requests_mocker):
response = adapter.get(
url='v1/sys/health',
)
self.assertEquals(
self.assertEqual(
first=expected_status_code,
second=response.status_code,
)

@parameterized.expand([
("kv secret lookup", 'v1/secret/some-secret'),
])
@requests_mock.Mocker()
def test_list(self, test_label, test_path, requests_mocker):
mock_response = {
'auth': None,
'data': {
'keys': ['things1', 'things2']
},
'lease_duration': 0,
'lease_id': '',
'renewable': False,
'request_id': 'ba933afe-84d4-410f-161b-592a5c016009',
'warnings': None,
'wrap_info': None
}
expected_status_code = 200
mock_url = '{0}/{1}'.format(adapters.DEFAULT_BASE_URI, test_path)
requests_mocker.register_uri(
method='LIST',
url=mock_url,
json=mock_response
)
adapter = adapters.Request()
response = adapter.list(
url=test_path,
)
self.assertEqual(
first=expected_status_code,
second=response.status_code,
)
self.assertEqual(
first=mock_response,
second=response.json()
)

0 comments on commit f57f38d

Please sign in to comment.