| 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.azure import azure, make_azure_blueprint |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture |
| 12 |
def make_app(): |
| 13 |
"A callable to create a Flask app with the Azure provider" |
| 14 |
|
| 15 |
def _make_app(*args, **kwargs): |
| 16 |
app = Flask(__name__) |
| 17 |
app.secret_key = "whatever" |
| 18 |
blueprint = make_azure_blueprint(*args, **kwargs) |
| 19 |
app.register_blueprint(blueprint) |
| 20 |
return app |
| 21 |
|
| 22 |
return _make_app |
| 23 |
|
| 24 |
|
| 25 |
def test_blueprint_factory(): |
| 26 |
azure_bp = make_azure_blueprint( |
| 27 |
client_id="foo", client_secret="bar", scope="user.read", redirect_to="index" |
| 28 |
) |
| 29 |
assert isinstance(azure_bp, OAuth2ConsumerBlueprint) |
| 30 |
assert azure_bp.session.scope == "user.read" |
| 31 |
assert azure_bp.session.base_url == "https://graph.microsoft.com" |
| 32 |
assert azure_bp.session.client_id == "foo" |
| 33 |
assert azure_bp.client_secret == "bar" |
| 34 |
assert ( |
| 35 |
azure_bp.authorization_url |
| 36 |
== "https://login.microsoftonline.com/common/oauth2/v2.0/authorize" |
| 37 |
) |
| 38 |
assert ( |
| 39 |
azure_bp.token_url |
| 40 |
== "https://login.microsoftonline.com/common/oauth2/v2.0/token" |
| 41 |
) |
| 42 |
assert azure_bp.auto_refresh_url is None |
| 43 |
|
| 44 |
|
| 45 |
def test_blueprint_factory_offline(): |
| 46 |
azure_bp = make_azure_blueprint( |
| 47 |
client_id="foo", |
| 48 |
client_secret="bar", |
| 49 |
scope=["user.read", "offline_access"], |
| 50 |
redirect_to="index", |
| 51 |
) |
| 52 |
assert isinstance(azure_bp, OAuth2ConsumerBlueprint) |
| 53 |
assert azure_bp.session.scope == ["user.read", "offline_access"] |
| 54 |
assert azure_bp.session.base_url == "https://graph.microsoft.com" |
| 55 |
assert azure_bp.session.client_id == "foo" |
| 56 |
assert azure_bp.client_secret == "bar" |
| 57 |
assert ( |
| 58 |
azure_bp.authorization_url |
| 59 |
== "https://login.microsoftonline.com/common/oauth2/v2.0/authorize" |
| 60 |
) |
| 61 |
assert ( |
| 62 |
azure_bp.token_url |
| 63 |
== "https://login.microsoftonline.com/common/oauth2/v2.0/token" |
| 64 |
) |
| 65 |
assert azure_bp.auto_refresh_url is azure_bp.token_url |
| 66 |
|
| 67 |
|
| 68 |
def test_blueprint_factory_with_domain_hint(): |
| 69 |
azure_domain_bp = make_azure_blueprint( |
| 70 |
client_id="foo", |
| 71 |
client_secret="bar", |
| 72 |
scope="user.read", |
| 73 |
redirect_to="index", |
| 74 |
domain_hint="Sample Hint", |
| 75 |
) |
| 76 |
assert isinstance(azure_domain_bp, OAuth2ConsumerBlueprint) |
| 77 |
assert azure_domain_bp.session.scope == "user.read" |
| 78 |
assert azure_domain_bp.session.base_url == "https://graph.microsoft.com" |
| 79 |
assert azure_domain_bp.session.client_id == "foo" |
| 80 |
assert azure_domain_bp.client_secret == "bar" |
| 81 |
assert azure_domain_bp.authorization_url_params["domain_hint"] == "Sample Hint" |
| 82 |
assert ( |
| 83 |
azure_domain_bp.authorization_url |
| 84 |
== "https://login.microsoftonline.com/common/oauth2/v2.0/authorize" |
| 85 |
) |
| 86 |
assert ( |
| 87 |
azure_domain_bp.token_url |
| 88 |
== "https://login.microsoftonline.com/common/oauth2/v2.0/token" |
| 89 |
) |
| 90 |
|
| 91 |
|
| 92 |
def test_blueprint_factory_with_login_hint(): |
| 93 |
azure_domain_bp = make_azure_blueprint( |
| 94 |
client_id="foo", |
| 95 |
client_secret="bar", |
| 96 |
scope="user.read", |
| 97 |
redirect_to="index", |
| 98 |
login_hint="Sample Login Hint", |
| 99 |
) |
| 100 |
assert isinstance(azure_domain_bp, OAuth2ConsumerBlueprint) |
| 101 |
assert azure_domain_bp.session.scope == "user.read" |
| 102 |
assert azure_domain_bp.session.base_url == "https://graph.microsoft.com" |
| 103 |
assert azure_domain_bp.session.client_id == "foo" |
| 104 |
assert azure_domain_bp.client_secret == "bar" |
| 105 |
assert azure_domain_bp.authorization_url_params["login_hint"] == "Sample Login Hint" |
| 106 |
assert ( |
| 107 |
azure_domain_bp.authorization_url |
| 108 |
== "https://login.microsoftonline.com/common/oauth2/v2.0/authorize" |
| 109 |
) |
| 110 |
assert ( |
| 111 |
azure_domain_bp.token_url |
| 112 |
== "https://login.microsoftonline.com/common/oauth2/v2.0/token" |
| 113 |
) |
| 114 |
|
| 115 |
|
| 116 |
def test_blueprint_factory_with_prompt(): |
| 117 |
azure_domain_bp = make_azure_blueprint( |
| 118 |
client_id="foo", |
| 119 |
client_secret="bar", |
| 120 |
scope="user.read", |
| 121 |
redirect_to="index", |
| 122 |
prompt="select_account", |
| 123 |
) |
| 124 |
assert isinstance(azure_domain_bp, OAuth2ConsumerBlueprint) |
| 125 |
assert azure_domain_bp.session.scope == "user.read" |
| 126 |
assert azure_domain_bp.session.base_url == "https://graph.microsoft.com" |
| 127 |
assert azure_domain_bp.session.client_id == "foo" |
| 128 |
assert azure_domain_bp.client_secret == "bar" |
| 129 |
assert azure_domain_bp.authorization_url_params["prompt"] == "select_account" |
| 130 |
assert ( |
| 131 |
azure_domain_bp.authorization_url |
| 132 |
== "https://login.microsoftonline.com/common/oauth2/v2.0/authorize" |
| 133 |
) |
| 134 |
assert ( |
| 135 |
azure_domain_bp.token_url |
| 136 |
== "https://login.microsoftonline.com/common/oauth2/v2.0/token" |
| 137 |
) |
| 138 |
|
| 139 |
|
| 140 |
def test_blueprint_factory_with_organization_tenant(): |
| 141 |
azure_orgs_bp = make_azure_blueprint( |
| 142 |
client_id="foo", |
| 143 |
client_secret="bar", |
| 144 |
scope="user.read", |
| 145 |
redirect_to="index", |
| 146 |
tenant="organizations", |
| 147 |
) |
| 148 |
assert isinstance(azure_orgs_bp, OAuth2ConsumerBlueprint) |
| 149 |
assert azure_orgs_bp.session.scope == "user.read" |
| 150 |
assert azure_orgs_bp.session.base_url == "https://graph.microsoft.com" |
| 151 |
assert azure_orgs_bp.session.client_id == "foo" |
| 152 |
assert azure_orgs_bp.client_secret == "bar" |
| 153 |
assert ( |
| 154 |
azure_orgs_bp.authorization_url |
| 155 |
== "https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize" |
| 156 |
) |
| 157 |
assert ( |
| 158 |
azure_orgs_bp.token_url |
| 159 |
== "https://login.microsoftonline.com/organizations/oauth2/v2.0/token" |
| 160 |
) |
| 161 |
|
| 162 |
|
| 163 |
def test_load_from_config(make_app): |
| 164 |
app = make_app(redirect_to="index") |
| 165 |
app.config["AZURE_OAUTH_CLIENT_ID"] = "foo" |
| 166 |
app.config["AZURE_OAUTH_CLIENT_SECRET"] = "bar" |
| 167 |
|
| 168 |
resp = app.test_client().get("/azure") |
| 169 |
url = resp.headers["Location"] |
| 170 |
client_id = URLObject(url).query.dict.get("client_id") |
| 171 |
assert client_id == "foo" |
| 172 |
|
| 173 |
|
| 174 |
@responses.activate |
| 175 |
def test_context_local(make_app): |
| 176 |
responses.add(responses.GET, "https://google.com") |
| 177 |
|
| 178 |
# set up two apps with two different set of auth tokens |
| 179 |
app1 = make_app( |
| 180 |
"foo1", |
| 181 |
"bar1", |
| 182 |
redirect_to="url1", |
| 183 |
storage=MemoryStorage({"access_token": "app1"}), |
| 184 |
) |
| 185 |
|
| 186 |
app2 = make_app( |
| 187 |
"foo2", |
| 188 |
"bar2", |
| 189 |
redirect_to="url2", |
| 190 |
storage=MemoryStorage({"access_token": "app2"}), |
| 191 |
) |
| 192 |
|
| 193 |
# outside of a request context, referencing functions on the `azure` object |
| 194 |
# will raise an exception |
| 195 |
with pytest.raises(RuntimeError): |
| 196 |
azure.get("https://google.com") |
| 197 |
|
| 198 |
# inside of a request context, `azure` should be a proxy to the correct |
| 199 |
# blueprint session |
| 200 |
with app1.test_request_context("/"): |
| 201 |
app1.preprocess_request() |
| 202 |
azure.get("https://google.com") |
| 203 |
request = responses.calls[0].request |
| 204 |
assert request.headers["Authorization"] == "Bearer app1" |
| 205 |
|
| 206 |
with app2.test_request_context("/"): |
| 207 |
app2.preprocess_request() |
| 208 |
azure.get("https://google.com") |
| 209 |
request = responses.calls[1].request |
| 210 |
assert request.headers["Authorization"] == "Bearer app2" |