2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require "rails_helper"
|
2015-03-06 16:44:54 -05:00
|
|
|
|
|
|
|
describe UserAnonymizer do
|
2018-04-30 13:42:51 -04:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
2018-05-02 12:25:06 -04:00
|
|
|
describe "event" do
|
|
|
|
let(:user) { Fabricate(:user, username: "edward") }
|
|
|
|
subject(:make_anonymous) { described_class.make_anonymous(user, admin, anonymize_ip: '2.2.2.2') }
|
|
|
|
|
|
|
|
it "triggers the event" do
|
|
|
|
events = DiscourseEvent.track_events do
|
|
|
|
make_anonymous
|
|
|
|
end
|
|
|
|
|
|
|
|
anon_event = events.detect { |e| e[:event_name] == :user_anonymized }
|
|
|
|
expect(anon_event).to be_present
|
|
|
|
params_hash = anon_event[:params][0]
|
|
|
|
|
|
|
|
expect(params_hash[:user]).to eq(user)
|
|
|
|
expect(params_hash[:opts][:anonymize_ip]).to eq('2.2.2.2')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-06 16:44:54 -05:00
|
|
|
describe "make_anonymous" do
|
2018-06-07 06:17:37 -04:00
|
|
|
let(:original_email) { "edward@example.net" }
|
2019-04-23 05:49:05 -04:00
|
|
|
let(:user) { Fabricate(:user, username: "edward", email: original_email) }
|
2019-05-10 06:59:31 -04:00
|
|
|
fab!(:another_user) { Fabricate(:evil_trout) }
|
2015-03-06 16:44:54 -05:00
|
|
|
subject(:make_anonymous) { described_class.make_anonymous(user, admin) }
|
|
|
|
|
|
|
|
it "changes username" do
|
|
|
|
make_anonymous
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(user.reload.username).to match(/^anon\d{3,}$/)
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
|
|
|
|
2019-01-30 09:14:36 -05:00
|
|
|
it "changes the primary email address" do
|
2015-03-06 16:44:54 -05:00
|
|
|
make_anonymous
|
2018-06-07 09:59:51 -04:00
|
|
|
expect(user.reload.email).to eq("#{user.username}@anonymized.invalid")
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
|
|
|
|
2019-01-30 09:14:36 -05:00
|
|
|
it "changes the primary email address when there is an email domain whitelist" do
|
|
|
|
SiteSetting.email_domains_whitelist = 'example.net|wayne.com|discourse.org'
|
|
|
|
|
|
|
|
make_anonymous
|
|
|
|
expect(user.reload.email).to eq("#{user.username}@anonymized.invalid")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes secondary email addresses" do
|
|
|
|
Fabricate(:secondary_email, user: user, email: "secondary_email@example.com")
|
|
|
|
|
|
|
|
make_anonymous
|
|
|
|
expect(user.reload.secondary_emails).to be_blank
|
|
|
|
end
|
|
|
|
|
2015-03-06 16:44:54 -05:00
|
|
|
it "turns off all notifications" do
|
2016-02-16 23:46:19 -05:00
|
|
|
user.user_option.update_columns(
|
2019-03-15 10:55:11 -04:00
|
|
|
email_level: UserOption.email_level_types[:always],
|
|
|
|
email_messages_level: UserOption.email_level_types[:always]
|
2016-02-16 23:46:19 -05:00
|
|
|
)
|
|
|
|
|
2015-03-06 16:44:54 -05:00
|
|
|
make_anonymous
|
|
|
|
user.reload
|
2016-02-16 23:46:19 -05:00
|
|
|
expect(user.user_option.email_digests).to eq(false)
|
2019-03-15 10:55:11 -04:00
|
|
|
expect(user.user_option.email_level).to eq(UserOption.email_level_types[:never])
|
|
|
|
expect(user.user_option.email_messages_level).to eq(UserOption.email_level_types[:never])
|
2016-02-16 23:46:19 -05:00
|
|
|
expect(user.user_option.mailing_list_mode).to eq(false)
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
|
|
|
|
2016-03-17 18:43:48 -04:00
|
|
|
context "Site Settings do not require full name" do
|
|
|
|
before do
|
|
|
|
SiteSetting.full_name_required = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "resets profile to default values" do
|
2019-04-28 23:58:52 -04:00
|
|
|
user.update!(
|
|
|
|
name: "Bibi",
|
|
|
|
date_of_birth: 19.years.ago,
|
|
|
|
title: "Super Star"
|
|
|
|
)
|
2016-03-17 18:43:48 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
profile = user.reload.user_profile
|
2019-04-28 23:58:52 -04:00
|
|
|
upload = Fabricate(:upload)
|
|
|
|
|
|
|
|
profile.update!(
|
2019-04-29 03:40:53 -04:00
|
|
|
location: "Moose Jaw",
|
2019-04-28 23:58:52 -04:00
|
|
|
website: "http://www.bim.com",
|
2019-04-29 03:40:53 -04:00
|
|
|
bio_raw: "I'm Bibi from Moosejaw. I sing and dance.",
|
|
|
|
bio_cooked: "I'm Bibi from Moosejaw. I sing and dance.",
|
2019-04-28 23:58:52 -04:00
|
|
|
profile_background_upload: upload,
|
2019-04-29 03:40:53 -04:00
|
|
|
bio_cooked_version: 2,
|
2019-04-28 23:58:52 -04:00
|
|
|
card_background_upload: upload
|
2019-04-29 03:40:53 -04:00
|
|
|
)
|
2016-03-17 21:32:09 -04:00
|
|
|
|
|
|
|
prev_username = user.username
|
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
UserAuthToken.generate!(user_id: user.id)
|
|
|
|
|
2016-03-17 18:43:48 -04:00
|
|
|
make_anonymous
|
|
|
|
user.reload
|
|
|
|
|
2016-03-17 21:32:09 -04:00
|
|
|
expect(user.username).not_to eq(prev_username)
|
2016-03-17 18:43:48 -04:00
|
|
|
expect(user.name).not_to be_present
|
|
|
|
expect(user.date_of_birth).to eq(nil)
|
|
|
|
expect(user.title).not_to be_present
|
2017-01-31 17:21:37 -05:00
|
|
|
expect(user.user_auth_tokens.count).to eq(0)
|
2016-03-17 18:43:48 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
profile = user.reload.user_profile
|
2016-03-17 18:43:48 -04:00
|
|
|
expect(profile.location).to eq(nil)
|
|
|
|
expect(profile.website).to eq(nil)
|
|
|
|
expect(profile.bio_cooked).to eq(nil)
|
2019-04-28 23:58:52 -04:00
|
|
|
expect(profile.profile_background_upload).to eq(nil)
|
|
|
|
expect(profile.bio_cooked_version).to eq(UserProfile::BAKED_VERSION)
|
|
|
|
expect(profile.card_background_upload).to eq(nil)
|
2016-03-17 18:43:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "Site Settings require full name" do
|
|
|
|
before do
|
|
|
|
SiteSetting.full_name_required = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "changes name to anonymized username" do
|
2016-03-17 21:32:09 -04:00
|
|
|
prev_username = user.username
|
|
|
|
|
2019-04-29 03:32:25 -04:00
|
|
|
user.update(name: "Bibi", date_of_birth: 19.years.ago, title: "Super Star")
|
2016-03-17 18:43:48 -04:00
|
|
|
|
|
|
|
make_anonymous
|
|
|
|
user.reload
|
2015-03-06 16:44:54 -05:00
|
|
|
|
2016-03-17 21:32:09 -04:00
|
|
|
expect(user.name).not_to eq(prev_username)
|
2016-03-17 18:43:48 -04:00
|
|
|
expect(user.name).to eq(user.username)
|
|
|
|
end
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "removes the avatar" do
|
|
|
|
upload = Fabricate(:upload, user: user)
|
|
|
|
user.user_avatar = UserAvatar.new(user_id: user.id, custom_upload_id: upload.id)
|
2015-11-27 14:37:20 -05:00
|
|
|
user.uploaded_avatar_id = upload.id # chosen in user preferences
|
2015-03-06 16:44:54 -05:00
|
|
|
user.save!
|
2018-12-13 00:42:52 -05:00
|
|
|
make_anonymous
|
2015-03-06 16:44:54 -05:00
|
|
|
user.reload
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(user.user_avatar).to eq(nil)
|
2015-11-27 14:37:20 -05:00
|
|
|
expect(user.uploaded_avatar_id).to eq(nil)
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
|
|
|
|
2018-05-13 10:25:56 -04:00
|
|
|
it "updates the avatar in posts" do
|
2019-03-14 10:47:38 -04:00
|
|
|
Jobs.run_immediately!
|
2018-05-13 10:25:56 -04:00
|
|
|
upload = Fabricate(:upload, user: user)
|
|
|
|
user.user_avatar = UserAvatar.new(user_id: user.id, custom_upload_id: upload.id)
|
|
|
|
user.uploaded_avatar_id = upload.id # chosen in user preferences
|
|
|
|
user.save!
|
|
|
|
|
|
|
|
topic = Fabricate(:topic, user: user)
|
|
|
|
quoted_post = create_post(user: user, topic: topic, post_number: 1, raw: "quoted post")
|
|
|
|
post = create_post(raw: <<~RAW)
|
|
|
|
Lorem ipsum
|
|
|
|
|
|
|
|
[quote="#{quoted_post.username}, post:1, topic:#{quoted_post.topic.id}"]
|
|
|
|
quoted post
|
|
|
|
[/quote]
|
|
|
|
RAW
|
|
|
|
|
|
|
|
old_avatar_url = user.avatar_template.gsub("{size}", "40")
|
|
|
|
expect(post.cooked).to include(old_avatar_url)
|
|
|
|
|
|
|
|
make_anonymous
|
|
|
|
post.reload
|
|
|
|
new_avatar_url = user.reload.avatar_template.gsub("{size}", "40")
|
|
|
|
|
|
|
|
expect(post.cooked).to_not include(old_avatar_url)
|
|
|
|
expect(post.cooked).to include(new_avatar_url)
|
|
|
|
end
|
|
|
|
|
2018-03-05 14:38:18 -05:00
|
|
|
it "logs the action with the original details" do
|
|
|
|
SiteSetting.log_anonymizer_details = true
|
|
|
|
helper = UserAnonymizer.new(user, admin)
|
|
|
|
orig_email = user.email
|
|
|
|
orig_username = user.username
|
|
|
|
helper.make_anonymous
|
|
|
|
|
|
|
|
history = helper.user_history
|
|
|
|
expect(history).to be_present
|
|
|
|
expect(history.email).to eq(orig_email)
|
|
|
|
expect(history.details).to match(orig_username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "logs the action without the original details" do
|
|
|
|
SiteSetting.log_anonymizer_details = false
|
|
|
|
helper = UserAnonymizer.new(user, admin)
|
|
|
|
orig_email = user.email
|
|
|
|
orig_username = user.username
|
|
|
|
helper.make_anonymous
|
|
|
|
|
|
|
|
history = helper.user_history
|
|
|
|
expect(history).to be_present
|
|
|
|
expect(history.email).not_to eq(orig_email)
|
|
|
|
expect(history.details).not_to match(orig_username)
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "removes external auth assocations" do
|
|
|
|
user.github_user_info = GithubUserInfo.create(user_id: user.id, screen_name: "example", github_user_id: "examplel123123")
|
2018-11-28 10:49:24 -05:00
|
|
|
user.user_associated_accounts = [UserAssociatedAccount.create(user_id: user.id, provider_uid: "example", provider_name: "facebook")]
|
2015-03-06 16:44:54 -05:00
|
|
|
user.single_sign_on_record = SingleSignOnRecord.create(user_id: user.id, external_id: "example", last_payload: "looks good")
|
2018-07-23 11:51:57 -04:00
|
|
|
user.oauth2_user_infos = [Oauth2UserInfo.create(user_id: user.id, uid: "example", provider: "example")]
|
2015-03-06 16:44:54 -05:00
|
|
|
make_anonymous
|
|
|
|
user.reload
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(user.github_user_info).to eq(nil)
|
2018-11-28 10:49:24 -05:00
|
|
|
expect(user.user_associated_accounts).to be_empty
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(user.single_sign_on_record).to eq(nil)
|
2018-07-23 11:51:57 -04:00
|
|
|
expect(user.oauth2_user_infos).to be_empty
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "removes api key" do
|
2019-11-29 10:16:06 -05:00
|
|
|
ApiKey.create(user_id: user.id)
|
2015-03-06 16:44:54 -05:00
|
|
|
expect { make_anonymous }.to change { ApiKey.count }.by(-1)
|
|
|
|
user.reload
|
2019-11-05 09:10:23 -05:00
|
|
|
expect(user.api_keys).to be_empty
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
|
|
|
|
2018-06-07 09:59:51 -04:00
|
|
|
context "executes job" do
|
|
|
|
before do
|
2019-03-14 10:47:38 -04:00
|
|
|
Jobs.run_immediately!
|
2018-06-07 09:59:51 -04:00
|
|
|
end
|
2018-06-07 06:17:37 -04:00
|
|
|
|
2018-06-07 09:59:51 -04:00
|
|
|
it "removes invites" do
|
2020-06-09 11:19:32 -04:00
|
|
|
Fabricate(:invited_user, invite: Fabricate(:invite), user: user)
|
|
|
|
Fabricate(:invited_user, invite: Fabricate(:invite), user: another_user)
|
2018-06-07 06:17:37 -04:00
|
|
|
|
2020-06-09 11:19:32 -04:00
|
|
|
expect { make_anonymous }.to change { InvitedUser.count }.by(-1)
|
|
|
|
expect(InvitedUser.where(user_id: user.id).count).to eq(0)
|
2018-06-07 09:59:51 -04:00
|
|
|
end
|
2018-06-07 06:17:37 -04:00
|
|
|
|
2018-06-07 09:59:51 -04:00
|
|
|
it "removes email tokens" do
|
|
|
|
Fabricate(:email_token, user: user)
|
|
|
|
Fabricate(:email_token, user: another_user)
|
2018-06-07 06:17:37 -04:00
|
|
|
|
2018-06-07 09:59:51 -04:00
|
|
|
expect { make_anonymous }.to change { EmailToken.count }.by(-1)
|
|
|
|
expect(EmailToken.where(user_id: user.id).count).to eq(0)
|
|
|
|
end
|
2018-06-07 06:17:37 -04:00
|
|
|
|
2018-06-07 09:59:51 -04:00
|
|
|
it "removes email log entries" do
|
|
|
|
Fabricate(:email_log, user: user)
|
|
|
|
Fabricate(:email_log, user: another_user)
|
2018-06-07 06:17:37 -04:00
|
|
|
|
2018-06-07 09:59:51 -04:00
|
|
|
expect { make_anonymous }.to change { EmailLog.count }.by(-1)
|
|
|
|
expect(EmailLog.where(user_id: user.id).count).to eq(0)
|
|
|
|
end
|
2018-06-07 06:17:37 -04:00
|
|
|
|
2018-06-07 09:59:51 -04:00
|
|
|
it "removes incoming emails" do
|
|
|
|
Fabricate(:incoming_email, user: user, from_address: user.email)
|
|
|
|
Fabricate(:incoming_email, from_address: user.email, error: "Some error")
|
|
|
|
Fabricate(:incoming_email, user: another_user, from_address: another_user.email)
|
2018-06-07 06:17:37 -04:00
|
|
|
|
2018-06-07 09:59:51 -04:00
|
|
|
expect { make_anonymous }.to change { IncomingEmail.count }.by(-2)
|
|
|
|
expect(IncomingEmail.where(user_id: user.id).count).to eq(0)
|
|
|
|
expect(IncomingEmail.where(from_address: original_email).count).to eq(0)
|
|
|
|
end
|
2018-06-07 06:17:37 -04:00
|
|
|
|
2018-06-07 09:59:51 -04:00
|
|
|
it "removes raw email from posts" do
|
|
|
|
post1 = Fabricate(:post, user: user, via_email: true, raw_email: "raw email from user")
|
|
|
|
post2 = Fabricate(:post, user: another_user, via_email: true, raw_email: "raw email from another user")
|
2018-06-07 06:17:37 -04:00
|
|
|
|
2018-06-07 09:59:51 -04:00
|
|
|
make_anonymous
|
|
|
|
|
|
|
|
expect(post1.reload).to have_attributes(via_email: true, raw_email: nil)
|
|
|
|
expect(post2.reload).to have_attributes(via_email: true, raw_email: "raw email from another user")
|
|
|
|
end
|
2018-06-11 12:42:31 -04:00
|
|
|
|
|
|
|
it "does not delete profile views" do
|
|
|
|
UserProfileView.add(user.id, '127.0.0.1', another_user.id, Time.now, true)
|
|
|
|
expect { make_anonymous }.to_not change { UserProfileView.count }
|
|
|
|
end
|
2018-09-06 18:02:47 -04:00
|
|
|
|
|
|
|
it "removes user field values" do
|
|
|
|
field1 = Fabricate(:user_field)
|
|
|
|
field2 = Fabricate(:user_field)
|
|
|
|
|
|
|
|
user.custom_fields = {
|
|
|
|
"some_field": "123",
|
|
|
|
"user_field_#{field1.id}": "foo",
|
|
|
|
"user_field_#{field2.id}": "bar",
|
|
|
|
"another_field": "456"
|
|
|
|
}
|
|
|
|
|
|
|
|
expect { make_anonymous }.to change { user.custom_fields }
|
|
|
|
expect(user.reload.custom_fields).to eq("some_field" => "123", "another_field" => "456")
|
|
|
|
end
|
2018-06-07 06:17:37 -04:00
|
|
|
end
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
|
|
|
|
2018-04-30 13:42:51 -04:00
|
|
|
describe "anonymize_ip" do
|
|
|
|
let(:old_ip) { "1.2.3.4" }
|
|
|
|
let(:anon_ip) { "0.0.0.0" }
|
|
|
|
let(:user) { Fabricate(:user, ip_address: old_ip, registration_ip_address: old_ip) }
|
2019-05-10 06:59:31 -04:00
|
|
|
fab!(:post) { Fabricate(:post) }
|
2018-04-30 13:42:51 -04:00
|
|
|
let(:topic) { post.topic }
|
|
|
|
|
|
|
|
it "doesn't anonymize ips by default" do
|
|
|
|
UserAnonymizer.make_anonymous(user, admin)
|
|
|
|
expect(user.ip_address).to eq(old_ip)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is called if you pass an option" do
|
|
|
|
UserAnonymizer.make_anonymous(user, admin, anonymize_ip: anon_ip)
|
|
|
|
user.reload
|
|
|
|
expect(user.ip_address).to eq(anon_ip)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "exhaustively replaces all user ips" do
|
2019-03-14 10:47:38 -04:00
|
|
|
Jobs.run_immediately!
|
2018-04-30 13:42:51 -04:00
|
|
|
link = IncomingLink.create!(current_user_id: user.id, ip_address: old_ip, post_id: post.id)
|
|
|
|
|
|
|
|
screened_email = ScreenedEmail.create!(email: user.email, ip_address: old_ip)
|
|
|
|
|
|
|
|
search_log = SearchLog.create!(
|
|
|
|
term: 'wat',
|
|
|
|
search_type: SearchLog.search_types[:header],
|
|
|
|
user_id: user.id,
|
|
|
|
ip_address: old_ip
|
|
|
|
)
|
|
|
|
|
|
|
|
topic_link = TopicLink.create!(
|
|
|
|
user_id: admin.id,
|
|
|
|
topic_id: topic.id,
|
|
|
|
url: 'https://discourse.org',
|
|
|
|
domain: 'discourse.org'
|
|
|
|
)
|
|
|
|
|
|
|
|
topic_link_click = TopicLinkClick.create!(
|
|
|
|
topic_link_id: topic_link.id,
|
|
|
|
user_id: user.id,
|
|
|
|
ip_address: old_ip
|
|
|
|
)
|
|
|
|
|
|
|
|
user_profile_view = UserProfileView.create!(
|
|
|
|
user_id: user.id,
|
|
|
|
user_profile_id: admin.user_profile.id,
|
|
|
|
ip_address: old_ip,
|
|
|
|
viewed_at: Time.now
|
|
|
|
)
|
|
|
|
|
|
|
|
TopicViewItem.create!(topic_id: topic.id, user_id: user.id, ip_address: old_ip, viewed_at: Time.now)
|
|
|
|
delete_history = StaffActionLogger.new(admin).log_user_deletion(user)
|
|
|
|
user_history = StaffActionLogger.new(user).log_backup_create
|
|
|
|
|
|
|
|
UserAnonymizer.make_anonymous(user, admin, anonymize_ip: anon_ip)
|
|
|
|
expect(user.registration_ip_address).to eq(anon_ip)
|
|
|
|
expect(link.reload.ip_address).to eq(anon_ip)
|
|
|
|
expect(screened_email.reload.ip_address).to eq(anon_ip)
|
|
|
|
expect(search_log.reload.ip_address).to eq(anon_ip)
|
|
|
|
expect(topic_link_click.reload.ip_address).to eq(anon_ip)
|
|
|
|
topic_view = TopicViewItem.where(topic_id: topic.id, user_id: user.id).first
|
|
|
|
expect(topic_view.ip_address).to eq(anon_ip)
|
|
|
|
expect(delete_history.reload.ip_address).to eq(anon_ip)
|
|
|
|
expect(user_history.reload.ip_address).to eq(anon_ip)
|
|
|
|
expect(user_profile_view.reload.ip_address).to eq(anon_ip)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|