Skip to content

Add support for testing webhooks (and some nitpicking) #69

@Daverball

Description

@Daverball

I need to test several functions involving a custom webhook created by my bot to send messages with a custom username and avatar. (It syncs messages with an external webchat). Currently this leads to a NotImplementedError when attempting to create the webhook. I suspect anything related to actually using the webhook is not implemented either.

It would be nice to be able to test this without having to mock a bunch of stuff. So the backend would need to keep track of created webhooks on TextChannel and handle the Webhook.send method. Ideally these messages would get added to the same Queue the bot messages get added so testing of webhook messages doesn't really look any different from testing bot messages.

I've encountered a bunch of other things that are just a little bit annoying right now, like triggering some of the bot events using the backend state without calling the methods directly, e.g. updating presence on a member or editing a message. But these are at least workaroundable by calling the parse_* methods on the backend state directly and writing a couple of short helper functions. So currently I have custom function for update_presence and edit_message. It would also be nice to have a set_guild_available and set_guild_unavailable, but I haven't gotten around to writing these yet.

async def edit_message(bot, message, **fields):
    # HACK: This gets around there not being a proper edit_message method
    state = backend.get_state()
    data = backend.facts.dict_from_message(message)
    data['channel_id'] = message.channel.id
    data.update(**fields)
    state.parse_message_update(data)
    await dpytest.run_all_events()
async def update_presence(bot, member, status):
    # HACK: This gets around there not being a proper presence update method
    state = backend.get_state()
    data = backend.facts.dict_from_member(member)
    data['status'] = status.name
    state.parse_presence_update(data)
    await dpytest.run_all_events()

It also seems like the bot's ready event never gets triggered by the test framework so if you have any custom tasks defined on the bot they never run unless you manually trigger the ready event (as long as you are actually waiting for the ready event in the task, which you usually want to do). This one is a bit nitpicky and you could certainly be of the opinion that for most tests the tasks shouldn't be running anyways, but it would be nice if it was formalized within the test framework i.e. when you configure the bot you could select whether you want to trigger the ready event or not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions