def __create_admin__(): """creates the admin """ from stalker import defaults from stalker.models.auth import User from stalker.models.department import Department logger.debug("creating the default administrator user") # create the admin department admin_department = Department.query.filter_by( name=defaults.admin_department_name ).first() if not admin_department: admin_department = Department(name=defaults.admin_department_name) from stalker.db.session import DBSession DBSession.add(admin_department) # create the admins group from stalker.models.auth import Group admins_group = Group.query \ .filter_by(name=defaults.admin_group_name) \ .first() if not admins_group: admins_group = Group(name=defaults.admin_group_name) DBSession.add(admins_group) # check if there is already an admin in the database admin = User.query.filter_by(name=defaults.admin_name).first() if admin: # there should be an admin user do nothing logger.debug("there is an admin already") return admin else: admin = User( name=defaults.admin_name, login=defaults.admin_login, password=defaults.admin_password, email=defaults.admin_email, departments=[admin_department], groups=[admins_group] ) admin.created_by = admin admin.updated_by = admin # update the department as created and updated by admin user admin_department.created_by = admin admin_department.updated_by = admin admins_group.created_by = admin admins_group.updated_by = admin DBSession.add(admin) DBSession.commit() return admin
def __create_admin__(): """creates the admin """ from stalker.models.auth import User from stalker.models.department import Department # check if there is already an admin in the database admin = User.query.filter_by(name=defaults.admin_name).first() if admin: # there should be an admin user do nothing logger.debug("there is an admin already") return logger.debug("creating the default administrator user") # create the admin department admin_department = Department.query.filter_by(name=defaults.admin_department_name).first() if not admin_department: admin_department = Department(name=defaults.admin_department_name) DBSession.add(admin_department) # create the admins group from stalker.models.auth import Group admins_group = Group.query.filter_by(name=defaults.admin_group_name).first() if not admins_group: admins_group = Group(name=defaults.admin_group_name) DBSession.add(admins_group) # # create the admin user # admin = User.query \ # .filter_by(name=defaults.admin_name) \ # .first() # if not admin: admin = User( name=defaults.admin_name, login=defaults.admin_login, password=defaults.admin_password, email=defaults.admin_email, departments=[admin_department], groups=[admins_group], ) admin.created_by = admin admin.updated_by = admin # update the department as created and updated by admin user admin_department.created_by = admin admin_department.updated_by = admin admins_group.created_by = admin admins_group.updated_by = admin DBSession.add(admin) DBSession.commit()
def test_users_attribute_updates_the_groups_attribute_in_the_given_User_instances(self): """testing if the Users given with the users attribute will have the current Group instance in their groups attribute """ test_users = self.kwargs.pop('users') new_group = Group(**self.kwargs) new_group.users = test_users for user in test_users: self.assertTrue(new_group in user.groups)
def test_users_argument_is_skipped(self): """testing if the users argument is skipped the users attribute will be an empty list """ self.kwargs.pop('users') new_group = Group(**self.kwargs) self.assertEqual(new_group.users, [])
def setUp(self): """set up the test in method level """ super(GroupTester, self).setUp() # create a couple of Users self.test_user1 = User( name='User1', login='******', password='******', email='*****@*****.**', ) self.test_user2 = User( name='User2', login='******', password='******', email='*****@*****.**', ) self.test_user3 = User( name='User3', login='******', password='******', email='*****@*****.**', ) # create a test group self.kwargs = { "name": "Test Group", "users": [self.test_user1, self.test_user2, self.test_user3] } self.test_group = Group(**self.kwargs)
def test_users_argument_updates_the_groups_attribute_in_the_given_User_instances(self): """testing if the Users given with the users argument will have the current Group instance in their groups attribute """ self.kwargs['name'] = 'New Group' new_group = Group(**self.kwargs) for user in self.kwargs['users']: self.assertTrue(new_group in user.groups)
def test_users_argument_is_not_a_list_of_User_instances(self): """testing if a TypeError will be raised when the users argument is not a list of User instances """ self.kwargs['users'] = [12, 'not a user'] with pytest.raises(TypeError) as cm: Group(**self.kwargs) assert str(cm.value) == \ 'Group.users attribute must all be stalker.models.auth.User ' \ 'instances not int'
def test_users_argument_is_not_a_list_of_User_instances(self): """testing if a TypeError will be raised when the users argument is not a list of User instances """ self.kwargs['users'] = [12, 'not a user'] with self.assertRaises(TypeError) as cm: Group(**self.kwargs) self.assertEqual( str(cm.exception), 'Group.users attribute must all be stalker.models.auth.User ' 'instances not int')
def test_permissions_argument_is_working_properly(self): """testing if permissions can be added to the Group on __init__() """ # create a couple of permissions from stalker import Permission perm1 = Permission('Allow', 'Create', 'User') perm2 = Permission('Allow', 'Read', 'User') perm3 = Permission('Deny', 'Delete', 'User') new_group = Group(name='Test Group', users=[self.test_user1, self.test_user2], permissions=[perm1, perm2, perm3]) self.assertEqual(new_group.permissions, [perm1, perm2, perm3])