2024-11-04 10:12:18 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-11-22 01:16:06 +08:00
|
|
|
RSpec.describe DiscourseAutomation::Destroy do
|
2024-11-04 10:12:18 +08:00
|
|
|
describe described_class::Contract, type: :model do
|
|
|
|
it { is_expected.to validate_presence_of :automation_id }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".call" do
|
|
|
|
subject(:result) { described_class.call(params:, **dependencies) }
|
|
|
|
|
|
|
|
fab!(:user) { Fabricate(:admin) }
|
|
|
|
fab!(:automation) { Fabricate(:automation) }
|
|
|
|
|
|
|
|
let(:guardian) { user.guardian }
|
|
|
|
let(:params) { { automation_id: automation.id } }
|
|
|
|
let(:dependencies) { { guardian: } }
|
|
|
|
|
2024-11-22 01:16:06 +08:00
|
|
|
context "when user can't destroy the automation" do
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it { is_expected.to fail_a_policy(:can_destroy_automation) }
|
2024-11-04 10:12:18 +08:00
|
|
|
end
|
|
|
|
|
2024-11-22 01:16:06 +08:00
|
|
|
context "when data is invalid" do
|
|
|
|
before { params[:automation_id] = nil }
|
|
|
|
|
|
|
|
it { is_expected.to fail_a_contract }
|
2024-11-04 10:12:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when the automation is not found" do
|
|
|
|
before { params[:automation_id] = 999 }
|
|
|
|
|
|
|
|
it { is_expected.to fail_to_find_a_model(:automation) }
|
|
|
|
end
|
|
|
|
|
2024-11-22 01:16:06 +08:00
|
|
|
context "when everything's ok" do
|
|
|
|
it "logs the action" do
|
|
|
|
expect { result }.to change { UserHistory.count }.by(1)
|
|
|
|
expect(UserHistory.last.details).to eq(
|
|
|
|
"id: #{automation.id}\nname: #{automation.name}\nscript: #{automation.script}\ntrigger: #{automation.trigger}",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "destroys the automation" do
|
|
|
|
expect { result }.to change { DiscourseAutomation::Automation.count }.by(-1)
|
|
|
|
end
|
2024-11-04 10:12:18 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|