Debian Codegraph @

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