Exemple #1
0
class App(object):

    def __init__(self, flask_app):
        super(App, self).__init__()
        self.flask_app = flask_app
        self.loopback = FlaskLoopback(self.flask_app)
        self.hostname = str(uuid1())
        self.url = URLObject("http://{0}".format(self.hostname))

    def activate(self):
        self.loopback.activate_address((self.hostname, 80))

    def deactivate(self):
        self.loopback.deactivate_address((self.hostname, 80))

    def get_page(self, page_size, page, path=None):
        if path is None:
            path = "objects"
        response = requests.get(self.url.add_path(path).set_query_param("page", str(page)).set_query_param("page_size", str(page_size)))
        response.raise_for_status()
        data = response.json()
        assert data["metadata"]["page"] == page
        assert data["metadata"]["page_size"] == page_size
        return data["result"]

    def get_all_paged(self, page_size, path=None):
        return list(itertools.chain.from_iterable(self.get_page(page_size, page, path=path) for page in range(1, int(self.num_objects / page_size) + 5)))
Exemple #2
0
class App(object):
    def __init__(self, flask_app):
        super(App, self).__init__()
        self.flask_app = flask_app
        self.loopback = FlaskLoopback(self.flask_app)
        self.hostname = str(uuid1())
        self.url = URLObject("http://{0}".format(self.hostname))

    def activate(self):
        self.loopback.activate_address((self.hostname, 80))

    def deactivate(self):
        self.loopback.deactivate_address((self.hostname, 80))

    def get_page(self, page_size, page, path=None):
        if path is None:
            path = "objects"
        response = requests.get(
            self.url.add_path(path).set_query_param("page",
                                                    str(page)).set_query_param(
                                                        "page_size",
                                                        str(page_size)))
        response.raise_for_status()
        data = response.json()
        assert data["metadata"]["page"] == page
        assert data["metadata"]["page_size"] == page_size
        return data["result"]

    def get_all_paged(self, page_size, path=None):
        return list(
            itertools.chain.from_iterable(
                self.get_page(page_size, page, path=path)
                for page in range(1,
                                  int(self.num_objects / page_size) + 5)))
def main(verbose, quiet):

    from _benchmark_queries import queries

    with logbook.NullHandler(), logbook.StreamHandler(sys.stderr, level=logbook.CRITICAL-verbose+quiet, bubble=False):

        loopback = FlaskLoopback(create_app())
        loopback.activate_address((_root_address, 80))


        num_attempts = 5
        for obj, query in queries:
            times = []
            has_error = False
            print(obj, '|', click.style(query, fg='cyan'), '--')
            for i in range(num_attempts):
                start_time = time.time()
                resp = requests.get(_root_url.add_path('/rest').add_path(obj).add_query_param('search', query).add_query_param('page_size', 25))
                end_time = time.time()
                if resp.status_code == requests.codes.internal_server_error:
                    print('\t', click.style('Timeout', fg='red'))
                    has_error=True
                    break
                else:
                    resp.raise_for_status()
                    times.append(end_time - start_time)
            if not has_error:
                print('\t', len(resp.json()[obj]), 'results --', '(Out of {}) Best: {:.03}s Avg: {:.03}s Worst: {:.03}s'.format(num_attempts, min(times), sum(times) / len(times), max(times)))
Exemple #4
0
class Webapp(object):

    def __init__(self, app):
        super(Webapp, self).__init__()
        self.app = app
        self.loopback = FlaskLoopback(self.app)
        self.hostname = str(uuid4())
        self.url = URL('http://{0}'.format(self.hostname))

    def activate(self):
        self.loopback.activate_address((self.hostname, 80))

    def deactivate(self):
        self.loopback.deactivate_address((self.hostname, 80))

    def _request(self, method, path, *args, **kwargs):
        raw_response = kwargs.pop("raw_response", False)
        if path.startswith("/"):
            path = path[1:]
            assert not path.startswith("/")
        returned = requests.request(
            method, "http://{0}/{1}".format(self.hostname, path), *args, **kwargs)
        if raw_response:
            return returned

        returned.raise_for_status()
        return returned.json()
Exemple #5
0
class Webapp(object):
    def __init__(self, app):
        super(Webapp, self).__init__()
        self.app = app
        self.loopback = FlaskLoopback(self.app)
        self.hostname = str(uuid4())
        self.url = URL('http://{0}'.format(self.hostname))

    def activate(self):
        self.loopback.activate_address((self.hostname, 80))

    def deactivate(self):
        self.loopback.deactivate_address((self.hostname, 80))

    def _request(self, method, path, *args, **kwargs):
        raw_response = kwargs.pop("raw_response", False)
        if path.startswith("/"):
            path = path[1:]
            assert not path.startswith("/")
        returned = requests.request(
            method, "http://{0}/{1}".format(self.hostname, path), *args,
            **kwargs)
        if raw_response:
            return returned

        returned.raise_for_status()
        return returned.json()
Exemple #6
0
def mailboxer_url(request, db_engine):
    loopback = FlaskLoopback(create_app())
    hostname = str(uuid.uuid1())
    loopback.activate_address((hostname, 80))
    @request.addfinalizer
    def close():
        loopback.deactivate_address((hostname, 80))
    return URL("http://{0}".format(hostname))
def url(request, flask_app):
    address = str(uuid1())
    returned = URL('http://{0}'.format(address))
    webapp = FlaskLoopback(flask_app)
    webapp.activate_address((address, 80))

    @request.addfinalizer
    def finalize():  # pylint: disable=unused-variable
        webapp.deactivate_address((address, 80))
    return returned
def mailboxer_url(request, db_engine):
    loopback = FlaskLoopback(create_app())
    hostname = str(uuid.uuid1())
    loopback.activate_address((hostname, 80))

    @request.addfinalizer
    def close():
        loopback.deactivate_address((hostname, 80))

    return URL("http://{0}".format(hostname))
Exemple #9
0
    def activate(self):
        self.app = build_app(use_cached=True)
        self.app.config["SECRET_KEY"] = "testing_key"
        self.app.config["TESTING"] = True

        self.loopback = FlaskLoopback(self.app)
        self.loopback.activate_address((self.hostname, 80))
Exemple #10
0
def app():
    returned = Flask(__name__)

    @returned.route("/echo", methods=["post"])
    def echo():  # pylint: disable=unused-variable
        return jsonify({"result": True})

    @returned.route("/request_vars")
    def get_request_vars():  # pylint: disable=unused-variable
        return jsonify(
            dict((name, getattr(request, name)) for name in ["host"]))

    @returned.route("/set_cookie")
    def set_cookie():  # pylint: disable=unused-variable
        returned = jsonify({})
        returned.set_cookie(_COOKIE_NAME, value=_COOKIE_VALUE)
        return returned

    @returned.route("/assert_no_cookies")
    def assert_no_cookies():  # pylint: disable=unused-variable
        assert not request.cookies
        return jsonify({"result": "ok"})

    @returned.route("/assert_cookies")
    def assert_cookies():  # pylint: disable=unused-variable
        assert request.cookies[_COOKIE_NAME] == _COOKIE_VALUE
        return jsonify({})

    @returned.route("/set_cookie_on_after_request")
    def set_cookie_on_after_request():  # pylint: disable=unused-variable
        g.cookies.append((_COOKIE_NAME, _COOKIE_VALUE))
        return jsonify({})

    @returned.route("/stream_upload", methods=["POST"])
    def stream_upload():  # pylint: disable=unused-variable
        return hashlib.sha512(request.stream.read()).hexdigest()

    @returned.route("/compressed")
    def compressed():  # pylint: disable=unused-variable
        orig = "uncompressed!".encode("utf-8")
        return Response(gzip_compress(orig),
                        headers={"Content-Encoding": "gzip"})

    @returned.before_request
    def before():  # pylint: disable=unused-variable
        g.cookies = []

    @returned.after_request
    def after_request(response):  # pylint: disable=unused-variable
        cookies = getattr(g, "cookies", [])
        while cookies:
            response.set_cookie(*cookies.pop())

        return response

    return FlaskLoopback(returned)
Exemple #11
0
 def __init__(self, flask_app):
     super(App, self).__init__()
     self.flask_app = flask_app
     self.loopback = FlaskLoopback(self.flask_app)
     self.hostname = str(uuid1())
     self.url = URLObject("http://{0}".format(self.hostname))
Exemple #12
0
 def __init__(self, flask_app):
     super(App, self).__init__()
     self.flask_app = flask_app
     self.loopback = FlaskLoopback(self.flask_app)
     self.hostname = str(uuid1())
     self.url = URLObject("http://{0}".format(self.hostname))
Exemple #13
0
from bba_config import SECRET_KEY
from bbadmintool import BbAdminTool

app = Flask(__name__, static_url_path='/static')
app.config.from_object('bba_config')
app.secret_key = SECRET_KEY

db = SQLAlchemy(app)
bb = BbAdminTool(app)

login_manager = LoginManager()
login_manager.init_app(app)

cors = CORS(app, allow_headers=[Authorization])

loopback = FlaskLoopback(app)

from views import *
from models import *

api_manager = APIManager(
    app,
    flask_sqlalchemy_db=db,
    preprocessors=dict(
        # GET=[auth_func], GET_SINGLE=[auth_func], GET_MANY=[auth_func],
        POST=[auth_func],
        POST_SINGLE=[auth_func],
        POST_MANY=[auth_func],
        PATCH=[auth_func],
        PATCH_SINGLE=[auth_func],
        PATCH_MANY=[auth_func],
Exemple #14
0
def scotty(api_call_logger):
    app = Flask(__name__)
    url = "http://mock-scotty"

    @app.route("/combadge")
    def combadge_file():
        api_call_logger.log_call(flask.request)
        combadge_version = flask.request.values.get("combadge_version")
        fixtures_folder = os.path.join(os.path.dirname(__file__), "fixtures")
        if combadge_version == "v1":
            file_name = "combadge.py"
        elif combadge_version == "v2":
            file_name = "combadge"
        else:
            raise ValueError(
                "Unknown combadge version {combadge_version}".format(
                    combadge_version=combadge_version))
        return send_file(os.path.join(fixtures_folder, file_name))

    @app.route("/beams", methods=["POST"])
    def beams():
        api_call_logger.log_call(flask.request)
        json = flask.request.json
        date = datetime.datetime(year=2020, month=2, day=27).isoformat() + "Z"
        return jsonify({
            "beam": {
                "id": 666,
                "start": date,
                "size": 0,
                "host": json["beam"].get("host"),
                "comment": json["beam"].get("comment"),
                "directory": json["beam"].get("directory"),
                "initiator": json["beam"].get("user"),
                "error": None,
                "combadge_contacted": False,
                "pending_deletion": False,
                "completed": False,
                "deleted": False,
                "pins": [],
                "tags": [],
                "associated_issues": [],
                "purge_time": 0,
            }
        })

    beam_count = 2
    all_beams = [{
        "id":
        i,
        "start":
        datetime.datetime(year=2020, month=2, day=27).isoformat() + "Z",
        "size":
        0,
        "host":
        "host{}".format(i),
        "comment":
        "comment{}".format(i),
        "directory":
        "directory{}".format(i),
        "initiator":
        "user{}".format(i),
        "error":
        None,
        "combadge_contacted":
        False,
        "pending_deletion":
        False,
        "completed":
        False,
        "deleted":
        False,
        "pins": [],
        "tags": [],
        "associated_issues": [],
        "purge_time":
        0,
    } for i in range(beam_count)]

    @app.route("/beams")
    def beams_index():
        api_call_logger.log_call(request)
        page = int(request.values.get("page", 1))
        return jsonify({
            "beams": all_beams[page - 1:page],
            "meta": {
                "total_pages": beam_count,
            },
        })

    @app.route("/beams/<int:beam>")
    def single_beam(beam):
        return jsonify({
            "beam": all_beams[beam],
        })

    @app.route("/info")
    def info():
        return jsonify({
            "transporter": "mock-transporter",
            "version": "0.0.0",
        })

    with FlaskLoopback(app).on(("mock-scotty", 80)):
        yield Scotty(url)
Exemple #15
0
 def __init__(self, app):
     super(Webapp, self).__init__()
     self.app = app
     self.loopback = FlaskLoopback(self.app)
     self.hostname = str(uuid4())
     self.url = URL('http://{0}'.format(self.hostname))
Exemple #16
0
 def __init__(self, app):
     super(Webapp, self).__init__()
     self.app = app
     self.loopback = FlaskLoopback(self.app)
     self.hostname = str(uuid4())
     self.url = URL('http://{0}'.format(self.hostname))
Exemple #17
0
class Webapp(object):

    def __init__(self):
        super(Webapp, self).__init__()
        self.hostname = str(uuid.uuid1())

    def __enter__(self):
        self.activate()
        return self

    def __exit__(self, *args):
        self.deactivate()

    def activate(self):
        self.app = build_app(use_cached=True)
        self.app.config["SECRET_KEY"] = "testing_key"
        self.app.config["TESTING"] = True

        self.loopback = FlaskLoopback(self.app)
        self.loopback.activate_address((self.hostname, 80))

    def deactivate(self):
        self.loopback.deactivate_address((self.hostname, 80))

    def request(self, method, path, *args, **kwargs):
        """Performs a request against the server app
        All arguments are similar to requests' ``request``, except for a few
        exceptions listed below

        :keyword raw_response: if ``True``, returns the raw response object and avoids checking for success
        :keyword data_json: if provided, sends the value provided serialized to JSON, and sets the content type to be json accordingly
        """
        if not isinstance(path, str):
            path = str(path)
        raw_response = kwargs.pop("raw_response", False)

        data_json = kwargs.pop('data_json', NOTHING)
        if data_json is not NOTHING:
            if 'data' in kwargs:
                raise RuntimeError('`data` is not supported when combined with `data_json`')
            kwargs['data'] = json.dumps(data_json)
            headers = kwargs['headers'] = dict(kwargs.get('headers', ()))
            if not any(k.lower() == 'content-type' for k in headers):
                headers['Content-type'] = 'application/json'

        if path.startswith("/"):
            path = path[1:]
            assert not path.startswith("/")
        returned = requests.request(method, f"http://{self.hostname}/{path}", *args, **kwargs)
        if raw_response:
            return returned

        returned.raise_for_status()
        return returned.json()

    def get(self, *args, **kwargs):
        """Shortcut for self.request('get', ...)
        """
        return self.request("get", *args, **kwargs)

    def post(self, *args, **kwargs):
        """Shortcut for self.request('post', ...)
        """
        return self.request("post", *args, **kwargs)

    def delete(self, *args, **kwargs):
        """Shortcut for self.request('delete', ...)
        """
        return self.request("delete", *args, **kwargs)

    def put(self, *args, **kwargs):
        """Shortcut for self.request('put', ...)
        """
        return self.request("put", *args, **kwargs)