Debian Codegraph @

tests/contrib/test_zoho.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.zoho import make_zoho_blueprint, zoho
9
10
11 @pytest.fixture
12 def make_app():
13 "A callable to create a Flask app with the Zoho provider"
14
15 def _make_app(*args, **kwargs):
16 app = Flask(__name__)
17 app.secret_key = "whatever"
18 blueprint = make_zoho_blueprint(*args, **kwargs)
19 app.register_blueprint(blueprint)
20 return app
21
22 return _make_app
23
24
25 def test_blueprint_factory():
26 zoho_bp = make_zoho_blueprint(client_id="foobar", client_secret="supersecret")
27 assert isinstance(zoho_bp, OAuth2ConsumerBlueprint)
28 assert zoho_bp.session.client_id == "foobar"
29 assert zoho_bp.client_secret == "supersecret"
30 assert zoho_bp.token_url == "https://accounts.zoho.com/oauth/v2/token"
31 assert zoho_bp.authorization_url == "https://accounts.zoho.com/oauth/v2/auth"
32
33
34 @responses.activate
35 def test_load_from_config(make_app):
36 app = make_app()
37 app.config["ZOHO_OAUTH_CLIENT_ID"] = "foo"
38 app.config["ZOHO_OAUTH_CLIENT_SECRET"] = "bar"
39
40 resp = app.test_client().get("/zoho")
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://google.com")
49 # set up two apps with two different set of auth tokens
50 app1 = make_app(
51 "foo1",
52 "bar1",
53 redirect_to="url1",
54 storage=MemoryStorage({"access_token": "app1"}),
55 )
56 app2 = make_app(
57 "foo2",
58 "bar2",
59 redirect_to="url2",
60 storage=MemoryStorage({"access_token": "app2"}),
61 )
62 # outside of a request context, referencing functions on the `zoho` object
63 # will raise an exception
64 with pytest.raises(RuntimeError):
65 zoho.get("https://google.com")
66 # inside of a request context, `zoho` should be a proxy to the correct
67 # blueprint session
68 with app1.test_request_context("/"):
69 app1.preprocess_request()
70 zoho.get("https://google.com")
71 request = responses.calls[0].request
72 assert request.headers["Authorization"] == "Zoho-oauthtoken app1"
73 with app2.test_request_context("/"):
74 app2.preprocess_request()
75 zoho.get("https://google.com")
76 request = responses.calls[1].request
77 assert request.headers["Authorization"] == "Zoho-oauthtoken app2"