From 252dcfbfa6705a47e86b375d5307b9bab49609d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 30 Sep 2024 17:42:00 +0200 Subject: [PATCH] FIX: create an automation with forced_triggerable enabled (#29030) When trying to create a new automation based on a scriptable that has "force_triggerable" enable, it would break because of a typo in the code. This fixes the typo and add a spec to ensure this code path is tested. --- .../admin_automations_controller.rb | 2 +- ...n_discourse_automation_automations_spec.rb | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/plugins/automation/app/controllers/discourse_automation/admin_automations_controller.rb b/plugins/automation/app/controllers/discourse_automation/admin_automations_controller.rb index e62e39f71fa..70721e9eeac 100644 --- a/plugins/automation/app/controllers/discourse_automation/admin_automations_controller.rb +++ b/plugins/automation/app/controllers/discourse_automation/admin_automations_controller.rb @@ -29,7 +29,7 @@ module DiscourseAutomation ) if automation.scriptable&.forced_triggerable - automation.trigger = scriptable.forced_triggerable[:triggerable].to_s + automation.trigger = automation.scriptable.forced_triggerable[:triggerable].to_s end automation.save! diff --git a/plugins/automation/spec/requests/admin_discourse_automation_automations_spec.rb b/plugins/automation/spec/requests/admin_discourse_automation_automations_spec.rb index 0ab1b3f578b..79647fb9e17 100644 --- a/plugins/automation/spec/requests/admin_discourse_automation_automations_spec.rb +++ b/plugins/automation/spec/requests/admin_discourse_automation_automations_spec.rb @@ -48,6 +48,48 @@ describe DiscourseAutomation::AdminAutomationsController do end end + describe "#create" do + let(:script) { "forced_triggerable" } + + before do + DiscourseAutomation::Scriptable.add(script) do + triggerable! :recurring, { recurrence: { interval: 1, frequency: "day" } } + end + end + + after { DiscourseAutomation::Scriptable.remove(script) } + + context "when logged in as an admin" do + before { sign_in(Fabricate(:admin)) } + + it "creates the 'forced triggerable' automation" do + post "/admin/plugins/discourse-automation/automations.json", + params: { + automation: { + name: "foobar", + script:, + }, + } + expect(response.status).to eq(200) + end + end + + context "when logged in as a regular user" do + before { sign_in(Fabricate(:user)) } + + it "raises a 404" do + post "/admin/plugins/discourse-automation/automations.json", + params: { + automation: { + name: "foobar", + script:, + }, + } + expect(response.status).to eq(404) + end + end + end + describe "#update" do context "when logged in as an admin" do before { sign_in(Fabricate(:admin)) }