def create_tables(): db.create_all() LOGGER.debug('Setting roles...') Permission.insert_permissions() Role.insert_roles() LOGGER.debug('Setting users...') User.insert_users(_config)
def update_user_request_not_found(query_mock, user_admin_valid, role_admin_valid, user_valid): from lorem_ipsum_auth.models import User, Role, Permission role = Role(id=role_admin_valid['id'], name=role_admin_valid['name'], default=role_admin_valid['default'], permissions=[ Permission.from_str(perm) for perm in role_admin_valid['permissions'] ]) Role.query.filter_by.return_value.first.return_value = role admin_user = User.from_dict(user_admin_valid) admin_user.role = role User.query.filter_by.return_value.filter_by.return_value.first.return_value = admin_user def _filter_by(*args, **kwargs): if kwargs.get('username') == user_admin_valid['username']: _mock = mock.MagicMock() _mock.first.return_value = admin_user return _mock else: _mock = mock.MagicMock() _mock.first.return_value = None return _mock User.query.filter_by.side_effect = _filter_by yield user_admin_valid
def add_role(): """ Add role. --- definitions: - schema: id: Role properties: name: type: string description: role name default: type: boolean description: is default permissions: type: array description: List of permissions items: oneOf: - $ref: "#/definitions/Permission" parameters: - in: body name: role required: true description: role schema: $ref: "#/definitions/Role" responses: 200: description: New role. schema: $ref: '#/definitions/Role' 401: description: Invalid username or password. """ _request = from_json(request.data.decode('utf-8')) api_role = api.Role(**_request) existing_role = Role.query.filter_by(name=api_role.name).first() if existing_role: raise ValidationError('Role already exists.') permissions = [] for api_permission in api_role.permissions: permission = Permission.query.filter_by( name=api_permission.name).first() if not permission: raise ValidationError( f'Permission {api_permission.name} not found.') permissions.append(permission) role = Role(name=api_role.name, default=api_role.default, permissions=permissions) db.session.add(role) db.session.commit() return jsonify(api.Role.from_orm(role).dict()), 200
def signup_valid_request(query_mock, user_admin_valid, role_admin_valid): from lorem_ipsum_auth.models import User, Role, Permission Role.query.filter_by.return_value.first.return_value = Role( id=role_admin_valid['id'], name=role_admin_valid['name'], permissions=[ Permission.from_str(perm) for perm in role_admin_valid['permissions'] ]) User.query.filter_by.return_value.first.return_value = None yield user_admin_valid
def issue_token(user: dict, role: dict) -> str: from lorem_ipsum_auth.models import User, Permission, Role from lorem_ipsum_auth.auth import issue_token_for_user role = Role(id=user['id'], name=role['name'], permissions=[ Permission.from_str(perm) for perm in role['permissions'] ]) Role.query.filter_by.return_value.first.return_value = role _user = User.from_dict(user) _user.role = role User.query.filter_by.return_value.filter_by.return_value.first.return_value = _user User.query.filter_by.return_value.first.return_value = _user return issue_token_for_user(_user)
def login_valid_request(query_mock, user_admin_valid, role_admin_valid): from lorem_ipsum_auth.models import User, Role, Permission role = Role(id=role_admin_valid['id'], name=role_admin_valid['name'], default=role_admin_valid['default'], permissions=[ Permission.from_str(perm) for perm in role_admin_valid['permissions'] ]) Role.query.filter_by.return_value.first.return_value = role admin_user = User.from_dict(user_admin_valid) admin_user.role = role User.query.filter_by.return_value.filter_by.return_value.first.return_value = admin_user User.query.filter_by.return_value.first.return_value = admin_user yield admin_user
def role_add_existing_request(query_mock, user_admin_valid, role_editor_valid): from lorem_ipsum_auth.models import User, Role, Permission Role.query.filter_by.return_value.first.return_value = Role( id=role_editor_valid['id'], name=role_editor_valid['name'], permissions=[ Permission.from_str(perm) for perm in role_editor_valid['permissions'] ]) User.query.filter_by.return_value.filter_by.return_value.first.return_value = User.from_dict( user_admin_valid) User.query.filter_by.return_value.first.return_value = User.from_dict( user_admin_valid) Permission.query.filter_by.return_value.first.return_value = Permission.from_str( 'books:add') yield user_admin_valid
def role_get_valid_request(query_mock, user_admin_valid, role_editor_valid): from lorem_ipsum_auth.models import Permission, Role orig_query = Role.query.filter_by.return_value role = Role(id=role_editor_valid['id'], name=role_editor_valid['name'], permissions=[ Permission.from_str(perm['name']) for perm in role_editor_valid['permissions'] ]) def _filter_by(*args, **kwargs): if kwargs.get('name') == role_editor_valid['name']: _mock = mock.MagicMock() _mock.first.return_value = role return _mock return orig_query Role.query.filter_by.side_effect = _filter_by yield role