| 1 |
import os |
| 2 |
|
| 3 |
import flask |
| 4 |
import pytest |
| 5 |
|
| 6 |
from flask_dance.consumer.storage import MemoryStorage |
| 7 |
from flask_dance.contrib.github import github, make_github_blueprint |
| 8 |
|
| 9 |
betamax = pytest.importorskip("betamax") |
| 10 |
GITHUB_ACCESS_TOKEN = os.environ.get("GITHUB_OAUTH_ACCESS_TOKEN", "fake-token") |
| 11 |
current_dir = os.path.dirname(__file__) |
| 12 |
|
| 13 |
|
| 14 |
with betamax.Betamax.configure() as config: |
| 15 |
config.cassette_library_dir = os.path.join(current_dir, "cassettes") |
| 16 |
config.define_cassette_placeholder("<AUTH_TOKEN>", GITHUB_ACCESS_TOKEN) |
| 17 |
|
| 18 |
pytestmark = pytest.mark.install_required |
| 19 |
|
| 20 |
|
| 21 |
@pytest.fixture |
| 22 |
def app(): |
| 23 |
_app = flask.Flask(__name__) |
| 24 |
_app.secret_key = "secret" |
| 25 |
github_bp = make_github_blueprint( |
| 26 |
storage=MemoryStorage({"access_token": GITHUB_ACCESS_TOKEN}) |
| 27 |
) |
| 28 |
_app.register_blueprint(github_bp, url_prefix="/login") |
| 29 |
|
| 30 |
@_app.route("/") |
| 31 |
def index(): |
| 32 |
if not github.authorized: |
| 33 |
return flask.redirect(flask.url_for("github.login")) |
| 34 |
resp = github.get("/user") |
| 35 |
assert resp.ok |
| 36 |
return "You are @{login} on GitHub".format(login=resp.json()["login"]) |
| 37 |
|
| 38 |
return _app |
| 39 |
|
| 40 |
|
| 41 |
@pytest.fixture |
| 42 |
def flask_dance_sessions(): |
| 43 |
return github |
| 44 |
|
| 45 |
|
| 46 |
@pytest.mark.usefixtures("betamax_record_flask_dance") |
| 47 |
def test_home_page(app): |
| 48 |
with app.test_client() as client: |
| 49 |
response = client.get("/", base_url="https://example.com") |
| 50 |
assert response.status_code == 200 |
| 51 |
text = response.get_data(as_text=True) |
| 52 |
assert text == "You are @singingwolfboy on GitHub" |