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