Class: BoltRb::Testing::PayloadFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt_rb/testing/payload_factory.rb

Overview

Factory class for creating fake Slack payloads for testing purposes.

This class provides methods to generate realistic payloads for various Slack event types, making it easy to write specs for handlers without needing actual Slack events.

Examples:

Creating a message event payload

payload = BoltRb::Testing::PayloadFactory.message(text: 'hello')
# => { 'type' => 'event_callback', 'event' => { 'type' => 'message', ... } }

Creating a slash command payload

payload = BoltRb::Testing::PayloadFactory.command(command: '/deploy', text: 'production')
# => { 'command' => '/deploy', 'text' => 'production', ... }

Class Method Summary collapse

Class Method Details

.action(action_id:, value: nil, user: 'U123TEST', block_id: nil, channel: 'C456TEST') ⇒ Hash

Creates a block_actions payload for interactive components



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bolt_rb/testing/payload_factory.rb', line 89

def action(action_id:, value: nil, user: 'U123TEST', block_id: nil, channel: 'C456TEST')
  {
    'type' => 'block_actions',
    'user' => { 'id' => user },
    'channel' => { 'id' => channel },
    'actions' => [{
      'action_id' => action_id,
      'block_id' => block_id || 'block_1',
      'value' => value
    }.compact],
    'response_url' => 'https://hooks.slack.com/actions/T123/456/xxx',
    'trigger_id' => "trigger_#{SecureRandom.hex(8)}"
  }
end

.app_mention(text:, user: 'U123TEST', channel: 'C456TEST') ⇒ Hash

Creates an app_mention event payload



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bolt_rb/testing/payload_factory.rb', line 50

def app_mention(text:, user: 'U123TEST', channel: 'C456TEST')
  {
    'type' => 'event_callback',
    'event' => {
      'type' => 'app_mention',
      'text' => text,
      'user' => user,
      'channel' => channel,
      'ts' => generate_ts
    }
  }
end

.assistant_message(text:, thread_ts:, user: 'U123TEST', channel: 'D456TEST') ⇒ Hash

Creates a user message inside an assistant thread



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/bolt_rb/testing/payload_factory.rb', line 162

def assistant_message(text:, thread_ts:, user: 'U123TEST', channel: 'D456TEST')
  {
    'type' => 'event_callback',
    'event' => {
      'type' => 'message',
      'channel_type' => 'im',
      'text' => text,
      'user' => user,
      'channel' => channel,
      'ts' => generate_ts,
      'thread_ts' => thread_ts
    }
  }
end

.assistant_thread_context_changed(user: 'U123TEST', channel: 'D456TEST', thread_ts: nil, context: nil) ⇒ Hash

Creates an assistant_thread_context_changed event payload



150
151
152
153
# File 'lib/bolt_rb/testing/payload_factory.rb', line 150

def assistant_thread_context_changed(user: 'U123TEST', channel: 'D456TEST', thread_ts: nil,
                                     context: nil)
  assistant_thread_event('assistant_thread_context_changed', user, channel, thread_ts, context)
end

.assistant_thread_started(user: 'U123TEST', channel: 'D456TEST', thread_ts: nil, context: nil) ⇒ Hash

Creates an assistant_thread_started event payload



139
140
141
# File 'lib/bolt_rb/testing/payload_factory.rb', line 139

def assistant_thread_started(user: 'U123TEST', channel: 'D456TEST', thread_ts: nil, context: nil)
  assistant_thread_event('assistant_thread_started', user, channel, thread_ts, context)
end

.command(command:, text: '', user: 'U123TEST', channel: 'C456TEST') ⇒ Hash

Creates a slash command payload



70
71
72
73
74
75
76
77
78
79
# File 'lib/bolt_rb/testing/payload_factory.rb', line 70

def command(command:, text: '', user: 'U123TEST', channel: 'C456TEST')
  {
    'command' => command,
    'text' => text,
    'user_id' => user,
    'channel_id' => channel,
    'response_url' => 'https://hooks.slack.com/commands/T123/456/xxx',
    'trigger_id' => "trigger_#{SecureRandom.hex(8)}"
  }
end

.message(text:, user: 'U123TEST', channel: 'C456TEST', ts: nil, thread_ts: nil) ⇒ Hash

Creates a message event payload



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bolt_rb/testing/payload_factory.rb', line 30

def message(text:, user: 'U123TEST', channel: 'C456TEST', ts: nil, thread_ts: nil)
  {
    'type' => 'event_callback',
    'event' => {
      'type' => 'message',
      'text' => text,
      'user' => user,
      'channel' => channel,
      'ts' => ts || generate_ts,
      'thread_ts' => thread_ts
    }.compact
  }
end

.shortcut(callback_id:, user: 'U123TEST', type: :global, message_text: nil) ⇒ Hash

Creates a shortcut payload (global or message)



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/bolt_rb/testing/payload_factory.rb', line 111

def shortcut(callback_id:, user: 'U123TEST', type: :global, message_text: nil)
  payload = {
    'type' => type == :message ? 'message_action' : 'shortcut',
    'callback_id' => callback_id,
    'user' => { 'id' => user },
    'trigger_id' => "trigger_#{SecureRandom.hex(8)}"
  }

  if type == :message
    payload['channel'] = { 'id' => 'C456TEST' }
    payload['message'] = {
      'type' => 'message',
      'text' => message_text || 'Original message',
      'user' => 'U789MSG',
      'ts' => generate_ts
    }
  end

  payload
end