0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-09 21:45:25 +08:00
discourse/plugins/discourse-zendesk-plugin/spec/requests/sync_controller_spec.rb
Arpit Jalan 96a277d2c1
FIX: preserve Zendesk webhook email attribution (#40115)
Previously, Zendesk sync resolved the webhook `email` before checking
whether the incoming comment needed to create a new post, and the
intended attribution behavior was not covered by a request spec.

This change moves that lookup to the post creation path and adds
coverage confirming that a valid Zendesk webhook with a matching `email`
creates the synced comment as that Discourse user.
2026-05-18 21:04:47 +05:30

87 lines
2.8 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe DiscourseZendeskPlugin::SyncController do
fab!(:category)
fab!(:topic) { Fabricate(:topic, category: category) }
fab!(:webhook_user, :user)
describe "#webhook" do
let(:token) { "secret-token" }
let(:ticket_id) { 12 }
let(:zendesk_url_default) { "https://your-url.zendesk.com/api/v2" }
let(:default_header) { { "Content-Type" => "application/json; charset=UTF-8" } }
let(:comment_id) { 321 }
let(:comment_body) { "trusted zendesk comment" }
let(:comment_response) do
{ comments: [{ id: comment_id, body: comment_body, public: true }] }.to_json
end
before do
SiteSetting.zendesk_enabled = true
SiteSetting.sync_comments_from_zendesk = true
SiteSetting.zendesk_incoming_webhook_token = token
end
it "raises an error when the token is missing" do
put "/zendesk-plugin/sync.json"
expect(response.status).to eq(400)
end
it "raises an error when the token is invalid" do
put "/zendesk-plugin/sync.json", params: { token: "token" }
expect(response.status).to eq(403)
end
it "raises an error if the plugin is disabled" do
SiteSetting.zendesk_enabled = false
put "/zendesk-plugin/sync.json", params: { token: token }
expect(response.status).to eq(404)
end
it "raises an error if `sync_comments_from_zendesk` is disabled" do
SiteSetting.sync_comments_from_zendesk = false
put "/zendesk-plugin/sync.json", params: { token: token }
expect(response.status).to eq(422)
end
it "raises an error if required parameters are missing" do
put "/zendesk-plugin/sync.json", params: { token: token, topic_id: topic.id }
expect(response.status).to eq(400)
end
it "raises an error when topic is not present" do
topic.destroy!
put "/zendesk-plugin/sync.json",
params: {
token: token,
topic_id: topic.id,
ticket_id: ticket_id,
}
expect(response.status).to eq(400)
end
it "attributes the synced comment to the authenticated webhook email" do
SiteSetting.zendesk_autogenerate_categories = category.id.to_s
stub_request(:get, "#{zendesk_url_default}/tickets/#{ticket_id}/comments").to_return(
status: 200,
body: comment_response,
headers: default_header,
)
expect {
put "/zendesk-plugin/sync.json",
params: {
token: token,
topic_id: topic.id,
ticket_id: ticket_id,
email: webhook_user.email,
}
}.to change { topic.reload.posts.count }.by(1)
expect(response.status).to eq(204)
expect(response.body).to be_blank
expect(topic.reload.posts.last.raw).to eq(comment_body)
expect(topic.posts.last.user).to eq(webhook_user)
end
end
end