| 1 |
import pytest |
| 2 |
import responses |
| 3 |
from flask import Flask |
| 4 |
from urlobject import URLObject |
| 5 |
|
| 6 |
from flask_dance.consumer import OAuth2ConsumerBlueprint |
| 7 |
from flask_dance.consumer.storage import MemoryStorage |
| 8 |
from flask_dance.contrib.strava import make_strava_blueprint, strava |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture |
| 12 |
def make_app(): |
| 13 |
"A callable to create a Flask app with the Strava provider" |
| 14 |
|
| 15 |
def _make_app(*args, **kwargs): |
| 16 |
app = Flask(__name__) |
| 17 |
app.secret_key = "whatever" |
| 18 |
blueprint = make_strava_blueprint(*args, **kwargs) |
| 19 |
app.register_blueprint(blueprint) |
| 20 |
return app |
| 21 |
|
| 22 |
return _make_app |
| 23 |
|
| 24 |
|
| 25 |
def test_blueprint_factory(): |
| 26 |
strava_bp = make_strava_blueprint( |
| 27 |
client_id="foo", client_secret="bar", scope="identity", redirect_to="index" |
| 28 |
) |
| 29 |
assert isinstance(strava_bp, OAuth2ConsumerBlueprint) |
| 30 |
assert strava_bp.session.scope == "identity" |
| 31 |
assert strava_bp.session.base_url == "https://www.strava.com/api/v3" |
| 32 |
assert strava_bp.session.client_id == "foo" |
| 33 |
assert strava_bp.client_secret == "bar" |
| 34 |
assert ( |
| 35 |
strava_bp.authorization_url == "https://www.strava.com/api/v3/oauth/authorize" |
| 36 |
) |
| 37 |
assert strava_bp.token_url == "https://www.strava.com/api/v3/oauth/token" |
| 38 |
|
| 39 |
|
| 40 |
def test_load_from_config(make_app): |
| 41 |
app = make_app() |
| 42 |
app.config["STRAVA_OAUTH_CLIENT_ID"] = "foo" |
| 43 |
app.config["STRAVA_OAUTH_CLIENT_SECRET"] = "bar" |
| 44 |
|
| 45 |
resp = app.test_client().get("/strava") |
| 46 |
url = resp.headers["Location"] |
| 47 |
client_id = URLObject(url).query.dict.get("client_id") |
| 48 |
assert client_id == "foo" |
| 49 |
|
| 50 |
|
| 51 |
@responses.activate |
| 52 |
def test_context_local(make_app): |
| 53 |
responses.add(responses.GET, "https://google.com") |
| 54 |
|
| 55 |
# set up two apps with two different set of auth tokens |
| 56 |
app1 = make_app( |
| 57 |
"foo1", |
| 58 |
"bar1", |
| 59 |
redirect_to="url1", |
| 60 |
storage=MemoryStorage({"access_token": "app1"}), |
| 61 |
) |
| 62 |
app2 = make_app( |
| 63 |
"foo2", |
| 64 |
"bar2", |
| 65 |
redirect_to="url2", |
| 66 |
storage=MemoryStorage({"access_token": "app2"}), |
| 67 |
) |
| 68 |
|
| 69 |
# outside of a request context, referencing functions on the `strava` object |
| 70 |
# will raise an exception |
| 71 |
with pytest.raises(RuntimeError): |
| 72 |
strava.get("https://google.com") |
| 73 |
|
| 74 |
# inside of a request context, `strava` should be a proxy to the correct |
| 75 |
# blueprint session |
| 76 |
with app1.test_request_context("/"): |
| 77 |
app1.preprocess_request() |
| 78 |
strava.get("https://google.com") |
| 79 |
request = responses.calls[0].request |
| 80 |
assert request.headers["Authorization"] == "Bearer app1" |
| 81 |
|
| 82 |
with app2.test_request_context("/"): |
| 83 |
app2.preprocess_request() |
| 84 |
strava.get("https://google.com") |
| 85 |
request = responses.calls[1].request |
| 86 |
assert request.headers["Authorization"] == "Bearer app2" |