diff options
author | azul <azul@riseup.net> | 2012-12-22 04:37:42 -0800 |
---|---|---|
committer | azul <azul@riseup.net> | 2012-12-22 04:37:42 -0800 |
commit | 9a23a5d63b2b5bcb1994137a5de9f8ebd88142f1 (patch) | |
tree | e65b4c4f75e2e8313c67b77aacbd210efbfddd71 /users | |
parent | 6e90c173662583ca392ca1cefa7e7dcdc92dbf13 (diff) | |
parent | c83ada50de6b9a7084a23037068ba7f12e09dfc9 (diff) |
Merge pull request #7 from leapcode/feature/validate-email-domain
validating email domain and displaying it as the placeholder
Diffstat (limited to 'users')
-rw-r--r-- | users/app/models/local_email.rb | 19 | ||||
-rw-r--r-- | users/app/models/user.rb | 18 | ||||
-rw-r--r-- | users/app/views/users/_email_aliases.html.haml | 2 | ||||
-rw-r--r-- | users/app/views/users/_email_field.html.haml | 2 | ||||
-rw-r--r-- | users/test/unit/email_aliases_test.rb | 60 | ||||
-rw-r--r-- | users/test/unit/email_test.rb | 55 | ||||
-rw-r--r-- | users/test/unit/user_test.rb | 9 |
7 files changed, 113 insertions, 52 deletions
diff --git a/users/app/models/local_email.rb b/users/app/models/local_email.rb index c654fcb..d23df71 100644 --- a/users/app/models/local_email.rb +++ b/users/app/models/local_email.rb @@ -3,15 +3,22 @@ class LocalEmail < Email validate :unique_on_server validate :unique_alias_for_user validate :differs_from_main_email + before_validation :add_domain_if_needed + validates :email, + :presence => true, + :format => { :with => /@#{APP_CONFIG[:domain]}\Z/, + :message => "needs to end in @#{APP_CONFIG[:domain]}"} validates :casted_by, :presence => true def to_partial_path "emails/email" end + protected + def unique_on_server - has_email = User.find_by_email_or_alias(email) - if has_email && has_email != self.base_doc + has_email = User.find_by_email_or_alias(email) + if has_email && has_email != self.base_doc errors.add :email, "has already been taken" end end @@ -24,11 +31,19 @@ class LocalEmail < Email end def differs_from_main_email + # If this has not changed but the email let's mark the email invalid instead. + return if self.persisted? user = self.casted_by if user.email == self.email errors.add :email, "may not be the same as your email address" end end + def add_domain_if_needed + if email.blank? + errors.add :email, "may not be empty." + end + self.email += "@" + APP_CONFIG[:domain] unless self.email.include?("@") + end end diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 1999a28..2a8a57b 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -32,6 +32,12 @@ class User < CouchRest::Model::Base validates :email, :format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"} + validates :email, + :format => { :with => /\A(.+@#{APP_CONFIG[:domain]})?\Z/, + :message => "needs to end in @#{APP_CONFIG[:domain]}"} + + validate :email_unique_on_server + validates :email_forward, :format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"} @@ -121,18 +127,28 @@ class User < CouchRest::Model::Base email_aliases.build(attrs.values.first) if attrs end + protected + ## # Validation Functions ## def email_differs_from_email_aliases + # If this has not changed but the email aliases let's not mark this invalid. return if email_aliases.any? and email_aliases.last.errors.any? if email_aliases.map(&:email).include?(email) errors.add(:email, "may not be the same as an alias") end end - protected + def email_unique_on_server + return unless email + has_email = User.find_by_email_or_alias(email) + if has_email && has_email != self.base_doc + errors.add :email, "has already been taken" + end + end + def password password_verifier end diff --git a/users/app/views/users/_email_aliases.html.haml b/users/app/views/users/_email_aliases.html.haml index 121acc4..e012429 100644 --- a/users/app/views/users/_email_aliases.html.haml +++ b/users/app/views/users/_email_aliases.html.haml @@ -3,4 +3,4 @@ =render @user.email_aliases .clearfix = f.simple_fields_for :email_aliases, @email_alias do |e| - = e.input :email, :placeholder => "alias@#{request.domain}" + = e.input :email, :placeholder => "alias@#{APP_CONFIG[:domain]}" diff --git a/users/app/views/users/_email_field.html.haml b/users/app/views/users/_email_field.html.haml index 36bbeca..edf62c9 100644 --- a/users/app/views/users/_email_field.html.haml +++ b/users/app/views/users/_email_field.html.haml @@ -1 +1 @@ -= f.input :email += f.input :email, :placeholder => "me@#{APP_CONFIG[:domain]}" diff --git a/users/test/unit/email_aliases_test.rb b/users/test/unit/email_aliases_test.rb index f680ac6..88f97f4 100644 --- a/users/test/unit/email_aliases_test.rb +++ b/users/test/unit/email_aliases_test.rb @@ -6,6 +6,11 @@ class EmailAliasTest < ActiveSupport::TestCase @attribs = User.valid_attributes_hash User.find_by_login(@attribs[:login]).try(:destroy) @user = User.new(@attribs) + @attribs.merge!(:login => "other_user") + User.find_by_login(@attribs[:login]).try(:destroy) + @other_user = User.create(@attribs) + @alias = "valid_alias@#{APP_CONFIG[:domain]}" + User.find_by_email_or_alias(@alias).try(:destroy) end test "no email aliases set in the beginning" do @@ -13,46 +18,53 @@ class EmailAliasTest < ActiveSupport::TestCase end test "adding email alias through params" do - email_alias = "valid_alias@domain.net" - @user.attributes = {:email_aliases_attributes => {"0" => {:email => email_alias}}} + @user.attributes = {:email_aliases_attributes => {"0" => {:email => @alias}}} assert @user.changed? assert @user.save - assert_equal email_alias, @user.email_aliases.first.to_s + assert_equal @alias, @user.email_aliases.first.to_s end test "adding email alias directly" do - email_alias = "valid_alias@domain.net" - @user.email_aliases.build :email => email_alias - assert @user.changed? + @user.email_aliases.build :email => @alias assert @user.save - assert_equal email_alias, @user.reload.email_aliases.first.to_s + assert_equal @alias, @user.reload.email_aliases.first.to_s end test "duplicated email aliases are invalid" do - email_alias = "valid_alias@domain.net" - @user.email_aliases.build :email => email_alias + @user.email_aliases.build :email => @alias @user.save - # add again - email_alias = @user.email_aliases.build :email => email_alias - assert !email_alias.valid? - assert @user.changed? - assert !@user.valid? + assert_invalid_alias @alias + end + + test "email alias needs to be different from other peoples email" do + @other_user.email = @alias + @other_user.save + assert_invalid_alias @alias + end + + test "email needs to be different from other peoples email aliases" do + @other_user.email_aliases.build :email => @alias + @other_user.save + assert_invalid_alias @alias end test "email is invalid as email alias" do - email_alias = "valid_alias@domain.net" - @user.email = email_alias - @user.email_aliases.build :email => email_alias - assert @user.changed? - assert !@user.valid? + @user.email = @alias + assert_invalid_alias @alias end test "find user by email alias" do - email_alias = "valid_alias@domain.net" - @user.email_aliases.build :email => email_alias + @user.email_aliases.build :email => @alias assert @user.save - assert_equal @user, User.find_by_email_or_alias(email_alias) - assert_equal @user, User.find_by_email_alias(email_alias) - assert_nil User.find_by_email(email_alias) + assert_equal @user, User.find_by_email_or_alias(@alias) + assert_equal @user, User.find_by_email_alias(@alias) + assert_nil User.find_by_email(@alias) + end + + def assert_invalid_alias(string) + email_alias = @user.email_aliases.build :email => string + assert !email_alias.valid? + assert !@user.valid? end + end diff --git a/users/test/unit/email_test.rb b/users/test/unit/email_test.rb index 6f9beaa..060ced5 100644 --- a/users/test/unit/email_test.rb +++ b/users/test/unit/email_test.rb @@ -10,6 +10,8 @@ class EmailTest < ActiveSupport::TestCase @attribs.merge!(:login => "other_user") User.find_by_login(@attribs[:login]).try(:destroy) @other_user = User.create(@attribs) + @email_string = "valid_alias@#{APP_CONFIG[:domain]}" + User.find_by_email_or_alias(@email_string).try(:destroy) end teardown do @@ -17,23 +19,48 @@ class EmailTest < ActiveSupport::TestCase @other_user.destroy if @other_user.persisted? end - - test "email aliases need to be unique" do - email_alias = "valid_alias@domain.net" - @other_user.email_aliases.build :email => email_alias + test "email needs to be different from other peoples email" do + @other_user.email = @email_string @other_user.save - @user.email_aliases.build :email => email_alias - assert @user.changed? - assert !@user.save - # TODO handle errors + assert_invalid_email @email_string end - test "email aliases may not conflict with emails" do - email_alias = "valid_alias@domain.net" - @other_user.email = email_alias + test "email needs to be different from other peoples email aliases" do + @other_user.email_aliases.build :email => @email_string @other_user.save - @user.email_aliases.build :email => email_alias - assert @user.changed? - assert !@user.save + assert_invalid_email @email_string + end + + test "email needs to be different from email aliases" do + @user.email_aliases.build :email => @email_string + @user.save + assert_invalid_email @email_string + end + + test "non local emails are invalid" do + assert_invalid_email "not_valid@mail.me" + end + + test "local emails are valid" do + local_email = "valid@#{APP_CONFIG[:domain]}" + @user.email = local_email + @user.valid? + assert_equal Hash.new, @user.errors.messages + end + + test "find user by email" do + email = "finding@test.me" + @user.email = email + @user.save + assert_equal @user, User.find_by_email(email) + assert_equal @user, User.find_by_email_or_alias(email) + assert_nil User.find_by_email_alias(email) end + + def assert_invalid_email(string) + @user.email = string + assert !@user.valid? + assert @user.errors.keys.include?(:email) + end + end diff --git a/users/test/unit/user_test.rb b/users/test/unit/user_test.rb index 5d2a7ea..0c79f1f 100644 --- a/users/test/unit/user_test.rb +++ b/users/test/unit/user_test.rb @@ -56,15 +56,6 @@ class UserTest < ActiveSupport::TestCase admin_user = User.new(attribs) assert admin_user.is_admin? assert !@user.is_admin? - end - test "find user by email" do - email = "tryto@find.me" - @user.email = email - @user.save - assert_equal @user, User.find_by_email(email) - assert_equal @user, User.find_by_email_or_alias(email) - assert_nil User.find_by_email_alias(email) - end end |