|
14 | 14 |
|
15 | 15 | import casbin |
16 | 16 | from tests.test_enforcer import get_examples, TestCaseBase |
| 17 | +from unittest import IsolatedAsyncioTestCase |
17 | 18 |
|
18 | 19 |
|
19 | 20 | class SampleWatcher: |
@@ -113,6 +114,103 @@ def start_watch(self): |
113 | 114 | pass |
114 | 115 |
|
115 | 116 |
|
| 117 | +class AsyncSampleWatcher: |
| 118 | + def __init__(self): |
| 119 | + self.callback = None |
| 120 | + self.notify_message = None |
| 121 | + |
| 122 | + async def close(self): |
| 123 | + pass |
| 124 | + |
| 125 | + async def set_update_callback(self, callback): |
| 126 | + """ |
| 127 | + sets the callback function to be called when the policy is updated |
| 128 | + :param callable callback: callback(event) |
| 129 | + - event: event received from the rabbitmq |
| 130 | + :return: |
| 131 | + """ |
| 132 | + self.callback = callback |
| 133 | + |
| 134 | + async def update(self, msg): |
| 135 | + """ |
| 136 | + update the policy |
| 137 | + """ |
| 138 | + self.notify_message = msg |
| 139 | + return True |
| 140 | + |
| 141 | + async def update_for_add_policy(self, section, ptype, *params): |
| 142 | + """ |
| 143 | + update for add policy |
| 144 | + :param section: section |
| 145 | + :param ptype: policy type |
| 146 | + :param params: other params |
| 147 | + :return: True if updated |
| 148 | + """ |
| 149 | + message = "called add policy" |
| 150 | + return await self.update(message) |
| 151 | + |
| 152 | + async def update_for_remove_policy(self, section, ptype, *params): |
| 153 | + """ |
| 154 | + update for remove policy |
| 155 | + :param section: section |
| 156 | + :param ptype: policy type |
| 157 | + :param params: other params |
| 158 | + :return: True if updated |
| 159 | + """ |
| 160 | + message = "called remove policy" |
| 161 | + return await self.update(message) |
| 162 | + |
| 163 | + async def update_for_remove_filtered_policy(self, section, ptype, field_index, *params): |
| 164 | + """ |
| 165 | + update for remove filtered policy |
| 166 | + :param section: section |
| 167 | + :param ptype: policy type |
| 168 | + :param field_index: field index |
| 169 | + :param params: other params |
| 170 | + :return: |
| 171 | + """ |
| 172 | + message = "called remove filtered policy" |
| 173 | + return await self.update(message) |
| 174 | + |
| 175 | + async def update_for_save_policy(self, model: casbin.Model): |
| 176 | + """ |
| 177 | + update for save policy |
| 178 | + :param model: casbin model |
| 179 | + :return: |
| 180 | + """ |
| 181 | + message = "called save policy" |
| 182 | + return await self.update(message) |
| 183 | + |
| 184 | + async def update_for_add_policies(self, section, ptype, *params): |
| 185 | + """ |
| 186 | + update for add policies |
| 187 | + :param section: section |
| 188 | + :param ptype: policy type |
| 189 | + :param params: other params |
| 190 | + :return: |
| 191 | + """ |
| 192 | + message = "called add policies" |
| 193 | + return await self.update(message) |
| 194 | + |
| 195 | + async def update_for_remove_policies(self, section, ptype, *params): |
| 196 | + """ |
| 197 | + update for remove policies |
| 198 | + :param section: section |
| 199 | + :param ptype: policy type |
| 200 | + :param params: other params |
| 201 | + :return: |
| 202 | + """ |
| 203 | + message = "called remove policies" |
| 204 | + return await self.update(message) |
| 205 | + |
| 206 | + async def start_watch(self): |
| 207 | + """ |
| 208 | + starts the watch thread |
| 209 | + :return: |
| 210 | + """ |
| 211 | + pass |
| 212 | + |
| 213 | + |
116 | 214 | class TestWatcherEx(TestCaseBase): |
117 | 215 | def get_enforcer(self, model=None, adapter=None): |
118 | 216 | return casbin.Enforcer( |
@@ -187,3 +285,83 @@ def test_auto_notify_disabled(self): |
187 | 285 |
|
188 | 286 | e.remove_policies(rules) |
189 | 287 | self.assertEqual(w.notify_message, None) |
| 288 | + |
| 289 | + |
| 290 | +class TestAsyncWatcherEx(IsolatedAsyncioTestCase): |
| 291 | + def get_enforcer(self, model=None, adapter=None): |
| 292 | + return casbin.AsyncEnforcer( |
| 293 | + model, |
| 294 | + adapter, |
| 295 | + ) |
| 296 | + |
| 297 | + async def test_auto_notify_enabled(self): |
| 298 | + e = self.get_enforcer( |
| 299 | + get_examples("basic_model.conf"), |
| 300 | + get_examples("basic_policy.csv"), |
| 301 | + ) |
| 302 | + await e.load_policy() |
| 303 | + |
| 304 | + w = AsyncSampleWatcher() |
| 305 | + e.set_watcher(w) |
| 306 | + e.enable_auto_notify_watcher(True) |
| 307 | + |
| 308 | + await e.save_policy() |
| 309 | + self.assertEqual(w.notify_message, "called save policy") |
| 310 | + |
| 311 | + await e.add_policy("admin", "data1", "read") |
| 312 | + self.assertEqual(w.notify_message, "called add policy") |
| 313 | + |
| 314 | + await e.remove_policy("admin", "data1", "read") |
| 315 | + self.assertEqual(w.notify_message, "called remove policy") |
| 316 | + |
| 317 | + await e.remove_filtered_policy(1, "data1") |
| 318 | + self.assertEqual(w.notify_message, "called remove filtered policy") |
| 319 | + |
| 320 | + rules = [ |
| 321 | + ["jack", "data4", "read"], |
| 322 | + ["katy", "data4", "write"], |
| 323 | + ["leyo", "data4", "read"], |
| 324 | + ["ham", "data4", "write"], |
| 325 | + ] |
| 326 | + await e.add_policies(rules) |
| 327 | + self.assertEqual(w.notify_message, "called add policies") |
| 328 | + |
| 329 | + await e.remove_policies(rules) |
| 330 | + self.assertEqual(w.notify_message, "called remove policies") |
| 331 | + |
| 332 | + async def test_auto_notify_disabled(self): |
| 333 | + e = self.get_enforcer( |
| 334 | + get_examples("basic_model.conf"), |
| 335 | + get_examples("basic_policy.csv"), |
| 336 | + ) |
| 337 | + await e.load_policy() |
| 338 | + |
| 339 | + w = SampleWatcher() |
| 340 | + e.set_watcher(w) |
| 341 | + e.enable_auto_notify_watcher(False) |
| 342 | + |
| 343 | + await e.save_policy() |
| 344 | + self.assertEqual(w.notify_message, "called save policy") |
| 345 | + |
| 346 | + w.notify_message = None |
| 347 | + |
| 348 | + await e.add_policy("admin", "data1", "read") |
| 349 | + self.assertEqual(w.notify_message, None) |
| 350 | + |
| 351 | + await e.remove_policy("admin", "data1", "read") |
| 352 | + self.assertEqual(w.notify_message, None) |
| 353 | + |
| 354 | + await e.remove_filtered_policy(1, "data1") |
| 355 | + self.assertEqual(w.notify_message, None) |
| 356 | + |
| 357 | + rules = [ |
| 358 | + ["jack", "data4", "read"], |
| 359 | + ["katy", "data4", "write"], |
| 360 | + ["leyo", "data4", "read"], |
| 361 | + ["ham", "data4", "write"], |
| 362 | + ] |
| 363 | + await e.add_policies(rules) |
| 364 | + self.assertEqual(w.notify_message, None) |
| 365 | + |
| 366 | + await e.remove_policies(rules) |
| 367 | + self.assertEqual(w.notify_message, None) |
0 commit comments