Files
once-campfire/test/models/webhook_test.rb
T
Marcello Costagliola 6288a10633 Post a webhook reply as an attachment only when the bot answered 200
A bot's reply becomes a message when the status is 200 and the type is text,
and an attachment otherwise, but the attachment branch never looked at the
status. Whenever a bot's endpoint failed, its error page landed in the room as
a file: a proxy's 502 page as attachment.html, a 404 as attachment.text.

Apply the text branch's 200 check to attachments too. The error reply test
answered without a content type, which skipped the attachment branch, so it
now answers with an HTML error page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JVFo3Lt9T8M5NR7KxVvsZ2
2026-10-05 02:58:11 +02:00

57 lines
2.3 KiB
Ruby

require "test_helper"
class WebhookTest < ActiveSupport::TestCase
test "payload" do
message = messages(:first)
message_path = Rails.application.routes.url_helpers.room_at_message_path(message.room, message)
bot_messages_path = Rails.application.routes.url_helpers.room_bot_messages_path(message.room, users(:bender).bot_key)
WebMock.stub_request(:post, webhooks(:bender).url).
with(body: hash_including(
user: { id: message.creator.id, name: message.creator.name },
room: { id: message.room.id, name: message.room.name, path: bot_messages_path },
message: { id: message.id, body: { html: "First post!", plain: "First post!" }, path: message_path },
))
response = webhooks(:bender).deliver(messages(:first))
assert_equal 200, response.code.to_i
end
test "delivery" do
WebMock.stub_request(:post, webhooks(:bender).url).to_return(status: 200, body: "", headers: {})
response = webhooks(:bender).deliver(messages(:first))
assert_equal 200, response.code.to_i
end
test "delivery with OK text reply" do
WebMock.stub_request(:post, webhooks(:bender).url).to_return(status: 200, body: "Hello back!", headers: { "Content-Type" => "text/plain" })
response = webhooks(:bender).deliver(messages(:first))
reply_message = Message.last
assert_equal "Hello back!", reply_message.body.to_plain_text
end
test "delivery with OK attachment reply" do
WebMock.stub_request(:post, webhooks(:bender).url).to_return(status: 200, body: file_fixture("moon.jpg"), headers: { "Content-Type" => "image/jpeg" })
response = webhooks(:bender).deliver(messages(:first))
reply_message = Message.last
assert reply_message.attachment.present?
end
test "delivery with error reply" do
assert_no_difference -> { Message.count } do
WebMock.stub_request(:post, webhooks(:bender).url).to_return(status: 500, body: "<h1>Internal Error!</h1>", headers: { "Content-Type" => "text/html" })
response = webhooks(:bender).deliver(messages(:first))
end
end
test "delivery that times out" do
Webhook.any_instance.stubs(:post).raises(Net::OpenTimeout)
response = webhooks(:bender).deliver(messages(:first))
reply_message = Message.last
assert_equal "Failed to respond within 7 seconds", reply_message.body.to_plain_text
end
end