Пример #1
1
def test_context_local():
    responses.add(responses.GET, "https://google.com")

    # set up two apps with two different set of auth tokens
    app1 = Flask(__name__)
    jbp1 = make_jira_blueprint(
        "https://t1.atlassian.com", "foo1", "bar1", redirect_to="url1"
    )
    app1.register_blueprint(jbp1)

    app2 = Flask(__name__)
    jbp2 = make_jira_blueprint(
        "https://t2.atlassian.com", "foo2", "bar2", redirect_to="url2"
    )
    app2.register_blueprint(jbp2)

    # outside of a request context, referencing functions on the `jira` object
    # will raise an exception
    with pytest.raises(RuntimeError):
        jira.get("https://google.com")

    # inside of a request context, `jira` should be a proxy to the correct
    # blueprint session
    with app1.test_request_context("/"):
        jbp1.session.auth.client.get_oauth_signature = mock.Mock(return_value="sig1")
        jbp2.session.auth.client.get_oauth_signature = mock.Mock(return_value="sig2")

        app1.preprocess_request()
        jira.get("https://google.com")
        auth_header = dict(
            parse_authorization_header(
                responses.calls[0].request.headers["Authorization"].decode("utf-8")
            )
        )
        assert auth_header["oauth_consumer_key"] == "foo1"
        assert auth_header["oauth_signature"] == "sig1"

    with app2.test_request_context("/"):
        jbp1.session.auth.client.get_oauth_signature = mock.Mock(return_value="sig1")
        jbp2.session.auth.client.get_oauth_signature = mock.Mock(return_value="sig2")

        app2.preprocess_request()
        jira.get("https://google.com")
        auth_header = dict(
            parse_authorization_header(
                responses.calls[1].request.headers["Authorization"].decode("utf-8")
            )
        )
        assert auth_header["oauth_consumer_key"] == "foo2"
        assert auth_header["oauth_signature"] == "sig2"
Пример #2
0
def test_context_local():
    responses.add(responses.GET, "https://google.com")

    # set up two apps with two different set of auth tokens
    app1 = Flask(__name__)
    jbp1 = make_jira_blueprint("https://t1.atlassian.com",
                               "foo1",
                               "bar1",
                               redirect_to="url1")
    app1.register_blueprint(jbp1)

    app2 = Flask(__name__)
    jbp2 = make_jira_blueprint("https://t2.atlassian.com",
                               "foo2",
                               "bar2",
                               redirect_to="url2")
    app2.register_blueprint(jbp2)

    # outside of a request context, referencing functions on the `jira` object
    # will raise an exception
    with pytest.raises(RuntimeError):
        jira.get("https://google.com")

    # inside of a request context, `jira` should be a proxy to the correct
    # blueprint session
    with app1.test_request_context("/"):
        jbp1.session.auth.client.get_oauth_signature = mock.Mock(
            return_value="sig1")
        jbp2.session.auth.client.get_oauth_signature = mock.Mock(
            return_value="sig2")

        app1.preprocess_request()
        jira.get("https://google.com")
        assert len(responses.calls) > 0
        auth_header = dict(
            parse_authorization_header(
                responses.calls[0].request.headers["Authorization"].decode(
                    "utf-8")))
        assert auth_header["oauth_consumer_key"] == "foo1"
        assert auth_header["oauth_signature"] == "sig1"

    with app2.test_request_context("/"):
        jbp1.session.auth.client.get_oauth_signature = mock.Mock(
            return_value="sig1")
        jbp2.session.auth.client.get_oauth_signature = mock.Mock(
            return_value="sig2")

        app2.preprocess_request()
        jira.get("https://google.com")
        assert len(responses.calls) > 0
        auth_header = dict(
            parse_authorization_header(
                responses.calls[1].request.headers["Authorization"].decode(
                    "utf-8")))
        assert auth_header["oauth_consumer_key"] == "foo2"
        assert auth_header["oauth_signature"] == "sig2"
Пример #3
0
def test_content_type(sign_func):
    responses.add(responses.GET, "https://flask.atlassian.net/")

    app = Flask(__name__)
    app.secret_key = "anything"
    app.debug = True
    backend = MemoryBackend({
        "oauth_token": "faketoken",
        "oauth_token_secret": "fakesecret",
        "oauth_session_handle": "fakehandle",
        "oauth_expires_in": "157680000",
        "oauth_authorization_expires_in": "160272000",
    })
    jira_bp = make_jira_blueprint(
        "https://flask.atlassian.net",
        rsa_key="fakersa",
        consumer_key="fakekey",
        backend=backend,
    )
    app.register_blueprint(jira_bp)

    @app.route("/test")
    def api_request():
        jira_bp.session.get("/")
        return "success"

    resp = app.test_client().get("/test")
    headers = responses.calls[0].request.headers
    assert "Content-Type" in headers
    assert headers["Content-Type"] == "application/json".encode('utf-8')
Пример #4
0
def test_rsa_key_file(tmp_path):
    rsa_key = tmp_path / "fake.key"
    rsa_key.write_text("my-fake-key")

    jira_bp = make_jira_blueprint(rsa_key=str(rsa_key),
                                  base_url="https://flask.atlassian.net")
    assert jira_bp.rsa_key == "my-fake-key"
Пример #5
0
def test_content_type(sign_func):
    responses.add(responses.GET, "https://flask.atlassian.net/")

    app = Flask(__name__)
    app.secret_key = "anything"
    app.debug = True
    backend = MemoryBackend({
        "oauth_token": "faketoken",
        "oauth_token_secret": "fakesecret",
        "oauth_session_handle": "fakehandle",
        "oauth_expires_in": "157680000",
        "oauth_authorization_expires_in": "160272000",
    })
    jira_bp = make_jira_blueprint(
        "https://flask.atlassian.net",
        consumer_key="fakekey",
        backend=backend,
    )
    app.register_blueprint(jira_bp)

    @app.route("/test")
    def api_request():
        jira_bp.session.get("/")
        return "success"

    resp = app.test_client().get("/test")
    headers = responses.calls[0].request.headers
    assert "Content-Type" in headers
    assert headers["Content-Type"] == "application/json"
Пример #6
0
def test_rsa_key_file(tmp_path):
    rsa_key = tmp_path / "fake.key"
    rsa_key.write_text("my-fake-key")

    jira_bp = make_jira_blueprint(
        rsa_key=str(rsa_key), base_url="https://flask.atlassian.net"
    )
    assert jira_bp.rsa_key == "my-fake-key"
Пример #7
0
def test_rsa_key_file():
    key_fd, key_file_path = tempfile.mkstemp()
    with os.fdopen(key_fd, 'w') as key_file:
        key_file.write("my-fake-key")

    jira_bp = make_jira_blueprint(
        rsa_key=key_file_path,
        base_url="https://flask.atlassian.net",
    )
    assert jira_bp.rsa_key == "my-fake-key"

    os.remove(key_file_path)
Пример #8
0
def test_rsa_key_file():
    key_fd, key_file_path = tempfile.mkstemp()
    with os.fdopen(key_fd, 'w') as key_file:
        key_file.write("my-fake-key")

    jira_bp = make_jira_blueprint(
        rsa_key=key_file_path,
        base_url="https://flask.atlassian.net",
    )
    assert jira_bp.rsa_key == "my-fake-key"

    os.remove(key_file_path)
Пример #9
0
def test_blueprint_factory():
    jira_bp = make_jira_blueprint(
        consumer_key="foobar",
        rsa_key="supersecret",
        base_url="https://flask.atlassian.net",
        redirect_to="index",
    )
    assert isinstance(jira_bp, OAuth1ConsumerBlueprint)
    assert jira_bp.session.base_url == "https://flask.atlassian.net"
    assert jira_bp.session.auth.client.client_key == "foobar"
    assert jira_bp.session.auth.client.rsa_key == "supersecret"
    assert jira_bp.request_token_url == "https://flask.atlassian.net/plugins/servlet/oauth/request-token"
    assert jira_bp.access_token_url == "https://flask.atlassian.net/plugins/servlet/oauth/access-token"
    assert jira_bp.authorization_url == "https://flask.atlassian.net/plugins/servlet/oauth/authorize"
Пример #10
0
def test_blueprint_factory():
    jira_bp = make_jira_blueprint(
        consumer_key="foobar",
        rsa_key="supersecret",
        base_url="https://flask.atlassian.net",
        redirect_to="index",
    )
    assert isinstance(jira_bp, OAuth1ConsumerBlueprint)
    assert jira_bp.session.base_url == "https://flask.atlassian.net"
    assert jira_bp.session.auth.client.client_key == "foobar"
    assert jira_bp.session.auth.client.rsa_key == "supersecret"
    assert jira_bp.request_token_url == "https://flask.atlassian.net/plugins/servlet/oauth/request-token"
    assert jira_bp.access_token_url == "https://flask.atlassian.net/plugins/servlet/oauth/access-token"
    assert jira_bp.authorization_url == "https://flask.atlassian.net/plugins/servlet/oauth/authorize"
Пример #11
0
def test_load_from_config(sign_func):
    responses.add(
        responses.POST,
        "https://flask.atlassian.net/plugins/servlet/oauth/request-token",
        body="oauth_token=faketoken&oauth_token_secret=fakesecret",
    )
    app = Flask(__name__)
    app.secret_key = "anything"
    app.config["JIRA_OAUTH_CONSUMER_KEY"] = "foo"
    app.config["JIRA_OAUTH_RSA_KEY"] = "bar"
    jira_bp = make_jira_blueprint("https://flask.atlassian.net", redirect_to="index")
    app.register_blueprint(jira_bp)

    resp = app.test_client().get("/jira")
    auth_header = dict(parse_authorization_header(
        responses.calls[0].request.headers['Authorization'].decode('utf-8')
    ))
    assert auth_header["oauth_consumer_key"] == "foo"
    assert sign_func.call_args[0][1] == "bar"
Пример #12
0
def test_load_from_config(sign_func):
    responses.add(
        responses.POST,
        "https://flask.atlassian.net/plugins/servlet/oauth/request-token",
        body="oauth_token=faketoken&oauth_token_secret=fakesecret",
    )
    app = Flask(__name__)
    app.secret_key = "anything"
    app.config["JIRA_OAUTH_CONSUMER_KEY"] = "foo"
    app.config["JIRA_OAUTH_RSA_KEY"] = "bar"
    jira_bp = make_jira_blueprint("https://flask.atlassian.net", redirect_to="index")
    app.register_blueprint(jira_bp)

    resp = app.test_client().get("/jira")
    auth_header = dict(parse_authorization_header(
        responses.calls[0].request.headers['Authorization'].decode('utf-8')
    ))
    assert auth_header["oauth_consumer_key"] == "foo"
    assert sign_func.call_args[0][1] == "bar"
Пример #13
0
    "JIRA_CONSUMER_KEY", "JIRA_RSA_KEY",
    "GITHUB_CLIENT_ID", "GITHUB_CLIENT_SECRET",
))
missing = req_env_vars - set(os.environ.keys())
if missing:
    raise Exception(
        "You must define the following variables in your environment: {vars} "
        "See the README for more information.".format(vars=", ".join(missing))
    )


## JIRA ##

jira_bp = make_jira_blueprint(
    consumer_key=os.environ["JIRA_CONSUMER_KEY"],
    rsa_key=os.environ["JIRA_RSA_KEY"],
    base_url="https://openedx.atlassian.net",
    redirect_to="index",
)
jira_bp.set_token_storage_sqlalchemy(OAuth, db.session)


@oauth_authorized.connect_via(jira_bp)
def jira_logged_in(blueprint, token):
    if token:
        flash("Successfully signed in with JIRA")
    else:
        flash("You denied the request to sign in with JIRA")

## GITHUB ##

github_bp = make_github_blueprint(
Пример #14
0
    "JIRA_CONSUMER_KEY",
    "JIRA_RSA_KEY",
    "GITHUB_CLIENT_ID",
    "GITHUB_CLIENT_SECRET",
))
missing = req_env_vars - set(os.environ.keys())
if missing:
    raise Exception(
        "You must define the following variables in your environment: {vars} "
        "See the README for more information.".format(vars=", ".join(missing)))

## JIRA ##

jira_bp = make_jira_blueprint(
    consumer_key=os.environ["JIRA_CONSUMER_KEY"],
    rsa_key=os.environ["JIRA_RSA_KEY"],
    base_url="https://openedx.atlassian.net",
    redirect_to="index",
)


@jira_bp.token_setter
def set_jira_token(token):
    creds = OAuthCredential(
        name="jira",
        token=token["oauth_token"],
        secret=token["oauth_token_secret"],
        created_on=datetime.utcnow(),
    )
    db.session.add(creds)
    db.session.commit()
Пример #15
0
import os
from flask import request, flash
from flask_dance.contrib.github import make_github_blueprint
from flask_dance.contrib.jira import make_jira_blueprint
from flask_dance.consumer import oauth_authorized, oauth_error
from flask_dance.consumer.backend.sqla import SQLAlchemyBackend
from openedx_webhooks import db
from openedx_webhooks.models import OAuth

## JIRA ##

jira_bp = make_jira_blueprint(
    # this *should* pick up the client_key and rsa_key from app.config,
    # but it doesn't seem to be doing so... :(
    consumer_key=os.environ.get("JIRA_OAUTH_CONSUMER_KEY"),
    rsa_key=os.environ.get("JIRA_OAUTH_RSA_KEY"),
    # these are actually necessary
    base_url="https://openedx.atlassian.net",
    backend=SQLAlchemyBackend(OAuth, db.session),
)


@oauth_authorized.connect_via(jira_bp)
def jira_logged_in(blueprint, token):
    if token:
        flash("Successfully signed in with JIRA")
    else:
        flash("You denied the request to sign in with JIRA")


@oauth_error.connect_via(jira_bp)
Пример #16
0
import os
from flask import request, flash
from flask_dance.contrib.github import make_github_blueprint
from flask_dance.contrib.jira import make_jira_blueprint
from flask_dance.consumer import oauth_authorized, oauth_error
from flask_dance.consumer.backend.sqla import SQLAlchemyBackend
from openedx_webhooks import db
from openedx_webhooks.models import OAuth

## JIRA ##

jira_bp = make_jira_blueprint(
    # this *should* pick up the client_key and rsa_key from app.config,
    # but it doesn't seem to be doing so... :(
    consumer_key=os.environ.get("JIRA_OAUTH_CONSUMER_KEY"),
    rsa_key=os.environ.get("JIRA_OAUTH_RSA_KEY"),
    # these are actually necessary
    base_url="https://openedx.atlassian.net",
    backend=SQLAlchemyBackend(OAuth, db.session),
)


@oauth_authorized.connect_via(jira_bp)
def jira_logged_in(blueprint, token):
    if token:
        flash("Successfully signed in with JIRA")
    else:
        flash("You denied the request to sign in with JIRA")


@oauth_error.connect_via(jira_bp)
Пример #17
0
 def _make_app(*args, **kwargs):
     app = Flask(__name__)
     app.secret_key = "whatever"
     blueprint = make_jira_blueprint(*args, **kwargs)
     app.register_blueprint(blueprint)
     return app
Пример #18
0
 def _make_app(*args, **kwargs):
     app = Flask(__name__)
     app.secret_key = "whatever"
     blueprint = make_jira_blueprint(*args, **kwargs)
     app.register_blueprint(blueprint)
     return app