summaryrefslogtreecommitdiff
path: root/users/test/unit
diff options
context:
space:
mode:
Diffstat (limited to 'users/test/unit')
-rw-r--r--users/test/unit/email_aliases_test.rb60
-rw-r--r--users/test/unit/email_test.rb55
-rw-r--r--users/test/unit/user_test.rb9
3 files changed, 77 insertions, 47 deletions
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