示例#1
0
 def setUp(self):
     from django.contrib.auth.models import User
     self.user = User.objects.create_user("test", "*****@*****.**", "test")
     
     self.auth = DjangoAuthorization()
示例#2
0
class TestDjangoAuthorization(TestCase):
    
    def setUp(self):
        from django.contrib.auth.models import User
        self.user = User.objects.create_user("test", "*****@*****.**", "test")
        
        self.auth = DjangoAuthorization()
    
    def test_can_create(self):
        request = HttpRequest()
        request.user = self.user
        
        resource = TestResource()
        
        response = self.auth.can_create(self.user, resource)
        self.assertFalse(response)
        
        permission = Permission.objects.get(codename="add_apple")
        self.user.user_permissions.add(permission)
        
        del self.user._perm_cache
        
        response = self.auth.can_create(self.user, resource)
        self.assertTrue(response)
    
    def test_can_delete(self):
        request = HttpRequest()
        request.user = self.user
        
        resource = TestResource()
        
        response = self.auth.can_delete(self.user, resource, None)
        self.assertFalse(response)
        
        permission = Permission.objects.get(codename="delete_apple")
        self.user.user_permissions.add(permission)
        
        del self.user._perm_cache
        
        response = self.auth.can_delete(self.user, resource, None)
        self.assertTrue(response)
    
    def test_can_edit(self):
        request = HttpRequest()
        request.user = self.user
        
        resource = TestResource()
        
        response = self.auth.can_edit(self.user, resource, None)
        self.assertFalse(response)
        
        permission = Permission.objects.get(codename="change_apple")
        self.user.user_permissions.add(permission)
        
        del self.user._perm_cache
        
        response = self.auth.can_edit(self.user, resource, None)
        self.assertTrue(response)
    
    def test_can_view(self):
        response = self.auth.can_view(None, None, None)
        self.assertTrue(response)