FIX: Destroy associated user api keys when making a user anonymous. (#11760)

This commit is contained in:
Roman Rizzi 2021-01-25 11:07:22 -03:00 committed by GitHub
parent 2092152b03
commit afe6db5f33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 6 deletions

View File

@ -59,11 +59,12 @@ class UserAnonymizer
) )
end end
@user.user_avatar.try(:destroy) @user.user_avatar&.destroy!
@user.single_sign_on_record.try(:destroy) @user.single_sign_on_record&.destroy!
@user.oauth2_user_infos.try(:destroy_all) @user.oauth2_user_infos.destroy_all
@user.user_associated_accounts.try(:destroy_all) @user.user_associated_accounts.destroy_all
@user.api_keys.find_each { |x| x.try(:destroy) } @user.api_keys.destroy_all
@user.user_api_keys.destroy_all
@user.user_emails.secondary.destroy_all @user.user_emails.secondary.destroy_all
@user_history = log_action @user_history = log_action

View File

@ -210,12 +210,23 @@ describe UserAnonymizer do
end end
it "removes api key" do it "removes api key" do
ApiKey.create(user_id: user.id) ApiKey.create!(user_id: user.id)
expect { make_anonymous }.to change { ApiKey.count }.by(-1) expect { make_anonymous }.to change { ApiKey.count }.by(-1)
user.reload user.reload
expect(user.api_keys).to be_empty expect(user.api_keys).to be_empty
end end
it "removes user api key" do
user_api_key = Fabricate(:user_api_key, user: user)
expect { make_anonymous }.to change { UserApiKey.count }.by(-1)
user.reload
expect(user.user_api_keys).to be_empty
end
context "executes job" do context "executes job" do
before do before do
Jobs.run_immediately! Jobs.run_immediately!