Exemplo n.º 1
0
    def test_remove_roles(self):
        """ test if the remove_roles method works fine """
        assign_role(self.john, Teacher, self.bob)
        remove_role(self.john, Teacher, self.bob)

        users_list = get_users(Teacher)
        self.assertEqual(list(users_list), [])

        assign_roles([self.john, self.mike], Teacher, self.bob)
        remove_roles([self.john, self.mike], Teacher)

        users_list = get_users(Teacher)
        self.assertEqual(list(users_list), [])

        assign_role(self.julie, Coordenator)
        self.assertTrue(has_permission(self.julie, 'testapp1.add_user'))
        remove_role(self.julie)
        self.assertFalse(has_permission(self.julie, 'testapp1.add_user'))

        # Remove all roles from the project.
        assign_roles([self.john, self.mike], Teacher, self.bob)
        remove_all(Coordenator)
        remove_all(Teacher)
        self.assertEqual(list(get_users(Coordenator)), [])
        self.assertEqual(list(get_users(Teacher)), [])
Exemplo n.º 2
0
    def test_assign_roles_unique(self):
        """ test if 'unique' attribute works fine """
        assign_role(self.john, Advisor, self.bob)
        assign_role(self.john, Advisor, self.julie)

        # Get the single user that has the role attached.
        self.assertEqual(get_user(Advisor), self.john)

        assign_role(self.mike, Advisor, self.john)

        # Now, multiple users have Advisor.
        # So the method should not be used.
        with self.assertRaises(NotAllowed):
            get_user(Advisor)

        # Trying to assign the multiple roles using a Role with unique=True
        with self.assertRaises(InvalidRoleAssignment):
            assign_roles([self.john, self.mike], Advisor, self.julie)

        # Trying to add the role again using a Role with unique=True
        with self.assertRaises(InvalidRoleAssignment):
            assign_role(self.mike, Advisor, self.bob)

        # Trying to add the role again using a Role with unique=True
        with self.assertRaises(InvalidRoleAssignment):
            assign_role(self.mike, Advisor, self.julie)

        users_list = get_users(Advisor)
        self.assertEqual(list(users_list), [self.john, self.mike])
Exemplo n.º 3
0
    def test_assign_roles(self):
        """ test if the assignment methods work fine """
        assign_roles([self.john, self.mike], Teacher, self.bob)

        users_list = get_users(Teacher, self.bob)
        self.assertEqual(list(users_list), [self.john, self.mike])

        with self.assertRaises(NotAllowed):
            assign_role(self.john, Teacher, Chapter)
Exemplo n.º 4
0
    def test_assign_roles_allmodels(self):
        """ test if the roles using ALL_MODELS work fine """
        assign_role(self.john, Coordenator)

        # Trying to assign a non-object role using a object
        with self.assertRaises(InvalidRoleAssignment):
            assign_role(self.john, Coordenator, self.bob)

        # Trying to assign a object role without object
        with self.assertRaises(InvalidRoleAssignment):
            assign_role(self.john, Advisor)

        users_list = get_users(Coordenator)
        self.assertEqual(list(users_list), [self.john])
Exemplo n.º 5
0
    def test_get_users(self):
        """ test if the get_user method works fine """

        # There is no user for the "library" yet.
        self.assertEqual(self.library.get_user(), None)

        self.book.assign_roles([self.bob], Author)
        self.john.assign_role(Advisor, self.bob)
        self.library.assign_role(self.mike, LibraryOwner)

        # Get single user instance.
        result = self.library.get_user()
        self.assertEqual(result, self.mike)

        # Get single user instance and role class.
        result = self.library.get_user(LibraryOwner)
        self.assertEqual(result, self.mike)

        # Try to get a user from a object with
        # Role class unique=False
        self.assertEqual(self.book.get_user(), None)

        # Get all users with who is Author of "book".
        self.book.assign_role(self.john, Author)
        result = self.book.get_users(Author)
        self.assertEqual(list(result), [self.bob, self.john])

        # Trying to use the reverse GerericRelation.
        reverse = list(self.book.roles.values_list('user', flat=True))
        self.assertEqual(reverse, [self.bob.id, self.john.id])

        # Get all users with any role to "library".
        result = self.library.get_users()
        self.assertEqual(list(result), [self.mike])

        # Try to get users list from a object
        # using a wrong Role class.
        with self.assertRaises(NotAllowed):
            self.library.get_users(Advisor)

        # Get all users with any role and any object.
        # As the result is a QuerySet, we use order_by()
        result = get_users().order_by('-username')
        self.assertEqual(list(result), [self.mike, self.john, self.bob])

        self.john.remove_role(Advisor)
        self.assertFalse(self.john.has_role(Advisor, self.bob))
Exemplo n.º 6
0
 def get_users(self, role_class=None):
     return shortcuts.get_users(role_class, self)