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__) zoho_bp1 = make_zoho_blueprint( "foo1", "bar1", redirect_to="url1", backend=MemoryBackend({"access_token": "app1"}), ) app1.register_blueprint(zoho_bp1) app2 = Flask(__name__) zoho_bp2 = make_zoho_blueprint( "foo2", "bar2", redirect_to="url2", backend=MemoryBackend({"access_token": "app2"}), ) app2.register_blueprint(zoho_bp2) # outside of a request context, referencing functions on the `zoho` object # will raise an exception with pytest.raises(RuntimeError): zoho.get("https://google.com") # inside of a request context, `zoho` should be a proxy to the correct # blueprint session with app1.test_request_context("/"): app1.preprocess_request() zoho.get("https://google.com") request = responses.calls[0].request assert request.headers["Authorization"] == "Zoho-oauthtoken app1" with app2.test_request_context("/"): app2.preprocess_request() zoho.get("https://google.com") request = responses.calls[1].request assert request.headers["Authorization"] == "Zoho-oauthtoken app2"
def test_context_local(make_app): responses.add(responses.GET, "https://google.com") # set up two apps with two different set of auth tokens app1 = make_app( "foo1", "bar1", redirect_to="url1", backend=MemoryBackend({"access_token": "app1"}), ) app2 = make_app( "foo2", "bar2", redirect_to="url2", backend=MemoryBackend({"access_token": "app2"}), ) # outside of a request context, referencing functions on the `google` object # will raise an exception with pytest.raises(RuntimeError): google.get("https://github.com") # inside of a request context, `google` should be a proxy to the correct # blueprint session with app1.test_request_context("/"): app1.preprocess_request() google.get("https://google.com") request = responses.calls[0].request assert request.headers["Authorization"] == "Bearer app1" with app2.test_request_context("/"): app2.preprocess_request() google.get("https://google.com") request = responses.calls[1].request assert request.headers["Authorization"] == "Bearer app2"
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')
def test_content_type(sign_func, make_app): responses.add(responses.GET, "https://flask.atlassian.net/") backend = MemoryBackend({ "oauth_token": "faketoken", "oauth_token_secret": "fakesecret", "oauth_session_handle": "fakehandle", "oauth_expires_in": "157680000", "oauth_authorization_expires_in": "160272000", }) app = make_app( "https://flask.atlassian.net", rsa_key="fakersa", consumer_key="fakekey", backend=backend, ) @app.route("/test") def api_request(): jira.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")
def test_auto_token_get(make_app): responses.add(responses.GET, "https://slack.com/api/chat.postMessage") app = make_app( client_id="foo", client_secret="bar", backend=MemoryBackend({"access_token": "abcde"}), ) with app.test_request_context("/"): app.preprocess_request() resp = slack.get( "chat.postMessage", data={ "channel": "#general", "text": "ping", "icon_emoji": ":robot_face:" }, ) request_data = url_decode(resp.request.body) assert request_data["channel"] == "#general" assert request_data["text"] == "ping" assert request_data["icon_emoji"] == ":robot_face:" # the `token` parameter should have been automatically added assert request_data["token"] == "abcde"
def test_override_token_post(make_app): responses.add(responses.POST, "https://slack.com/api/chat.postMessage") app = make_app( client_id="foo", client_secret="bar", backend=MemoryBackend({"access_token": "abcde"}), ) with app.test_request_context("/"): app.preprocess_request() resp = slack.post( "chat.postMessage", data={ "token": "xyz", "channel": "#general", "text": "ping", "icon_emoji": ":robot_face:", }, ) request_data = url_decode(resp.request.body) assert request_data["token"] == "xyz" assert request_data["channel"] == "#general" assert request_data["text"] == "ping" assert request_data["icon_emoji"] == ":robot_face:" # should not be present url = URLObject(resp.request.url) assert "token" not in url.query_dict
def test_override_token_get(): responses.add(responses.GET, "https://slack.com/api/chat.postMessage") app = Flask(__name__) slack_bp = make_slack_blueprint( client_id="foo", client_secret="bar", backend=MemoryBackend({"access_token": "abcde"}), ) app.register_blueprint(slack_bp, url_prefix="/login") with app.test_request_context("/"): app.preprocess_request() resp = slack.get("chat.postMessage", data={ "token": "xyz", "channel": "#general", "text": "ping", "icon_emoji": ":robot_face:", }) request_data = url_decode(resp.request.body) assert request_data["token"] == "xyz" assert request_data["channel"] == "#general" assert request_data["text"] == "ping" assert request_data["icon_emoji"] == ":robot_face:" # should not be present in URL url = URLObject(resp.request.url) assert "token" not in url.query_dict
def test_auto_token_post(): responses.add(responses.POST, "https://slack.com/api/chat.postMessage") app = Flask(__name__) slack_bp = make_slack_blueprint( client_id="foo", client_secret="bar", backend=MemoryBackend({"access_token": "abcde"}), ) app.register_blueprint(slack_bp, url_prefix="/login") with app.test_request_context("/"): app.preprocess_request() resp = slack.post("chat.postMessage", data={ "channel": "#general", "text": "ping", "icon_emoji": ":robot_face:", }) request_data = url_decode(resp.request.body) assert request_data["channel"] == "#general" assert request_data["text"] == "ping" assert request_data["icon_emoji"] == ":robot_face:" # the `token` parameter should have been automatically added assert request_data["token"] == "abcde"
def test_authorization_required_decorator_allowed(): app, blueprint = make_app() @app.route("/restricted") @blueprint.session.authorization_required def restricted_view(): return "allowed" blueprint.backend = MemoryBackend({"access_token": "faketoken"}) with app.test_client() as client: resp = client.get( "/restricted", base_url="https://a.b.c", ) assert resp.status_code == 200 text = resp.get_data(as_text=True) assert text == "allowed"