| 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.google import google, make_google_blueprint |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture |
| 12 |
def make_app(): |
| 13 |
"A callable to create a Flask app with the Google provider" |
| 14 |
|
| 15 |
def _make_app(*args, **kwargs): |
| 16 |
app = Flask(__name__) |
| 17 |
app.secret_key = "whatever" |
| 18 |
blueprint = make_google_blueprint(*args, **kwargs) |
| 19 |
app.register_blueprint(blueprint) |
| 20 |
return app |
| 21 |
|
| 22 |
return _make_app |
| 23 |
|
| 24 |
|
| 25 |
def test_blueprint_factory(): |
| 26 |
google_bp = make_google_blueprint( |
| 27 |
client_id="foo", client_secret="bar", redirect_to="index" |
| 28 |
) |
| 29 |
assert isinstance(google_bp, OAuth2ConsumerBlueprint) |
| 30 |
assert google_bp.session.scope == [ |
| 31 |
"https://www.googleapis.com/auth/userinfo.profile" |
| 32 |
] |
| 33 |
assert google_bp.session.base_url == "https://www.googleapis.com/" |
| 34 |
assert google_bp.session.client_id == "foo" |
| 35 |
assert google_bp.client_secret == "bar" |
| 36 |
assert google_bp.authorization_url == "https://accounts.google.com/o/oauth2/auth" |
| 37 |
assert google_bp.token_url == "https://accounts.google.com/o/oauth2/token" |
| 38 |
assert google_bp.auto_refresh_url is None |
| 39 |
|
| 40 |
|
| 41 |
def test_load_from_config(make_app): |
| 42 |
app = make_app(redirect_to="index") |
| 43 |
app.config["GOOGLE_OAUTH_CLIENT_ID"] = "foo" |
| 44 |
app.config["GOOGLE_OAUTH_CLIENT_SECRET"] = "bar" |
| 45 |
|
| 46 |
resp = app.test_client().get("/google") |
| 47 |
url = resp.headers["Location"] |
| 48 |
client_id = URLObject(url).query.dict.get("client_id") |
| 49 |
assert client_id == "foo" |
| 50 |
|
| 51 |
|
| 52 |
def test_blueprint_factory_scope(): |
| 53 |
google_bp = make_google_blueprint( |
| 54 |
client_id="foo", client_secret="bar", scope="customscope", redirect_to="index" |
| 55 |
) |
| 56 |
assert google_bp.session.scope == "customscope" |
| 57 |
|
| 58 |
|
| 59 |
def test_blueprint_factory_offline(): |
| 60 |
google_bp = make_google_blueprint( |
| 61 |
client_id="foo", client_secret="bar", redirect_to="index", offline=True |
| 62 |
) |
| 63 |
assert google_bp.auto_refresh_url == "https://accounts.google.com/o/oauth2/token" |
| 64 |
|
| 65 |
|
| 66 |
def test_blueprint_factory_hosted_domain(): |
| 67 |
google_bp = make_google_blueprint( |
| 68 |
client_id="foo", |
| 69 |
client_secret="bar", |
| 70 |
redirect_to="index", |
| 71 |
hosted_domain="example.com", |
| 72 |
) |
| 73 |
|
| 74 |
assert google_bp.authorization_url_params["hd"] == "example.com" |
| 75 |
|
| 76 |
|
| 77 |
def test_blueprint_factory_rule_kwargs(make_app): |
| 78 |
app = make_app( |
| 79 |
client_id="foo", |
| 80 |
client_secret="bar", |
| 81 |
redirect_to="index", |
| 82 |
rule_kwargs={"host": "example2.com"}, |
| 83 |
) |
| 84 |
rules = [ |
| 85 |
rule for rule in app.url_map.iter_rules() if rule.endpoint.startswith("google.") |
| 86 |
] |
| 87 |
assert all(rule.host == "example2.com" for rule in rules) |
| 88 |
assert len(rules) == 2 |
| 89 |
|
| 90 |
|
| 91 |
def test_blueprint_factory_no_rule_kwargs(make_app): |
| 92 |
app = make_app( |
| 93 |
client_id="foo", |
| 94 |
client_secret="bar", |
| 95 |
redirect_to="index", |
| 96 |
) |
| 97 |
rules = [ |
| 98 |
rule for rule in app.url_map.iter_rules() if rule.endpoint.startswith("google.") |
| 99 |
] |
| 100 |
assert all(rule.host is None for rule in rules) |
| 101 |
assert len(rules) == 2 |
| 102 |
|
| 103 |
|
| 104 |
@responses.activate |
| 105 |
def test_context_local(make_app): |
| 106 |
responses.add(responses.GET, "https://google.com") |
| 107 |
|
| 108 |
# set up two apps with two different set of auth tokens |
| 109 |
app1 = make_app( |
| 110 |
"foo1", |
| 111 |
"bar1", |
| 112 |
redirect_to="url1", |
| 113 |
storage=MemoryStorage({"access_token": "app1"}), |
| 114 |
) |
| 115 |
app2 = make_app( |
| 116 |
"foo2", |
| 117 |
"bar2", |
| 118 |
redirect_to="url2", |
| 119 |
storage=MemoryStorage({"access_token": "app2"}), |
| 120 |
) |
| 121 |
|
| 122 |
# outside of a request context, referencing functions on the `google` object |
| 123 |
# will raise an exception |
| 124 |
with pytest.raises(RuntimeError): |
| 125 |
google.get("https://github.com") |
| 126 |
|
| 127 |
# inside of a request context, `google` should be a proxy to the correct |
| 128 |
# blueprint session |
| 129 |
with app1.test_request_context("/"): |
| 130 |
app1.preprocess_request() |
| 131 |
google.get("https://google.com") |
| 132 |
request = responses.calls[0].request |
| 133 |
assert request.headers["Authorization"] == "Bearer app1" |
| 134 |
|
| 135 |
with app2.test_request_context("/"): |
| 136 |
app2.preprocess_request() |
| 137 |
google.get("https://google.com") |
| 138 |
request = responses.calls[1].request |
| 139 |
assert request.headers["Authorization"] == "Bearer app2" |
| 140 |
|
| 141 |
|
| 142 |
def test_offline(make_app): |
| 143 |
app = make_app("foo", "bar", offline=True) |
| 144 |
|
| 145 |
with app.test_client() as client: |
| 146 |
resp = client.get("/google", base_url="https://a.b.c", follow_redirects=False) |
| 147 |
# check that there is a `access_type=offline` query param in the redirect URL |
| 148 |
assert resp.status_code == 302 |
| 149 |
location = URLObject(resp.headers["Location"]) |
| 150 |
assert location.query_dict["access_type"] == "offline" |
| 151 |
|
| 152 |
|
| 153 |
def test_hd(make_app): |
| 154 |
app = make_app("foo", "bar", hosted_domain="example.com") |
| 155 |
|
| 156 |
with app.test_client() as client: |
| 157 |
resp = client.get("/google", base_url="https://a.b.c", follow_redirects=False) |
| 158 |
# check that there is a `hd=example.com` query param in the redirect URL |
| 159 |
assert resp.status_code == 302 |
| 160 |
location = URLObject(resp.headers["Location"]) |
| 161 |
assert location.query_dict["hd"] == "example.com" |
| 162 |
|
| 163 |
|
| 164 |
def test_offline_consent(make_app): |
| 165 |
app = make_app("foo", "bar", offline=True, reprompt_consent=True) |
| 166 |
|
| 167 |
with app.test_client() as client: |
| 168 |
resp = client.get("/google", base_url="https://a.b.c", follow_redirects=False) |
| 169 |
assert resp.status_code == 302 |
| 170 |
location = URLObject(resp.headers["Location"]) |
| 171 |
assert location.query_dict["access_type"] == "offline" |
| 172 |
assert location.query_dict["prompt"] == "consent" |
| 173 |
|
| 174 |
|
| 175 |
def test_offline_select_account(make_app): |
| 176 |
app = make_app("foo", "bar", offline=True, reprompt_select_account=True) |
| 177 |
|
| 178 |
with app.test_client() as client: |
| 179 |
resp = client.get("/google", base_url="https://a.b.c", follow_redirects=False) |
| 180 |
assert resp.status_code == 302 |
| 181 |
location = URLObject(resp.headers["Location"]) |
| 182 |
assert location.query_dict["access_type"] == "offline" |
| 183 |
assert location.query_dict["prompt"] == "select_account" |
| 184 |
|
| 185 |
|
| 186 |
def test_offline_select_account_and_consent(make_app): |
| 187 |
app = make_app( |
| 188 |
"foo", "bar", offline=True, reprompt_consent=True, reprompt_select_account=True |
| 189 |
) |
| 190 |
|
| 191 |
with app.test_client() as client: |
| 192 |
resp = client.get("/google", base_url="https://a.b.c", follow_redirects=False) |
| 193 |
assert resp.status_code == 302 |
| 194 |
location = URLObject(resp.headers["Location"]) |
| 195 |
assert location.query_dict["access_type"] == "offline" |
| 196 |
assert location.query_dict["prompt"] == "consent select_account" |