summaryrefslogtreecommitdiff
path: root/users/test
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2012-12-13 17:16:05 +0100
committerAzul <azul@leap.se>2012-12-13 17:16:05 +0100
commit32db6e2ee3ae449f2fe0f947f9ac4aafde340a9d (patch)
tree1f9abefddc5f3cfb49a96f87e184a78feb8fb506 /users/test
parent1a26988cb7ff30061473f4a781166b184b3037ce (diff)
parentfeee17e3a9970ac04f5a789255fbf4c3e8e89eb8 (diff)
Merge branch 'feature/email-aliases-model'
Diffstat (limited to 'users/test')
-rw-r--r--users/test/unit/email_aliases_test.rb57
-rw-r--r--users/test/unit/email_test.rb39
-rw-r--r--users/test/unit/user_test.rb9
3 files changed, 105 insertions, 0 deletions
diff --git a/users/test/unit/email_aliases_test.rb b/users/test/unit/email_aliases_test.rb
new file mode 100644
index 0000000..762aaea
--- /dev/null
+++ b/users/test/unit/email_aliases_test.rb
@@ -0,0 +1,57 @@
+require 'test_helper'
+
+class EmailAliasTest < ActiveSupport::TestCase
+
+ setup do
+ @attribs = User.valid_attributes_hash
+ User.find_by_login(@attribs[:login]).try(:destroy)
+ @user = User.new(@attribs)
+ end
+
+ test "no email aliases set in the beginning" do
+ assert_equal [], @user.email_aliases
+ end
+
+ test "adding email alias through params" do
+ email_alias = "valid_alias@domain.net"
+ @user.attributes = {:email_aliases_attributes => {"0" => {:email => email_alias}}}
+ assert @user.changed?
+ assert @user.save
+ assert_equal email_alias, @user.email_aliases.first.to_s
+ end
+
+ test "adding email alias directly" do
+ email_alias = "valid_alias@domain.net"
+ @user.add_email_alias(email_alias)
+ assert @user.changed?
+ assert @user.save
+ assert_equal email_alias, @user.reload.email_aliases.first.to_s
+ end
+
+ test "duplicated email aliases are invalid" do
+ email_alias = "valid_alias@domain.net"
+ @user.add_email_alias(email_alias)
+ @user.save
+ # add again
+ @user.add_email_alias(email_alias)
+ assert @user.changed?
+ assert !@user.valid?
+ end
+
+ test "email is invalid as email alias" do
+ email_alias = "valid_alias@domain.net"
+ @user.email = email_alias
+ @user.add_email_alias(email_alias)
+ assert @user.changed?
+ assert !@user.valid?
+ end
+
+ test "find user by email alias" do
+ email_alias = "valid_alias@domain.net"
+ @user.add_email_alias(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)
+ end
+end
diff --git a/users/test/unit/email_test.rb b/users/test/unit/email_test.rb
new file mode 100644
index 0000000..1e216d6
--- /dev/null
+++ b/users/test/unit/email_test.rb
@@ -0,0 +1,39 @@
+require 'test_helper'
+
+class EmailTest < ActiveSupport::TestCase
+
+ setup do
+ # TODO build helper for this ... make_record(User)
+ @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)
+ end
+
+ teardown do
+ @user.destroy if @user.persisted? # just in case
+ @other_user.destroy
+ end
+
+
+ test "email aliases need to be unique" do
+ email_alias = "valid_alias@domain.net"
+ @other_user.add_email_alias email_alias
+ @other_user.save
+ @user.add_email_alias email_alias
+ assert @user.changed?
+ assert !@user.save
+ # TODO handle errors
+ end
+
+ test "email aliases may not conflict with emails" do
+ email_alias = "valid_alias@domain.net"
+ @other_user.email = email_alias
+ @other_user.save
+ @user.add_email_alias email_alias
+ assert @user.changed?
+ assert !@user.save
+ end
+end
diff --git a/users/test/unit/user_test.rb b/users/test/unit/user_test.rb
index cce11c2..29f6a89 100644
--- a/users/test/unit/user_test.rb
+++ b/users/test/unit/user_test.rb
@@ -49,4 +49,13 @@ class UserTest < ActiveSupport::TestCase
assert_equal client_rnd, srp_session.aa
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