| 1 |
from unittest import mock |
| 2 |
from urllib.parse import quote_plus |
| 3 |
|
| 4 |
import flask |
| 5 |
import pytest |
| 6 |
import responses |
| 7 |
from oauthlib.oauth1.rfc5849.utils import parse_authorization_header |
| 8 |
from werkzeug.middleware.proxy_fix import ProxyFix |
| 9 |
|
| 10 |
from flask_dance.consumer import ( |
| 11 |
OAuth1ConsumerBlueprint, |
| 12 |
oauth_authorized, |
| 13 |
oauth_before_login, |
| 14 |
oauth_error, |
| 15 |
) |
| 16 |
from flask_dance.consumer.requests import OAuth1Session |
| 17 |
from flask_dance.consumer.storage import MemoryStorage |
| 18 |
|
| 19 |
try: |
| 20 |
import blinker |
| 21 |
except ImportError: |
| 22 |
blinker = None |
| 23 |
requires_blinker = pytest.mark.skipif(not blinker, reason="requires blinker") |
| 24 |
|
| 25 |
|
| 26 |
def make_app(login_url=None): |
| 27 |
blueprint = OAuth1ConsumerBlueprint( |
| 28 |
"test-service", |
| 29 |
__name__, |
| 30 |
client_key="client_key", |
| 31 |
client_secret="client_secret", |
| 32 |
base_url="https://example.com", |
| 33 |
request_token_url="https://example.com/oauth/request_token", |
| 34 |
access_token_url="https://example.com/oauth/access_token", |
| 35 |
authorization_url="https://example.com/oauth/authorize", |
| 36 |
redirect_to="index", |
| 37 |
login_url=login_url, |
| 38 |
) |
| 39 |
app = flask.Flask(__name__) |
| 40 |
app.secret_key = "secret" |
| 41 |
app.register_blueprint(blueprint, url_prefix="/login") |
| 42 |
app.config["SERVER_NAME"] = "a.b.c" |
| 43 |
|
| 44 |
@app.route("/") |
| 45 |
def index(): |
| 46 |
return "index" |
| 47 |
|
| 48 |
return app, blueprint |
| 49 |
|
| 50 |
|
| 51 |
def test_generate_login_url(): |
| 52 |
app, _ = make_app() |
| 53 |
with app.test_request_context("/"): |
| 54 |
login_url = flask.url_for("test-service.login") |
| 55 |
assert login_url == "/login/test-service" |
| 56 |
|
| 57 |
|
| 58 |
def test_override_login_url(): |
| 59 |
app, _ = make_app(login_url="/crazy/custom/url") |
| 60 |
with app.test_request_context("/"): |
| 61 |
login_url = flask.url_for("test-service.login") |
| 62 |
assert login_url == "/login/crazy/custom/url" |
| 63 |
|
| 64 |
|
| 65 |
@responses.activate |
| 66 |
def test_login_url(): |
| 67 |
responses.add( |
| 68 |
responses.POST, |
| 69 |
"https://example.com/oauth/request_token", |
| 70 |
body="oauth_token=foobar&oauth_token_secret=bazqux", |
| 71 |
) |
| 72 |
app, _ = make_app() |
| 73 |
client = app.test_client() |
| 74 |
resp = client.get( |
| 75 |
"/login/test-service", base_url="https://a.b.c", follow_redirects=False |
| 76 |
) |
| 77 |
# check that we obtained a request token |
| 78 |
assert len(responses.calls) == 1 |
| 79 |
assert "Authorization" in responses.calls[0].request.headers |
| 80 |
auth_header = dict( |
| 81 |
parse_authorization_header( |
| 82 |
responses.calls[0].request.headers["Authorization"].decode("utf-8") |
| 83 |
) |
| 84 |
) |
| 85 |
assert auth_header["oauth_consumer_key"] == "client_key" |
| 86 |
assert "oauth_signature" in auth_header |
| 87 |
assert auth_header["oauth_callback"] == quote_plus( |
| 88 |
"https://a.b.c/login/test-service/authorized" |
| 89 |
) |
| 90 |
# check that we redirected the client |
| 91 |
assert resp.status_code == 302 |
| 92 |
assert ( |
| 93 |
resp.headers["Location"] |
| 94 |
== "https://example.com/oauth/authorize?oauth_token=foobar" |
| 95 |
) |
| 96 |
|
| 97 |
|
| 98 |
@responses.activate |
| 99 |
def test_login_url_forwarded_proto(): |
| 100 |
responses.add( |
| 101 |
responses.POST, |
| 102 |
"https://example.com/oauth/request_token", |
| 103 |
body="oauth_token=foobar&oauth_token_secret=bazqux", |
| 104 |
) |
| 105 |
app, _ = make_app() |
| 106 |
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1) |
| 107 |
with app.test_client() as client: |
| 108 |
resp = client.get( |
| 109 |
"/login/test-service", |
| 110 |
base_url="http://a.b.c", |
| 111 |
headers={"X-Forwarded-Proto": "https"}, |
| 112 |
follow_redirects=False, |
| 113 |
) |
| 114 |
auth_header = dict( |
| 115 |
parse_authorization_header( |
| 116 |
responses.calls[0].request.headers["Authorization"].decode("utf-8") |
| 117 |
) |
| 118 |
) |
| 119 |
# this should be https |
| 120 |
assert auth_header["oauth_callback"] == quote_plus( |
| 121 |
"https://a.b.c/login/test-service/authorized" |
| 122 |
) |
| 123 |
|
| 124 |
|
| 125 |
@responses.activate |
| 126 |
def test_authorized_url(): |
| 127 |
responses.add( |
| 128 |
responses.POST, |
| 129 |
"https://example.com/oauth/access_token", |
| 130 |
body="oauth_token=xxx&oauth_token_secret=yyy", |
| 131 |
) |
| 132 |
app, _ = make_app() |
| 133 |
with app.test_client() as client: |
| 134 |
resp = client.get( |
| 135 |
"/login/test-service/authorized?oauth_token=foobar&oauth_verifier=xyz", |
| 136 |
base_url="https://a.b.c", |
| 137 |
) |
| 138 |
# check that we redirected the client |
| 139 |
assert resp.status_code == 302 |
| 140 |
assert resp.headers["Location"] in ("https://a.b.c/", "/") |
| 141 |
# check that we obtained an access token |
| 142 |
assert len(responses.calls) == 1 |
| 143 |
assert "Authorization" in responses.calls[0].request.headers |
| 144 |
auth_header = dict( |
| 145 |
parse_authorization_header( |
| 146 |
responses.calls[0].request.headers["Authorization"].decode("utf-8") |
| 147 |
) |
| 148 |
) |
| 149 |
assert auth_header["oauth_consumer_key"] == "client_key" |
| 150 |
assert auth_header["oauth_token"] == "foobar" |
| 151 |
assert auth_header["oauth_verifier"] == "xyz" |
| 152 |
# check that we stored the access token and secret in the session |
| 153 |
assert flask.session["test-service_oauth_token"] == { |
| 154 |
"oauth_token": "xxx", |
| 155 |
"oauth_token_secret": "yyy", |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
@responses.activate |
| 160 |
def test_redirect_url(): |
| 161 |
responses.add( |
| 162 |
responses.POST, |
| 163 |
"https://example.com/oauth/access_token", |
| 164 |
body="oauth_token=xxx&oauth_token_secret=yyy", |
| 165 |
) |
| 166 |
blueprint = OAuth1ConsumerBlueprint( |
| 167 |
"test-service", |
| 168 |
__name__, |
| 169 |
client_key="client_key", |
| 170 |
client_secret="client_secret", |
| 171 |
base_url="https://example.com", |
| 172 |
request_token_url="https://example.com/oauth/request_token", |
| 173 |
access_token_url="https://example.com/oauth/access_token", |
| 174 |
authorization_url="https://example.com/oauth/authorize", |
| 175 |
redirect_url="http://mysite.cool/whoa?query=basketball", |
| 176 |
) |
| 177 |
app = flask.Flask(__name__) |
| 178 |
app.secret_key = "secret" |
| 179 |
app.register_blueprint(blueprint, url_prefix="/login") |
| 180 |
|
| 181 |
with app.test_client() as client: |
| 182 |
resp = client.get( |
| 183 |
"/login/test-service/authorized?oauth_token=foobar&oauth_verifier=xyz", |
| 184 |
base_url="https://a.b.c", |
| 185 |
) |
| 186 |
# check that we redirected the client |
| 187 |
assert resp.status_code == 302 |
| 188 |
assert resp.headers["Location"] == "http://mysite.cool/whoa?query=basketball" |
| 189 |
|
| 190 |
|
| 191 |
@responses.activate |
| 192 |
def test_redirect_to(): |
| 193 |
responses.add( |
| 194 |
responses.POST, |
| 195 |
"https://example.com/oauth/access_token", |
| 196 |
body="oauth_token=xxx&oauth_token_secret=yyy", |
| 197 |
) |
| 198 |
blueprint = OAuth1ConsumerBlueprint( |
| 199 |
"test-service", |
| 200 |
__name__, |
| 201 |
client_key="client_key", |
| 202 |
client_secret="client_secret", |
| 203 |
base_url="https://example.com", |
| 204 |
request_token_url="https://example.com/oauth/request_token", |
| 205 |
access_token_url="https://example.com/oauth/access_token", |
| 206 |
authorization_url="https://example.com/oauth/authorize", |
| 207 |
redirect_to="my_view", |
| 208 |
) |
| 209 |
app = flask.Flask(__name__) |
| 210 |
app.secret_key = "secret" |
| 211 |
app.register_blueprint(blueprint, url_prefix="/login") |
| 212 |
|
| 213 |
@app.route("/blargl") |
| 214 |
def my_view(): |
| 215 |
return "check out my url" |
| 216 |
|
| 217 |
with app.test_client() as client: |
| 218 |
resp = client.get( |
| 219 |
"/login/test-service/authorized?oauth_token=foobar&oauth_verifier=xyz", |
| 220 |
base_url="https://a.b.c", |
| 221 |
) |
| 222 |
# check that we redirected the client |
| 223 |
assert resp.status_code == 302 |
| 224 |
assert resp.headers["Location"] in ("https://a.b.c/blargl", "/blargl") |
| 225 |
|
| 226 |
|
| 227 |
@responses.activate |
| 228 |
def test_redirect_fallback(): |
| 229 |
responses.add( |
| 230 |
responses.POST, |
| 231 |
"https://example.com/oauth/access_token", |
| 232 |
body="oauth_token=xxx&oauth_token_secret=yyy", |
| 233 |
) |
| 234 |
blueprint = OAuth1ConsumerBlueprint( |
| 235 |
"test-service", |
| 236 |
__name__, |
| 237 |
client_key="client_key", |
| 238 |
client_secret="client_secret", |
| 239 |
base_url="https://example.com", |
| 240 |
request_token_url="https://example.com/oauth/request_token", |
| 241 |
access_token_url="https://example.com/oauth/access_token", |
| 242 |
authorization_url="https://example.com/oauth/authorize", |
| 243 |
) |
| 244 |
app = flask.Flask(__name__) |
| 245 |
app.secret_key = "secret" |
| 246 |
app.register_blueprint(blueprint, url_prefix="/login") |
| 247 |
|
| 248 |
with app.test_client() as client: |
| 249 |
resp = client.get( |
| 250 |
"/login/test-service/authorized?oauth_token=foobar&oauth_verifier=xyz", |
| 251 |
base_url="https://a.b.c", |
| 252 |
) |
| 253 |
# check that we redirected the client |
| 254 |
assert resp.status_code == 302 |
| 255 |
assert resp.headers["Location"] in ("https://a.b.c/", "/") |
| 256 |
|
| 257 |
|
| 258 |
def test_authorization_required_decorator_allowed(): |
| 259 |
app, blueprint = make_app() |
| 260 |
|
| 261 |
@app.route("/restricted") |
| 262 |
@blueprint.session.authorization_required |
| 263 |
def restricted_view(): |
| 264 |
return "allowed" |
| 265 |
|
| 266 |
blueprint.storage = MemoryStorage( |
| 267 |
{"oauth_token": "test1", "oauth_token_secret": "test2"} |
| 268 |
) |
| 269 |
|
| 270 |
with app.test_client() as client: |
| 271 |
resp = client.get("/restricted", base_url="https://a.b.c") |
| 272 |
assert resp.status_code == 200 |
| 273 |
text = resp.get_data(as_text=True) |
| 274 |
assert text == "allowed" |
| 275 |
|
| 276 |
|
| 277 |
def test_authorization_required_decorator_redirect(): |
| 278 |
app, blueprint = make_app() |
| 279 |
|
| 280 |
@app.route("/restricted") |
| 281 |
@blueprint.session.authorization_required |
| 282 |
def restricted_view(): |
| 283 |
return "allowed" |
| 284 |
|
| 285 |
with app.test_client() as client: |
| 286 |
resp = client.get("/restricted", base_url="https://a.b.c") |
| 287 |
# check that we redirected the client |
| 288 |
assert resp.status_code == 302 |
| 289 |
assert resp.headers["Location"] in ( |
| 290 |
"https://a.b.c/login/test-service", |
| 291 |
"/login/test-service", |
| 292 |
) |
| 293 |
|
| 294 |
|
| 295 |
@requires_blinker |
| 296 |
def test_signal_oauth_authorized(request): |
| 297 |
app, bp = make_app() |
| 298 |
fake_token = {"access_token": "test-token"} |
| 299 |
bp.session.fetch_access_token = mock.Mock(return_value=fake_token) |
| 300 |
|
| 301 |
calls = [] |
| 302 |
|
| 303 |
def callback(*args, **kwargs): |
| 304 |
calls.append((args, kwargs)) |
| 305 |
|
| 306 |
oauth_authorized.connect(callback) |
| 307 |
request.addfinalizer(lambda: oauth_authorized.disconnect(callback)) |
| 308 |
|
| 309 |
with app.test_client() as client: |
| 310 |
resp = client.get( |
| 311 |
"/login/test-service/authorized?oauth_token=foobar&oauth_verifier=xyz" |
| 312 |
) |
| 313 |
# check that we stored the token |
| 314 |
assert flask.session["test-service_oauth_token"] == fake_token |
| 315 |
|
| 316 |
assert len(calls), 1 |
| 317 |
assert calls[0][0] == (bp,) |
| 318 |
assert calls[0][1] == {"token": fake_token} |
| 319 |
|
| 320 |
|
| 321 |
@requires_blinker |
| 322 |
def test_signal_oauth_authorized_abort(request): |
| 323 |
app, bp = make_app() |
| 324 |
bp.session.fetch_access_token = mock.Mock(return_value="test-token") |
| 325 |
|
| 326 |
calls = [] |
| 327 |
|
| 328 |
def callback(*args, **kwargs): |
| 329 |
calls.append((args, kwargs)) |
| 330 |
return False |
| 331 |
|
| 332 |
oauth_authorized.connect(callback) |
| 333 |
request.addfinalizer(lambda: oauth_authorized.disconnect(callback)) |
| 334 |
|
| 335 |
with app.test_client() as client: |
| 336 |
resp = client.get( |
| 337 |
"/login/test-service/authorized?oauth_token=foobar&oauth_verifier=xyz" |
| 338 |
) |
| 339 |
# check that we did NOT store the token |
| 340 |
assert "test-token_oauth_token" not in flask.session |
| 341 |
|
| 342 |
# the callback should still have been called |
| 343 |
assert len(calls) == 1 |
| 344 |
|
| 345 |
|
| 346 |
@requires_blinker |
| 347 |
def test_signal_oauth_before_login(request): |
| 348 |
app, bp = make_app() |
| 349 |
bp.session.fetch_request_token = mock.Mock(return_value="test-token") |
| 350 |
|
| 351 |
def callback(*args, **kwargs): |
| 352 |
del flask.session["user_id"] |
| 353 |
return False |
| 354 |
|
| 355 |
oauth_before_login.connect(callback) |
| 356 |
request.addfinalizer(lambda: oauth_before_login.disconnect(callback)) |
| 357 |
with app.test_request_context(): |
| 358 |
with app.test_client() as client: |
| 359 |
flask.session["user_id"] = 1 |
| 360 |
assert flask.session["user_id"] == 1 |
| 361 |
client.get("/login/test-service") |
| 362 |
assert "user_id" not in flask.session |
| 363 |
|
| 364 |
|
| 365 |
@requires_blinker |
| 366 |
def test_signal_oauth_authorized_response(request): |
| 367 |
app, bp = make_app() |
| 368 |
bp.session.fetch_access_token = mock.Mock(return_value="test-token") |
| 369 |
|
| 370 |
calls = [] |
| 371 |
|
| 372 |
def callback(*args, **kwargs): |
| 373 |
calls.append((args, kwargs)) |
| 374 |
return flask.redirect("/url") |
| 375 |
|
| 376 |
oauth_authorized.connect(callback) |
| 377 |
request.addfinalizer(lambda: oauth_authorized.disconnect(callback)) |
| 378 |
|
| 379 |
with app.test_client() as client: |
| 380 |
resp = client.get( |
| 381 |
"/login/test-service/authorized?oauth_token=foobar&oauth_verifier=xyz" |
| 382 |
) |
| 383 |
assert resp.status_code == 302 |
| 384 |
assert resp.headers["Location"] in ("/url", "http://localhost/url") |
| 385 |
# check that we did NOT store the token |
| 386 |
assert "test-token_oauth_token" not in flask.session |
| 387 |
|
| 388 |
# the callback should still have been called |
| 389 |
assert len(calls) == 1 |
| 390 |
|
| 391 |
|
| 392 |
@requires_blinker |
| 393 |
def test_signal_sender_oauth_authorized(request): |
| 394 |
app, bp = make_app() |
| 395 |
bp2 = OAuth1ConsumerBlueprint( |
| 396 |
"test2", |
| 397 |
__name__, |
| 398 |
client_key="client_key", |
| 399 |
client_secret="client_secret", |
| 400 |
base_url="https://example.com", |
| 401 |
request_token_url="https://example.com/oauth/request_token", |
| 402 |
access_token_url="https://example.com/oauth/access_token", |
| 403 |
authorization_url="https://example.com/oauth/authorize", |
| 404 |
redirect_to="index", |
| 405 |
) |
| 406 |
app.register_blueprint(bp2, url_prefix="/login") |
| 407 |
|
| 408 |
calls = [] |
| 409 |
|
| 410 |
def callback(*args, **kwargs): |
| 411 |
calls.append((args, kwargs)) |
| 412 |
|
| 413 |
oauth_authorized.connect(callback, sender=bp) |
| 414 |
request.addfinalizer(lambda: oauth_authorized.disconnect(callback, sender=bp)) |
| 415 |
|
| 416 |
with app.test_client() as client: |
| 417 |
bp.session.fetch_access_token = mock.Mock(return_value="test-token") |
| 418 |
bp2.session.fetch_access_token = mock.Mock(return_value="test2-token") |
| 419 |
resp = client.get( |
| 420 |
"/login/test2/authorized?oauth_token=foobar&oauth_verifier=xyz" |
| 421 |
) |
| 422 |
|
| 423 |
assert len(calls) == 0 |
| 424 |
|
| 425 |
with app.test_client() as client: |
| 426 |
bp.session.fetch_access_token = mock.Mock(return_value="test-token") |
| 427 |
bp2.session.fetch_access_token = mock.Mock(return_value="test2-token") |
| 428 |
resp = client.get( |
| 429 |
"/login/test-service/authorized?oauth_token=foobar&oauth_verifier=xyz" |
| 430 |
) |
| 431 |
|
| 432 |
assert len(calls) == 1 |
| 433 |
assert calls[0][0] == (bp,) |
| 434 |
assert calls[0][1] == {"token": "test-token"} |
| 435 |
|
| 436 |
with app.test_client() as client: |
| 437 |
bp.session.fetch_access_token = mock.Mock(return_value="test-token") |
| 438 |
bp2.session.fetch_access_token = mock.Mock(return_value="test2-token") |
| 439 |
resp = client.get( |
| 440 |
"/login/test2/authorized?oauth_token=foobar&oauth_verifier=xyz" |
| 441 |
) |
| 442 |
|
| 443 |
assert len(calls) == 1 # unchanged |
| 444 |
|
| 445 |
|
| 446 |
@requires_blinker |
| 447 |
@responses.activate |
| 448 |
def test_signal_oauth_error_login(request): |
| 449 |
responses.add( |
| 450 |
responses.POST, |
| 451 |
"https://example.com/oauth/request_token", |
| 452 |
body="oauth_problem=nonce_used", |
| 453 |
status=401, |
| 454 |
) |
| 455 |
app, bp = make_app() |
| 456 |
|
| 457 |
calls = [] |
| 458 |
|
| 459 |
def callback(*args, **kwargs): |
| 460 |
calls.append((args, kwargs)) |
| 461 |
|
| 462 |
oauth_error.connect(callback) |
| 463 |
request.addfinalizer(lambda: oauth_error.disconnect(callback)) |
| 464 |
|
| 465 |
with app.test_client() as client: |
| 466 |
resp = client.get("/login/test-service", base_url="https://a.b.c") |
| 467 |
|
| 468 |
assert len(calls) == 1 |
| 469 |
assert calls[0][0] == (bp,) |
| 470 |
assert ( |
| 471 |
calls[0][1]["message"] |
| 472 |
== "Token request failed with code 401, response was 'oauth_problem=nonce_used'." |
| 473 |
) |
| 474 |
assert resp.status_code == 302 |
| 475 |
location = resp.headers["Location"] |
| 476 |
assert location in ("/", "https://a.b.c/") |
| 477 |
|
| 478 |
|
| 479 |
@requires_blinker |
| 480 |
@responses.activate |
| 481 |
def test_signal_oauth_error_authorized(request): |
| 482 |
responses.add( |
| 483 |
responses.POST, |
| 484 |
"https://example.com/oauth/access_token", |
| 485 |
body="Invalid request token.", |
| 486 |
status=401, |
| 487 |
) |
| 488 |
app, bp = make_app() |
| 489 |
|
| 490 |
calls = [] |
| 491 |
|
| 492 |
def callback(*args, **kwargs): |
| 493 |
calls.append((args, kwargs)) |
| 494 |
|
| 495 |
oauth_error.connect(callback) |
| 496 |
request.addfinalizer(lambda: oauth_error.disconnect(callback)) |
| 497 |
|
| 498 |
with app.test_client() as client: |
| 499 |
resp = client.get( |
| 500 |
"/login/test-service/authorized?" |
| 501 |
"oauth_token=faketoken&" |
| 502 |
"oauth_token_secret=fakesecret&" |
| 503 |
"oauth_verifier=fakeverifier", |
| 504 |
base_url="https://a.b.c", |
| 505 |
) |
| 506 |
|
| 507 |
assert len(calls) == 1 |
| 508 |
assert calls[0][0] == (bp,) |
| 509 |
assert ( |
| 510 |
calls[0][1]["message"] |
| 511 |
== "Token request failed with code 401, response was 'Invalid request token.'." |
| 512 |
) |
| 513 |
assert resp.status_code == 302 |
| 514 |
|
| 515 |
|
| 516 |
@requires_blinker |
| 517 |
def test_signal_oauth_notoken_authorized(request): |
| 518 |
app, bp = make_app() |
| 519 |
|
| 520 |
calls = [] |
| 521 |
|
| 522 |
def callback(*args, **kwargs): |
| 523 |
calls.append((args, kwargs)) |
| 524 |
|
| 525 |
oauth_error.connect(callback) |
| 526 |
request.addfinalizer(lambda: oauth_error.disconnect(callback)) |
| 527 |
|
| 528 |
with app.test_client() as client: |
| 529 |
resp = client.get( |
| 530 |
"/login/test-service/authorized?" "denied=faketoken", |
| 531 |
base_url="https://a.b.c", |
| 532 |
) |
| 533 |
|
| 534 |
assert len(calls) == 1 |
| 535 |
assert calls[0][0] == (bp,) |
| 536 |
assert "Response does not contain a token" in calls[0][1]["message"] |
| 537 |
assert calls[0][1]["response"] == {"denied": "faketoken"} |
| 538 |
assert resp.status_code == 302 |
| 539 |
location = resp.headers["Location"] |
| 540 |
assert location in ("/", "https://a.b.c/") |
| 541 |
|
| 542 |
|
| 543 |
class CustomOAuth1Session(OAuth1Session): |
| 544 |
my_attr = "foobar" |
| 545 |
|
| 546 |
|
| 547 |
def test_custom_session_class(): |
| 548 |
bp = OAuth1ConsumerBlueprint( |
| 549 |
"test", |
| 550 |
__name__, |
| 551 |
client_key="client_key", |
| 552 |
client_secret="client_secret", |
| 553 |
base_url="https://example.com", |
| 554 |
request_token_url="https://example.com/oauth/request_token", |
| 555 |
access_token_url="https://example.com/oauth/access_token", |
| 556 |
authorization_url="https://example.com/oauth/authorize", |
| 557 |
redirect_to="index", |
| 558 |
session_class=CustomOAuth1Session, |
| 559 |
) |
| 560 |
assert isinstance(bp.session, CustomOAuth1Session) |
| 561 |
assert bp.session.my_attr == "foobar" |
| 562 |
|
| 563 |
|
| 564 |
def test_rule_kwargs(): |
| 565 |
blueprint = OAuth1ConsumerBlueprint( |
| 566 |
"test-service", |
| 567 |
__name__, |
| 568 |
client_key="client_key", |
| 569 |
client_secret="client_secret", |
| 570 |
base_url="https://example.com", |
| 571 |
request_token_url="https://example.com/oauth/request_token", |
| 572 |
access_token_url="https://example.com/oauth/access_token", |
| 573 |
authorization_url="https://example.com/oauth/authorize", |
| 574 |
redirect_to="my_view", |
| 575 |
rule_kwargs={"host": "example2.com"}, |
| 576 |
) |
| 577 |
app = flask.Flask(__name__) |
| 578 |
app.secret_key = "secret" |
| 579 |
app.register_blueprint(blueprint, url_prefix="/login") |
| 580 |
rules = [ |
| 581 |
rule |
| 582 |
for rule in app.url_map.iter_rules() |
| 583 |
if rule.endpoint.startswith("test-service.") |
| 584 |
] |
| 585 |
assert all(rule.host == "example2.com" for rule in rules) |
| 586 |
assert len(rules) == 2 |