summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2012-12-12 15:41:14 +0100
committerAzul <azul@leap.se>2012-12-12 15:41:14 +0100
commit60e8fc3e1309d1c972a7695e3344e63f5d633a06 (patch)
treed4bdc2d2e45702d5241c22404d77e8e4b2205378
parentd9fa19106998bc6ac484494334dcf150bb6aa5d5 (diff)
find users by email and aliases
-rw-r--r--users/app/models/email.rb4
-rw-r--r--users/app/models/user.rb30
-rw-r--r--users/test/unit/email_aliases_test.rb9
-rw-r--r--users/test/unit/user_test.rb9
4 files changed, 43 insertions, 9 deletions
diff --git a/users/app/models/email.rb b/users/app/models/email.rb
index 7c88c51..45101c1 100644
--- a/users/app/models/email.rb
+++ b/users/app/models/email.rb
@@ -3,11 +3,9 @@ class Email
property :email, String
- validates :email_forward,
+ validates :email,
:format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/, :message => "needs to be a valid email address"}
- timestamps!
-
def to_s
email
end
diff --git a/users/app/models/user.rb b/users/app/models/user.rb
index a41554d..7d1691a 100644
--- a/users/app/models/user.rb
+++ b/users/app/models/user.rb
@@ -28,16 +28,42 @@ class User < CouchRest::Model::Base
# TODO: write a proper email validator to be used in the different places
validates :email,
- :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/, :message => "needs to be a valid email address"}
+ :format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"}
validates :email_forward,
- :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/, :message => "needs to be a valid email address"}
+ :format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"}
timestamps!
design do
view :by_login
view :by_created_at
+ view :by_email
+ view :by_email_alias,
+ :map => <<-EOJS
+ function(doc) {
+ if (doc.type != 'User') {
+ return;
+ }
+ doc.email_aliases.forEach(function(alias){
+ emit(alias.email, doc);
+ });
+ }
+ EOJS
+ view :by_email_or_alias,
+ :map => <<-EOJS
+ function(doc) {
+ if (doc.type != 'User') {
+ return;
+ }
+ if (doc.email) {
+ emit(doc.email, doc);
+ }
+ doc.email_aliases.forEach(function(alias){
+ emit(alias.email, doc);
+ });
+ }
+ EOJS
end
class << self
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