Skip to content

Commit c73b62d

Browse files
committed
Revise controller integration tests for access permissions
Improves the existing controller access levels and adds a test for model access permissions
1 parent 49a65ab commit c73b62d

1 file changed

Lines changed: 74 additions & 40 deletions

File tree

tests/integration/test_controller.py

Lines changed: 74 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
import uuid
33
import hvac
44

5+
from juju import access
56
from juju.client.connection import Connection
67
from juju.client import client
7-
from juju.errors import JujuAPIError
8+
from juju.errors import JujuAPIError, JujuError
89

910
import pytest
1011

@@ -105,26 +106,6 @@ async def test_reset_user_password(event_loop):
105106
raise AssertionError()
106107

107108

108-
@base.bootstrapped
109-
@pytest.mark.asyncio
110-
async def test_grant_revoke(event_loop):
111-
async with base.CleanController() as controller:
112-
username = 'test-grant{}'.format(uuid.uuid4())
113-
user = await controller.add_user(username)
114-
await user.grant('superuser')
115-
assert user.access == 'superuser'
116-
fresh = await controller.get_user(username) # fetch fresh copy
117-
assert fresh.access == 'superuser'
118-
await user.grant('login') # already has 'superuser', so no-op
119-
assert user.access == 'superuser'
120-
fresh = await controller.get_user(username) # fetch fresh copy
121-
assert fresh.access == 'superuser'
122-
await user.revoke()
123-
assert user.access == ''
124-
fresh = await controller.get_user(username) # fetch fresh copy
125-
assert fresh.access == ''
126-
127-
128109
@base.bootstrapped
129110
@pytest.mark.asyncio
130111
async def test_list_models(event_loop):
@@ -134,25 +115,6 @@ async def test_list_models(event_loop):
134115
assert model.name in result
135116

136117

137-
@base.bootstrapped
138-
@pytest.mark.asyncio
139-
async def test_list_models_user_access(event_loop):
140-
async with base.CleanController() as controller:
141-
username = 'test-grant{}'.format(uuid.uuid4())
142-
user = await controller.add_user(username)
143-
await user.grant(acl='superuser')
144-
assert user.access == 'superuser'
145-
models1 = await controller.list_models(username)
146-
await user.revoke(acl='superuser')
147-
models2 = await controller.list_models(username)
148-
assert len(models1) > len(models2)
149-
150-
# testing all flag
151-
await user.grant(acl='superuser')
152-
models_all = await controller.list_models(username, all=True)
153-
assert len(models_all) > len(models2)
154-
155-
156118
@base.bootstrapped
157119
@pytest.mark.asyncio
158120
async def test_get_model(event_loop):
@@ -314,3 +276,75 @@ async def test_secrets_backend_lifecycle(event_loop):
314276
list_after = await controller.list_secret_backends()
315277
assert len(list_after["results"]) == 1
316278
assert list_after["results"][0]["result"].name == "internal"
279+
280+
281+
@base.bootstrapped
282+
@pytest.mark.asyncio
283+
async def test_grant_revoke_controller_access(event_loop):
284+
async with base.CleanController() as controller:
285+
username = 'test-grant{}'.format(uuid.uuid4())
286+
user = await controller.add_user(username)
287+
await user.grant('superuser')
288+
assert user.access == 'superuser'
289+
fresh = await controller.get_user(username) # fetch fresh copy
290+
assert fresh.access == 'superuser'
291+
await user.grant('login') # already has 'superuser', so no-op
292+
assert user.access == 'superuser'
293+
fresh = await controller.get_user(username) # fetch fresh copy
294+
assert fresh.access == 'superuser'
295+
await user.revoke()
296+
assert user.access == ''
297+
fresh = await controller.get_user(username) # fetch fresh copy
298+
assert fresh.access == ''
299+
try:
300+
# try removing the created user
301+
await controller.remove_user(username)
302+
except JujuError as e:
303+
if 'state changing too quickly' in str(e):
304+
pass
305+
else:
306+
raise
307+
308+
309+
@base.bootstrapped
310+
@pytest.mark.asyncio
311+
async def test_grant_revoke_model_access(event_loop):
312+
async with base.CleanController() as controller:
313+
username = 'test-grant{}'.format(uuid.uuid4())
314+
user = await controller.add_user(username)
315+
316+
model_name = 'test-{}'.format(uuid.uuid4())
317+
model = await controller.add_model(model_name)
318+
319+
with pytest.raises(JujuError):
320+
# superuser is a controller access level, i.e. not a valid model acl
321+
await user.grant('superuser', model_name=model_name)
322+
323+
models1 = await controller.list_models(username)
324+
assert models1 == []
325+
326+
# grant user the access to see the model
327+
await user.grant(access.READ_ACCESS, model_name=model_name)
328+
models2 = await controller.list_models(username)
329+
330+
# assert that the user sees the model
331+
assert model_name in models2
332+
333+
# now let's revoke the read access
334+
await user.revoke(access.READ_ACCESS, model_name=model_name)
335+
models3 = await controller.list_models(username)
336+
337+
# user shouldn't be able to see the model
338+
assert models3 == []
339+
340+
# cleanup
341+
await model.disconnect()
342+
await controller.destroy_model(model_name)
343+
try:
344+
# try removing the created user
345+
await controller.remove_user(username)
346+
except JujuError as e:
347+
if 'state changing too quickly' in str(e):
348+
pass
349+
else:
350+
raise

0 commit comments

Comments
 (0)