| 1 |
from urllib.parse import parse_qs |
| 2 |
|
| 3 |
import pytest |
| 4 |
import requests_oauthlib |
| 5 |
import responses |
| 6 |
from flask import Flask |
| 7 |
from urlobject import URLObject |
| 8 |
|
| 9 |
from flask_dance.consumer import OAuth2ConsumerBlueprint |
| 10 |
from flask_dance.consumer.storage import MemoryStorage |
| 11 |
from flask_dance.contrib.slack import make_slack_blueprint, slack |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture |
| 15 |
def make_app(): |
| 16 |
"A callable to create a Flask app with the Slack provider" |
| 17 |
|
| 18 |
def _make_app(*args, **kwargs): |
| 19 |
app = Flask(__name__) |
| 20 |
app.secret_key = "whatever" |
| 21 |
blueprint = make_slack_blueprint(*args, **kwargs) |
| 22 |
app.register_blueprint(blueprint) |
| 23 |
return app |
| 24 |
|
| 25 |
return _make_app |
| 26 |
|
| 27 |
|
| 28 |
def test_blueprint_factory(): |
| 29 |
slack_bp = make_slack_blueprint( |
| 30 |
client_id="foo", |
| 31 |
client_secret="bar", |
| 32 |
scope=["identity", "im:write"], |
| 33 |
redirect_to="index", |
| 34 |
) |
| 35 |
assert isinstance(slack_bp, OAuth2ConsumerBlueprint) |
| 36 |
assert slack_bp.session.scope == ["identity", "im:write"] |
| 37 |
assert slack_bp.session.base_url == "https://slack.com/api/" |
| 38 |
assert slack_bp.session.client_id == "foo" |
| 39 |
assert slack_bp.client_secret == "bar" |
| 40 |
assert slack_bp.authorization_url == "https://slack.com/oauth/authorize" |
| 41 |
assert slack_bp.token_url == "https://slack.com/api/oauth.access" |
| 42 |
|
| 43 |
|
| 44 |
def test_blueprint_factory_with_subdomain(): |
| 45 |
slack_bp = make_slack_blueprint( |
| 46 |
client_id="foo", |
| 47 |
client_secret="bar", |
| 48 |
scope=["identity", "im:write"], |
| 49 |
redirect_to="index", |
| 50 |
subdomain="my-team", |
| 51 |
) |
| 52 |
assert slack_bp.authorization_url == "https://my-team.slack.com/oauth/authorize" |
| 53 |
assert slack_bp.token_url == "https://slack.com/api/oauth.access" |
| 54 |
|
| 55 |
|
| 56 |
def test_load_from_config(make_app): |
| 57 |
app = make_app() |
| 58 |
app.config["SLACK_OAUTH_CLIENT_ID"] = "foo" |
| 59 |
app.config["SLACK_OAUTH_CLIENT_SECRET"] = "bar" |
| 60 |
|
| 61 |
resp = app.test_client().get("/slack") |
| 62 |
url = resp.headers["Location"] |
| 63 |
client_id = URLObject(url).query.dict.get("client_id") |
| 64 |
assert client_id == "foo" |
| 65 |
|
| 66 |
|
| 67 |
@responses.activate |
| 68 |
def test_context_local(make_app): |
| 69 |
responses.add(responses.GET, "https://slack.com") |
| 70 |
|
| 71 |
# set up two apps with two different set of auth tokens |
| 72 |
app1 = make_app( |
| 73 |
"foo1", |
| 74 |
"bar1", |
| 75 |
redirect_to="url1", |
| 76 |
storage=MemoryStorage({"access_token": "app1"}), |
| 77 |
) |
| 78 |
app2 = make_app( |
| 79 |
"foo2", |
| 80 |
"bar2", |
| 81 |
redirect_to="url2", |
| 82 |
storage=MemoryStorage({"access_token": "app2"}), |
| 83 |
) |
| 84 |
|
| 85 |
# outside of a request context, referencing functions on the `slack` object |
| 86 |
# will raise an exception |
| 87 |
with pytest.raises(RuntimeError): |
| 88 |
slack.get("https://slack.com") |
| 89 |
|
| 90 |
# inside of a request context, `slack` should be a proxy to the correct |
| 91 |
# blueprint session |
| 92 |
with app1.test_request_context("/"): |
| 93 |
app1.preprocess_request() |
| 94 |
slack.get("https://slack.com") |
| 95 |
request = responses.calls[0].request |
| 96 |
assert request.headers["Authorization"] == "Bearer app1" |
| 97 |
|
| 98 |
with app2.test_request_context("/"): |
| 99 |
app2.preprocess_request() |
| 100 |
slack.get("https://slack.com") |
| 101 |
request = responses.calls[1].request |
| 102 |
assert request.headers["Authorization"] == "Bearer app2" |
| 103 |
|
| 104 |
|
| 105 |
@responses.activate |
| 106 |
def test_auto_token_get(make_app): |
| 107 |
responses.add(responses.GET, "https://slack.com/api/chat.postMessage") |
| 108 |
|
| 109 |
app = make_app( |
| 110 |
client_id="foo", |
| 111 |
client_secret="bar", |
| 112 |
storage=MemoryStorage({"access_token": "abcde"}), |
| 113 |
) |
| 114 |
|
| 115 |
with app.test_request_context("/"): |
| 116 |
app.preprocess_request() |
| 117 |
resp = slack.get( |
| 118 |
"chat.postMessage", |
| 119 |
data={"channel": "#general", "text": "ping", "icon_emoji": ":robot_face:"}, |
| 120 |
) |
| 121 |
request_data = parse_qs(resp.request.body) |
| 122 |
assert request_data["channel"] == ["#general"] |
| 123 |
assert request_data["text"] == ["ping"] |
| 124 |
assert request_data["icon_emoji"] == [":robot_face:"] |
| 125 |
# the `token` parameter should have been automatically added |
| 126 |
assert request_data["token"] == ["abcde"] |
| 127 |
|
| 128 |
|
| 129 |
@responses.activate |
| 130 |
def test_auto_token_post(make_app): |
| 131 |
responses.add(responses.POST, "https://slack.com/api/chat.postMessage") |
| 132 |
|
| 133 |
app = make_app( |
| 134 |
client_id="foo", |
| 135 |
client_secret="bar", |
| 136 |
storage=MemoryStorage({"access_token": "abcde"}), |
| 137 |
) |
| 138 |
|
| 139 |
with app.test_request_context("/"): |
| 140 |
app.preprocess_request() |
| 141 |
resp = slack.post( |
| 142 |
"chat.postMessage", |
| 143 |
data={"channel": "#general", "text": "ping", "icon_emoji": ":robot_face:"}, |
| 144 |
) |
| 145 |
request_data = parse_qs(resp.request.body) |
| 146 |
assert request_data["channel"] == ["#general"] |
| 147 |
assert request_data["text"] == ["ping"] |
| 148 |
assert request_data["icon_emoji"] == [":robot_face:"] |
| 149 |
# the `token` parameter should have been automatically added |
| 150 |
assert request_data["token"] == ["abcde"] |
| 151 |
|
| 152 |
|
| 153 |
@responses.activate |
| 154 |
def test_auto_token_post_no_token(make_app): |
| 155 |
responses.add(responses.POST, "https://slack.com/api/chat.postMessage") |
| 156 |
|
| 157 |
app = make_app(client_id="foo", client_secret="bar") |
| 158 |
|
| 159 |
with app.test_request_context("/"): |
| 160 |
app.preprocess_request() |
| 161 |
resp = slack.post( |
| 162 |
"chat.postMessage", |
| 163 |
data={"channel": "#general", "text": "ping", "icon_emoji": ":robot_face:"}, |
| 164 |
) |
| 165 |
request_data = parse_qs(resp.request.body) |
| 166 |
assert request_data["channel"] == ["#general"] |
| 167 |
assert request_data["text"] == ["ping"] |
| 168 |
assert request_data["icon_emoji"] == [":robot_face:"] |
| 169 |
assert "token" not in request_data |
| 170 |
url = URLObject(resp.request.url) |
| 171 |
assert "token" not in url.query_dict |
| 172 |
|
| 173 |
|
| 174 |
@responses.activate |
| 175 |
def test_override_token_get(make_app): |
| 176 |
responses.add(responses.GET, "https://slack.com/api/chat.postMessage") |
| 177 |
|
| 178 |
app = make_app( |
| 179 |
client_id="foo", |
| 180 |
client_secret="bar", |
| 181 |
storage=MemoryStorage({"access_token": "abcde"}), |
| 182 |
) |
| 183 |
|
| 184 |
with app.test_request_context("/"): |
| 185 |
app.preprocess_request() |
| 186 |
resp = slack.get( |
| 187 |
"chat.postMessage", |
| 188 |
data={ |
| 189 |
"token": "xyz", |
| 190 |
"channel": "#general", |
| 191 |
"text": "ping", |
| 192 |
"icon_emoji": ":robot_face:", |
| 193 |
}, |
| 194 |
) |
| 195 |
request_data = parse_qs(resp.request.body) |
| 196 |
assert request_data["token"] == ["xyz"] |
| 197 |
assert request_data["channel"] == ["#general"] |
| 198 |
assert request_data["text"] == ["ping"] |
| 199 |
assert request_data["icon_emoji"] == [":robot_face:"] |
| 200 |
# should not be present in URL |
| 201 |
url = URLObject(resp.request.url) |
| 202 |
assert "token" not in url.query_dict |
| 203 |
|
| 204 |
|
| 205 |
@responses.activate |
| 206 |
def test_override_token_post(make_app): |
| 207 |
responses.add(responses.POST, "https://slack.com/api/chat.postMessage") |
| 208 |
|
| 209 |
app = make_app( |
| 210 |
client_id="foo", |
| 211 |
client_secret="bar", |
| 212 |
storage=MemoryStorage({"access_token": "abcde"}), |
| 213 |
) |
| 214 |
|
| 215 |
with app.test_request_context("/"): |
| 216 |
app.preprocess_request() |
| 217 |
resp = slack.post( |
| 218 |
"chat.postMessage", |
| 219 |
data={ |
| 220 |
"token": "xyz", |
| 221 |
"channel": "#general", |
| 222 |
"text": "ping", |
| 223 |
"icon_emoji": ":robot_face:", |
| 224 |
}, |
| 225 |
) |
| 226 |
request_data = parse_qs(resp.request.body) |
| 227 |
assert request_data["token"] == ["xyz"] |
| 228 |
assert request_data["channel"] == ["#general"] |
| 229 |
assert request_data["text"] == ["ping"] |
| 230 |
assert request_data["icon_emoji"] == [":robot_face:"] |
| 231 |
# should not be present |
| 232 |
url = URLObject(resp.request.url) |
| 233 |
assert "token" not in url.query_dict |