Debian Codegraph @

tests/contrib/test_dropbox.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.dropbox import dropbox, make_dropbox_blueprint
9
10
11 @pytest.fixture
12 def make_app():
13 "A callable to create a Flask app with the Dropbox provider"
14
15 def _make_app(*args, **kwargs):
16 app = Flask(__name__)
17 app.secret_key = "whatever"
18 blueprint = make_dropbox_blueprint(*args, **kwargs)
19 app.register_blueprint(blueprint)
20 return app
21
22 return _make_app
23
24
25 def test_blueprint_factory():
26 dropbox_bp = make_dropbox_blueprint(app_key="foo", app_secret="bar")
27 assert isinstance(dropbox_bp, OAuth2ConsumerBlueprint)
28 assert dropbox_bp.session.base_url == "https://api.dropbox.com/2/"
29 assert dropbox_bp.session.client_id == "foo"
30 assert dropbox_bp.client_secret == "bar"
31 assert dropbox_bp.authorization_url == "https://www.dropbox.com/oauth2/authorize"
32 assert dropbox_bp.token_url == "https://api.dropbox.com/oauth2/token"
33
34
35 def test_load_from_config(make_app):
36 app = make_app()
37 app.config["DROPBOX_OAUTH_CLIENT_ID"] = "foo"
38 app.config["DROPBOX_OAUTH_CLIENT_SECRET"] = "bar"
39
40 resp = app.test_client().get("/dropbox")
41 url = resp.headers["Location"]
42 client_id = URLObject(url).query.dict.get("client_id")
43 assert client_id == "foo"
44
45
46 @responses.activate
47 def test_context_local(make_app):
48 responses.add(responses.GET, "https://dropbox.com")
49
50 # set up two apps with two different set of auth tokens
51 app1 = make_app(
52 "foo1",
53 "bar1",
54 redirect_to="url1",
55 storage=MemoryStorage({"access_token": "app1"}),
56 )
57 app2 = make_app(
58 "foo2",
59 "bar2",
60 redirect_to="url2",
61 storage=MemoryStorage({"access_token": "app2"}),
62 )
63
64 # outside of a request context, referencing functions on the `dropbox` object
65 # will raise an exception
66 with pytest.raises(RuntimeError):
67 dropbox.get("https://dropbox.com")
68
69 # inside of a request context, `dropbox` should be a proxy to the correct
70 # blueprint session
71 with app1.test_request_context("/"):
72 app1.preprocess_request()
73 dropbox.get("https://dropbox.com")
74 request = responses.calls[0].request
75 assert request.headers["Authorization"] == "Bearer app1"
76
77 with app2.test_request_context("/"):
78 app2.preprocess_request()
79 dropbox.get("https://dropbox.com")
80 request = responses.calls[1].request
81 assert request.headers["Authorization"] == "Bearer app2"
82
83
84 def app_redirect_location(app):
85 with app.test_client() as client:
86 resp = client.get("/dropbox", base_url="https://a.b.c", follow_redirects=False)
87 assert resp.status_code == 302
88 return URLObject(resp.headers["Location"])
89
90
91 def test_default_redirect_params(make_app):
92 app = make_app("foo", "bar")
93 query_dict = app_redirect_location(app).query_dict
94 assert isinstance(query_dict.pop("state"), str)
95 assert query_dict == {
96 "client_id": "foo",
97 "redirect_uri": "https://a.b.c/dropbox/authorized",
98 "response_type": "code",
99 }
100
101
102 def test_force_reapprove(make_app):
103 app = make_app("foo", "bar", force_reapprove=True)
104 assert app_redirect_location(app).query_dict["force_reapprove"] == "true"
105
106
107 def test_disable_signup(make_app):
108 app = make_app("foo", "bar", disable_signup=True)
109 assert app_redirect_location(app).query_dict["disable_signup"] == "true"
110
111
112 def test_require_role(make_app):
113 app = make_app("foo", "bar", require_role="work")
114 assert app_redirect_location(app).query_dict["require_role"] == "work"
115
116
117 def test_offline(make_app):
118 app = make_app("foo", "bar", offline=True)
119 assert app_redirect_location(app).query_dict["token_access_type"] == "offline"