Skip to content

Commit 4085943

Browse files
committed
add tests for all updates with token update fn
1 parent 61ca857 commit 4085943

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

consulate/api/acl.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,45 @@ def create_token(self,
237237
roles=roles,
238238
service_identities=service_identities)))
239239

240+
def update_token(self,
241+
accessor_id,
242+
description=None,
243+
expiration_time=None,
244+
expiration_ttl=None,
245+
local=False,
246+
policies=None,
247+
roles=None,
248+
secret_id=None,
249+
service_identities=None):
250+
"""Create a token from the roles, policies, and service identities
251+
provided.
252+
253+
:param str accessor_id: A UUID for accessing the token.
254+
:param str description: A human-readable description of the token.
255+
:param str expiration_time: The amount of time till the token expires.
256+
:param str expiration_ttl: Sets expiration_time to creation time +
257+
expiration_ttl value.
258+
:param bool local: Whether the token is only locally available in the
259+
current datacenter or to all datacenters defined.
260+
:param PolicyLinks policies: A PolicyLink array.
261+
:param RoleLinks roles: A RoleLink array.
262+
:param str secret_id: A UUID for making requests to consul.
263+
:param ServiceIdentities service_identities: A ServiceIdentity array.
264+
:param rtype: dict
265+
266+
"""
267+
return self._put_response_body(
268+
["token", accessor_id], {},
269+
dict(
270+
model.ACLToken(accessor_id=accessor_id,
271+
description=description,
272+
expiration_time=expiration_time,
273+
expiration_ttl=expiration_ttl,
274+
local=local,
275+
policies=policies,
276+
roles=roles,
277+
service_identities=service_identities)))
278+
240279
def delete_token(self, accessor_id):
241280
"""Delete an existing token with the given AcccessorID.
242281

tests/acl_tests.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,23 @@
2828
}
2929
"""
3030

31+
ACL_NEW_UPDATE_RULES = """key_prefix "" {
32+
policy = "deny"
33+
}
34+
key "foo/" {
35+
policy = "read"
36+
}
37+
"""
38+
3139
POLICYLINKS_SAMPLE = [
3240
dict(ID="783beef3-783f-f41f-7422-7087dc272765"),
3341
]
3442

43+
POLICYLINKS_UPDATE_SAMPLE = [
44+
dict(ID="783beef3-783f-f41f-7422-7087dc272765"),
45+
dict(Name="some_policy_name")
46+
]
47+
3548
SERVICE_IDENTITIES_SAMPLE = [dict(ServiceName="db", Datacenters=["dc1"])]
3649

3750
ROLELINKS_SAMPLE = [dict(Name="some_role_name")]
@@ -162,6 +175,14 @@ def test_create_and_read_policy(self):
162175
result = self.consul.acl.read_policy(value["ID"])
163176
self.assertEqual(result['Rules'], ACL_NEW_RULES)
164177

178+
def test_create_and_update_policy(self):
179+
value = self.consul.acl.create_policy("unittest_read_policy",
180+
rules=ACL_NEW_RULES)
181+
result = self.consul.acl.update_policy(value["ID"],
182+
value["Name"],
183+
policy=ACL_NEW_UPDATE_RULES)
184+
self.assertGreater(result["ModifyIndex"], result["CreateIndex"])
185+
165186
def test_create_and_delete_policy(self):
166187
value = self.consul.acl.create_policy("unittest_delete_policy",
167188
rules=ACL_NEW_RULES)
@@ -190,6 +211,15 @@ def test_create_and_read_role(self):
190211
self.assertEqual(result['Policies'][0]['ID'],
191212
POLICYLINKS_SAMPLE[0]["ID"])
192213

214+
def test_create_and_update_role(self):
215+
value = self.consul.acl.create_role(
216+
"unittest_read_role",
217+
policies=POLICYLINKS_SAMPLE,
218+
service_identities=SERVICE_IDENTITIES_SAMPLE)
219+
result = self.consul.acl.update_role(
220+
value["ID"], policies=POLICYLINKS_UPDATE_SAMPLE)
221+
self.assertGreater(result["ModifyIndex"], result["CreateIndex"])
222+
193223
def test_create_and_delete_role(self):
194224
value = self.consul.acl.create_role(
195225
"unittest_delete_role",
@@ -227,6 +257,19 @@ def test_create_and_read_token(self):
227257
result = self.consul.acl.read_token(value["AccessorID"])
228258
self.assertEqual(result['AccessorID'], accessor_id)
229259

260+
def test_create_and_update_token(self):
261+
secret_id = self.uuidv4()
262+
accessor_id = self.uuidv4()
263+
value = self.consul.acl.create_token(
264+
accessor_id=accessor_id,
265+
secret_id=secret_id,
266+
roles=ROLELINKS_SAMPLE,
267+
policies=POLICYLINKS_SAMPLE,
268+
service_identities=SERVICE_IDENTITIES_SAMPLE)
269+
result = self.consul.acl.update_token(
270+
value["AccessorID"], policies=POLICYLINKS_UPDATE_SAMPLE)
271+
self.assertGreater(result["ModifyIndex"], result["CreateIndex"])
272+
230273
def test_create_and_delete_token(self):
231274
secret_id = self.uuidv4()
232275
accessor_id = self.uuidv4()

0 commit comments

Comments
 (0)