0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-11 02:59:07 +08:00
discourse/plugins/automation/spec/triggers/post_flag_created_spec.rb
Sam e67d9b5f66
FEATURE: New automation for emailing specific users when a post is flagged (#36580)
This commit adds a new "Email on flagged post" automation which can be configured to send emails to users when a post is flagged. The automation can be configured to trigger only when posts are flagged in certain categories or only when certain tags are present on the post's topic.

The body of the email sent can also be configured with the following placeholders available: 

* topic_url
* topic_title
* post_url
* post_number
* flagger_username
* flagged_username
* flag_type
* category
* tags
* post_excerpt
2025-12-11 10:31:13 +08:00

82 lines
2.9 KiB
Ruby
Vendored

# frozen_string_literal: true
describe DiscourseAutomation::Triggers::POST_FLAG_CREATED do
fab!(:flagger) { Fabricate(:user, trust_level: TrustLevel[2]) }
fab!(:second_flagger) { Fabricate(:user, trust_level: TrustLevel[2]) }
fab!(:category)
fab!(:tag)
fab!(:topic) { Fabricate(:topic, category: category, tags: [tag]) }
fab!(:post) { Fabricate(:post, topic: topic) }
fab!(:automation) do
Fabricate(:automation, trigger: DiscourseAutomation::Triggers::POST_FLAG_CREATED)
end
before { SiteSetting.discourse_automation_enabled = true }
it "ignores the tags field if tagging is disabled" do
SiteSetting.tagging_enabled = false
automation.upsert_field!("tags", "tags", { value: [tag.name] }, target: "trigger")
expect do
result = PostActionCreator.spam(flagger, Fabricate(:post, topic: Fabricate(:topic)))
expect(result.success).to eq(true)
end.to change { automation.reload.stats.count }.by(1)
end
it "only triggers the automation if flagged post is in a topic with the specified tags defined by the tags field" do
automation.upsert_field!("tags", "tags", { value: [tag.name] }, target: "trigger")
post_action_id = nil
triggered_automations =
capture_contexts do
expect do
result = PostActionCreator.spam(flagger, post)
expect(result.success).to eq(true)
post_action_id = result.post_action.id
result = PostActionCreator.spam(flagger, Fabricate(:post, topic: Fabricate(:topic)))
expect(result.success).to eq(true)
end.to change { automation.reload.stats.count }.by(1)
end
expect(triggered_automations.length).to eq(1)
triggered_automation = triggered_automations.first
expect(triggered_automation["kind"]).to eq(DiscourseAutomation::Triggers::POST_FLAG_CREATED)
expect(triggered_automation["post_action_id"]).to eq(post_action_id)
end
it "only triggers the automation if flagged post is in a topic with the specified categories defined by the categories field" do
automation.upsert_field!(
"categories",
"categories",
{ value: [category.id] },
target: "trigger",
)
post_action_id = nil
triggered_automations =
capture_contexts do
expect do
result = PostActionCreator.spam(flagger, post)
expect(result.success).to eq(true)
post_action_id = result.post_action.id
result = PostActionCreator.spam(flagger, Fabricate(:post, topic: Fabricate(:topic)))
expect(result.success).to eq(true)
end.to change { automation.reload.stats.count }.by(1)
end
expect(triggered_automations.length).to eq(1)
triggered_automation = triggered_automations.first
expect(triggered_automation["kind"]).to eq(DiscourseAutomation::Triggers::POST_FLAG_CREATED)
expect(triggered_automation["post_action_id"]).to eq(post_action_id)
end
end