def test_get_role_as_dict(self): """ Get get a dictionary representation of role""" role = Role(**self.data) data = role.to_dict() self.assertIn('id', data) self.assertEqual(self.data['handle'].lower(), data['handle']) self.assertEqual(self.data['title'], data['title']) self.assertEqual(self.data['description'], data['description'])
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)
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)
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)
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_populate_at_creation(self): """ Populating at creation """ data = self.data role = Role(**data) self.assertEqual(data['handle'].lower(), role.handle) self.assertEqual(data['title'], role.title) self.assertEqual(data['description'], role.description) self.assertIsNone(role.id)
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)
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))
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')
def create(self, handle, title=None, description=None): """ Create a role """ role = Role(handle=handle, title=title, description=description) schema = RoleSchema() valid = schema.process(role) if not valid: return valid db.session.add(role) db.session.commit() events.role_created_event.send(role) return role
def test_process_role_with_schema(self): """ Processing role with schema """ data = dict(handle=' HA ', title=' Role title ', description=' Role description ') role = Role(**data) schema = RoleSchema() result = schema.process(role) self.assertFalse(result) self.assertEqual('ha', role.handle) self.assertEqual('Role title', role.title) self.assertEqual('Role description', role.description)
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)
def test_dont_implicitly_convert_handles_to_strings(self): """ REGRESSION: do not implicitly convert role handles to strings """ role = Role(handle=None) self.assertEquals(None, role.handle)
def test_get_printable_repr(self): """ Getting printable representation of role """ role = Role(**self.data) repr = role.__repr__() self.assertTrue(repr.startswith('<Role id='))
def test_adding_invalid_role_raises_exception(self): """ Raise exception in adding bad role to user """ user = User(**self.data) role = Role(id=123) with self.assertRaises(x.UserException): user.add_role(role)
def test_access_role_users(self): """ Accessing users with role """ role = Role() users = role.users self.assertTrue(type(users) is tuple) self.assertEqual(0, len(users))
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