Permalink
Browse files

added close account to user

  • Loading branch information...
1 parent 5bf7865 commit a77204db51257eec784f5eb61cb0e7ed8be22215 @danielgrippi danielgrippi committed Nov 8, 2011
View
5 app/models/account_deletion.rb
@@ -33,6 +33,7 @@ def perform!
delete_photos
delete_posts
tombstone_person_and_profile
+ tombstone_user
end
#user deletions
@@ -89,6 +90,10 @@ def tombstone_person_and_profile
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
View
7 app/models/person.rb
@@ -40,7 +40,7 @@ def downcase_diaspora_handle
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 @@ def initialize(params={})
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 @@ def self.find_from_id_or_username(params)
p
end
-
def self.search_query_string(query)
query = query.downcase
like_operator = postgres? ? "ILIKE" : "LIKE"
@@ -276,8 +275,6 @@ def self.url_batch_update(people, url)
end
end
-
-
# @param person [Person]
# @param url [String]
def update_url(url)
View
4 app/models/profile.rb
@@ -148,7 +148,7 @@ def construct_full_name
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 @@ def valid_birthday
end
private
- def clearable_profile_fields
+ def clearable_fields
self.attributes.keys - Profile.protected_attributes.to_a - ["created_at", "updated_at", "person_id"]
end
View
12 app/models/user.rb
@@ -483,4 +483,16 @@ def no_person_with_same_username
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
View
10 spec/models/account_deletion_spec.rb
@@ -30,7 +30,8 @@
: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 @@
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}
View
9 spec/models/profile_spec.rb
@@ -264,7 +264,6 @@
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 @@
@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 @@
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",
View
55 spec/models/user_spec.rb
@@ -983,4 +983,59 @@
end
end
end
+
+ 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
+ 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

0 comments on commit a77204d

Please sign in to comment.