From b6153fc1552d63aafe912d58d3473446c723a7c2 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 25 Feb 2025 15:49:54 +1100 Subject: [PATCH] Rescue from invalid DFC catalog URLs This mistake should be dealt with differently but I wanted to make sure that a really invalid URL is handled well, too. --- app/controllers/admin/dfc_product_imports_controller.rb | 3 +++ config/locales/en.yml | 1 + spec/system/admin/dfc_product_import_spec.rb | 8 ++++++++ 3 files changed, 12 insertions(+) diff --git a/app/controllers/admin/dfc_product_imports_controller.rb b/app/controllers/admin/dfc_product_imports_controller.rb index a95e24ffdb..df2cec0347 100644 --- a/app/controllers/admin/dfc_product_imports_controller.rb +++ b/app/controllers/admin/dfc_product_imports_controller.rb @@ -27,6 +27,9 @@ module Admin @enterprise.supplied_variants.linked_to(subject.semanticId)&.product ] end + rescue URI::InvalidURIError + flash[:error] = t ".invalid_url" + redirect_to admin_product_import_path rescue Faraday::Error, Addressable::URI::InvalidURIError, ActionController::ParameterMissing => e diff --git a/config/locales/en.yml b/config/locales/en.yml index e6c3df1973..eeecb2cb65 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -862,6 +862,7 @@ en: one: "1 selected" other: "%{count} selected" import: Import + invalid_url: This catalog URL is not valid. import: title: "DFC product catalog import" imported_products: "Imported products:" diff --git a/spec/system/admin/dfc_product_import_spec.rb b/spec/system/admin/dfc_product_import_spec.rb index 7955217ea7..8e45169409 100644 --- a/spec/system/admin/dfc_product_import_spec.rb +++ b/spec/system/admin/dfc_product_import_spec.rb @@ -22,7 +22,15 @@ RSpec.describe "DFC Product Import" do # We are testing against our own catalog for now but we want to replace # this with the URL of another app when available. + # We also add a common mistake: copying the URL with an extra space. host = Rails.application.default_url_options[:host] + url = " http://#{host}/api/dfc/enterprises/#{enterprise.id}/catalog_items" + fill_in "catalog_url", with: url + select enterprise.name, from: "Create products for enterprise" + click_button "Preview" + + expect(page).to have_content "This catalog URL is not valid" + url = "http://#{host}/api/dfc/enterprises/#{enterprise.id}/catalog_items" fill_in "catalog_url", with: url select enterprise.name, from: "Create products for enterprise"