Debian Codegraph @

tests/contrib/test_facebook.py

raw
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.facebook import facebook, make_facebook_blueprint
9
10
11 @pytest.fixture
12 def make_app():
13 "A callable to create a Flask app with the Facebook provider"
14
15 def _make_app(*args, **kwargs):
16 app = Flask(__name__)
17 app.secret_key = "whatever"
18 blueprint = make_facebook_blueprint(*args, **kwargs)
19 app.register_blueprint(blueprint)
20 return app
21
22 return _make_app
23
24
25 def test_blueprint_factory():
26 facebook_bp = make_facebook_blueprint(
27 client_id="foo", client_secret="bar", scope="user:email", redirect_to="index"
28 )
29 assert isinstance(facebook_bp, OAuth2ConsumerBlueprint)
30 assert facebook_bp.session.scope == "user:email"
31 assert facebook_bp.session.base_url == "https://graph.facebook.com/"
32 assert facebook_bp.session.client_id == "foo"
33 assert facebook_bp.client_secret == "bar"
34 assert facebook_bp.authorization_url == "https://www.facebook.com/dialog/oauth"
35 assert facebook_bp.token_url == "https://graph.facebook.com/oauth/access_token"
36
37
38 def test_load_from_config(make_app):
39 app = make_app(redirect_to="index")
40 app.config["FACEBOOK_OAUTH_CLIENT_ID"] = "foo"
41 app.config["FACEBOOK_OAUTH_CLIENT_SECRET"] = "bar"
42
43 resp = app.test_client().get("/facebook")
44 url = resp.headers["Location"]
45 client_id = URLObject(url).query.dict.get("client_id")
46 assert client_id == "foo"
47
48
49 @responses.activate
50 def test_context_local(make_app):
51 responses.add(responses.GET, "https://google.com")
52
53 # set up two apps with two different set of auth tokens
54 app1 = make_app(
55 "foo1",
56 "bar1",
57 redirect_to="url1",
58 storage=MemoryStorage({"access_token": "app1"}),
59 )
60 app2 = make_app(
61 "foo2",
62 "bar2",
63 redirect_to="url2",
64 storage=MemoryStorage({"access_token": "app2"}),
65 )
66
67 # outside of a request context, referencing functions on the `facebook` object
68 # will raise an exception
69 with pytest.raises(RuntimeError):
70 facebook.get("https://google.com")
71
72 # inside of a request context, `facebook` should be a proxy to the correct
73 # blueprint session
74 with app1.test_request_context("/"):
75 app1.preprocess_request()
76 facebook.get("https://google.com")
77 request = responses.calls[0].request
78 assert request.headers["Authorization"] == "Bearer app1"
79
80 with app2.test_request_context("/"):
81 app2.preprocess_request()
82 facebook.get("https://google.com")
83 request = responses.calls[1].request
84 assert request.headers["Authorization"] == "Bearer app2"
85
86
87 @pytest.mark.parametrize("rerequest", (True, False))
88 def test_rerequest_declined_scopes(make_app, rerequest):
89 """
90 Tests that the rerequest_declined_permissions flag in the facebook blueprint sends
91 toggles the header reasking oauth permissions as detailed in the facebook docs https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow#reaskperms
92
93 Tests both that the header is set with the flag (rerequest=True) and that it is missing without the flag (rerequest=False)
94 """
95 app = make_app(scope="user_posts", rerequest_declined_permissions=rerequest)
96 with app.test_client() as client:
97 resp = client.get("/facebook", base_url="https://a.b.c", follow_redirects=False)
98 assert resp.status_code == 302
99 location = URLObject(resp.headers["Location"])
100 if rerequest:
101 assert location.query_dict["auth_type"] == "rerequest"
102 else:
103 assert "auth_type" not in location.query_dict