Exemplo n.º 1
1
def test_auto_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={
            "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"
Exemplo n.º 2
0
def test_override_token_get(make_app):
    responses.add(responses.GET, "https://slack.com/api/chat.postMessage")

    app = make_app(
        client_id="foo",
        client_secret="bar",
        storage=MemoryStorage({"access_token": "abcde"}),
    )

    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
Exemplo n.º 3
0
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",
        storage=MemoryStorage({"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"
Exemplo n.º 4
0
def get_slack_user(slack_token):
    response = slack.get('/api/users.identity?token={}'.format(slack_token))
    identity = response.json()
    if identity['ok'] and identity['team']['id'] == SLACK_TEAM_ID:
        return identity['user'] # {'name': '', 'id': ''}
    else:
        return None
Exemplo n.º 5
0
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
Exemplo n.º 6
0
def test_auto_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={
                             "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"
Exemplo n.º 7
0
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
Exemplo n.º 8
0
def test_context_local(make_app):
    responses.add(responses.GET, "https://slack.com")

    # set up two apps with two different set of auth tokens
    app1 = make_app(
        "foo1",
        "bar1",
        redirect_to="url1",
        storage=MemoryStorage({"access_token": "app1"}),
    )
    app2 = make_app(
        "foo2",
        "bar2",
        redirect_to="url2",
        storage=MemoryStorage({"access_token": "app2"}),
    )

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

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

    with app2.test_request_context("/"):
        app2.preprocess_request()
        slack.get("https://slack.com")
        request = responses.calls[1].request
        assert request.headers["Authorization"] == "Bearer app2"
Exemplo n.º 9
0
def slack_login():
    if not slack.authorized:
        flash('Access denied to Slack', 'error')
        return redirect(url_for("auth.login"))

    resp = slack.get("https://slack.com/api/users.identity")
    if not resp.ok:
        flash('Unable to access Slack data', 'error')
        return redirect(url_for("auth.login"))
    resp_data = resp.json()
    if not 'user' in resp_data:
        flash('Invalid Slack data format', 'error')
        print(resp_data)
        return redirect(url_for("auth.login"))

    resp_user = resp_data['user']
    user = User.query.filter_by(sso_id=resp_user['id']).first()
    if not user:
        if current_user and current_user.is_authenticated:
            user = current_user
            user.sso_id = resp_user['id']
        else:
            user = User.query.filter_by(email=resp_user['email']).first()
            if user:
                # Update SSO identifier
                user.sso_id = resp_user['id']
                db.session.add(user)
                db.session.commit()
            else:
                user = User.create(username=resp_user['name'].lower().replace(
                    " ", "_"),
                                   sso_id=resp_user['id'],
                                   email=resp_user['email'],
                                   password=random_password(),
                                   active=True)
            user.socialize()
            login_user(user, remember=True)
            flash("Please complete your user account", 'info')
            return redirect(url_for("auth.user_profile"))
    login_user(user, remember=True)
    flash(u'Logged in via Slack')
    return redirect(url_for("public.home"))
Exemplo n.º 10
0
def slack_login():
    if not slack.authorized:
        flash('Access denied to Slack', 'danger')
        return redirect(url_for("auth.login", local=1))

    resp = slack.get("https://slack.com/api/users.identity")
    if not resp.ok:
        flash('Unable to access Slack data', 'danger')
        return redirect(url_for("auth.login", local=1))
    resp_data = resp.json()
    if 'user' not in resp_data:
        flash('Invalid Slack data format', 'danger')
        # print(resp_data)
        return redirect(url_for("auth.login", local=1))
    resp_user = resp_data['user']
    return get_or_create_sso_user(
        resp_user['id'],
        resp_user['name'],
        resp_user['email'],
    )
Exemplo n.º 11
0
def test_context_local():
    responses.add(responses.GET, "https://slack.com")

    # set up two apps with two different set of auth tokens
    app1 = Flask(__name__)
    sbp1 = make_slack_blueprint(
        "foo1", "bar1", redirect_to="url1",
        backend=MemoryBackend({"access_token": "app1"}),
    )
    app1.register_blueprint(sbp1)

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

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

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

    with app2.test_request_context("/"):
        app2.preprocess_request()
        slack.get("https://slack.com")
        request = responses.calls[1].request
        assert request.headers["Authorization"] == "Bearer app2"