Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

[Question] How to test events or commands? #12

Closed
1oglop1 opened this issue Aug 18, 2020 · 2 comments
Closed

[Question] How to test events or commands? #12

1oglop1 opened this issue Aug 18, 2020 · 2 comments

Comments

@1oglop1
Copy link

1oglop1 commented Aug 18, 2020

Hello, could you please help me to create some examples how to test handlers?

eg.
This is a handler using a save_text function, I know I cam simply test save_text and other parts.


from fastapi import FastAPI
from slackers.hooks import commands
from slackers.models import SlackCommand
from slackers.server import router

app = FastAPI(debug=True)
app.include_router(router)
app.include_router(router, prefix="/slack")


async def save_text(text: str):
    with open("command_log.txt", "a+") as outf:
        print(text, file=outf)


@commands.on("cmd")
async def handle_command(payload):
    cmd = SlackCommand(**payload).text
    # .. more complex parsing of cmd in between
    await save_text(cmd)

This is my test:

from fastapi.testclient import TestClient


def test_shout_command( client: TestClient):
    command_data = {
        "token": "1234erfghjuiop",
        "command": "/cmd",
        "response_url": "https://hooks.slack.com/commands/ABCDEFG/1289665550278/6RQ7UIUXhMzAFXwApOPiMB1o",
        "trigger_id": "1296602173907.524169929122.0fa6d05ec3b37886607b56011efcbc9d",
        "user_id": "UFE4ZTC8J",
        "user_name": "1user1",
        "team_id": "TFE4ZTB3L",
        "channel_id": "D0184EACFNK",
        "text": "to <@UJ03ECZLG|1user1_1> <@UJ03E1ESU|1user1_2> for less",
    }
    head = {
        "x-slack-signature": "mySecret",
        "x-slack-request-timestamp": "1531420618"
    }
    resp = client.post("/slack/commands", data=command_data, headers=head)
    print(resp)
    assert resp.ok

But response I receive is 403.

Could you please recommend how can I test handle_command with pytest?

@uhavin
Copy link
Owner

uhavin commented Aug 18, 2020

Hi @1oglop1

Thanks for using Slackers. The 403 would occur if the header you are passing does not get verified correctly. For it to pass, you'd have to create a valid signature/timestamp combination. Slack itself explains how signatures get calculated: [docs]. You can create one on the fly in your test, or alternatively, you can calculate one manually and use the resulting string in your test. You should then use something like freeze_gun to have a fixed time in your test. Don't use real secrets, you can fake one as long as your secret in the test environment is the same as you are using to create the signature with.

Please let me know if this is helpful. I have some more time later today, when I can look into it more deeply for you.

@1oglop1
Copy link
Author

1oglop1 commented Aug 18, 2020

@uhavin Thanks for the prompt response,
I realised that I should explore slackers tests and borrowed fixtures with headers and patched verification.

BTW If you are interested to share any kind of your python story check our community at https://py.amsterdam.

@1oglop1 1oglop1 closed this as completed Aug 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants