summaryrefslogtreecommitdiff
path: root/users
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2012-12-13 16:01:59 +0100
committerAzul <azul@leap.se>2012-12-13 16:01:59 +0100
commit9762a37ab932ee1a94e973977520c7f4673d78b1 (patch)
tree2aa6eb4e27cd9b7cc4651c2c6465be10bcfc422b /users
parent6ff4bd80e0394260c8cd300cfb051451fc7e358f (diff)
refactor: allow adding email aliases directly
Diffstat (limited to 'users')
-rw-r--r--users/app/models/user.rb9
-rw-r--r--users/test/unit/email_aliases_test.rb26
2 files changed, 23 insertions, 12 deletions
diff --git a/users/app/models/user.rb b/users/app/models/user.rb
index 0eb244c..7c9002b 100644
--- a/users/app/models/user.rb
+++ b/users/app/models/user.rb
@@ -112,9 +112,16 @@ class User < CouchRest::Model::Base
APP_CONFIG['admins'].include? self.login
end
+ def add_email(email)
+ email = LocalEmail.new({:email => email}) unless email.is_a? Email
+ email_aliases << email
+ end
+
+ # this currently only adds the first email address submitted.
+ # All the ui needs for now.
def email_aliases_attributes=(attrs)
if attrs
- email_alias = Email.new(attrs.values.first)
+ email_alias = LocalEmail.new(attrs.values.first)
email_aliases << email_alias
end
end
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)