summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2012-12-13 15:47:08 +0100
committerAzul <azul@leap.se>2012-12-13 15:47:08 +0100
commit6ff4bd80e0394260c8cd300cfb051451fc7e358f (patch)
tree38c89677c8cef54c5c412c8d6fd3388e8786692d
parentd7890d7c8af6691df2817a9b6654acf9377847bd (diff)
ensure users do not have duplicate email aliases
nor aliases that are the same as the original email for that matter
-rw-r--r--users/app/models/user.rb21
-rw-r--r--users/test/unit/email_aliases_test.rb22
2 files changed, 43 insertions, 0 deletions
diff --git a/users/app/models/user.rb b/users/app/models/user.rb
index e5e388b..0eb244c 100644
--- a/users/app/models/user.rb
+++ b/users/app/models/user.rb
@@ -33,6 +33,10 @@ class User < CouchRest::Model::Base
validates :email_forward,
:format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"}
+ validate :no_duplicate_email_aliases
+
+ validate :email_aliases_differ_from_email
+
timestamps!
design do
@@ -115,6 +119,23 @@ class User < CouchRest::Model::Base
end
end
+ ##
+ # Validation Functions
+ ##
+
+ # TODO: How do we handle these errors?
+ def no_duplicate_email_aliases
+ if email_aliases.count != email_aliases.map(&:email).uniq.count
+ errors.add(:email_aliases, "include a duplicate")
+ end
+ end
+
+ def email_aliases_differ_from_email
+ if email_aliases.map(&:email).include?(email)
+ errors.add(:email_aliases, "include the original email address")
+ end
+ end
+
protected
def password
password_verifier
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}}}