Merge pull request #10922 from jibees/10903-white-label-make-logo-url-link-more-tolerant-in-terms-of-format-and-add-example

[White Label] Make logo url link more tolerant in terms of format
This commit is contained in:
Filipe
2023-06-07 10:00:31 +01:00
committed by GitHub
3 changed files with 57 additions and 6 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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