Debian Codegraph @

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