| 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.twitch import make_twitch_blueprint, twitch |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture |
| 12 |
def make_app(): |
| 13 |
"A callable to create a Flask app with the Twitch provider" |
| 14 |
|
| 15 |
def _make_app(*args, **kwargs): |
| 16 |
app = Flask(__name__) |
| 17 |
app.secret_key = "whatever" |
| 18 |
blueprint = make_twitch_blueprint(*args, **kwargs) |
| 19 |
app.register_blueprint(blueprint) |
| 20 |
return app |
| 21 |
|
| 22 |
return _make_app |
| 23 |
|
| 24 |
|
| 25 |
def test_blueprint_factory(): |
| 26 |
scope = [ |
| 27 |
"user:read:subscriptions", |
| 28 |
"channel:read:subscriptions", |
| 29 |
"user:read:subscriptions", |
| 30 |
] |
| 31 |
|
| 32 |
twitch_bp = make_twitch_blueprint( |
| 33 |
client_id="foo", |
| 34 |
client_secret="bar", |
| 35 |
scope=scope, |
| 36 |
redirect_to="index", |
| 37 |
) |
| 38 |
assert isinstance(twitch_bp, OAuth2ConsumerBlueprint) |
| 39 |
assert twitch_bp.session.scope == scope |
| 40 |
assert twitch_bp.session.base_url == "https://api.twitch.tv/helix/" |
| 41 |
assert twitch_bp.session.client_id == "foo" |
| 42 |
assert twitch_bp.client_secret == "bar" |
| 43 |
assert twitch_bp.authorization_url == "https://id.twitch.tv/oauth2/authorize" |
| 44 |
assert twitch_bp.token_url == "https://id.twitch.tv/oauth2/token" |
| 45 |
|
| 46 |
|
| 47 |
def test_load_from_config(make_app): |
| 48 |
app = make_app() |
| 49 |
app.config["TWITCH_OAUTH_CLIENT_ID"] = "foo" |
| 50 |
app.config["TWITCH_OAUTH_CLIENT_SECRET"] = "bar" |
| 51 |
|
| 52 |
resp = app.test_client().get("/twitch") |
| 53 |
url = resp.headers["Location"] |
| 54 |
client_id = URLObject(url).query.dict.get("client_id") |
| 55 |
assert client_id == "foo" |
| 56 |
|
| 57 |
|
| 58 |
@responses.activate |
| 59 |
def test_context_local(make_app): |
| 60 |
responses.add(responses.GET, "https://google.com") |
| 61 |
|
| 62 |
# set up two apps with two different set of auth tokens |
| 63 |
app1 = make_app( |
| 64 |
"foo1", |
| 65 |
"bar1", |
| 66 |
redirect_to="url1", |
| 67 |
storage=MemoryStorage({"access_token": "app1"}), |
| 68 |
) |
| 69 |
|
| 70 |
app2 = make_app( |
| 71 |
"foo2", |
| 72 |
"bar2", |
| 73 |
redirect_to="url2", |
| 74 |
storage=MemoryStorage({"access_token": "app2"}), |
| 75 |
) |
| 76 |
|
| 77 |
# outside of a request context, referencing functions on the `twitch` object |
| 78 |
# will raise an exception |
| 79 |
with pytest.raises(RuntimeError): |
| 80 |
twitch.get("https://google.com") |
| 81 |
|
| 82 |
# inside of a request context, `twitch` should be a proxy to the correct |
| 83 |
# blueprint session |
| 84 |
with app1.test_request_context("/"): |
| 85 |
app1.preprocess_request() |
| 86 |
twitch.get("https://google.com") |
| 87 |
request = responses.calls[0].request |
| 88 |
assert request.headers["Authorization"] == "Bearer app1" |
| 89 |
assert request.headers["client-id"] == "foo1" |
| 90 |
|
| 91 |
with app2.test_request_context("/"): |
| 92 |
app2.preprocess_request() |
| 93 |
twitch.get("https://google.com") |
| 94 |
request = responses.calls[1].request |
| 95 |
assert request.headers["Authorization"] == "Bearer app2" |
| 96 |
assert request.headers["client-id"] == "foo2" |