From 8a7868be077d4e4d142f94d6e588c766d58a604a Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Sat, 6 Feb 2016 02:19:48 +0530 Subject: [PATCH] FIX: validate user website --- app/models/user_profile.rb | 1 + app/services/user_updater.rb | 1 + spec/models/user_profile_spec.rb | 12 ++++++++++++ 3 files changed, 14 insertions(+) diff --git a/app/models/user_profile.rb b/app/models/user_profile.rb index 14aed1333df..32c0e2047c3 100644 --- a/app/models/user_profile.rb +++ b/app/models/user_profile.rb @@ -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 diff --git a/app/services/user_updater.rb b/app/services/user_updater.rb index 9ce7f57197d..c6f236d2b32 100644 --- a/app/services/user_updater.rb +++ b/app/services/user_updater.rb @@ -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 diff --git a/spec/models/user_profile_spec.rb b/spec/models/user_profile_spec.rb index 66720ec790c..cbacd417693 100644 --- a/spec/models/user_profile_spec.rb +++ b/spec/models/user_profile_spec.rb @@ -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) }