discourse/plugins/automation/spec/serializers/automation_serializer_spec.rb
Osama Sayegh 3d4faf3272
FEATURE: Merge discourse-automation (#26432)
Automation (previously known as discourse-automation) is now a core plugin.
2024-04-03 18:20:43 +03:00

118 lines
3.4 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
require_relative "../discourse_automation_helper"
describe DiscourseAutomation::AutomationSerializer do
fab!(:user)
fab!(:automation) do
Fabricate(
:automation,
script: DiscourseAutomation::Scripts::FLAG_POST_ON_WORDS,
trigger: DiscourseAutomation::Triggers::POST_CREATED_EDITED,
)
end
context "when there are pending automations" do
before { automation.pending_automations.create!(execute_at: 2.hours.from_now) }
it "has a next_pending_automation_at field" do
serializer =
DiscourseAutomation::AutomationSerializer.new(
automation,
scope: Guardian.new(user),
root: false,
)
expect(serializer.next_pending_automation_at).to be_within_one_minute_of(2.hours.from_now)
end
end
context "when has no pending automation" do
it "doesnt have a next_pending_automation_at field" do
serializer =
DiscourseAutomation::AutomationSerializer.new(
automation,
scope: Guardian.new(user),
root: false,
)
expect(serializer.next_pending_automation_at).to_not be
end
end
context "when script with fields limited to a specific trigger" do
before do
DiscourseAutomation::Scriptable.add("foo") do
field :bar, component: :text, triggerable: DiscourseAutomation::Triggers::TOPIC
end
end
context "when automation is not using the specific trigger" do
fab!(:automation) do
Fabricate(
:automation,
script: "foo",
trigger: DiscourseAutomation::Triggers::POST_CREATED_EDITED,
)
end
it "filters the field" do
serializer =
DiscourseAutomation::AutomationSerializer.new(
automation,
scope: Guardian.new(user),
root: false,
)
expect(serializer.script[:templates]).to eq([])
end
end
context "when automation is using the specific trigger" do
fab!(:automation) do
Fabricate(:automation, script: "foo", trigger: DiscourseAutomation::Triggers::TOPIC)
end
it "doesnt filter the field" do
serializer =
DiscourseAutomation::AutomationSerializer.new(
automation,
scope: Guardian.new(user),
root: false,
)
expect(serializer.script[:templates].length).to eq(1)
expect(serializer.script[:templates].first[:name]).to eq(:bar)
end
end
end
describe "#placeholders" do
before do
DiscourseAutomation::Scriptable.add("foo_bar") do
version 1
placeholder :foo
placeholder :bar
placeholder :bar, triggerable: :user_updated
placeholder :bar, triggerable: :user_updated
placeholder :bar, triggerable: :something
placeholder(triggerable: :user_updated) { :cool }
placeholder(triggerable: :whatever) { :not_cool }
placeholder { "Why not" }
triggerables %i[user_updated something whatever]
end
end
fab!(:automation) { Fabricate(:automation, script: :foo_bar, trigger: :user_updated) }
it "correctly renders placeholders" do
serializer =
DiscourseAutomation::AutomationSerializer.new(
automation,
scope: Guardian.new(user),
root: false,
)
expect(serializer.placeholders).to eq(%w[foo bar cool why_not])
end
end
end