Browse Source

Redo patch_user_acl test util

rafalp 6 years ago
parent
commit
7e79a26512
2 changed files with 18 additions and 30 deletions
  1. 4 17
      misago/acl/test.py
  2. 14 13
      misago/acl/tests/test_patching_user_acl.py

+ 4 - 17
misago/acl/test.py

@@ -19,19 +19,9 @@ class patch_user_acl(ExitStack):
     Patch should be a dict or callable.
     Patch should be a dict or callable.
     """
     """
 
 
-    def __init__(self, *args):
+    def __init__(self, *patches):
         super().__init__()
         super().__init__()
-
-        self._global_patch = None
-        self._user_patches = {}
-
-        if len(args) == 2:
-            user, patch = args
-            self._user_patches[user.id] = patch
-        elif len(args) == 1:
-            self._global_patch = args[0]
-        else:
-            raise ValueError("patch_user_acl takes one or two arguments.")
+        self._patches = patches
 
 
     def patched_get_user_acl(self, user, cache_versions):
     def patched_get_user_acl(self, user, cache_versions):
         user_acl = get_user_acl(user, cache_versions)
         user_acl = get_user_acl(user, cache_versions)
@@ -39,11 +29,8 @@ class patch_user_acl(ExitStack):
         return user_acl
         return user_acl
 
 
     def apply_acl_patches(self, user, user_acl):
     def apply_acl_patches(self, user, user_acl):
-        if self._global_patch:
-            self.apply_acl_patch(user, user_acl, self._global_patch)
-        if user.id in self._user_patches:
-            user_acl_patch = self._user_patches[user.id]
-            self.apply_acl_patch(user, user_acl, user_acl_patch)
+        for acl_patch in self._patches:
+            self.apply_acl_patch(user, user_acl, acl_patch)
 
 
     def apply_acl_patch(self, user, user_acl, acl_patch):
     def apply_acl_patch(self, user, user_acl, acl_patch):
         if callable(acl_patch):
         if callable(acl_patch):

+ 14 - 13
misago/acl/tests/test_patching_user_acl.py

@@ -47,19 +47,6 @@ class PatchingUserACLTests(TestCase):
         user_acl = useracl.get_user_acl(user, cache_versions)
         user_acl = useracl.get_user_acl(user, cache_versions)
         assert "is_patched" not in user_acl
         assert "is_patched" not in user_acl
 
 
-    def test_context_manager_patches_specified_user_acl(self):
-        user = User.objects.create_user("User", "user@example.com")
-        with patch_user_acl(user, {"can_rename_users": "patched"}):
-            user_acl = useracl.get_user_acl(user, cache_versions)
-            assert user_acl["can_rename_users"] == "patched"
-
-    def test_other_user_acl_is_not_changed_by_user_specific_context_manager(self):
-        patched_user = User.objects.create_user("User", "user@example.com")
-        other_user = User.objects.create_user("User2", "user2@example.com")
-        with patch_user_acl(patched_user, {"can_rename_users": "patched"}):
-            other_user_acl = useracl.get_user_acl(other_user, cache_versions)
-            assert other_user_acl["can_rename_users"] != "patched"
-
     @patch_user_acl(callable_acl_patch)
     @patch_user_acl(callable_acl_patch)
     def test_callable_patch_is_called_with_user_and_acl_by_decorator(self):
     def test_callable_patch_is_called_with_user_and_acl_by_decorator(self):
         user = User.objects.create_user("User", "user@example.com")
         user = User.objects.create_user("User", "user@example.com")
@@ -71,3 +58,17 @@ class PatchingUserACLTests(TestCase):
         with patch_user_acl(callable_acl_patch):
         with patch_user_acl(callable_acl_patch):
             user_acl = useracl.get_user_acl(user, cache_versions)
             user_acl = useracl.get_user_acl(user, cache_versions)
             assert user_acl["patched_for_user_id"] == user.id
             assert user_acl["patched_for_user_id"] == user.id
+
+    @patch_user_acl(callable_acl_patch, {"other_acl_path": True})
+    def test_multiple_acl_patches_are_applied_by_decorator(self):
+        user = User.objects.create_user("User", "user@example.com")
+        user_acl = useracl.get_user_acl(user, cache_versions)
+        assert user_acl["patched_for_user_id"] == user.id
+        assert user_acl["other_acl_path"]
+
+    def test_multiple_acl_patches_are_applied_by_context_manager(self):
+        user = User.objects.create_user("User", "user@example.com")
+        with patch_user_acl(callable_acl_patch, {"other_acl_path": True}):
+            user_acl = useracl.get_user_acl(user, cache_versions)
+            assert user_acl["patched_for_user_id"] == user.id
+            assert user_acl["other_acl_path"]