ubicloud/spec/routes/api/cli/fw/modify-rule_spec.rb
Jeremy Evans 3f4d2ee998 Support modifying firewall rules in web UI and API/SDK/CLI
This is loosely based on the postgres firewall rule modification
support.

This introduces a new pattern, where web paths do not exactly match
api paths. The API uses PATCH (as postgres firewall rules did), but
the web uses POST. This allows the web portion to work without
javascript, so normal testing with capybara works. I think it would
be good to expand the usage of this pattern to the rest of the web
routes and views, but that's a future change.

Inside of an inline edit component (which requires javascript), just
use text boxes for each input and normal form submission buttons.
2025-10-29 04:32:41 +09:00

56 lines
2 KiB
Ruby

# frozen_string_literal: true
require_relative "../spec_helper"
RSpec.describe Clover, "cli fw modify-rule" do
before do
cli(%w[fw eu-central-h1/test-fw create])
cli(%W[fw eu-central-h1/test-fw add-rule 1.2.3.0/24])
@fwr = FirewallRule.first
end
it "can support all options" do
body = cli(%W[fw eu-central-h1/test-fw modify-rule -c 1.2.4.0/24 -s 1 -e 2 -d my-desc #{@fwr.ubid}])
expect(body).to eq "Modified firewall rule with id: #{@fwr.ubid}\n"
@fwr.reload
expect(@fwr.cidr.to_s).to eq "1.2.4.0/24"
expect(@fwr.port_range.to_range).to eq(1...3)
expect(@fwr.description).to eq "my-desc"
end
it "can modify cidr with -c" do
body = cli(%W[fw eu-central-h1/test-fw modify-rule -c 1.2.4.0/24 #{@fwr.ubid}])
expect(body).to eq "Modified firewall rule with id: #{@fwr.ubid}\n"
@fwr.reload
expect(@fwr.cidr.to_s).to eq "1.2.4.0/24"
expect(@fwr.port_range.to_range).to eq(0...65536)
expect(@fwr.description).to be_nil
end
it "can modify port range with -s" do
body = cli(%W[fw eu-central-h1/test-fw modify-rule -s 1 #{@fwr.ubid}])
expect(body).to eq "Modified firewall rule with id: #{@fwr.ubid}\n"
@fwr.reload
expect(@fwr.cidr.to_s).to eq "1.2.3.0/24"
expect(@fwr.port_range.to_range).to eq(1...2)
expect(@fwr.description).to be_nil
end
it "can modify port range with -e" do
body = cli(%W[fw eu-central-h1/test-fw modify-rule -e 2 #{@fwr.ubid}])
expect(body).to eq "Modified firewall rule with id: #{@fwr.ubid}\n"
@fwr.reload
expect(@fwr.cidr.to_s).to eq "1.2.3.0/24"
expect(@fwr.port_range.to_range).to eq(0...3)
expect(@fwr.description).to be_nil
end
it "can modify description with -d" do
body = cli(%W[fw eu-central-h1/test-fw modify-rule -d my-desc #{@fwr.ubid}])
expect(body).to eq "Modified firewall rule with id: #{@fwr.ubid}\n"
@fwr.reload
expect(@fwr.cidr.to_s).to eq "1.2.3.0/24"
expect(@fwr.port_range.to_range).to eq(0...65536)
expect(@fwr.description).to eq "my-desc"
end
end