def test_blueprint_factory():
    nylas_bp = make_nylas_blueprint(
        client_id="foo", client_secret="bar", redirect_to="index"
    )
    assert isinstance(nylas_bp, OAuth2ConsumerBlueprint)
    assert nylas_bp.session.scope == "email"
    assert nylas_bp.session.base_url == "https://api.nylas.com/"
    assert nylas_bp.session.client_id == "foo"
    assert nylas_bp.client_secret == "bar"
    assert nylas_bp.authorization_url == "https://api.nylas.com/oauth/authorize"
    assert nylas_bp.token_url == "https://api.nylas.com/oauth/token"
Exemple #2
0
def test_blueprint_factory():
    nylas_bp = make_nylas_blueprint(client_id="foo",
                                    client_secret="bar",
                                    redirect_to="index")
    assert isinstance(nylas_bp, OAuth2ConsumerBlueprint)
    assert nylas_bp.session.scope == "email"
    assert nylas_bp.session.base_url == "https://api.nylas.com/"
    assert nylas_bp.session.client_id == "foo"
    assert nylas_bp.client_secret == "bar"
    assert nylas_bp.authorization_url == "https://api.nylas.com/oauth/authorize"
    assert nylas_bp.token_url == "https://api.nylas.com/oauth/token"
Exemple #3
0
def test_load_from_config():
    app = Flask(__name__)
    app.secret_key = "anything"
    app.config["NYLAS_OAUTH_CLIENT_ID"] = "foo"
    app.config["NYLAS_OAUTH_CLIENT_SECRET"] = "bar"
    nylas_bp = make_nylas_blueprint(redirect_to="index")
    app.register_blueprint(nylas_bp)

    resp = app.test_client().get("/nylas")
    url = resp.headers["Location"]
    client_id = URLObject(url).query.dict.get("client_id")
    assert client_id == "foo"
Exemple #4
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__)
    nbp1 = make_nylas_blueprint(
        "foo1", "bar1", redirect_to="url1",
        backend=MemoryBackend({"access_token": "app1"}),
    )
    app1.register_blueprint(nbp1)

    app2 = Flask(__name__)
    nbp2 = make_nylas_blueprint(
        "foo2", "bar2", redirect_to="url2",
        backend=MemoryBackend({"access_token": "app2"}),
    )
    app2.register_blueprint(nbp2)

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

    # inside of a request context, `nylas` should be a proxy to the correct
    # blueprint session
    with app1.test_request_context("/"):
        app1.preprocess_request()
        nylas.get("https://google.com")
        request = responses.calls[0].request
        assert request.headers["Authorization"] == "Bearer app1"

    with app2.test_request_context("/"):
        app2.preprocess_request()
        nylas.get("https://google.com")
        request = responses.calls[1].request
        assert request.headers["Authorization"] == "Bearer app2"
Exemple #5
0
if cfg_needs_replacing:
    message = textwrap.dedent(
        """
        This example will only work if you replace the fake configuration
        values in `config.json` with real configuration values.
        The following config values need to be replaced:
        {keys}
        Consult the README.md file in this directory for more information.
    """
    ).format(keys=", ".join(cfg_needs_replacing))
    print(message, file=sys.stderr)
    sys.exit(1)

# Use Flask-Dance to automatically set up the OAuth endpoints for Nylas.
# For more information, check out the documentation: http://flask-dance.rtfd.org
nylas_bp = make_nylas_blueprint()
app.register_blueprint(nylas_bp, url_prefix="/login")

# Teach Flask how to find out that it's behind an ngrok proxy
app.wsgi_app = ProxyFix(app.wsgi_app)


# Define what Flask should do when someone visits the root URL of this website.
@app.route("/")
def index():
    # If the user has already connected to Nylas via OAuth,
    # `nylas.authorized` will be True. Otherwise, it will be False.
    if not nylas.authorized:
        # OAuth requires HTTPS. The template will display a handy warning,
        # unless we've overridden the check.
        return render_template(
 def _make_app(*args, **kwargs):
     app = Flask(__name__)
     app.secret_key = "whatever"
     blueprint = make_nylas_blueprint(*args, **kwargs)
     app.register_blueprint(blueprint)
     return app
Exemple #7
0
 def _make_app(*args, **kwargs):
     app = Flask(__name__)
     app.secret_key = "whatever"
     blueprint = make_nylas_blueprint(*args, **kwargs)
     app.register_blueprint(blueprint)
     return app