diff options
author | Azul <azul@leap.se> | 2012-12-20 17:38:36 +0100 |
---|---|---|
committer | Azul <azul@leap.se> | 2012-12-20 17:38:36 +0100 |
commit | c83ada50de6b9a7084a23037068ba7f12e09dfc9 (patch) | |
tree | 75c0eef72cb7a2c4e0cfb8350a79631e5c5a8595 /users | |
parent | c4f87b3447ab2a96dc9181d0b89be4f6f025042a (diff) |
fixed tests, testing corner cases, fixed these
Diffstat (limited to 'users')
-rw-r--r-- | users/app/models/local_email.rb | 10 | ||||
-rw-r--r-- | users/app/models/user.rb | 16 | ||||
-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 |
5 files changed, 99 insertions, 51 deletions
diff --git a/users/app/models/local_email.rb b/users/app/models/local_email.rb index 24624e7..d23df71 100644 --- a/users/app/models/local_email.rb +++ b/users/app/models/local_email.rb @@ -5,6 +5,7 @@ class LocalEmail < Email 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 @@ -16,8 +17,8 @@ class LocalEmail < Email 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 @@ -30,6 +31,8 @@ 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" @@ -37,6 +40,9 @@ class LocalEmail < Email 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 diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 8f776a3..2a8a57b 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -33,9 +33,11 @@ class User < CouchRest::Model::Base :format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"} validates :email, - :format => { :with => /@#{APP_CONFIG[:domain]}\Z/, + :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"} @@ -125,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/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 |