From 6c410ed09366bef17ccc109c4c957e0c9fb45ed5 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Fri, 30 Jan 2015 01:11:41 +0530 Subject: [PATCH] FIX: strip whitespaces from user email --- app/models/user.rb | 9 ++++++--- spec/models/user_spec.rb | 10 ++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index b90c5544d22..4a8cd72b099 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -62,7 +62,7 @@ class User < ActiveRecord::Base delegate :last_sent_email_address, :to => :email_logs - before_validation :downcase_email + before_validation :strip_downcase_email validates_presence_of :username validate :username_validator @@ -764,8 +764,11 @@ class User < ActiveRecord::Base self.username_lower = username.downcase end - def downcase_email - self.email = self.email.downcase if self.email + def strip_downcase_email + if self.email + self.email = self.email.strip + self.email = self.email.downcase + end end def username_validator diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index a13dc476f32..69bd8cb3a6c 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -261,8 +261,14 @@ describe User do it "downcases email addresses" do user = Fabricate.build(:user, email: 'Fancy.Caps.4.U@gmail.com') - user.save - expect(user.reload.email).to eq('fancy.caps.4.u@gmail.com') + user.valid? + expect(user.email).to eq('fancy.caps.4.u@gmail.com') + end + + it "strips whitespace from email addresses" do + user = Fabricate.build(:user, email: ' example@gmail.com ') + user.valid? + expect(user.email).to eq('example@gmail.com') end end