Skip to content

Commit

Permalink
Only allow HTTP(S) URLs in example app (#2003)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablobm committed Jun 17, 2021
1 parent c0f32ed commit e1baea3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 9 additions & 1 deletion spec/example_app/app/models/product.rb
Expand Up @@ -11,8 +11,10 @@ def self.policy_class
has_many :pages, dependent: :destroy
has_one :product_meta_tag, dependent: :destroy

before_validation :trim_image_url

validates :description, presence: true
validates :image_url, presence: true
validates :image_url, presence: true, format: %r{\Ahttps?://}
validates :name, presence: true
validates :price, presence: true
validates :release_year,
Expand Down Expand Up @@ -40,4 +42,10 @@ def valid_slug
errors.add :name, "must have letters or numbers for the URL"
end
end

private

def trim_image_url
image_url&.strip!
end
end
19 changes: 19 additions & 0 deletions spec/example_app/spec/models/product_spec.rb
@@ -0,0 +1,19 @@
require "rails_helper"

RSpec.describe Product do
describe "validations" do
it { should allow_value("http://example.com/foo/bar").for(:image_url) }
it { should allow_value("https://example.com/foo/bar").for(:image_url) }
it { should_not allow_value("ftp://example.com/foo/bar").for(:image_url) }
end

describe "#image_url" do
it "is trimmed on save" do
product = FactoryBot.create(
:product,
image_url: "\n https://example.com/foo/bar \n",
)
expect(product.image_url).to eq("https://example.com/foo/bar")
end
end
end

0 comments on commit e1baea3

Please sign in to comment.