Ejemplo n.º 1
0
class BackendTestAnonymousUser(RubberStampTestCase):
    fixtures = ['users.json', 'objects.json', 'permissions.json', 'assigned.json', 'assigned_object.json']
    
    def setUp(self):
        self.anon = AnonymousUser()
        self.user = User.objects.get(pk=2)
        self.object = TestModel.objects.get(pk=1)
    
    def test_no_type_or_object(self):
        self.assertFalse(self.anon.has_perm('testapp.use'))
    
    def test_with_type(self):
        self.assertFalse(self.anon.has_perm('testapp.use.testapp.testmodel'))
    
    def test_with_object(self):
        self.assertFalse(self.anon.has_perm('testapp.use', obj=self.object))
    
    def test_with_both(self):
        self.assertFalse(self.anon.has_perm('testapp.use.testapp.testmodel', obj=self.object))
    
    def test_with_both_mismatched(self):
        self.assertFalse(self.anon.has_perm('testapp.use.auth.user', obj=self.object))
        self.assertFalse(self.anon.has_perm('testapp.use.testapp.testmodel', obj=self.user))
    
    def test_has_module_perms(self):
        self.assertFalse(self.anon.has_module_perms('testapp'))
    
    def test_get_all(self):
        self.assertEqual(len(self.anon.get_all_permissions()), 0)
        self.assertEqual(len(self.anon.get_all_permissions(obj=self.object)), 0)
        self.assertEqual(len(self.anon.get_all_permissions(obj=self.user)), 0)
Ejemplo n.º 2
0
class AnonymousUserBackendTest(TestCase):
    """
    Tests for AnonymousUser delegating to backend.
    """

    backend = 'django.contrib.auth.tests.auth_backends.SimpleRowlevelBackend'

    def setUp(self):
        self.curr_auth = settings.AUTHENTICATION_BACKENDS
        settings.AUTHENTICATION_BACKENDS = (self.backend, )
        self.user1 = AnonymousUser()

    def tearDown(self):
        settings.AUTHENTICATION_BACKENDS = self.curr_auth

    def test_has_perm(self):
        self.assertEqual(self.user1.has_perm('perm', TestObj()), False)
        self.assertEqual(self.user1.has_perm('anon', TestObj()), True)

    def test_has_perms(self):
        self.assertEqual(self.user1.has_perms(['anon'], TestObj()), True)
        self.assertEqual(self.user1.has_perms(['anon', 'perm'], TestObj()),
                         False)

    def test_has_module_perms(self):
        self.assertEqual(self.user1.has_module_perms("app1"), True)
        self.assertEqual(self.user1.has_module_perms("app2"), False)

    def test_get_all_permissions(self):
        self.assertEqual(self.user1.get_all_permissions(TestObj()),
                         set(['anon']))
Ejemplo n.º 3
0
class AnonymousUserBackendTest(TestCase):
    """
    Tests for AnonymousUser delegating to backend.
    """

    backend = 'django.contrib.auth.tests.auth_backends.SimpleRowlevelBackend'

    def setUp(self):
        self.curr_auth = settings.AUTHENTICATION_BACKENDS
        settings.AUTHENTICATION_BACKENDS = (self.backend,)
        self.user1 = AnonymousUser()

    def tearDown(self):
        settings.AUTHENTICATION_BACKENDS = self.curr_auth

    def test_has_perm(self):
        self.assertEqual(self.user1.has_perm('perm', TestObj()), False)
        self.assertEqual(self.user1.has_perm('anon', TestObj()), True)

    def test_has_perms(self):
        self.assertEqual(self.user1.has_perms(['anon'], TestObj()), True)
        self.assertEqual(self.user1.has_perms(['anon', 'perm'], TestObj()), False)

    def test_has_module_perms(self):
        self.assertEqual(self.user1.has_module_perms("app1"), True)
        self.assertEqual(self.user1.has_module_perms("app2"), False)

    def test_get_all_permissions(self):
        self.assertEqual(self.user1.get_all_permissions(TestObj()), set(['anon']))
Ejemplo n.º 4
0
class NoAnonymousUserBackendTest(TestCase):
    """
    Tests that AnonymousUser does not delegate to backend if it has 'supports_anonymous_user' = False
    """
    backend = 'django.contrib.auth.tests.auth_backends.NoAnonymousUserBackend'

    def setUp(self):
        self.curr_auth = settings.AUTHENTICATION_BACKENDS
        settings.AUTHENTICATION_BACKENDS = tuple(
            self.curr_auth) + (self.backend, )
        self.user1 = AnonymousUser()

    def tearDown(self):
        settings.AUTHENTICATION_BACKENDS = self.curr_auth

    def test_has_perm(self):
        self.assertEqual(self.user1.has_perm('perm', TestObj()), False)
        self.assertEqual(self.user1.has_perm('anon', TestObj()), False)

    def test_has_perms(self):
        self.assertEqual(self.user1.has_perms(['anon'], TestObj()), False)

    def test_has_module_perms(self):
        self.assertEqual(self.user1.has_module_perms("app1"), False)
        self.assertEqual(self.user1.has_module_perms("app2"), False)

    def test_get_all_permissions(self):
        self.assertEqual(self.user1.get_all_permissions(TestObj()), set())
Ejemplo n.º 5
0
class NoAnonymousUserBackendTest(TestCase):
    """
    Tests that AnonymousUser does not delegate to backend if it has 'supports_anonymous_user' = False
    """
    backend = 'django.contrib.auth.tests.auth_backends.NoAnonymousUserBackend'

    def setUp(self):
        self.curr_auth = settings.AUTHENTICATION_BACKENDS
        settings.AUTHENTICATION_BACKENDS = tuple(self.curr_auth) + (self.backend,)
        self.user1 = AnonymousUser()

    def tearDown(self):
        settings.AUTHENTICATION_BACKENDS = self.curr_auth

    def test_has_perm(self):
        self.assertEqual(self.user1.has_perm('perm', TestObj()), False)
        self.assertEqual(self.user1.has_perm('anon', TestObj()), False)

    def test_has_perms(self):
        self.assertEqual(self.user1.has_perms(['anon'], TestObj()), False)

    def test_has_module_perms(self):
        self.assertEqual(self.user1.has_module_perms("app1"), False)
        self.assertEqual(self.user1.has_module_perms("app2"), False)

    def test_get_all_permissions(self):
        self.assertEqual(self.user1.get_all_permissions(TestObj()), set())
Ejemplo n.º 6
0
class AnonymousUserBackendTest(TestCase):
    """
    Tests for AnonymousUser delegating to backend if it has 'supports_anonymous_user' = True
    """

    backend = "django.contrib.auth.tests.auth_backends.AnonymousUserBackend"

    def setUp(self):
        self.curr_auth = settings.AUTHENTICATION_BACKENDS
        settings.AUTHENTICATION_BACKENDS = (self.backend,)
        self.user1 = AnonymousUser()

    def tearDown(self):
        settings.AUTHENTICATION_BACKENDS = self.curr_auth

    def test_has_perm(self):
        self.assertEqual(self.user1.has_perm("perm", TestObj()), False)
        self.assertEqual(self.user1.has_perm("anon", TestObj()), True)

    def test_has_perms(self):
        self.assertEqual(self.user1.has_perms(["anon"], TestObj()), True)
        self.assertEqual(self.user1.has_perms(["anon", "perm"], TestObj()), False)

    def test_has_module_perms(self):
        self.assertEqual(self.user1.has_module_perms("app1"), True)
        self.assertEqual(self.user1.has_module_perms("app2"), False)

    def test_get_all_permissions(self):
        self.assertEqual(self.user1.get_all_permissions(TestObj()), set(["anon"]))
Ejemplo n.º 7
0
class AnonymousUserBackendTest(SimpleTestCase):
    """
    Tests for AnonymousUser delegating to backend.
    """
    def setUp(self):
        self.user1 = AnonymousUser()

    def test_has_perm(self):
        self.assertIs(self.user1.has_perm('perm', TestObj()), False)
        self.assertIs(self.user1.has_perm('anon', TestObj()), True)

    def test_has_perms(self):
        self.assertIs(self.user1.has_perms(['anon'], TestObj()), True)
        self.assertIs(self.user1.has_perms(['anon', 'perm'], TestObj()), False)

    def test_has_perms_perm_list_invalid(self):
        msg = 'perm_list must be an iterable of permissions.'
        with self.assertRaisesMessage(ValueError, msg):
            self.user1.has_perms('perm')
        with self.assertRaisesMessage(ValueError, msg):
            self.user1.has_perms(object())

    def test_has_module_perms(self):
        self.assertIs(self.user1.has_module_perms("app1"), True)
        self.assertIs(self.user1.has_module_perms("app2"), False)

    def test_get_all_permissions(self):
        self.assertEqual(self.user1.get_all_permissions(TestObj()), {'anon'})
Ejemplo n.º 8
0
 def test_anonymous(self):
     person = AnonymousUser()
     event = EventFactory()
     with self.assertNumQueries(0):
         self.assertEqual(
             person.get_all_permissions(event.organization),
             set(),
         )
     with self.assertNumQueries(0):
         self.assertFalse(person.has_perm('view', event.organization))
         self.assertFalse(person.has_perm('edit', event.organization))
         self.assertFalse(person.has_perm('change_permissions', event.organization))
class AnonymousUserBackendTest(SimpleTestCase):
    """
    Tests for AnonymousUser delegating to backend.
    """
    def setUp(self):
        self.user1 = AnonymousUser()

    def test_has_perm(self):
        self.assertIs(self.user1.has_perm('perm', TestObj()), False)
        self.assertIs(self.user1.has_perm('anon', TestObj()), True)

    def test_has_perms(self):
        self.assertIs(self.user1.has_perms(['anon'], TestObj()), True)
        self.assertIs(self.user1.has_perms(['anon', 'perm'], TestObj()), False)

    def test_has_module_perms(self):
        self.assertIs(self.user1.has_module_perms("app1"), True)
        self.assertIs(self.user1.has_module_perms("app2"), False)

    def test_get_all_permissions(self):
        self.assertEqual(self.user1.get_all_permissions(TestObj()), {'anon'})
Ejemplo n.º 10
0
class AnonymousUserBackendTest(SimpleTestCase):
    """
    Tests for AnonymousUser delegating to backend.
    """

    def setUp(self):
        self.user1 = AnonymousUser()

    def test_has_perm(self):
        self.assertIs(self.user1.has_perm('perm', TestObj()), False)
        self.assertIs(self.user1.has_perm('anon', TestObj()), True)

    def test_has_perms(self):
        self.assertIs(self.user1.has_perms(['anon'], TestObj()), True)
        self.assertIs(self.user1.has_perms(['anon', 'perm'], TestObj()), False)

    def test_has_module_perms(self):
        self.assertIs(self.user1.has_module_perms("app1"), True)
        self.assertIs(self.user1.has_module_perms("app2"), False)

    def test_get_all_permissions(self):
        self.assertEqual(self.user1.get_all_permissions(TestObj()), {'anon'})