diff options
Diffstat (limited to 'users')
-rw-r--r-- | users/app/models/local_email.rb | 11 | ||||
-rw-r--r-- | users/app/models/user.rb | 2 | ||||
-rw-r--r-- | users/test/unit/email_test.rb | 36 |
3 files changed, 33 insertions, 16 deletions
diff --git a/users/app/models/local_email.rb b/users/app/models/local_email.rb new file mode 100644 index 0000000..5762a33 --- /dev/null +++ b/users/app/models/local_email.rb @@ -0,0 +1,11 @@ +class LocalEmail < Email + + validate :unique_on_server + + def unique_on_server + has_email = User.find_by_email_or_alias(email) + if has_email && has_email != self.base_doc + errors.add(:email, "has already been taken") + end + end +end diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 7d1691a..e5e388b 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -6,7 +6,7 @@ class User < CouchRest::Model::Base property :email, String, :accessible => true property :email_forward, String, :accessible => true - property :email_aliases, [Email] + property :email_aliases, [LocalEmail] validates :login, :password_salt, :password_verifier, :presence => true 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 |