From e6ed2d397a9c0e280083934dbf2d387e8a4a226c Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Tue, 8 Nov 2011 13:48:38 -0800 Subject: [PATCH] added close account to user --- app/models/account_deletion.rb | 5 +++ app/models/person.rb | 7 +--- app/models/profile.rb | 4 +- app/models/user.rb | 12 ++++++ spec/models/account_deletion_spec.rb | 10 ++++- spec/models/profile_spec.rb | 9 ++--- spec/models/user_spec.rb | 56 +++++++++++++++++++++++++++- 7 files changed, 89 insertions(+), 14 deletions(-) diff --git a/app/models/account_deletion.rb b/app/models/account_deletion.rb index 8ece18e0e..1a40c89c1 100644 --- a/app/models/account_deletion.rb +++ b/app/models/account_deletion.rb @@ -33,6 +33,7 @@ class AccountDeletion delete_photos delete_posts tombstone_person_and_profile + tombstone_user end #user deletions @@ -89,6 +90,10 @@ class AccountDeletion self.person.close_account! end + def tombstone_user + self.user.close_account! + end + def delete_contacts_of_me Contact.all_contacts_of_person(self.person).destroy_all end diff --git a/app/models/person.rb b/app/models/person.rb index eab06e394..4db5da134 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -40,7 +40,7 @@ class Person < ActiveRecord::Base before_destroy :remove_all_traces before_validation :clean_url - + validates :url, :presence => true validates :profile, :presence => true validates :serialized_public_key, :presence => true @@ -78,7 +78,7 @@ class Person < ActiveRecord::Base super self.profile ||= Profile.new unless profile_set end - + def self.find_from_id_or_username(params) p = if params[:id].present? Person.where(:id => params[:id]).first @@ -91,7 +91,6 @@ class Person < ActiveRecord::Base p end - def self.search_query_string(query) query = query.downcase like_operator = postgres? ? "ILIKE" : "LIKE" @@ -276,8 +275,6 @@ class Person < ActiveRecord::Base end end - - # @param person [Person] # @param url [String] def update_url(url) diff --git a/app/models/profile.rb b/app/models/profile.rb index fc41de397..cdbb1b2be 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -148,7 +148,7 @@ class Profile < ActiveRecord::Base def tombstone! self.taggings.delete_all - clearable_profile_fields.each do |field| + clearable_fields.each do |field| self[field] = nil end self.save @@ -174,7 +174,7 @@ class Profile < ActiveRecord::Base end private - def clearable_profile_fields + def clearable_fields self.attributes.keys - Profile.protected_attributes.to_a - ["created_at", "updated_at", "person_id"] end diff --git a/app/models/user.rb b/app/models/user.rb index 269c00797..d0a8be3db 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -488,4 +488,16 @@ class User < ActiveRecord::Base errors[:base] << 'That username has already been taken' end end + + def close_account! + clearable_fields.each do |field| + self[field] = nil + end + self.save + end + + private + def clearable_fields + self.attributes.keys - ["username", "encrypted_password", "created_at", "updated_at"] + end end diff --git a/spec/models/account_deletion_spec.rb b/spec/models/account_deletion_spec.rb index 4b10fdcbf..1a4ed4699 100644 --- a/spec/models/account_deletion_spec.rb +++ b/spec/models/account_deletion_spec.rb @@ -30,7 +30,8 @@ describe AccountDeletion do :delete_posts, :tombstone_person_and_profile, :remove_share_visibilities, - :remove_conversation_visibilities].each do |method| + :remove_conversation_visibilities, + :tombstone_user].each do |method| it "calls ##{method.to_s}" do @account_deletion.should_receive(method) @@ -141,6 +142,13 @@ describe AccountDeletion do end end + describe "#tombstone_user" do + it 'calls strip_model on user' do + bob.should_receive(:close_account!) + @account_deletion.tombstone_user + end + end + it 'has all user association keys accounted for' do all_keys = (@account_deletion.normal_ar_user_associates_to_delete + @account_deletion.special_ar_user_associations + @account_deletion.ignored_ar_user_associations) all_keys.sort{|x, y| x.to_s <=> y.to_s}.should == User.reflections.keys.sort{|x, y| x.to_s <=> y.to_s} diff --git a/spec/models/profile_spec.rb b/spec/models/profile_spec.rb index 9a703fe78..2905c6b05 100644 --- a/spec/models/profile_spec.rb +++ b/spec/models/profile_spec.rb @@ -264,7 +264,6 @@ describe Profile do end describe '#receive' do - it 'updates the profile in place' do local_luke, local_leia, remote_raphael = set_up_friends new_profile = Factory.build :profile @@ -281,8 +280,8 @@ describe Profile do @profile = bob.person.profile end it "clears the profile fields" do - attributes = @profile.send(:clearable_profile_fields) - + attributes = @profile.send(:clearable_fields) + @profile.tombstone! @profile.reload attributes.each{ |attr| @@ -296,10 +295,10 @@ describe Profile do end end - describe "#clearable_profile_fields" do + describe "#clearable_fields" do it 'returns the current profile fields' do profile = Factory.build :profile - profile.send(:clearable_profile_fields).sort.should == + profile.send(:clearable_fields).sort.should == ["diaspora_handle", "first_name", "last_name", diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 431a745be..a2ef86505 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1003,6 +1003,60 @@ describe User do user = Factory :user Resque.should_receive(:enqueue).with(Jobs::ResetPassword, user.id) user.send_reset_password_instructions + + context "close account" do + before do + @user = bob + end + + describe "#close_account!" do + it 'resets the password to a random string' do + random_pass = "12345678909876543210" + ActiveSupport::SecureRandom.should_receive(:hex).and_return(random_pass) + @user.close_account! + @user.valid_password?(random_pass) + end + + it 'clears all the clearable fields' do + attributes = @user.send(:clearable_fields) + @user.close_account! + + attributes.each do |attr| + @user.send(attr.to_sym).should be_blank + end + end + end + + describe "#clearable_attributes" do + it 'has all the attributes' do + user = Factory.build :user + user.send(:clearable_fields).sort.should == %w{ + serialized_private_key + getting_started + disable_mail + language + email + invitation_token + invitation_sent_at + reset_password_token + remember_token + remember_created_at + sign_in_count + current_sign_in_at + last_sign_in_at + current_sign_in_ip + last_sign_in_ip + invitation_service + invitation_identifier + invitation_limit + invited_by_id + invited_by_type + authentication_token + unconfirmed_email + confirm_email_token + locked_at + show_community_spotlight_in_stream + }.sort + end end end -end