1+ import json
2+
13from aioresponses import aioresponses
24from bento_lib .auth .types import EvaluationResultMatrix
3- from rest_framework .test import APITestCase
5+ from rest_framework .test import APITransactionTestCase
46from typing import Literal
57
68from ..types import DataPermissionsDict
79
810
911__all__ = [
10- "mock_authz_eval_one_result" ,
11- "mock_authz_eval_result" ,
1212 "DTAccessLevel" ,
1313 "AuthzAPITestCase" ,
1414 "PermissionsTestCaseMixin" ,
1515]
1616
1717
18- def mock_authz_eval_one_result (m : aioresponses , result : bool ):
19- m .post ("http://authz.local/policy/evaluate" , payload = {"result" : [[result ]]})
20-
21-
22- def mock_authz_eval_result (m : aioresponses , result : EvaluationResultMatrix | list [list [bool ]]):
23- m .post ("http://authz.local/policy/evaluate" , payload = {"result" : result })
24-
25-
2618DTAccessLevel = Literal ["none" , "bool" , "counts" , "full" ]
2719
2820
29- class AuthzAPITestCase (APITestCase ):
21+ class AuthzAPITestCase (APITransactionTestCase ):
3022 # data type permissions: bool, counts, data
3123 dt_none_eval_res = [[False , False , False ]]
3224 dt_bool_eval_res = [[True , False , False ]]
@@ -42,44 +34,65 @@ class AuthzAPITestCase(APITestCase):
4234
4335 # ------------------------------------------------------------------------------------------------------------------
4436
45- def _one_authz_post (self , authz_res : bool , url : str , * args , ** kwargs ):
37+ @staticmethod
38+ def mock_authz_eval_one_result (m : aioresponses , result : bool ):
39+ m .post ("http://authz.local/policy/evaluate" , payload = {"result" : [[result ]]})
40+
41+ @staticmethod
42+ def mock_authz_eval_result (m : aioresponses , result : EvaluationResultMatrix | list [list [bool ]]):
43+ m .post ("http://authz.local/policy/evaluate" , payload = {"result" : result })
44+
45+ # ------------------------------------------------------------------------------------------------------------------
46+
47+ def _one_authz_generic (
48+ self , method : Literal ["get" , "post" , "put" , "patch" , "delete" ], authz_res : bool , url : str , * args , ** kwargs
49+ ):
50+ if "json" in kwargs :
51+ kwargs ["data" ] = json .dumps (kwargs ["json" ])
52+ del kwargs ["json" ]
53+
54+ if method in ("post" , "put" , "patch" ) and "format" not in kwargs :
55+ kwargs ["content_type" ] = "application/json"
56+
4657 with aioresponses () as m :
47- mock_authz_eval_one_result (m , authz_res )
48- return self .client .post (url , * args , content_type = "application/json" , ** kwargs )
58+ self .mock_authz_eval_one_result (m , authz_res )
59+ return getattr (self .client , method )(url , * args , ** kwargs )
60+
61+ def _one_authz_get (self , authz_res : bool , url : str , * args , ** kwargs ):
62+ return self ._one_authz_generic ("get" , authz_res , url , * args , ** kwargs )
63+
64+ def one_authz_get (self , url : str , * args , ** kwargs ):
65+ """Mocks a single True response from the authorization service and executes a GET request."""
66+ return self ._one_authz_get (True , url , * args , ** kwargs )
67+
68+ def one_no_authz_get (self , url : str , * args , ** kwargs ):
69+ """Mocks a single False response from the authorization service and executes a GET request."""
70+ return self ._one_authz_get (False , url , * args , ** kwargs )
71+
72+ def _one_authz_post (self , authz_res : bool , url : str , * args , ** kwargs ):
73+ return self ._one_authz_generic ("post" , authz_res , url , * args , ** kwargs )
4974
5075 def one_authz_post (self , url : str , * args , ** kwargs ):
51- """
52- Mocks a single True response from the authorization service and executes a JSON POST request.
53- """
76+ """Mocks a single True response from the authorization service and executes a JSON POST request."""
5477 return self ._one_authz_post (True , url , * args , ** kwargs )
5578
5679 def one_no_authz_post (self , url : str , * args , ** kwargs ):
57- """
58- Mocks a single False response from the authorization service and executes a JSON POST request.
59- """
80+ """Mocks a single False response from the authorization service and executes a JSON POST request."""
6081 return self ._one_authz_post (False , url , * args , ** kwargs )
6182
6283 def _one_authz_put (self , authz_res : bool , url : str , * args , ** kwargs ):
63- with aioresponses () as m :
64- mock_authz_eval_one_result (m , authz_res )
65- return self .client .put (url , * args , content_type = "application/json" , ** kwargs )
84+ return self ._one_authz_generic ("put" , authz_res , url , * args , ** kwargs )
6685
6786 def one_authz_put (self , url : str , * args , ** kwargs ):
68- """
69- Mocks a single True response from the authorization service and executes a JSON PUT request.
70- """
87+ """Mocks a single True response from the authorization service and executes a JSON PUT request."""
7188 return self ._one_authz_put (True , url , * args , ** kwargs )
7289
7390 def one_no_authz_put (self , url : str , * args , ** kwargs ):
74- """
75- Mocks a single False response from the authorization service and executes a JSON PUT request.
76- """
91+ """Mocks a single False response from the authorization service and executes a JSON PUT request."""
7792 return self ._one_authz_put (False , url , * args , ** kwargs )
7893
7994 def _one_authz_patch (self , authz_res : bool , url : str , * args , ** kwargs ):
80- with aioresponses () as m :
81- mock_authz_eval_one_result (m , authz_res )
82- return self .client .patch (url , * args , content_type = "application/json" , ** kwargs )
95+ return self ._one_authz_generic ("patch" , authz_res , url , * args , ** kwargs )
8396
8497 def one_authz_patch (self , url : str , * args , ** kwargs ):
8598 """
@@ -89,12 +102,12 @@ def one_authz_patch(self, url: str, *args, **kwargs):
89102
90103 def _one_authz_delete (self , authz_res : bool , url : str , * args , ** kwargs ):
91104 with aioresponses () as m :
92- mock_authz_eval_one_result (m , authz_res )
105+ self . mock_authz_eval_one_result (m , authz_res )
93106 return self .client .delete (url , * args , ** kwargs )
94107
95108 async def _async_one_authz_delete (self , authz_res : bool , url : str , * args , ** kwargs ):
96109 with aioresponses () as m :
97- mock_authz_eval_one_result (m , authz_res )
110+ self . mock_authz_eval_one_result (m , authz_res )
98111 return await self .async_client .delete (url , * args , ** kwargs )
99112
100113 def one_authz_delete (self , url : str , * args , ** kwargs ):
@@ -119,12 +132,12 @@ def one_no_authz_delete(self, url: str, *args, **kwargs):
119132
120133 def dt_get (self , level : Literal ["none" , "bool" , "counts" , "full" ], url : str , * args , ** kwargs ):
121134 with aioresponses () as m :
122- mock_authz_eval_result (m , self .dt_levels [level ]) # data type permissions: bool, counts, data
135+ self . mock_authz_eval_result (m , self .dt_levels [level ]) # data type permissions: bool, counts, data
123136 return self .client .get (url , * args , ** kwargs )
124137
125138 def dt_post (self , level : Literal ["none" , "bool" , "counts" , "full" ], url : str , * args , ** kwargs ):
126139 with aioresponses () as m :
127- mock_authz_eval_result (m , self .dt_levels [level ]) # data type permissions: bool, counts, data
140+ self . mock_authz_eval_result (m , self .dt_levels [level ]) # data type permissions: bool, counts, data
128141 return self .client .post (url , * args , ** kwargs )
129142
130143 def dt_authz_none_get (self , url : str , * args , ** kwargs ):
@@ -136,6 +149,9 @@ def dt_authz_bool_get(self, url: str, *args, **kwargs):
136149 def dt_authz_counts_get (self , url : str , * args , ** kwargs ):
137150 return self .dt_get ("counts" , url , * args , ** kwargs )
138151
152+ def dt_authz_counts_post (self , url : str , * args , ** kwargs ):
153+ return self .dt_post ("counts" , url , * args , ** kwargs )
154+
139155 def dt_authz_full_get (self , url : str , * args , ** kwargs ):
140156 return self .dt_get ("full" , url , * args , ** kwargs )
141157
0 commit comments