diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 9ef3176274..e7b81947ae 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 @@ -454,6 +455,18 @@ class Enterprise < ApplicationRecord private + def validate_white_label_logo_link + return if white_label_logo.blank? + + 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 + 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..0eb4ec083b 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -313,6 +313,36 @@ 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 "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 + end + end end describe "callbacks" do diff --git a/spec/system/admin/enterprises_spec.rb b/spec/system/admin/enterprises_spec.rb index 2acbccf544..58e68d34f8 100644 --- a/spec/system/admin/enterprises_spec.rb +++ b/spec/system/admin/enterprises_spec.rb @@ -698,12 +698,20 @@ 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" + 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