Skip to content

Commit

Permalink
clean up tests for acl
Browse files Browse the repository at this point in the history
  • Loading branch information
abaez committed Sep 17, 2019
1 parent 4b84119 commit d79796a
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions tests/acl_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"""

ACL_NEW_RULES = """key_prefix "" {
policy = "read
policy = "read"
}
key "foo/" {
policy = "write"
Expand All @@ -46,9 +46,7 @@
dict(Name="policylink_update_sample")
]

SERVICE_IDENTITIES_SAMPLE = [dict(ServiceName="db", Datacenters=["dc1"])]

ROLELINKS_SAMPLE = [dict(Name="role_sample")]
SERVICE_IDENTITIES_SAMPLE = [dict(ServiceName="db", Datacenters=list("dc1"))]


class TestCase(base.TestCase):
Expand All @@ -58,15 +56,16 @@ def uuidv4():

@staticmethod
def random():
return str(random.random())
return str(random.randint(0, 999999))

def _generate_policies(self):
sample = self.consul.acl.create_policy(self.random())
sample_update = self.consul.acl.create_policy(self.random())
return [dict(ID=sample["ID"]), dict(ID=sample_update["ID"])]

def _generate_roles(self):
return [dict(ID=self.consul.acl.create_role(self.random()))]
role = self.consul.acl.create_role(self.random())
return [dict(ID=role["ID"])]

def test_bootstrap_request_exception(self):
@httmock.all_requests
Expand Down Expand Up @@ -178,27 +177,27 @@ def test_update_forbidden(self):
# NOTE: Everything above here is deprecated post consul-1.4.0

def test_create_policy(self):
result = self.consul.acl.create_policy("unittest_create_policy",
rules=ACL_NEW_RULES)
name = self.random()
result = self.consul.acl.create_policy(name=name, rules=ACL_NEW_RULES)
self.assertEqual(result['Rules'], ACL_NEW_RULES)

def test_create_and_read_policy(self):
value = self.consul.acl.create_policy("unittest_read_policy",
rules=ACL_NEW_RULES)
name = self.random()
value = self.consul.acl.create_policy(name=name, rules=ACL_NEW_RULES)
result = self.consul.acl.read_policy(value["ID"])
self.assertEqual(result['Rules'], ACL_NEW_RULES)

def test_create_and_update_policy(self):
value = self.consul.acl.create_policy("unittest_update_policy",
rules=ACL_NEW_RULES)
name = self.random()
value = self.consul.acl.create_policy(name=name, rules=ACL_NEW_RULES)
result = self.consul.acl.update_policy(value["ID"],
value["Name"],
str(value["Name"]),
rules=ACL_NEW_UPDATE_RULES)
self.assertGreater(result["ModifyIndex"], result["CreateIndex"])

def test_create_and_delete_policy(self):
value = self.consul.acl.create_policy("unittest_delete_policy",
rules=ACL_NEW_RULES)
name = self.random()
value = self.consul.acl.create_policy(name=name, rules=ACL_NEW_RULES)
result = self.consul.acl.delete_policy(value["ID"])
self.assertTrue(result)

Expand All @@ -208,34 +207,38 @@ def test_list_policy_exception(self):
self.consul.acl.list_policies()

def test_create_role(self):
name = self.random()
result = self.consul.acl.create_role(
"unittest_create_role",
name=name,
policies=self._generate_policies(),
service_identities=SERVICE_IDENTITIES_SAMPLE,
self.assertEqual(result['ServiceIdentities'][0]['ID'], SERVICE_IDENTITIES_SAMPLE[0]['ID'])
service_identities=SERVICE_IDENTITIES_SAMPLE)
self.assertEqual(result['Name'], name)

def test_create_and_read_role(self):
name = self.random()
value = self.consul.acl.create_role(
"unittest_read_role",
name=name,
policies=self._generate_policies(),
service_identities=SERVICE_IDENTITIES_SAMPLE)
result = self.consul.acl.read_role(value["ID"])
self.assertEqual(result['ID'], value['ID'])

def test_create_and_update_role(self):
name = self.random()
value = self.consul.acl.create_role(
"unittest_update_role",
name=name,
policies=self._generate_policies(),
service_identities=SERVICE_IDENTITIES_SAMPLE)
result = self.consul.acl.update_role(
value["ID"],
"unittest_update_role",
policies=POLICYLINKS_UPDATE_SAMPLE)
str(value["Name"]),
policies=self._generate_policies())
self.assertGreater(result["ModifyIndex"], result["CreateIndex"])

def test_create_and_delete_role(self):
name = self.random()
value = self.consul.acl.create_role(
"unittest_delete_role",
name=name,
policies=self._generate_policies(),
service_identities=SERVICE_IDENTITIES_SAMPLE)
result = self.consul.acl.delete_role(value["ID"])
Expand Down Expand Up @@ -280,7 +283,7 @@ def test_create_and_update_token(self):
policies=self._generate_policies(),
service_identities=SERVICE_IDENTITIES_SAMPLE)
result = self.consul.acl.update_token(
value["AccessorID"], policies=self._generate_policies())
str(value["AccessorID"]), policies=self._generate_policies())
self.assertGreater(result["ModifyIndex"], result["CreateIndex"])

def test_create_and_clone_token(self):
Expand All @@ -290,11 +293,11 @@ def test_create_and_clone_token(self):
value = self.consul.acl.create_token(
accessor_id=accessor_id,
secret_id=secret_id,
roles=ROLELINKS_SAMPLE,
policies=POLICYLINKS_SAMPLE,
roles=self._generate_roles(),
policies=self._generate_policies(),
service_identities=SERVICE_IDENTITIES_SAMPLE)
result = self.consul.acl.clone_token(value["AccessorID"],
description="clone")
description=clone_description)
self.assertEqual(result["Description"], clone_description)

def test_create_and_delete_token(self):
Expand All @@ -303,8 +306,8 @@ def test_create_and_delete_token(self):
value = self.consul.acl.create_token(
accessor_id=accessor_id,
secret_id=secret_id,
roles=ROLELINKS_SAMPLE,
policies=POLICYLINKS_SAMPLE,
roles=self._generate_roles(),
policies=self._generate_policies(),
service_identities=SERVICE_IDENTITIES_SAMPLE)
result = self.consul.acl.delete_token(value["AccessorID"])
self.assertTrue(result)

0 comments on commit d79796a

Please sign in to comment.