|
@@ -1,4 +1,4 @@
|
|
|
-from contextlib import ExitStack
|
|
|
+from contextlib import ContextDecorator, ExitStack, contextmanager
|
|
|
from functools import wraps
|
|
|
from unittest.mock import patch
|
|
|
|
|
@@ -7,7 +7,7 @@ from .useracl import get_user_acl
|
|
|
__all__ = ["patch_user_acl"]
|
|
|
|
|
|
|
|
|
-class patch_user_acl(ExitStack):
|
|
|
+class patch_user_acl(ContextDecorator, ExitStack):
|
|
|
"""Testing utility that patches get_user_acl results
|
|
|
|
|
|
Can be used as decorator or context manager.
|
|
@@ -19,9 +19,11 @@ class patch_user_acl(ExitStack):
|
|
|
Patch should be a dict or callable.
|
|
|
"""
|
|
|
|
|
|
- def __init__(self, *patches):
|
|
|
+ _acl_patches = []
|
|
|
+
|
|
|
+ def __init__(self, acl_patch):
|
|
|
super().__init__()
|
|
|
- self._patches = patches
|
|
|
+ self.acl_patch = acl_patch
|
|
|
|
|
|
def patched_get_user_acl(self, user, cache_versions):
|
|
|
user_acl = get_user_acl(user, cache_versions)
|
|
@@ -29,7 +31,7 @@ class patch_user_acl(ExitStack):
|
|
|
return user_acl
|
|
|
|
|
|
def apply_acl_patches(self, user, user_acl):
|
|
|
- for acl_patch in self._patches:
|
|
|
+ for acl_patch in self._acl_patches:
|
|
|
self.apply_acl_patch(user, user_acl, acl_patch)
|
|
|
|
|
|
def apply_acl_patch(self, user, user_acl, acl_patch):
|
|
@@ -40,21 +42,19 @@ class patch_user_acl(ExitStack):
|
|
|
|
|
|
def __enter__(self):
|
|
|
super().__enter__()
|
|
|
- self.enter_context(
|
|
|
- patch(
|
|
|
- "misago.acl.useracl.get_user_acl",
|
|
|
- side_effect=self.patched_get_user_acl,
|
|
|
- )
|
|
|
+ self.enter_context(self.enable_acl_patch())
|
|
|
+ self.enter_context(self.patch_user_acl())
|
|
|
+
|
|
|
+ @contextmanager
|
|
|
+ def enable_acl_patch(self):
|
|
|
+ try:
|
|
|
+ self._acl_patches.append(self.acl_patch)
|
|
|
+ yield
|
|
|
+ finally:
|
|
|
+ self._acl_patches.pop(-1)
|
|
|
+
|
|
|
+ def patch_user_acl(self):
|
|
|
+ return patch(
|
|
|
+ "misago.acl.useracl.get_user_acl",
|
|
|
+ side_effect=self.patched_get_user_acl,
|
|
|
)
|
|
|
-
|
|
|
- def __call__(self, f):
|
|
|
- @wraps(f)
|
|
|
- def inner(*args, **kwargs):
|
|
|
- with self:
|
|
|
- with patch(
|
|
|
- "misago.acl.useracl.get_user_acl",
|
|
|
- side_effect=self.patched_get_user_acl,
|
|
|
- ):
|
|
|
- return f(*args, **kwargs)
|
|
|
-
|
|
|
- return inner
|