2024-07-03 06:45:37 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe(Flags::UpdateFlag) do
|
|
|
|
subject(:result) do
|
|
|
|
described_class.call(
|
|
|
|
id: flag.id,
|
|
|
|
guardian: current_user.guardian,
|
|
|
|
name: name,
|
|
|
|
description: description,
|
|
|
|
applies_to: applies_to,
|
2024-07-18 08:10:22 +08:00
|
|
|
require_message: require_message,
|
2024-07-03 06:45:37 +08:00
|
|
|
enabled: enabled,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-08-28 20:54:10 +08:00
|
|
|
fab!(:flag)
|
|
|
|
|
2024-07-03 06:45:37 +08:00
|
|
|
after { flag.destroy }
|
|
|
|
|
|
|
|
let(:name) { "edited custom flag name" }
|
|
|
|
let(:description) { "edited custom flag description" }
|
|
|
|
let(:applies_to) { ["Topic"] }
|
2024-07-18 08:10:22 +08:00
|
|
|
let(:require_message) { true }
|
2024-07-03 06:45:37 +08:00
|
|
|
let(:enabled) { false }
|
|
|
|
|
|
|
|
context "when user is not allowed to perform the action" do
|
|
|
|
fab!(:current_user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it { is_expected.to fail_a_policy(:invalid_access) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when applies to is invalid" do
|
|
|
|
fab!(:current_user) { Fabricate(:admin) }
|
|
|
|
let(:applies_to) { ["User"] }
|
|
|
|
|
|
|
|
it { is_expected.to fail_a_contract }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when title is empty" do
|
|
|
|
fab!(:current_user) { Fabricate(:admin) }
|
|
|
|
let(:name) { nil }
|
|
|
|
|
|
|
|
it { is_expected.to fail_a_contract }
|
|
|
|
end
|
|
|
|
|
2024-09-30 07:17:19 +08:00
|
|
|
context "when title is not unique" do
|
|
|
|
fab!(:current_user) { Fabricate(:admin) }
|
|
|
|
fab!(:flag_2) { Fabricate(:flag, name: "edited custom flag name") }
|
|
|
|
|
|
|
|
it { is_expected.to fail_a_policy(:unique_name) }
|
|
|
|
|
|
|
|
after { Flag.destroy_by(name: "edited custom flag name") }
|
|
|
|
end
|
|
|
|
|
2024-07-03 06:45:37 +08:00
|
|
|
context "when title is too long" do
|
|
|
|
fab!(:current_user) { Fabricate(:admin) }
|
|
|
|
let(:name) { "a" * 201 }
|
|
|
|
|
|
|
|
it { is_expected.to fail_a_contract }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when description is empty" do
|
|
|
|
fab!(:current_user) { Fabricate(:admin) }
|
|
|
|
let(:description) { nil }
|
|
|
|
|
|
|
|
it { is_expected.to fail_a_contract }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when description is too long" do
|
|
|
|
fab!(:current_user) { Fabricate(:admin) }
|
|
|
|
let(:description) { "a" * 1001 }
|
|
|
|
|
|
|
|
it { is_expected.to fail_a_contract }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is allowed to perform the action" do
|
|
|
|
fab!(:current_user) { Fabricate(:admin) }
|
|
|
|
|
2024-08-28 20:54:10 +08:00
|
|
|
it { is_expected.to run_successfully }
|
2024-07-03 06:45:37 +08:00
|
|
|
|
|
|
|
it "updates the flag" do
|
|
|
|
result
|
|
|
|
expect(flag.reload.name).to eq("edited custom flag name")
|
|
|
|
expect(flag.description).to eq("edited custom flag description")
|
|
|
|
expect(flag.applies_to).to eq(["Topic"])
|
2024-07-18 08:10:22 +08:00
|
|
|
expect(flag.require_message).to be true
|
2024-07-03 06:45:37 +08:00
|
|
|
expect(flag.enabled).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "logs the action" do
|
|
|
|
expect { result }.to change { UserHistory.count }.by(1)
|
|
|
|
expect(UserHistory.last).to have_attributes(
|
|
|
|
custom_type: "update_flag",
|
|
|
|
details:
|
2024-07-18 08:10:22 +08:00
|
|
|
"name: edited custom flag name\ndescription: edited custom flag description\napplies_to: [\"Topic\"]\nrequire_message: true\nenabled: false",
|
2024-07-03 06:45:37 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|