FIX: correctly checks for chat enabled in incoming webhooks controller (#20730)

This commit also moves the spec into the correct folder
This commit is contained in:
Joffrey JAFFEUX 2023-03-20 09:42:36 +01:00 committed by GitHub
parent 0364ef5efe
commit 25d06faa8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 5 deletions

View File

@ -2,6 +2,8 @@
module Chat module Chat
class IncomingWebhooksController < ::ApplicationController class IncomingWebhooksController < ::ApplicationController
requires_plugin Chat::PLUGIN_NAME
WEBHOOK_MESSAGES_PER_MINUTE_LIMIT = 10 WEBHOOK_MESSAGES_PER_MINUTE_LIMIT = 10
skip_before_action :verify_authenticity_token, :redirect_to_login_if_required skip_before_action :verify_authenticity_token, :redirect_to_login_if_required

View File

@ -8,7 +8,18 @@ RSpec.describe Chat::IncomingWebhooksController do
before { SiteSetting.chat_debug_webhook_payloads = true } before { SiteSetting.chat_debug_webhook_payloads = true }
let(:valid_payload) { { text: "A new signup woo!" } }
describe "#create_message" do describe "#create_message" do
context "with chat disabled" do
before { SiteSetting.chat_enabled = false }
it "returns a 404" do
post "/chat/hooks/#{webhook.key}.json", params: valid_payload
expect(response.status).to eq(404)
end
end
it "errors with invalid key" do it "errors with invalid key" do
post "/chat/hooks/null.json" post "/chat/hooks/null.json"
expect(response.status).to eq(400) expect(response.status).to eq(400)
@ -28,9 +39,9 @@ RSpec.describe Chat::IncomingWebhooksController do
end end
it "creates a new chat message" do it "creates a new chat message" do
expect { expect { post "/chat/hooks/#{webhook.key}.json", params: valid_payload }.to change {
post "/chat/hooks/#{webhook.key}.json", params: { text: "A new signup woo!" } Chat::Message.where(chat_channel: chat_channel).count
}.to change { Chat::Message.where(chat_channel: chat_channel).count }.by(1) }.by(1)
expect(response.status).to eq(200) expect(response.status).to eq(200)
chat_webhook_event = Chat::WebhookEvent.last chat_webhook_event = Chat::WebhookEvent.last
expect(chat_webhook_event.chat_message_id).to eq(Chat::Message.last.id) expect(chat_webhook_event.chat_message_id).to eq(Chat::Message.last.id)
@ -62,15 +73,24 @@ RSpec.describe Chat::IncomingWebhooksController do
it "rate limits" do it "rate limits" do
RateLimiter.enable RateLimiter.enable
RateLimiter.clear_all! RateLimiter.clear_all!
10.times { post "/chat/hooks/#{webhook.key}.json", params: { text: "A new signup woo!" } } 10.times { post "/chat/hooks/#{webhook.key}.json", params: valid_payload }
expect(response.status).to eq(200) expect(response.status).to eq(200)
post "/chat/hooks/#{webhook.key}.json", params: { text: "A new signup woo!" } post "/chat/hooks/#{webhook.key}.json", params: valid_payload
expect(response.status).to eq(429) expect(response.status).to eq(429)
end end
end end
describe "#create_message_slack_compatible" do describe "#create_message_slack_compatible" do
context "with chat disabled" do
before { SiteSetting.chat_enabled = false }
it "returns a 404" do
post "/chat/hooks/#{webhook.key}/slack.json", params: valid_payload
expect(response.status).to eq(404)
end
end
it "processes the text param with SlackCompatibility" do it "processes the text param with SlackCompatibility" do
expect { expect {
post "/chat/hooks/#{webhook.key}/slack.json", params: { text: "A new signup woo <!here>!" } post "/chat/hooks/#{webhook.key}/slack.json", params: { text: "A new signup woo <!here>!" }