Validates white_label_logo_link on enterprise model

This commit is contained in:
Jean-Baptiste Bellet
2023-06-05 11:04:22 +02:00
parent 880ba85791
commit 40111910b6
2 changed files with 36 additions and 8 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
@@ -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)

View File

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