FIX: validate user website

This commit is contained in:
Arpit Jalan 2016-02-06 02:19:48 +05:30
parent ea0e63b150
commit 8a7868be07
3 changed files with 14 additions and 0 deletions

View File

@ -2,6 +2,7 @@ class UserProfile < ActiveRecord::Base
belongs_to :user, inverse_of: :user_profile
validates :bio_raw, length: { maximum: 3000 }
validates :website, format: { with: /(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,9}(([0-9]{1,5})?\/.*)?$)/ix }, allow_blank: true
validates :user, presence: true
before_save :cook
after_save :trigger_badges

View File

@ -104,6 +104,7 @@ class UserUpdater
attr_reader :user, :guardian
def format_url(website)
return nil if website.blank?
website =~ /^http/ ? website : "http://#{website}"
end
end

View File

@ -37,6 +37,18 @@ describe UserProfile do
expect(user_profile).not_to be_valid
end
it "doesn't support invalid website" do
user_profile = Fabricate.build(:user_profile, website: "http://https://google.com")
user_profile.user = Fabricate.build(:user)
expect(user_profile).not_to be_valid
end
it "supports valid website" do
user_profile = Fabricate.build(:user_profile, website: "https://google.com")
user_profile.user = Fabricate.build(:user)
expect(user_profile.valid?).to be true
end
describe 'after save' do
let(:user) { Fabricate(:user) }