|
1 | 1 | import pytest |
2 | 2 |
|
3 | 3 | from ansible_base.lib.utils.response import get_relative_url |
| 4 | +from ansible_base.rbac.remote import RemoteObject |
4 | 5 |
|
5 | 6 |
|
6 | 7 | @pytest.mark.django_db |
@@ -30,26 +31,25 @@ def test_create_remote_role_definition(admin_api_client, foo_type, foo_permissio |
30 | 31 | assert response.data['permissions'] == ['foo.foo_foo'] |
31 | 32 |
|
32 | 33 |
|
33 | | -# TODO: check that assignment endpoint works |
34 | | - |
35 | | -# @pytest.mark.django_db |
36 | | -# def test_give_remote_permission(rando, foo_type, foo_permission, foo_rd): |
37 | | -# assert foo_type.service == 'foo' # a place, a domain, a server, known as foo |
38 | | -# assert foo_type.api_slug == 'foo.foo' # there lives a foo in foo |
39 | | - |
40 | | -# assert foo_permission.api_slug == 'foo.foo_foo' # expression of the ability that one may foo a foo |
41 | | - |
42 | | -# a_foo = RemoteObject(content_type=foo_type, object_id=42) |
43 | | -# assignment = foo_rd.give_permission(rando, a_foo) |
44 | | - |
45 | | -# assignment = RoleUserAssignment.objects.get(pk=assignment.pk) |
46 | | -# assert isinstance(assignment.content_object, RemoteObject) |
47 | | - |
48 | | -# # We can do evaluation querysets, but these can not return objects, just id values |
49 | | -# assert set(foo_type.model_class().access_ids_qs(actor=rando, codename='foo')) == {(int(assignment.object_id),)} |
50 | | - |
51 | | -# # Test that user-attached methods also work |
52 | | -# assert rando.has_obj_perm(a_foo, 'foo') |
53 | | -# with pytest.raises(RuntimeError) as exc: |
54 | | -# assert not rando.has_obj_perm(a_foo, 'bar') # not a valid permission |
55 | | -# assert 'The permission bar_foo is not valid for model foo' in str(exc) |
| 34 | +@pytest.mark.django_db |
| 35 | +def test_give_remote_permission(admin_api_client, rando, foo_type, foo_rd): |
| 36 | + a_foo = RemoteObject(content_type=foo_type, object_id=42) |
| 37 | + assignment = foo_rd.give_permission(rando, a_foo) |
| 38 | + assignment.content_object |
| 39 | + |
| 40 | + assert isinstance(assignment.content_object, RemoteObject) |
| 41 | + |
| 42 | + # Should show up in the assignments list |
| 43 | + url = get_relative_url('roleuserassignment-list') |
| 44 | + response = admin_api_client.get(url, format="json") |
| 45 | + assert response.status_code == 200, response.data |
| 46 | + |
| 47 | + data_by_rd = {item['role_definition']: item for item in response.data['results']} |
| 48 | + assert foo_rd.id in data_by_rd |
| 49 | + item = data_by_rd[foo_rd.id] |
| 50 | + assert item['user'] == rando.id |
| 51 | + assert item['object_id'] == str(a_foo.object_id) |
| 52 | + assert 'summary_fields' in item |
| 53 | + sf = item['summary_fields'] |
| 54 | + assert 'content_object' in sf |
| 55 | + assert sf['content_object'] == {'<remote_object_placeholder>': True, 'model_name': 'foo', 'service': 'foo', 'pk': 42} |
0 commit comments