def put_permission(permission_id: int) -> ApiResponse: permission = PermissionModel.find_by_id(permission_id) if not permission: abort( 404, description=ERROR_404.format("Permission", "id", permission_id), ) permission_json = request.get_json() permission_by_name = PermissionModel.find_by_name( permission_json.get("permission_name")) if permission_by_name and permission_by_name.id != permission_id: abort( 409, description=ERROR_409.format( "Permission", "permission_name", permission_json.get("permission_name"), ), ) permission.permission_name = permission_json.get("permission_name") permission.save_to_db() return ( jsonify({ "message": MODIFIED.format("Permission"), "permission": permission_schema.dump(permission), }), 200, )
def get(self, id = None): queryData = request.args.to_dict() if id: permission = PermissionModel.find_by_id(id) if permission: return permission.json() else: return {'error': 'user_group not found'}, 404 permissions = PermissionModel.find(**queryData) return {'permissions': list(map(lambda x: x.json(), permissions))}, 200
def post(self): data = Permission.parse.parse_args() permisson = PermissionModel.query.filter_by(id=data['id']).first() if permisson: return { "PermissionAlreadyExistsError": { "message": "Permission with given id already exists", "status": 400 }} permission = PermissionModel(data['name'], data['desc']) permission.save_to_db() return {"Message": "Permission Add..", "status": 200}
def get_permissions() -> ApiResponse: permission_list = PermissionModel.find_all() return ( jsonify({"permissions": permission_list_schema.dump(permission_list)}), 200, )
def test_find_by_name(self): with self.app_context: permission_id = self.permission_1.save_to_db().id permission = PermissionModel.find_by_name("post:keynote") self.assertEqual(permission.id, permission_id)
def put(self, id): data = json.loads(request.data) permission = PermissionModel.find_by_id(id) if permission: permission.update(**data) return permission.json() return {'error': 'permission not found'}, 404
def get_permission(permission_id: int) -> ApiResponse: permission = PermissionModel.find_by_id(permission_id) if not permission: abort( 404, description=ERROR_404.format("Permission", "id", permission_id), ) return ( jsonify({ "permission": permission_schema.dump(permission), }), 200, )
def post(self): data = json.loads(request.data) if PermissionModel.find_by_name(data["name"]): return {'error': "A permission with name '{}' already exists.".format(data["name"])}, 400 permission = PermissionModel(**data) try: permission.save_to_db() except: return {"error": "An error occurred creating the permission."}, 500 return permission.json(), 201
def delete_permission(permission_id: int) -> ApiResponse: permission = PermissionModel.find_by_id(permission_id) if not permission: abort( 404, description=ERROR_404.format("Permission", "id", permission_id), ) permission.delete_from_db() return ( jsonify({ "message": DELETED.format("Permission"), "permission": permission_schema.dump(permission), }), 200, )
def post_permission() -> ApiResponse: permission_json = request.get_json() if PermissionModel.find_by_name(permission_json.get("permission_name")): abort( 409, description=ERROR_409.format( "Permission", "permission_name", permission_json.get("permission_name"), ), ) permission = permission_schema.load(permission_json) permission.save_to_db() return ( jsonify({ "message": CREATED.format("Permission"), "permission": permission_schema.dump(permission), }), 201, )
def add_permission_to_db( self, permission: PermissionModel ) -> PermissionModel: return permission.save_to_db()
def get(self): return {'permissions': list(map(lambda x: x.json(), PermissionModel.find_all()))}, 200
def delete(self, name): permission = PermissionModel.find_by_name(name) if permission: permission.delete_from_db() return {'success': 'permission deleted'}, 202 return {'error': 'permission not found'}, 404
def set_permissions(self, data): self.permissions = [] for permission in data: permission_obj = PermissionModel.find_by_id(permission["id"]) if permission_obj: self.permissions.append(permission_obj)
class BaseTest(TestCase): """Base class which is inherited by all test classes""" @classmethod def setUpClass(cls) -> None: pass def setUp(self) -> None: """Create all db tables before each test""" self.client = app.test_client() self.app_context = app.app_context() with self.app_context: db.create_all() self.keynote_1 = KeynoteModel(**TEST_KEYNOTE_1) self.keynote_2 = KeynoteModel(**TEST_KEYNOTE_2) self.member_1 = MemberModel(**TEST_MEMBER_1) self.member_2 = MemberModel(**TEST_MEMBER_2) self.meeting_1 = MeetingModel(**TEST_MEETING_1) self.meeting_2 = MeetingModel(**TEST_MEETING_2) self.permission_1 = PermissionModel(**TEST_PERMISSION_1) self.permission_2 = PermissionModel(**TEST_PERMISSION_2) self.permission_3 = PermissionModel(**TEST_PERMISSION_3) self.permission_4 = PermissionModel(**TEST_PERMISSION_4) self.permission_5 = PermissionModel(**TEST_PERMISSION_5) self.permission_6 = PermissionModel(**TEST_PERMISSION_6) self.permission_7 = PermissionModel(**TEST_PERMISSION_7) self.permission_8 = PermissionModel(**TEST_PERMISSION_8) self.project_1 = ProjectModel(**TEST_PROJECT_1) self.project_2 = ProjectModel(**TEST_PROJECT_2) self.role_1 = RoleModel(**TEST_ROLE_1) self.role_2 = RoleModel(**TEST_ROLE_2) self.role_3 = RoleModel(**TEST_ROLE_3) self.role_4 = RoleModel(**TEST_ROLE_4) self.role_5 = RoleModel(**TEST_ROLE_5) self.speaker_1 = SpeakerModel(**TEST_SPEAKER_1) self.speaker_2 = SpeakerModel(**TEST_SPEAKER_2) def tearDown(self) -> None: """Clear db tables after each test""" with self.app_context: db.drop_all() def login( self, client: app.test_client, email: str, password: str ) -> LoginJSON: results = client.post( f"/members/login", data=json.dumps( { "email": email, "password": password, } ), headers={"Content-Type": "application/json"}, ) return json.loads(results.data) def add_permissions_to_admin(self): role_1 = self.role_1.save_to_db() permission_1 = self.permission_1.save_to_db() permission_2 = self.permission_2.save_to_db() permission_3 = self.permission_3.save_to_db() permission_4 = self.permission_4.save_to_db() permission_5 = self.permission_5.save_to_db() permission_6 = self.permission_6.save_to_db() permission_7 = self.permission_7.save_to_db() permission_8 = self.permission_8.save_to_db() role_1.permissions.append(permission_1) role_1.permissions.append(permission_2) role_1.permissions.append(permission_3) role_1.permissions.append(permission_4) role_1.permissions.append(permission_5) role_1.permissions.append(permission_6) role_1.permissions.append(permission_7) role_1.permissions.append(permission_8) return role_1 def add_keynote_to_db( self, keynote: KeynoteModel, role: RoleModel, member: MemberModel, speaker: SpeakerModel, meeting: MeetingModel, ) -> Tuple[ KeynoteModel, MemberModel, RoleModel, SpeakerModel, MeetingModel ]: speaker = self.add_speaker_to_db(speaker) meeting, member, role = self.add_meeting_to_db(meeting, member, role) keynote = keynote.save_to_db() return keynote, member, role, speaker, meeting def add_meeting_to_db( self, meeting: MeetingModel, member: MemberModel, role: RoleModel ) -> Tuple[MeetingModel, MemberModel, RoleModel]: member, role = self.add_member_to_db(member, role) meeting = meeting.save_to_db() return meeting, member, role def add_member_to_db( self, member: MemberModel, role: RoleModel ) -> Tuple[MemberModel, RoleModel]: role = self.add_role_to_db(role) member = member.save_to_db() return member, role def add_permission_to_db( self, permission: PermissionModel ) -> PermissionModel: return permission.save_to_db() def add_role_to_db(self, role: RoleModel) -> RoleModel: return role.save_to_db() def add_speaker_to_db(self, speaker: SpeakerModel) -> SpeakerModel: return speaker.save_to_db()
def setUp(self) -> None: """Create all db tables before each test""" self.client = app.test_client() self.app_context = app.app_context() with self.app_context: db.create_all() self.keynote_1 = KeynoteModel(**TEST_KEYNOTE_1) self.keynote_2 = KeynoteModel(**TEST_KEYNOTE_2) self.member_1 = MemberModel(**TEST_MEMBER_1) self.member_2 = MemberModel(**TEST_MEMBER_2) self.meeting_1 = MeetingModel(**TEST_MEETING_1) self.meeting_2 = MeetingModel(**TEST_MEETING_2) self.permission_1 = PermissionModel(**TEST_PERMISSION_1) self.permission_2 = PermissionModel(**TEST_PERMISSION_2) self.permission_3 = PermissionModel(**TEST_PERMISSION_3) self.permission_4 = PermissionModel(**TEST_PERMISSION_4) self.permission_5 = PermissionModel(**TEST_PERMISSION_5) self.permission_6 = PermissionModel(**TEST_PERMISSION_6) self.permission_7 = PermissionModel(**TEST_PERMISSION_7) self.permission_8 = PermissionModel(**TEST_PERMISSION_8) self.project_1 = ProjectModel(**TEST_PROJECT_1) self.project_2 = ProjectModel(**TEST_PROJECT_2) self.role_1 = RoleModel(**TEST_ROLE_1) self.role_2 = RoleModel(**TEST_ROLE_2) self.role_3 = RoleModel(**TEST_ROLE_3) self.role_4 = RoleModel(**TEST_ROLE_4) self.role_5 = RoleModel(**TEST_ROLE_5) self.speaker_1 = SpeakerModel(**TEST_SPEAKER_1) self.speaker_2 = SpeakerModel(**TEST_SPEAKER_2)
def post(self): data = ProjectShare.projectshare_parse.parse_args() user = UserModel.query.filter_by(id=data['share_with_id']).first() if user == None: return { "UserNotExistsError": { "message": "User with given id doesn't exists", "status": 400 } } else: if user.status: project = ProjectModel.find_by_id(data['uuid']) if project: if project.created_by_id == get_jwt_identity(): if project.created_by_id == data['share_with_id']: return { "SharingProjectError": { "message": "You don't Share Project itself.", "status": 401 } } else: permission = PermissionModel.find_by_name( data['permission']) if permission: share = ShareProjectModel( data['uuid'], data['share_with_id'], data['permission']) share.save_to_db() return { "Message": "Project Share successfull", "Share with": user.name } return { "PermissionError": { "message": "permission not found", "status": 401 } } return { "SharingProjectError": { "message": "You don't have Sharing permission", "status": 401 } } return { "ProjectNotExistsError": { "message": "Project with given name doesn't exists", "status": 400 } } return { "UserNotActiveError": { "message": "User with give id is not Active.", "status": 401 } }