From ced8f8aa589250151ab97d6290d51cc85b6f45ba Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Wed, 31 May 2023 11:46:29 +0200 Subject: [PATCH 1/4] Use `shared_examples` as we will test many URLs later on "It's a Surprise Tool That Will Help Us Later" --- spec/system/admin/enterprises_spec.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/spec/system/admin/enterprises_spec.rb b/spec/system/admin/enterprises_spec.rb index 2acbccf544..6b59886dfa 100644 --- a/spec/system/admin/enterprises_spec.rb +++ b/spec/system/admin/enterprises_spec.rb @@ -698,12 +698,18 @@ describe ' expect(distributor1.white_label_logo).to_not be_attached end - it "can edit the text field white_label_logo_link" do - fill_in "enterprise_white_label_logo_link", with: "https://www.openfoodnetwork.org" - click_button 'Update' - expect(flash_message) - .to eq('Enterprise "First Distributor" has been successfully updated!') - expect(distributor1.reload.white_label_logo_link).to eq("https://www.openfoodnetwork.org") + shared_examples "edit link with" do |url, result| + it "url: #{url}" do + fill_in "enterprise_white_label_logo_link", with: url + click_button 'Update' + expect(flash_message) + .to eq('Enterprise "First Distributor" has been successfully updated!') + expect(distributor1.reload.white_label_logo_link).to eq(result) + end + end + + context "can edit white label logo link" do + it_behaves_like "edit link with", "https://www.openfoodnetwork.org", "https://www.openfoodnetwork.org" end end From 880ba85791ea1fa002a50208dde4262a930c1494 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 1 Jun 2023 17:36:59 +0200 Subject: [PATCH 2/4] Add `http` to white label logo link if this do not start with `http` --- app/models/enterprise.rb | 8 ++++++++ spec/system/admin/enterprises_spec.rb | 2 ++ 2 files changed, 10 insertions(+) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 9ef3176274..789884ce54 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -317,6 +317,14 @@ class Enterprise < ApplicationRecord ) end + def white_label_logo_link + return nil if self[:white_label_logo_link].blank? + + return self[:white_label_logo_link] if self[:white_label_logo_link].start_with?('http') + + "http://#{self[:white_label_logo_link]}" + end + def website strip_url self[:website] end diff --git a/spec/system/admin/enterprises_spec.rb b/spec/system/admin/enterprises_spec.rb index 6b59886dfa..58e68d34f8 100644 --- a/spec/system/admin/enterprises_spec.rb +++ b/spec/system/admin/enterprises_spec.rb @@ -710,6 +710,8 @@ describe ' context "can edit white label logo link" do it_behaves_like "edit link with", "https://www.openfoodnetwork.org", "https://www.openfoodnetwork.org" + it_behaves_like "edit link with", "www.openfoodnetwork.org", "http://www.openfoodnetwork.org" + it_behaves_like "edit link with", "openfoodnetwork.org", "http://openfoodnetwork.org" end end From 40111910b6e332804c4e1c7d08f8cd218193e3fe Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 5 Jun 2023 11:04:22 +0200 Subject: [PATCH 3/4] Validates `white_label_logo_link` on enterprise model --- app/models/enterprise.rb | 20 ++++++++++++-------- spec/models/enterprise_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 789884ce54..0ecca4734c 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -117,6 +117,7 @@ class Enterprise < ApplicationRecord with: VALID_INSTAGRAM_REGEX, message: Spree.t('errors.messages.invalid_instagram_url') }, allow_blank: true + validate :validate_white_label_logo_link before_validation :initialize_permalink, if: lambda { permalink.nil? } before_validation :set_unused_address_fields @@ -317,14 +318,6 @@ class Enterprise < ApplicationRecord ) end - def white_label_logo_link - return nil if self[:white_label_logo_link].blank? - - return self[:white_label_logo_link] if self[:white_label_logo_link].start_with?('http') - - "http://#{self[:white_label_logo_link]}" - end - def website strip_url self[:website] end @@ -462,6 +455,17 @@ class Enterprise < ApplicationRecord private + def validate_white_label_logo_link + return if white_label_logo.blank? + + return if white_label_logo_link.blank? + + uri = URI(white_label_logo_link) + self.white_label_logo_link = "http://#{white_label_logo_link}" if uri.scheme.nil? + rescue URI::InvalidURIError + errors.add(:white_label_logo_link, I18n.t(:invalid_url)) + end + def current_exchange_variants ExchangeVariant.joins(exchange: :order_cycle) .merge(Exchange.outgoing) diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 555ff925b9..1f624ba6a5 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -313,6 +313,30 @@ describe Enterprise do expect(enterprise).to be_invalid end end + + describe "white label logo link" do + before do + # validate white_label_logo_link only if white_label_logo is present + allow_any_instance_of(Enterprise).to receive(:white_label_logo).and_return(true) + end + + it "validates the white_label_logo_link attribute" do + e = build(:enterprise, white_label_logo_link: 'http://www.example.com') + expect(e).to be_valid + expect(e.white_label_logo_link).to eq "http://www.example.com" + end + + it "adds http:// to the white_label_logo_link attribute if it is missing" do + e = build(:enterprise, white_label_logo_link: 'www.example.com') + expect(e).to be_valid + expect(e.white_label_logo_link).to eq "http://www.example.com" + end + + it "does not validate if URL is invalid and can't be infered" do + e = build(:enterprise, white_label_logo_link: 'with spaces') + expect(e).to be_invalid + end + end end describe "callbacks" do From 1ebacf96ade6f8f52096d103162abe545552519c Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 7 Jun 2023 13:12:06 +1000 Subject: [PATCH 4/4] Ignore accidental spaces around whitelabel URL --- app/models/enterprise.rb | 1 + spec/models/enterprise_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 0ecca4734c..e7b81947ae 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -460,6 +460,7 @@ class Enterprise < ApplicationRecord return if white_label_logo_link.blank? + white_label_logo_link.strip! uri = URI(white_label_logo_link) self.white_label_logo_link = "http://#{white_label_logo_link}" if uri.scheme.nil? rescue URI::InvalidURIError diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 1f624ba6a5..0eb4ec083b 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -332,6 +332,12 @@ describe Enterprise do expect(e.white_label_logo_link).to eq "http://www.example.com" end + it "ignores whitespace around the URL form copying and pasting" do + e = build(:enterprise, white_label_logo_link: ' www.example.com ') + expect(e).to be_valid + expect(e.white_label_logo_link).to eq "http://www.example.com" + end + it "does not validate if URL is invalid and can't be infered" do e = build(:enterprise, white_label_logo_link: 'with spaces') expect(e).to be_invalid