Commits

Anonymous committed 7601eb6

difference

  • Participants
  • Parent commits 8551412

Comments (0)

Files changed (2)

flaskext/principal.py

         """Does the same thing as ``self.union(other)``
         """
         return self.union(other)
+    
+    def __or__(self, other):
+        """Does the same thing as ``self.union(other)``
+        """
+        return self.difference(other)
 
     def __contains__(self, other):
         """Does the same thing as ``other.issubset(self)``.
         :param other: The other permission
         """
         p = Permission(*self.needs.union(other.needs))
-        p.excludes.update(self.excludes)
-        p.excludes.update(other.excludes)
+        p.excludes.update(self.excludes.union(other.excludes))
+        return p
+
+    def difference(self, other):
+        """Create a new permission consisting of requirements in this 
+        permission and not in the other.
+        """
+
+        p = Permission(*self.needs.difference(other.needs))
+        p.excludes.update(self.excludes.difference(other.excludes))
         return p
 
     def issubset(self, other):

tests/test_principal.py

     assert p1.issubset(p3)
     assert p2.issubset(p3)
 
+def test_permission_difference():
+    p1 = Permission(('a', 'b'), ('a', 'c'))
+    p2 = Permission(('a', 'c'), ('d', 'e'))
+    p3 = p1.difference(p2)
+    assert p3.needs == set([('a', 'b')])
+    p4 = p2.difference(p1)
+    assert p4.needs == set([('d', 'e')])
+
+
 def test_permission_union_denial():
     p1 = Permission(('a', 'b'))
     p2 = Denial(('a', 'c'))
     assert p1.issubset(p3)
     assert p2.issubset(p3)
 
+def test_permission_difference_denial():
+    p1 = Denial(('a', 'b'), ('a', 'c'))
+    p2 = Denial(('a', 'c'), ('d', 'e'))
+    p3 = p1.difference(p2)
+    assert p3.excludes == set([('a', 'b')])
+    p4 = p2.difference(p1)
+    assert p4.excludes == set([('d', 'e')])
 
 def test_reverse_permission():
 
 
     assert p3.needs == p4.needs
 
+def test_permission_or():
+
+    p1 = Permission(RoleNeed('boss'), RoleNeed('lackey'))
+    p2 = Permission(RoleNeed('lackey'), RoleNeed('underling'))
+
+    p3 = p1 | p2
+    p4 = p1.union(p2)
+
+    assert p3.needs == p4.needs
+
 def test_contains():
 
     p1 = Permission(RoleNeed('boss'), RoleNeed('lackey'))
     assert 'not admin' in response.data
     assert 'now admin' in response.data
 
-
 def test_denied_passes():
 
     client = mkapp().test_client()