Esempio n. 1
0
 def test_can_add_role(self):
     """ Adding role to user """
     role = Role(handle='demo', title='Demo role')
     role_service.save(role)
     user = User(**self.data)
     user.add_role(role)
     self.assertIn(role, user.roles)
Esempio n. 2
0
 def test_save_role_emits_event(self):
     """ Saving role emits event """
     role = Role(handle='admin')
     with events.events.disconnect_receivers():
         spy = mock.Mock()
         events.role_saved_event.connect(spy, weak=False)
         role_service.save(role)
         spy.assert_called_with(role)
Esempio n. 3
0
    def test_can_remove_role(self):
        """ Removing role from user """
        role = Role(handle='demo', title='Demo role')
        role_service.save(role)
        user = User(**self.data)
        user.add_role(role)
        self.assertIn(role, user.roles)

        user.remove_role(role)
        self.assertNotIn(role, user.roles)
    def test_adding_role_emits_event(self):
        """ Adding role to user emits event """
        with user_events.disconnect_receivers():
            user = self.create_user()
            role = Role(handle='test_role', title='Testing')
            role_service.save(role, user)

            spy = mock.Mock()
            events.user_got_role_event.connect(spy, weak=False)
            user_service.add_role_to_user(user, role)
            spy.assert_called_with(user, role=role)
Esempio n. 5
0
 def test_save_role_possible(self):
     """ Saving valid role possible """
     role = Role(handle='admin')
     with events.events.disconnect_receivers():
         role = role_service.save(role)
         self.assertIsInstance(role, Role)
         self.assertIsNotNone(role.id)
Esempio n. 6
0
    def test_can_check_if_user_has_role(self):
        """ Checking if user has role """
        user = User(**self.data)

        role1 = Role(handle='testrole1', title='Test role 1')
        role_service.save(role1)
        user.add_role(role1)

        role2 = Role(handle='testrole2', title='Test role 2')
        role_service.save(role2)

        # check by handle
        self.assertTrue(user.has_role('testrole1'))
        self.assertFalse(user.has_role('testrole2'))

        # check by object
        self.assertTrue(user.has_role(role1))
        self.assertFalse(user.has_role(role2))
    def test_can_add_role_to_user(self):
        """ Adding role to user """
        with events.events.disconnect_receivers():
            user = self.create_user()
            role = Role(handle='test_role', title='Testing')
            ok = role_service.save(role)
            if not ok:
                self.fail('Role invalid')

            user_service.add_role_to_user(user, role)
            self.assertTrue(user.has_role(role))
Esempio n. 8
0
def create_role(*_, **kwargs):
    """ Create user role """
    click.echo(green('\nCreating new role:'))
    click.echo(green('-' * 40))

    with get_app().app_context():
        role = Role(**kwargs)
        result = role_service.save(role)
        if not isinstance(result, Role):
            print_validation_errors(result)

        click.echo(green('Created: ') + str(role) + '\n')
Esempio n. 9
0
 def create_role(self):
     """ Create a role for testing"""
     with events.events.disconnect_receivers():
         role = Role(handle='test', title='testing role')
         role_service.save(role)
     return role
Esempio n. 10
0
 def test_save_role_returns_errors_on_invalid(self):
     """ Saving invalid role returns error object """
     role = Role()
     res = role_service.save(role)
     self.assertIsInstance(res, Result)
     self.assertFalse(res)