def test_remove_users_is_working_properly_with_non_related_user(self): """testing if remove_users() method is working properly with users that is not related to the department """ from stalker import db # add some users first to see if patching works self.test_department1.users = \ [self.test_user1, self.test_user2] db.DBSession.commit() from stalker_pyramid.testing import DummyRequest, DummyMultiDict request = DummyRequest() request.matchdict['id'] = self.test_department1.id request.params = DummyMultiDict() request.params['user_id'] = [self.test_user2.id, self.test_user3.id] department_view = department.DepartmentViews(request) response = department_view.remove_users() from stalker import Department test_dep_db = Department.query\ .filter(Department.id == self.test_department1.id)\ .first() self.assertEqual(sorted(test_dep_db.users), sorted([self.test_user1]))
def test_update_entity_is_working_properly(self): """testing if the update_entity() method is working properly """ from stalker_pyramid.testing import DummyRequest, DummyMultiDict request = DummyRequest() request.matchdict['id'] = self.test_department1.id request.params = DummyMultiDict() new_name = 'New Department Name' new_description = 'New Description' request.params['name'] = new_name request.params['description'] = new_description request.params['user_id'] = [self.test_user1.id, self.test_user2.id] department_view = department.DepartmentViews(request) self.patch_logged_in_user(request) response = department_view.update_entity() from stalker import Department test_department1_db = Department.query\ .filter(Department.id == self.test_department1.id)\ .first() self.assertEqual(test_department1_db.name, new_name) self.assertEqual(test_department1_db.description, new_description) self.assertEqual(sorted(test_department1_db.users), sorted([self.test_user1, self.test_user2]))
def test_delete_entity_is_working_properly(self): """testing if the delete_entity() method is working properly """ from stalker_pyramid.testing import DummyRequest request = DummyRequest() request.matchdict['id'] = self.test_department1.id department_view = department.DepartmentViews(request) department_view.delete_entity() from stalker import Department dep_db = Department.query\ .filter(Department.id == self.test_department1.id)\ .first() self.assertIsNone(dep_db)
def test_update_user_roles_is_working_properly_with_non_related_user(self): """testing if update_user_roles() method is working properly with non related user """ from stalker import db, DepartmentUser # create a role for the user du1 = DepartmentUser(department=self.test_department1, user=self.test_user1, role=self.test_role1) db.DBSession.add(du1) db.DBSession.commit() du2 = DepartmentUser(department=self.test_department1, user=self.test_user2, role=self.test_role1) db.DBSession.add(du2) db.DBSession.commit() # now update the user role to something new from stalker_pyramid.testing import DummyRequest, DummyMultiDict request = DummyRequest() request.matchdict['id'] = self.test_department1.id request.method = 'POST' request.params = DummyMultiDict() request.params['user_role'] = [ '%s,%s' % (self.test_user1.id, self.test_role2.id), '%s,%s' % (self.test_user2.id, self.test_role3.id), '%s,%s' % (self.test_user3.id, self.test_role1.id) ] department_view = department.DepartmentViews(request) department_view.update_user_role() du1_db = DepartmentUser.query\ .filter(DepartmentUser.department == self.test_department1)\ .filter(DepartmentUser.user == self.test_user1)\ .first() du2_db = DepartmentUser.query\ .filter(DepartmentUser.department == self.test_department1)\ .filter(DepartmentUser.user == self.test_user2)\ .first() self.assertEqual(du1_db.role, self.test_role2) self.assertEqual(du2_db.role, self.test_role3)
def test_get_entities_is_working_properly(self): """testing if the get_entities() method is working properly """ from stalker_pyramid.testing import DummyRequest request = DummyRequest() department_view = department.DepartmentViews(request) response = department_view.get_entities() from stalker import Department all_deps = Department.query.all() self.assertEqual(sorted(response.json_body), [{ 'id': d.id, 'name': d.name, '$ref': '/api/departments/%s' % d.id, 'entity_type': d.entity_type } for d in all_deps])
def test_remove_user_roles_is_working_properly(self): """testing if remove_user_roles() method is working properly """ from stalker import db, DepartmentUser # create a role for the user du1 = DepartmentUser(department=self.test_department1, user=self.test_user1, role=self.test_role1) db.DBSession.add(du1) db.DBSession.commit() du2 = DepartmentUser(department=self.test_department1, user=self.test_user2, role=self.test_role1) db.DBSession.add(du2) db.DBSession.commit() # now update the user role to something new from stalker_pyramid.testing import DummyRequest, DummyMultiDict request = DummyRequest() request.matchdict['id'] = self.test_department1.id request.method = 'POST' request.params = DummyMultiDict() request.params['user_id'] = [self.test_user1.id, self.test_user2.id] department_view = department.DepartmentViews(request) department_view.remove_user_role() du1_db = DepartmentUser.query\ .filter(DepartmentUser.department == self.test_department1)\ .filter(DepartmentUser.user == self.test_user1)\ .first() du2_db = DepartmentUser.query\ .filter(DepartmentUser.department == self.test_department1)\ .filter(DepartmentUser.user == self.test_user2)\ .first() self.assertIsNone(du1_db.role) self.assertIsNone(du2_db.role)
def test_get_users_is_working_properly(self): """testing if the get_users() method is working properly """ self.test_department1.users = [self.test_user1, self.test_user2] from stalker import db db.DBSession.commit() from stalker_pyramid.testing import DummyRequest request = DummyRequest() request.matchdict['id'] = self.test_department1.id department_view = department.DepartmentViews(request) response = department_view.get_users() self.assertEqual( sorted(response.json_body), sorted([{ 'id': u.id, 'name': u.name, '$ref': '/api/users/%s' % u.id, 'entity_type': 'User' } for u in [self.test_user1, self.test_user2]]))
def test_get_department_user_roles_is_working_properly(self): """testing if the get_department_user_roles() method is working properly """ self.test_department1.users = [self.test_user1, self.test_user2] from stalker import db db.DBSession.commit() from stalker_pyramid.testing import DummyRequest request = DummyRequest() request.matchdict['id'] = self.test_department1.id department_view = department.DepartmentViews(request) response = department_view.get_user_roles() from stalker import DepartmentUser all_dep_users = DepartmentUser.query\ .filter(DepartmentUser.department == self.test_department1)\ .all() self.maxDiff = None self.assertEqual( sorted(response.json_body), sorted([{ 'user': { 'id': du.user.id, 'name': du.user.name, 'entity_type': du.user.entity_type, '$ref': '/api/users/%s' % du.user.id }, 'role': { 'id': du.role.id, 'name': du.role.name, 'entity_type': du.role.entity_type, '$ref': '/api/roles/%s' % du.role.id } if du.role else None } for du in all_dep_users]))
def test_get_entity_is_working_properly(self): """testing if the get_entity() method is working properly """ from stalker_pyramid.testing import DummyRequest request = DummyRequest() request.matchdict['id'] = self.test_department1.id department_view = department.DepartmentViews(request) response = department_view.get_entity() import stalker from stalker_pyramid.views import EntityViewBase self.maxDiff = None self.assertEqual( response.json_body, { 'created_by': { 'id': self.admin.id, 'name': 'admin', '$ref': '/api/users/%s' % self.admin.id, 'entity_type': 'User' }, 'date_created': EntityViewBase.milliseconds_since_epoch( self.test_department1.date_created), 'date_updated': EntityViewBase.milliseconds_since_epoch( self.test_department1.date_updated), 'description': '', 'entity_type': 'Department', 'generic_text': '', 'generic_data': { '$ref': '/api/simple_entities/%s/generic_data' % self.test_department1.id, 'length': 0 }, 'id': self.test_department1.id, 'name': 'Test Department 1', 'notes': { '$ref': '/api/entities/%s/notes' % self.test_department1.id, 'length': 0 }, 'stalker_version': stalker.__version__, 'tags': { '$ref': '/api/entities/%s/tags' % self.test_department1.id, 'length': 0 }, 'thumbnail': None, 'type': None, 'updated_by': { 'id': self.admin.id, 'name': 'admin', '$ref': '/api/users/%s' % self.admin.id, 'entity_type': 'User' }, 'user_roles': { '$ref': '/api/departments/%s/user_roles' % self.test_department1.id, 'length': 0 }, 'users': { '$ref': '/api/departments/%s/users' % self.test_department1.id, 'length': 0 }, })
def test_create_entity_is_working_properly(self): """testing if the create_entity() method is working properly """ from stalker_pyramid.testing import DummyRequest, DummyMultiDict request = DummyRequest() request.params = DummyMultiDict() request.params['name'] = 'New Department' request.params['description'] = 'New department description' request.params['user_id'] = [self.test_user1.id, self.test_user2.id] department_view = department.DepartmentViews(request) print('code is here 1') self.patch_logged_in_user(request) print('code is here 2') response = department_view.create_entity() print('code is here 3') from stalker import Department new_dep = Department.query\ .filter(Department.name == 'New Department')\ .first() from stalker_pyramid.views import EntityViewBase import stalker self.maxDiff = None self.assertEqual( response.json_body, { 'created_by': { 'id': self.admin.id, 'name': 'admin', '$ref': '/api/users/%s' % self.admin.id, 'entity_type': 'User' }, 'date_created': EntityViewBase.milliseconds_since_epoch(new_dep.date_created), 'date_updated': EntityViewBase.milliseconds_since_epoch(new_dep.date_updated), 'description': 'New department description', 'entity_type': 'Department', 'generic_text': '', 'generic_data': { '$ref': '/api/simple_entities/%s/generic_data' % new_dep.id, 'length': 0 }, 'id': new_dep.id, 'name': 'New Department', 'notes': { '$ref': '/api/entities/%s/notes' % new_dep.id, 'length': 0 }, 'stalker_version': stalker.__version__, 'tags': { '$ref': '/api/entities/%s/tags' % new_dep.id, 'length': 0 }, 'thumbnail': None, 'type': None, 'updated_by': { 'id': self.admin.id, 'name': 'admin', '$ref': '/api/users/%s' % self.admin.id, 'entity_type': 'User' }, 'user_roles': { '$ref': '/api/departments/%s/user_roles' % new_dep.id, 'length': 2 }, 'users': { '$ref': '/api/departments/%s/users' % new_dep.id, 'length': 2 }, })