From 0634428f3068c369dcbb9bbf1e40047d0ceccc42 Mon Sep 17 00:00:00 2001 From: Sean Rankine Date: Fri, 30 Aug 2024 15:59:13 +0100 Subject: [PATCH] Unset the backend_id for redirects and gone routes Backends should only be set for routes with the "backend" handler. Redirects and gone routes are handled directly in router and are not proxied. This creates confusion in the data model, as redirect can currently have a backend (however, its not sent to the backend). --- app/models/route.rb | 5 +++++ spec/models/route_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/app/models/route.rb b/app/models/route.rb index 010330d9..59efbb9e 100644 --- a/app/models/route.rb +++ b/app/models/route.rb @@ -36,6 +36,7 @@ class Route end before_validation :default_segments_mode + before_save :remove_backend_id_for_non_backend_routes after_create :cleanup_child_gone_routes scope :excluding, ->(route) { where(id: { :$ne => route.id }) } @@ -132,6 +133,10 @@ def validate_backend_id end end + def remove_backend_id_for_non_backend_routes + self.backend_id = nil unless backend? + end + def cleanup_child_gone_routes return unless route_type == "prefix" diff --git a/spec/models/route_spec.rb b/spec/models/route_spec.rb index aeb3cf85..1ab81c98 100644 --- a/spec/models/route_spec.rb +++ b/spec/models/route_spec.rb @@ -215,6 +215,12 @@ end end + describe "backend_id" do + it "is set to nil" do + expect(route.backend_id).to be nil + end + end + context "and segments_mode set to 'ignore'" do subject(:route) { FactoryBot.build(:redirect_route, segments_mode: "ignore") } @@ -249,6 +255,20 @@ end end + describe "changing backend route to redirect route" do + it "will clear the backend_id" do + route = FactoryBot.create(:backend_route) + route.update!( + handler: "redirect", + redirect_to: "/", + redirect_type: "permanent", + ) + route.reload + + expect(route.backend_id).to be nil + end + end + describe "as_json" do subject(:route) { FactoryBot.build(:redirect_route) }