0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-14 13:58:53 +08:00
discourse/plugins/automation/spec/requests/admin_discourse_automation_automations_spec.rb
Joffrey JAFFEUX 932bd6ba85
UX: logs when an automation is destroyed (#29565)
A `UserHistory` entry will now be created when an automation is destroyed. This is visible in `/admin/logs/staff_action_logs`. id, name, trigger and script will be logged.

This commit also creates a service `DestroyAutomation` to hold all the destroy automation logic.

---------
Co-authored-by: Martin Brennan <mjrbrennan@gmail.com>
2024-11-04 11:12:18 +09:00

256 lines
7.2 KiB
Ruby
Vendored
Raw Permalink 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
describe DiscourseAutomation::AdminAutomationsController do
fab!(:automation)
before do
SiteSetting.discourse_automation_enabled = true
I18n.backend.store_translations(
:en,
{
discourse_automation: {
scriptables: {
something_about_us: {
title: "Something about us.",
description: "We rock!",
},
},
triggerables: {
title: "Triggerables",
description: "Triggerables",
},
},
},
)
end
after { I18n.backend.reload! }
describe "#show" do
context "when logged in as an admin" do
before { sign_in(Fabricate(:admin)) }
it "shows the automation" do
get "/admin/plugins/discourse-automation/automations/#{automation.id}.json"
expect(response.status).to eq(200)
expect(response.parsed_body["automation"]["id"]).to eq(automation.id)
end
end
context "when logged in as a regular user" do
before { sign_in(Fabricate(:user)) }
it "raises a 404" do
get "/admin/plugins/discourse-automation/automations/#{automation.id}.json"
expect(response.status).to eq(404)
end
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)) }
it "updates the automation" do
put "/admin/plugins/discourse-automation/automations/#{automation.id}.json",
params: {
automation: {
trigger: "another-trigger",
},
}
expect(response.status).to eq(200)
end
describe "invalid fields component" do
it "errors" do
put "/admin/plugins/discourse-automation/automations/#{automation.id}.json",
params: {
automation: {
script: automation.script,
trigger: automation.trigger,
fields: [{ name: "foo", component: "bar" }],
},
}
expect(response.status).to eq(422)
end
end
context "with required field" do
before do
DiscourseAutomation::Scriptable.add("test_required") do
field :foo, component: :text, required: true
end
automation.update!(script: "test_required")
end
it "errors" do
put "/admin/plugins/discourse-automation/automations/#{automation.id}.json",
params: {
automation: {
script: automation.script,
trigger: automation.trigger,
fields: [
{ name: "foo", component: "text", target: "script", metadata: { value: nil } },
],
},
}
expect(response.status).to eq(422)
end
end
context "when changing trigger and script of an enabled automation" do
it "forces the automation to be disabled" do
expect(automation.enabled).to eq(true)
put "/admin/plugins/discourse-automation/automations/#{automation.id}.json",
params: {
automation: {
script: "bar",
trigger: "foo",
enabled: true,
},
}
expect(automation.reload.enabled).to eq(false)
end
end
context "when changing trigger of an enabled automation" do
it "forces the automation to be disabled" do
expect(automation.enabled).to eq(true)
put "/admin/plugins/discourse-automation/automations/#{automation.id}.json",
params: {
automation: {
script: automation.script,
trigger: "foo",
enabled: true,
},
}
expect(automation.reload.enabled).to eq(false)
end
end
context "when changing script of an enabled automation" do
it "disables the automation" do
expect(automation.enabled).to eq(true)
put "/admin/plugins/discourse-automation/automations/#{automation.id}.json",
params: {
automation: {
trigger: automation.trigger,
script: "foo",
enabled: true,
},
}
expect(automation.reload.enabled).to eq(false)
end
end
context "with invalid fields metadata" do
it "errors" do
put "/admin/plugins/discourse-automation/automations/#{automation.id}.json",
params: {
automation: {
script: automation.script,
trigger: automation.trigger,
fields: [{ name: "sender", component: "users", metadata: { baz: 1 } }],
},
}
expect(response.status).to eq(422)
end
end
end
context "when logged in as a regular user" do
before { sign_in(Fabricate(:user)) }
it "raises a 404" do
put "/admin/plugins/discourse-automation/automations/#{automation.id}.json",
params: {
automation: {
trigger: "another-trigger",
},
}
expect(response.status).to eq(404)
end
end
end
describe "#destroy" do
fab!(:automation)
context "when logged in as an admin" do
before { sign_in(Fabricate(:admin)) }
it "destroys the automation" do
delete "/admin/plugins/discourse-automation/automations/#{automation.id}.json"
expect(DiscourseAutomation::Automation.find_by(id: automation.id)).to eq(nil)
end
context "when the automation is not found" do
it "raises a 404" do
delete "/admin/plugins/discourse-automation/automations/999.json"
expect(response.status).to eq(404)
end
end
end
context "when logged in as a regular user" do
before { sign_in(Fabricate(:user)) }
it "raises a 404" do
delete "/admin/plugins/discourse-automation/automations/#{automation.id}.json"
expect(response.status).to eq(404)
end
end
end
end