Example #1
0
    def setUp(self):
        app = Flask(__name__)
        app.config["TESTING"] = True
        app.config["DEBUG"] = True
        app.config["TRAP_HTTP_EXCEPTIONS"] = True

        secret_key = "really big secret"
        token_manager = TokenManager(secret_key)

        token_auth = TokenAuth(secret_key)

        @app.route("/token")
        def get_token():
            user = "test_user"
            token = token_manager.generate(user).decode("utf-8")
            doc = {"token": token}
            return jsonify(doc)

        @app.route("/")
        def root():
            return jsonify({"message": "OK"})

        @app.route("/token_required")
        @token_auth.token_required
        def token_required():
            response = jsonify({"message": "OK"})
            response.status_code = 200
            return response

        self.app = app
        self.client = app.test_client()
        self.secret_key = secret_key
        self.token_manager = token_manager

        return self.app
Example #2
0
import logging
from flask import Flask, request, json, make_response, abort
from models import Customer, Visitor
from werkzeug.exceptions import HTTPException, BadRequest, InternalServerError, Conflict

# Logging config
logger = logging.getLogger("APE")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())  # Log to STDOUT for now

# The app
app = Flask(__name__, static_folder="static", static_url_path="")

# Required to ensure HTTP exception handers work in debug & test
# See: http://flask.pocoo.org/docs/0.10/api/#flask.Flask.trap_http_exception
app.config["TRAP_HTTP_EXCEPTIONS"] = True

JSONP_CALLBACK = "_ape.callback"


def make_jsonp_response(payload=dict(), code=200):
    """Make a jsonp response object from a payload dict"""
    # Response always returns 200 code, to ensure client can handle callback
    # Actual HTTP code sent in payload
    payload["status_code"] = code
    body = "%s(%s)" % (JSONP_CALLBACK, json.dumps(payload))
    response = make_response(body, 200)
    response.headers["Content-Type"] = "application/javascript;charset=utf-8"
    logger.debug(" JSONP %s" % body)
    return response