def client(): # pylint: disable=missing-param-doc,missing-type-doc """Create a Flask test client fixture Yields: flask.testing.FlaskClient: a Flask testing client """ api = CometApi(hmac_secret="secret") @api.register_auth() def override(): if g.test_authorized_for: return g.test_authorized_for return [] @api.register_hydrator() def test_hydrate(issues): return [{"fingerprint": x.fingerprint} for x in issues] @api.register_request_hydrator() def request_hydrator(request): return dict(request.headers) app = api.create_app() with app.app_context(): yield app.test_client()
def test_assert_valid_token(): fp = "test_fingerprint" test_hmac_secret = "secret" token = fingerprint_hmac(fp, test_hmac_secret) api = CometApi(hmac_secret=test_hmac_secret) with api.create_app().app_context(): assert_valid_token(fp, token)
def app_context_with_request_hydrator(): api = CometApi() @api.register_request_hydrator() def request_hydrator(request): return request app = api.create_app() yield app.app_context()
def bad_client(): # pylint: disable=missing-param-doc,missing-type-doc """Create a bad Flask test client fixture Yields: flask.testing.FlaskClient: a Flask testing client """ api = CometApi() app = api.create_app() with app.app_context(): yield app.test_client()
def test_get_issues_no_hydrator(): app = CometApi().create_app() with app.app_context(): client = app.test_client() assert client.get('/v0/issues')
from flask import Response, request LOG = logging.getLogger(__name__) # configuration, you might want to read this from a config file instead CONFIG = { 'cors_origins': [], 'database_uri': 'sqlite:///comet-example.db', 'host': '0.0.0.0', 'port': 5000, 'unsafe_skip_authorization': True, # /!\ WARNING, using unauthenticated API calls for this example /!\ } # initialize the API application API = CometApi(cors_origins=CONFIG.get('cors_origins'), database_uri=CONFIG.get('database_uri'), host=CONFIG.get('host'), port=CONFIG.get('port')) @API.register_auth() def auth(): """Checks authorization headers and verifies that proper credentials were provided. /!\ WARNING: this example does NOT do any authorization, you have to implement it here to secure access to your API. Returns: Union[List[str], flask.Response]: list of authenticated usernames or a HTTP 401 Response in case of no or invalid authorization header provided """ if CONFIG.get('unsafe_skip_authorization', False): return ['*****@*****.**']
def test_no_request_hydrator(): api = CometApi() request_mock = mock.Mock() with api.create_app().app_context(): assert not hydrate_with_request_headers(request_mock)
def test_no_hydrator(): api = CometApi() with api.create_app().app_context(): assert not hydrate_open_issues([])
def app_context(): api = CometApi() app = api.create_app() yield app.app_context()
def test_get_issues_no_hydrator(): """Test the get_issues endpoint still works while there is no hydrator""" app = CometApi().create_app() with app.app_context(): client = app.test_client() assert client.get("/v0/issues")