Skip to content
Permalink
Browse files

Add pretty_print arg to create_or_update_policy (#342)

* Add pretty_print arg

* Skip new test on Vault v0.11.0
  • Loading branch information...
jeffwecan committed Nov 27, 2018
1 parent 7f9a593 commit 517901a1d4942b9e753618a085a6607e863cc76d
@@ -37,7 +37,7 @@ def read_policy(self, name):
)
return response.json()

def create_or_update_policy(self, name, policy):
def create_or_update_policy(self, name, policy, pretty_print=True):
"""Add a new or update an existing policy.
Once a policy is updated, it takes effect immediately to all associated users.
@@ -49,11 +49,17 @@ def create_or_update_policy(self, name, policy):
:type name: str | unicode
:param policy: Specifies the policy document.
:type policy: str | unicode | dict
:param pretty_print: If True, and provided a dict for the policy argument, send the policy JSON to Vault with
"pretty" formatting.
:type pretty_print: bool
:return: The response of the request.
:rtype: requests.Response
"""
if isinstance(policy, dict):
policy = json.dumps(policy)
if pretty_print:
policy = json.dumps(policy, indent=4, sort_keys=True)
else:
policy = json.dumps(policy)
params = {
'policy': policy,
}
@@ -1,12 +1,60 @@
from unittest import TestCase
from unittest import skipIf
import json
import logging
from unittest import TestCase, skipIf

from parameterized import parameterized, param

from tests import utils
from tests.utils.hvac_integration_test_case import HvacIntegrationTestCase


@skipIf(utils.skip_if_vault_version_lt('0.9.0'), "Policy class uses new parameters added >= Vault 0.9.0")
class TestPolicy(HvacIntegrationTestCase, TestCase):
TEST_POLICY_NAME = 'test-policy-policy'

def tearDown(self):
self.client.sys.delete_policy(
name=self.TEST_POLICY_NAME,
)
super(TestPolicy, self).tearDown()

@parameterized.expand([
param(
'success',
),
param(
'pretty print false',
pretty_print=False,
),
])
@skipIf(utils.skip_if_vault_version_eq('0.11.0'), "Policy parsing broken in Vault version 0.11.0")
def test_create_or_update_policy(self, label, pretty_print=True):
test_policy = {
'path': {
'test-path': {
'capabilities': ['read'],
},
},
}
create_policy_response = self.client.sys.create_or_update_policy(
name=self.TEST_POLICY_NAME,
policy=test_policy,
pretty_print=pretty_print,
)
logging.debug('create_policy_response: %s' % create_policy_response)
self.assertEqual(
first=create_policy_response.status_code,
second=204,
)

read_policy_response = self.client.sys.read_policy(
name=self.TEST_POLICY_NAME,
)
logging.debug('read_policy_response: %s' % read_policy_response)
self.assertDictEqual(
d1=json.loads(read_policy_response['data']['rules']),
d2=test_policy,
)

def test_policy_manipulation(self):
self.assertIn(
@@ -43,6 +43,10 @@ def skip_if_vault_version_ge(supported_version):
return skip_if_vault_version(supported_version, comparison=operator.ge)


def skip_if_vault_version_eq(supported_version):
return skip_if_vault_version(supported_version, comparison=operator.eq)


def create_client(**kwargs):
"""Small helper to instantiate a :py:class:`hvac.v1.Client` class with the appropriate parameters for the test env.

0 comments on commit 517901a

Please sign in to comment.
You can’t perform that action at this time.