From 4dffda05477f74c90197948bb6f13bedc8b2a19f Mon Sep 17 00:00:00 2001 From: Azul Date: Sun, 9 Dec 2012 10:52:23 +0100 Subject: first steps towards email aliases * unit tests draft * controller draft --- users/test/unit/email_alias_test.rb | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 users/test/unit/email_alias_test.rb (limited to 'users/test') diff --git a/users/test/unit/email_alias_test.rb b/users/test/unit/email_alias_test.rb new file mode 100644 index 0000000..86cb10f --- /dev/null +++ b/users/test/unit/email_alias_test.rb @@ -0,0 +1,50 @@ +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" do + email_alias = "valid_alias@domain.net" + @user.add_email_alias(email_alias) + assert_equal [email_alias], @user.email_aliases + end + + test "email aliases need to be unique" do + # todo build helper for this ... make_record(User) + email_alias = "valid_alias@domain.net" + attribs = User.valid_attributes_hash.merge(:login => "other_user") + User.find_by_login(attribs[:login]).try(:destroy) + other_user = User.new(attribs) + other_user.add_email_alias(email_alias) + @user.add_email_alias(email_alias) + # todo: how do we handle errors? Should email_alias become an ActiveModel? + assert_equal [], @user.email_aliases + end + + test "email aliases may not conflict with emails" do + # todo build helper for this ... make_record(User) + email_alias = "valid_alias@domain.net" + attribs = User.valid_attributes_hash.merge(:login => "other_user", :email => email_alias) + User.find_by_login(attribs[:login]).try(:destroy) + other_user = User.new(attribs) + @user.add_email_alias(email_alias) + # todo: how do we handle errors? Should email_alias become an ActiveModel? + assert_equal [], @user.email_aliases + end + + test "can retrieve user by email alias" do + email_alias = "valid_alias@domain.net" + @user.add_email_alias(email_alias) + assert_equal @user, User.find_by_email_alias(email_alias) + assert_equal @user, User.find_by_email(email_alias) + end +end -- cgit v1.2.3 From 28b21959f39b0e28c450bba54b8696632a0187fa Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 10 Dec 2012 11:00:24 +0100 Subject: created generic Email class and use it with EmailAliases --- users/test/unit/email_alias_test.rb | 50 ----------------------------------- users/test/unit/email_aliases_test.rb | 30 +++++++++++++++++++++ users/test/unit/email_test.rb | 33 +++++++++++++++++++++++ 3 files changed, 63 insertions(+), 50 deletions(-) delete mode 100644 users/test/unit/email_alias_test.rb create mode 100644 users/test/unit/email_aliases_test.rb create mode 100644 users/test/unit/email_test.rb (limited to 'users/test') diff --git a/users/test/unit/email_alias_test.rb b/users/test/unit/email_alias_test.rb deleted file mode 100644 index 86cb10f..0000000 --- a/users/test/unit/email_alias_test.rb +++ /dev/null @@ -1,50 +0,0 @@ -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" do - email_alias = "valid_alias@domain.net" - @user.add_email_alias(email_alias) - assert_equal [email_alias], @user.email_aliases - end - - test "email aliases need to be unique" do - # todo build helper for this ... make_record(User) - email_alias = "valid_alias@domain.net" - attribs = User.valid_attributes_hash.merge(:login => "other_user") - User.find_by_login(attribs[:login]).try(:destroy) - other_user = User.new(attribs) - other_user.add_email_alias(email_alias) - @user.add_email_alias(email_alias) - # todo: how do we handle errors? Should email_alias become an ActiveModel? - assert_equal [], @user.email_aliases - end - - test "email aliases may not conflict with emails" do - # todo build helper for this ... make_record(User) - email_alias = "valid_alias@domain.net" - attribs = User.valid_attributes_hash.merge(:login => "other_user", :email => email_alias) - User.find_by_login(attribs[:login]).try(:destroy) - other_user = User.new(attribs) - @user.add_email_alias(email_alias) - # todo: how do we handle errors? Should email_alias become an ActiveModel? - assert_equal [], @user.email_aliases - end - - test "can retrieve user by email alias" do - email_alias = "valid_alias@domain.net" - @user.add_email_alias(email_alias) - assert_equal @user, User.find_by_email_alias(email_alias) - assert_equal @user, User.find_by_email(email_alias) - end -end diff --git a/users/test/unit/email_aliases_test.rb b/users/test/unit/email_aliases_test.rb new file mode 100644 index 0000000..e737773 --- /dev/null +++ b/users/test/unit/email_aliases_test.rb @@ -0,0 +1,30 @@ +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" 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 "can retrieve user by email alias" do + email_alias = "valid_alias@domain.net" + @user.attributes = {:email_aliases => [email_alias]} + @user.save + assert_equal @user, User.find_by_email_alias(email_alias) + assert_equal @user, 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..d421b77 --- /dev/null +++ b/users/test/unit/email_test.rb @@ -0,0 +1,33 @@ +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 "email aliases need to be unique" do + # TODO build helper for this ... make_record(User) + email_alias = "valid_alias@domain.net" + attribs = User.valid_attributes_hash.merge(:login => "other_user") + User.find_by_login(attribs[:login]).try(:destroy) + other_user = User.new(attribs) + other_user.attributes = {:email_aliases => [email_alias]} + other_user.save + @user.attributes = {:email_aliases => [email_alias]} + assert !@user.valid? + # TODO handle errors + end + + test "email aliases may not conflict with emails" do + # TODO build helper for this ... make_record(User) + email_alias = "valid_alias@domain.net" + attribs = User.valid_attributes_hash.merge(:login => "other_user", :email => email_alias) + User.find_by_login(attribs[:login]).try(:destroy) + other_user = User.new(attribs) + @user.attributes = {:email_aliases => [email_alias]} + assert !@user.valid? + end +end -- cgit v1.2.3 From 60e8fc3e1309d1c972a7695e3344e63f5d633a06 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 12 Dec 2012 15:41:14 +0100 Subject: find users by email and aliases --- users/test/unit/email_aliases_test.rb | 9 +++++---- users/test/unit/user_test.rb | 9 +++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'users/test') diff --git a/users/test/unit/email_aliases_test.rb b/users/test/unit/email_aliases_test.rb index e737773..5ded5bb 100644 --- a/users/test/unit/email_aliases_test.rb +++ b/users/test/unit/email_aliases_test.rb @@ -20,11 +20,12 @@ class EmailAliasTest < ActiveSupport::TestCase assert_equal email_alias, @user.email_aliases.first.to_s end - test "can retrieve user by email alias" do + test "find user by email alias" do email_alias = "valid_alias@domain.net" - @user.attributes = {:email_aliases => [email_alias]} - @user.save + @user.attributes = {:email_aliases_attributes => {"0" => {:email => 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_equal @user, User.find_by_email(email_alias) + assert_nil User.find_by_email(email_alias) 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 -- cgit v1.2.3 From d7890d7c8af6691df2817a9b6654acf9377847bd Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 13 Dec 2012 11:49:26 +0100 Subject: LocalEmail added - will validate uniqueness amongst emails and aliases --- users/test/unit/email_test.rb | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'users/test') diff --git a/users/test/unit/email_test.rb b/users/test/unit/email_test.rb index d421b77..cba88a2 100644 --- a/users/test/unit/email_test.rb +++ b/users/test/unit/email_test.rb @@ -1,33 +1,39 @@ require 'test_helper' -class EmailAliasTest < ActiveSupport::TestCase +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 - # TODO build helper for this ... make_record(User) email_alias = "valid_alias@domain.net" - attribs = User.valid_attributes_hash.merge(:login => "other_user") - User.find_by_login(attribs[:login]).try(:destroy) - other_user = User.new(attribs) - other_user.attributes = {:email_aliases => [email_alias]} - other_user.save - @user.attributes = {:email_aliases => [email_alias]} - assert !@user.valid? + @other_user.attributes = {:email_aliases_attributes => {"0" => {:email => email_alias}}} + @other_user.save + @user.attributes = {:email_aliases_attributes => {"0" => {:email => email_alias}}} + assert @user.changed? + assert !@user.save # TODO handle errors end test "email aliases may not conflict with emails" do - # TODO build helper for this ... make_record(User) email_alias = "valid_alias@domain.net" - attribs = User.valid_attributes_hash.merge(:login => "other_user", :email => email_alias) - User.find_by_login(attribs[:login]).try(:destroy) - other_user = User.new(attribs) - @user.attributes = {:email_aliases => [email_alias]} - assert !@user.valid? + @other_user.email = email_alias + @other_user.save + @user.attributes = {:email_aliases_attributes => {"0" => {:email => email_alias}}} + assert @user.changed? + assert !@user.save end end -- cgit v1.2.3 From 6ff4bd80e0394260c8cd300cfb051451fc7e358f Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 13 Dec 2012 15:47:08 +0100 Subject: ensure users do not have duplicate email aliases nor aliases that are the same as the original email for that matter --- users/test/unit/email_aliases_test.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'users/test') diff --git a/users/test/unit/email_aliases_test.rb b/users/test/unit/email_aliases_test.rb index 5ded5bb..cec0c0b 100644 --- a/users/test/unit/email_aliases_test.rb +++ b/users/test/unit/email_aliases_test.rb @@ -20,6 +20,28 @@ class EmailAliasTest < ActiveSupport::TestCase assert_equal email_alias, @user.email_aliases.first.to_s end + test "duplicated email aliases are invalid" do + email_alias = "valid_alias@domain.net" + email_aliases = { + "0" => {:email => email_alias}, + "1" => {:email => email_alias} + } + @user.attributes = {:email_aliases_attributes => email_aliases} + @user.save + # add some more + @user.attributes = {:email_aliases_attributes => email_aliases} + assert @user.changed? + assert !@user.valid? + end + + test "email is invalid as email alias" do + email_alias = "valid_alias@domain.net" + email_aliases = { "0" => {:email => email_alias} } + @user.attributes = {:email_aliases_attributes => email_aliases, :email => email_alias} + assert @user.changed? + assert !@user.valid? + end + test "find user by email alias" do email_alias = "valid_alias@domain.net" @user.attributes = {:email_aliases_attributes => {"0" => {:email => email_alias}}} -- cgit v1.2.3 From 9762a37ab932ee1a94e973977520c7f4673d78b1 Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 13 Dec 2012 16:01:59 +0100 Subject: refactor: allow adding email aliases directly --- users/test/unit/email_aliases_test.rb | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'users/test') diff --git a/users/test/unit/email_aliases_test.rb b/users/test/unit/email_aliases_test.rb index cec0c0b..3c731e1 100644 --- a/users/test/unit/email_aliases_test.rb +++ b/users/test/unit/email_aliases_test.rb @@ -12,7 +12,7 @@ class EmailAliasTest < ActiveSupport::TestCase assert_equal [], @user.email_aliases end - test "adding email alias" do + 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? @@ -20,31 +20,35 @@ class EmailAliasTest < ActiveSupport::TestCase 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(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" - email_aliases = { - "0" => {:email => email_alias}, - "1" => {:email => email_alias} - } - @user.attributes = {:email_aliases_attributes => email_aliases} + @user.add_email(email_alias) @user.save - # add some more - @user.attributes = {:email_aliases_attributes => email_aliases} + # add again + @user.add_email(email_alias) assert @user.changed? assert !@user.valid? end test "email is invalid as email alias" do email_alias = "valid_alias@domain.net" - email_aliases = { "0" => {:email => email_alias} } - @user.attributes = {:email_aliases_attributes => email_aliases, :email => email_alias} + @user.email = email_alias + @user.add_email(email_alias) assert @user.changed? assert !@user.valid? end test "find user by email alias" do email_alias = "valid_alias@domain.net" - @user.attributes = {:email_aliases_attributes => {"0" => {:email => email_alias}}} + @user.add_email(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) -- cgit v1.2.3 From aed03c04270bc9b7d48048ffb2f24fecc993c7ac Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 13 Dec 2012 16:13:32 +0100 Subject: refactor: changed add_email to add_email_alias that's what it does. Changed all tests to use it instead of the attributes method --- users/test/unit/email_aliases_test.rb | 10 +++++----- users/test/unit/email_test.rb | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'users/test') diff --git a/users/test/unit/email_aliases_test.rb b/users/test/unit/email_aliases_test.rb index 3c731e1..762aaea 100644 --- a/users/test/unit/email_aliases_test.rb +++ b/users/test/unit/email_aliases_test.rb @@ -22,7 +22,7 @@ class EmailAliasTest < ActiveSupport::TestCase test "adding email alias directly" do email_alias = "valid_alias@domain.net" - @user.add_email(email_alias) + @user.add_email_alias(email_alias) assert @user.changed? assert @user.save assert_equal email_alias, @user.reload.email_aliases.first.to_s @@ -30,10 +30,10 @@ class EmailAliasTest < ActiveSupport::TestCase test "duplicated email aliases are invalid" do email_alias = "valid_alias@domain.net" - @user.add_email(email_alias) + @user.add_email_alias(email_alias) @user.save # add again - @user.add_email(email_alias) + @user.add_email_alias(email_alias) assert @user.changed? assert !@user.valid? end @@ -41,14 +41,14 @@ class EmailAliasTest < ActiveSupport::TestCase test "email is invalid as email alias" do email_alias = "valid_alias@domain.net" @user.email = email_alias - @user.add_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(email_alias) + @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) diff --git a/users/test/unit/email_test.rb b/users/test/unit/email_test.rb index cba88a2..1e216d6 100644 --- a/users/test/unit/email_test.rb +++ b/users/test/unit/email_test.rb @@ -20,9 +20,9 @@ class EmailTest < ActiveSupport::TestCase test "email aliases need to be unique" do email_alias = "valid_alias@domain.net" - @other_user.attributes = {:email_aliases_attributes => {"0" => {:email => email_alias}}} + @other_user.add_email_alias email_alias @other_user.save - @user.attributes = {:email_aliases_attributes => {"0" => {:email => email_alias}}} + @user.add_email_alias email_alias assert @user.changed? assert !@user.save # TODO handle errors @@ -32,7 +32,7 @@ class EmailTest < ActiveSupport::TestCase email_alias = "valid_alias@domain.net" @other_user.email = email_alias @other_user.save - @user.attributes = {:email_aliases_attributes => {"0" => {:email => email_alias}}} + @user.add_email_alias email_alias assert @user.changed? assert !@user.save end -- cgit v1.2.3