From c6ca580f1ee6991801d86f5d01a2ea482f24800e Mon Sep 17 00:00:00 2001 From: syed-ali-tw Date: Tue, 12 Nov 2024 20:11:48 +0000 Subject: [PATCH] Add constraint to allow only allowed edition types --- .../allowed_content_types_constraint.rb | 12 ++++++ .../new_design_system_constraint.rb | 5 +++ config/routes.rb | 2 +- test/integration/routes_test.rb | 41 +++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 app/constraints/allowed_content_types_constraint.rb create mode 100644 app/constraints/new_design_system_constraint.rb diff --git a/app/constraints/allowed_content_types_constraint.rb b/app/constraints/allowed_content_types_constraint.rb new file mode 100644 index 000000000..5765bd79c --- /dev/null +++ b/app/constraints/allowed_content_types_constraint.rb @@ -0,0 +1,12 @@ +class AllowedContentTypesConstraint + def initialize(allowed_content_types) + @allowed_content_types = allowed_content_types + end + + def matches?(request) + request_path_parameters = "action_dispatch.request.path_parameters" + if request.env[request_path_parameters] && request.env[request_path_parameters][:id] + @allowed_content_types.include?(Edition.find(request.env[request_path_parameters][:id]).class) + end + end +end diff --git a/app/constraints/new_design_system_constraint.rb b/app/constraints/new_design_system_constraint.rb new file mode 100644 index 000000000..3682fe2d6 --- /dev/null +++ b/app/constraints/new_design_system_constraint.rb @@ -0,0 +1,5 @@ +class NewDesignSystemConstraint + def matches?(request) + AllowedContentTypesConstraint.new([AnswerEdition, HelpPageEdition]).matches?(request) && FeatureConstraint.new("design_system_edit").matches?(request) + end +end diff --git a/config/routes.rb b/config/routes.rb index 702ec8bb0..d26998815 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,7 +16,7 @@ resources :artefacts, only: %i[new create update] - constraints FeatureConstraint.new("design_system_edit") do + constraints NewDesignSystemConstraint.new do resources :editions do member do get "metadata" diff --git a/test/integration/routes_test.rb b/test/integration/routes_test.rb index d73d54216..d3b3f0e21 100644 --- a/test/integration/routes_test.rb +++ b/test/integration/routes_test.rb @@ -11,4 +11,45 @@ class RoutesTest < LegacyIntegrationTest should "route to new downtimes controller new downtime" do assert_routing("/editions/1/downtime/new", controller: "downtimes", action: "new", edition_id: "1") end + + # rubocop:disable Rails/SaveBang + context "new design system" do + setup do + @test_strategy = Flipflop::FeatureSet.current.test! + @test_strategy.switch!(:design_system_edit, true) + end + + context "allowed content types" do + %i[answer_edition help_page_edition].each do |content_type| + context content_type do + setup do + @edition = FactoryBot.create(content_type) + end + + should "route to editions controller" do + assert_routing("/editions/#{@edition.id}", controller: "editions", action: "show", id: @edition.id.to_s) + end + end + end + end + + context "not allowed content types" do + %i[guide_edition local_transaction_edition completed_transaction_edition place_edition simple_smart_answer_edition transaction_edition].each do |content_type| + context content_type do + setup do + @edition = FactoryBot.create(content_type) + end + + should "route to legacy editions controller" do + assert_routing("/editions/#{@edition.id}", controller: "legacy_editions", action: "show", id: @edition.id.to_s) + end + end + end + end + + teardown do + @test_strategy.switch!(:design_system_edit, false) + end + end + # rubocop:enable Rails/SaveBang end