slack-ruby / slack-ruby-bot-server-events

Slack commands, interactive buttons, and events extension for slack-ruby-bot-server.
MIT License
71 stars 10 forks source link

Help for testing Slash commands with RSpec #20

Open IsabelleLePivain opened 1 year ago

IsabelleLePivain commented 1 year ago

Hello,

I am trying to test a Slash command by sending it an HTTP payload to the URL set for Slash commands for our app.

I used the ping command spec on the sample repository to build my test. The only impactful difference I can notice with my current test is that I skipped the team fabrication (it returned an error, and I am not clear with what it should be doing).

RSpec seems to load the library and configure Slash commands, but it looks like the command is not actually being run in my test. I already checked the route and it looks good.

I am not getting an error message. What happens is that RSpec seems to load the library and the command file properly and enter the SlackRubyBotServer::Events.configure do |config| scope. The post command does not return any error. But the code in the config.on :command, '/help' do |command| block is not run.

Here is the code of my Slash command. It is located under ./app/lib/slack/commands/help.rb

puts "We are loading the help.rb file" #This shows in my logs
SlackRubyBotServer::Events.configure do |config|
  puts "we are configuring the events class from the library" #This shows in my logs
  config.on :command, '/help' do |command|
    puts "We are calling the help command" #This does not show in my logs

    client = Slack::Web::Client.new(token: Rails.application.credentials[Rails.env.to_sym][:slack][:access_token])

    client.views_open({
        trigger_id: command[:trigger_id],
        view: view
      }
    )
    nil
  end
end

Here is the code of my Slash command. It is located under ./app/lib/slack/commands/help.rb

# frozen_string_literal: true
require 'rails_helper'

# The help.rb file is loaded.
# Routing looks good: chat/api/slack/command is the correct path to send the /help command.
# Changed the payload format to JSON
# The 'slack-ruby-bot-server' library is loaded automatically. 
# The Events class is accessible.

# The help command is not called. The test does not run the config.on :command block.

describe '/help', type: :request do
  include Rack::Test::Methods

  def app
    SlackRubyBotServer::Api::Middleware.instance
  end

  context 'without signature checks' do
    before do
      allow_any_instance_of(Slack::Events::Request).to receive(:verify!)
    end

    # context 'with a team' do
      # let!(:team) { Fabricate(:team) }

      # Reduced payload
      let(:command) do
        {
          command: "/help",
          trigger_id: "triggerID123"
        }
      end

      let(:slack_client){double(:client)}  

      it 'opens a non-empty view with the trigger ID from the command' do
        # my future tests
        post '/chat/api/slack/command', params: command #looks like the right route?!
      end 
    end
  end
# end

The only parameters that my Slash command uses from the payload are the trigger_id an the name of the command.

I am a beginner with both programming and RSpec. Apologies if there is any inaccuracy, or if this is an obvious question. I spent a couple of hours investigating without success.

Thank you!

IsabelleLePivain commented 1 year ago

Hi,

Here is another attempt I had: I tried the test with the full original payload from Slack:

{"token"=>"ourtoken123", 
        "team_id"=>"T04...", 
        "team_domain"=>"sosvdev",
        "channel_id"=>"D04....",
        "channel_name"=>"directmessage",
        "user_id"=>"U04...",
        "user_name"=>"isabelle.lepivain",
        "command"=>"/help",
        "text"=>"",
        "api_app_id"=>"ABC...",
        "is_enterprise_install"=>"false",
        "response_url"=>"https...",
        "trigger_id"=>"123..."}

I mimicked the params here, but in my code I have copy pasted the actual original payload from our test workspace. I also tried converting this hash into JSON. None of this seems to trigger the command from our tests.