33
44import pytest
55from http import HTTPStatus
6+
7+ from sqlmodel import select
68from starlette .testclient import TestClient
79import responses
810
9- from database .authorization import register_user , PermissionType , Permission
11+ from database .authorization import register_user , PermissionType , Permission , set_permission
1012from database .model .agent .organisation import Organisation
1113from database .session import DbSession
1214
@@ -80,7 +82,7 @@ def test_add_permission_by_name(
8082 )
8183 request_mock .add (
8284 responses .GET ,
83- "http://keycloak:8080/aiod-auth/admin/realms/aiod/users?username=Bob&exact=True& max=100&first=0 " ,
85+ "http://keycloak:8080/aiod-auth/admin/realms/aiod/users?username=bob& max=1&exact=True " ,
8486 json = users_response ,
8587 )
8688
@@ -96,3 +98,57 @@ def test_add_permission_by_name(
9698 permission = session .get (Permission , {"aiod_entry_identifier" : 1 , "user_identifier" : BOB ._subject_identifier })
9799 assert permission is not None
98100 assert permission .type_ == PermissionType .WRITE
101+
102+
103+ def test_show_permission (
104+ client : TestClient ,
105+ publication : Publication ,
106+ ):
107+ identifier = register_asset (publication , owner = ALICE )
108+ with DbSession () as session :
109+ register_user (BOB , session )
110+ publication = session .scalar (select (Publication ))
111+ set_permission (BOB , publication .aiod_entry , session , type_ = PermissionType .WRITE )
112+ session .commit ()
113+
114+ cached_api_token = path_test_resources () / "authentication" / "admin_connect.json"
115+ with cached_api_token .open ("r" ) as f :
116+ connect_response = json .load (f )
117+
118+ cached_user_response = path_test_resources () / "authentication" / "query_alice.json"
119+ with cached_user_response .open ("r" ) as f :
120+ alice_response = json .load (f )
121+ alice_response = alice_response [0 ] # default json returns "multiple" user response
122+
123+ cached_user_response = path_test_resources () / "authentication" / "query_bob.json"
124+ with cached_user_response .open ("r" ) as f :
125+ bob_response = json .load (f )
126+ bob_response = bob_response [0 ] # default json returns "multiple" user response
127+
128+ # The second time around the cached KeycloakAdmin is used, so the connect is not called
129+ with responses .RequestsMock (assert_all_requests_are_fired = False ) as request_mock :
130+ request_mock .add (
131+ responses .POST ,
132+ "http://keycloak:8080/aiod-auth/realms/aiod/protocol/openid-connect/token" ,
133+ json = connect_response ,
134+ )
135+ request_mock .add (
136+ responses .GET ,
137+ "http://keycloak:8080/aiod-auth/admin/realms/aiod/users/bob00000-0000-0000-0000-000000000000" ,
138+ json = bob_response ,
139+ )
140+
141+ request_mock .add (
142+ responses .GET ,
143+ "http://keycloak:8080/aiod-auth/admin/realms/aiod/users/alice000-0000-0000-0000-000000000000" ,
144+ json = alice_response ,
145+ )
146+
147+ with logged_in_user (ALICE ):
148+ response = client .get (
149+ f"/assets/permissions/{ identifier } " ,
150+ headers = {"Authorization" : "fake-token" },
151+ )
152+ assert response .status_code == HTTPStatus .OK
153+ server_permissions = {p ["name" ]: p ["permission" ] for p in response .json ()}
154+ assert server_permissions == {"Alice" : "admin" , "Bob" : "write" }
0 commit comments