2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe UserDestroyer do
|
2020-12-22 01:30:37 -05:00
|
|
|
fab!(:user) { Fabricate(:user_with_secondary_email) }
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2018-03-14 15:11:35 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe ".new" do
|
2013-04-11 16:04:20 -04:00
|
|
|
it "raises an error when user is nil" do
|
|
|
|
expect { UserDestroyer.new(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when user is not a User" do
|
|
|
|
expect { UserDestroyer.new(5) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "#destroy" do
|
2013-04-11 16:04:20 -04:00
|
|
|
it "raises an error when user is nil" do
|
2020-12-22 01:30:37 -05:00
|
|
|
expect { UserDestroyer.new(admin).destroy(nil) }.to raise_error(Discourse::InvalidParameters)
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when user is not a User" do
|
2020-12-22 01:30:37 -05:00
|
|
|
expect { UserDestroyer.new(admin).destroy("nothing") }.to raise_error(
|
|
|
|
Discourse::InvalidParameters,
|
|
|
|
)
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
|
2014-02-13 11:42:35 -05:00
|
|
|
it "raises an error when regular user tries to delete another user" do
|
2020-12-22 01:30:37 -05:00
|
|
|
expect { UserDestroyer.new(user).destroy(Fabricate(:user)) }.to raise_error(
|
|
|
|
Discourse::InvalidAccess,
|
|
|
|
)
|
2014-02-13 11:42:35 -05:00
|
|
|
end
|
|
|
|
|
2013-07-24 13:48:55 -04:00
|
|
|
shared_examples "successfully destroy a user" do
|
|
|
|
it "should delete the user" do
|
|
|
|
expect { destroy }.to change { User.count }.by(-1)
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
|
2013-07-24 13:48:55 -04:00
|
|
|
it "should return the deleted user record" do
|
|
|
|
return_value = destroy
|
2020-12-22 01:30:37 -05:00
|
|
|
expect(return_value).to eq(user)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(return_value).to be_destroyed
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
|
2013-07-24 13:48:55 -04:00
|
|
|
it "should log the action" do
|
2020-12-22 01:30:37 -05:00
|
|
|
StaffActionLogger.any_instance.expects(:log_user_deletion).with(user, anything).once
|
2013-07-24 13:48:55 -04:00
|
|
|
destroy
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
2017-10-03 11:28:41 -04:00
|
|
|
|
|
|
|
it "should not log the action if quiet is true" do
|
|
|
|
expect {
|
2020-12-22 01:30:37 -05:00
|
|
|
UserDestroyer.new(admin).destroy(user, destroy_opts.merge(quiet: true))
|
2017-10-03 11:28:41 -04:00
|
|
|
}.to_not change { UserHistory.where(action: UserHistory.actions[:delete_user]).count }
|
|
|
|
end
|
2018-07-23 03:49:49 -04:00
|
|
|
|
|
|
|
it "triggers a extensibility event" do
|
|
|
|
event = DiscourseEvent.track_events { destroy }.last
|
|
|
|
|
|
|
|
expect(event[:event_name]).to eq(:user_destroyed)
|
2020-12-22 01:30:37 -05:00
|
|
|
expect(event[:params].first).to eq(user)
|
2018-07-23 03:49:49 -04:00
|
|
|
end
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
|
2013-07-25 15:30:03 -04:00
|
|
|
shared_examples "email block list" do
|
|
|
|
it "doesn't add email to block list by default" do
|
2013-08-14 11:05:53 -04:00
|
|
|
ScreenedEmail.expects(:block).never
|
2013-07-25 15:30:03 -04:00
|
|
|
destroy
|
|
|
|
end
|
|
|
|
|
2017-07-31 18:27:29 -04:00
|
|
|
it "adds emails to block list if block_email is true" do
|
2017-07-25 11:44:46 -04:00
|
|
|
expect {
|
2020-12-22 01:30:37 -05:00
|
|
|
UserDestroyer.new(admin).destroy(user, destroy_opts.merge(block_email: true))
|
2017-07-31 18:27:29 -04:00
|
|
|
}.to change { ScreenedEmail.count }.by(2)
|
2013-07-25 15:30:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when user deletes self" do
|
2020-12-22 01:30:37 -05:00
|
|
|
subject(:destroy) { UserDestroyer.new(user).destroy(user, destroy_opts) }
|
2014-02-13 11:42:35 -05:00
|
|
|
|
2023-06-21 10:00:19 -04:00
|
|
|
let(:destroy_opts) { { delete_posts: true, context: "/u/username/preferences/account" } }
|
|
|
|
|
2014-02-13 11:42:35 -05:00
|
|
|
include_examples "successfully destroy a user"
|
2018-06-18 01:57:51 -04:00
|
|
|
|
|
|
|
it "should log proper context" do
|
|
|
|
destroy
|
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:delete_user]).last.context).to eq(
|
|
|
|
I18n.t("staff_action_logs.user_delete_self", url: "/u/username/preferences/account"),
|
|
|
|
)
|
|
|
|
end
|
2014-02-13 11:42:35 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when context is missing" do
|
2021-02-23 06:17:54 -05:00
|
|
|
it "logs warning message if context is missing" do
|
|
|
|
logger = track_log_messages { UserDestroyer.new(admin).destroy(user) }
|
2022-05-04 21:50:43 -04:00
|
|
|
expect(logger.warnings).to include(/User destroyed without context from:/)
|
2021-02-23 06:17:54 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
context "with a reviewable post" do
|
|
|
|
let!(:reviewable) { Fabricate(:reviewable, created_by: user) }
|
2015-04-24 16:04:44 -04:00
|
|
|
|
|
|
|
it "removes the queued post" do
|
|
|
|
UserDestroyer.new(admin).destroy(user)
|
2019-01-03 12:03:01 -05:00
|
|
|
expect(Reviewable.where(created_by_id: user.id).count).to eq(0)
|
2015-04-24 16:04:44 -04:00
|
|
|
end
|
2017-01-16 11:46:05 -05:00
|
|
|
end
|
|
|
|
|
2019-09-04 13:12:48 -04:00
|
|
|
context "with a reviewable user" do
|
|
|
|
let(:reviewable) { Fabricate(:reviewable, created_by: admin) }
|
|
|
|
|
|
|
|
it "sets the reviewable user as rejected" do
|
|
|
|
UserDestroyer.new(admin).destroy(reviewable.target)
|
|
|
|
|
2021-12-08 12:12:24 -05:00
|
|
|
expect(reviewable.reload).to be_rejected
|
2019-09-04 13:12:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-16 11:46:05 -05:00
|
|
|
context "with a directory item record" do
|
|
|
|
it "removes the directory item" do
|
|
|
|
DirectoryItem.create!(
|
|
|
|
user: user,
|
|
|
|
period_type: 1,
|
|
|
|
likes_received: 0,
|
|
|
|
likes_given: 0,
|
|
|
|
topics_entered: 0,
|
|
|
|
topic_count: 0,
|
|
|
|
post_count: 0,
|
|
|
|
)
|
|
|
|
UserDestroyer.new(admin).destroy(user)
|
|
|
|
expect(DirectoryItem.where(user_id: user.id).count).to eq(0)
|
|
|
|
end
|
2015-04-24 16:04:44 -04:00
|
|
|
end
|
|
|
|
|
2015-08-24 02:05:08 -04:00
|
|
|
context "with a draft" do
|
2019-10-31 02:15:41 -04:00
|
|
|
let!(:draft) { Draft.set(user, "test", 0, "test") }
|
2015-08-24 02:05:08 -04:00
|
|
|
|
|
|
|
it "removed the draft" do
|
|
|
|
UserDestroyer.new(admin).destroy(user)
|
|
|
|
expect(Draft.where(user_id: user.id).count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when user has posts" do
|
2013-09-04 15:35:10 -04:00
|
|
|
let!(:topic_starter) { Fabricate(:user) }
|
|
|
|
let!(:topic) { Fabricate(:topic, user: topic_starter) }
|
|
|
|
let!(:first_post) { Fabricate(:post, user: topic_starter, topic: topic) }
|
2020-12-22 01:30:37 -05:00
|
|
|
let!(:post) { Fabricate(:post, user: user, topic: topic) }
|
2013-04-11 16:04:20 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when delete_posts is false" do
|
2020-12-22 01:30:37 -05:00
|
|
|
subject(:destroy) { UserDestroyer.new(admin).destroy(user) }
|
2023-06-21 10:00:19 -04:00
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
before do
|
2020-12-22 01:30:37 -05:00
|
|
|
user.stubs(:post_count).returns(1)
|
|
|
|
user.stubs(:first_post_created_at).returns(Time.zone.now)
|
2014-07-28 13:17:37 -04:00
|
|
|
end
|
2013-07-24 13:48:55 -04:00
|
|
|
|
2018-03-28 04:20:08 -04:00
|
|
|
it "should raise the right error" do
|
2013-07-24 13:48:55 -04:00
|
|
|
StaffActionLogger.any_instance.expects(:log_user_deletion).never
|
2018-03-28 04:20:08 -04:00
|
|
|
expect { destroy }.to raise_error(UserDestroyer::PostsExistError)
|
|
|
|
expect(user.reload.id).to be_present
|
2013-04-15 14:52:07 -04:00
|
|
|
end
|
2013-07-24 13:48:55 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when delete_posts is true" do
|
2013-07-25 15:30:03 -04:00
|
|
|
let(:destroy_opts) { { delete_posts: true } }
|
2013-07-24 13:48:55 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when staff deletes user" do
|
2020-12-22 01:30:37 -05:00
|
|
|
subject(:destroy) { UserDestroyer.new(admin).destroy(user, destroy_opts) }
|
2014-02-13 11:42:35 -05:00
|
|
|
|
|
|
|
include_examples "successfully destroy a user"
|
|
|
|
include_examples "email block list"
|
|
|
|
|
|
|
|
it "deletes the posts" do
|
|
|
|
destroy
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(post.reload.deleted_at).not_to eq(nil)
|
|
|
|
expect(post.user_id).to eq(nil)
|
2014-02-13 11:42:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not delete topics started by others in which the user has replies" do
|
|
|
|
destroy
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(topic.reload.deleted_at).to eq(nil)
|
|
|
|
expect(topic.user_id).not_to eq(nil)
|
2014-02-13 11:42:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes topics started by the deleted user" do
|
2020-12-22 01:30:37 -05:00
|
|
|
spammer_topic = Fabricate(:topic, user: user)
|
|
|
|
Fabricate(:post, user: user, topic: spammer_topic)
|
2014-02-13 11:42:35 -05:00
|
|
|
destroy
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(spammer_topic.reload.deleted_at).not_to eq(nil)
|
|
|
|
expect(spammer_topic.user_id).to eq(nil)
|
2014-02-13 11:42:35 -05:00
|
|
|
end
|
2014-10-20 10:59:06 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when delete_as_spammer is true" do
|
2014-10-20 10:59:06 -04:00
|
|
|
before { destroy_opts[:delete_as_spammer] = true }
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
it "approves reviewable flags" do
|
2020-12-22 01:30:37 -05:00
|
|
|
spammer_post = Fabricate(:post, user: user)
|
|
|
|
reviewable = PostActionCreator.inappropriate(admin, spammer_post).reviewable
|
2019-01-03 12:03:01 -05:00
|
|
|
expect(reviewable).to be_pending
|
2014-10-20 10:59:06 -04:00
|
|
|
|
|
|
|
destroy
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
reviewable.reload
|
|
|
|
expect(reviewable).to be_approved
|
2014-10-20 10:59:06 -04:00
|
|
|
end
|
|
|
|
end
|
2013-04-29 11:38:43 -04:00
|
|
|
end
|
2013-09-04 15:35:10 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when users deletes self" do
|
2020-12-22 01:30:37 -05:00
|
|
|
subject(:destroy) { UserDestroyer.new(user).destroy(user, destroy_opts) }
|
2014-02-13 11:42:35 -05:00
|
|
|
|
|
|
|
include_examples "successfully destroy a user"
|
|
|
|
include_examples "email block list"
|
2013-09-04 15:35:10 -04:00
|
|
|
|
2014-02-13 11:42:35 -05:00
|
|
|
it "deletes the posts" do
|
|
|
|
destroy
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(post.reload.deleted_at).not_to eq(nil)
|
|
|
|
expect(post.user_id).to eq(nil)
|
2014-02-13 11:42:35 -05:00
|
|
|
end
|
2013-09-04 15:35:10 -04:00
|
|
|
end
|
2013-07-24 13:48:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when user was invited" do
|
2021-05-03 12:49:53 -04:00
|
|
|
it "should delete the invite of user" do
|
|
|
|
invite = Fabricate(:invite)
|
|
|
|
topic_invite = invite.topic_invites.create!(topic: Fabricate(:topic))
|
|
|
|
invited_group = invite.invited_groups.create!(group: Fabricate(:group))
|
|
|
|
user = Fabricate(:user)
|
|
|
|
user.user_emails.create!(email: invite.email)
|
|
|
|
|
|
|
|
UserDestroyer.new(admin).destroy(user)
|
|
|
|
|
|
|
|
expect(Invite.exists?(invite.id)).to eq(false)
|
|
|
|
expect(InvitedGroup.exists?(invited_group.id)).to eq(false)
|
|
|
|
expect(TopicInvite.exists?(topic_invite.id)).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when user created category" do
|
2020-12-22 23:45:53 -05:00
|
|
|
let!(:topic) { Fabricate(:topic, user: user) }
|
|
|
|
let!(:first_post) { Fabricate(:post, user: user, topic: topic) }
|
|
|
|
let!(:second_post) { Fabricate(:post, user: user, topic: topic) }
|
|
|
|
let!(:category) { Fabricate(:category, user: user, topic_id: topic.id) }
|
2020-12-22 22:19:30 -05:00
|
|
|
|
|
|
|
it "changes author of first category post to system user and still deletes second post" do
|
2020-12-22 23:45:53 -05:00
|
|
|
UserDestroyer.new(admin).destroy(user, delete_posts: true)
|
2020-12-22 22:19:30 -05:00
|
|
|
|
|
|
|
expect(first_post.reload.deleted_at).to eq(nil)
|
|
|
|
expect(first_post.user_id).to eq(Discourse.system_user.id)
|
|
|
|
|
|
|
|
expect(second_post.reload.deleted_at).not_to eq(nil)
|
|
|
|
expect(second_post.user_id).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when user has no posts, but user_stats table has post_count > 0" do
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:destroy) { UserDestroyer.new(user).destroy(user, delete_posts: false) }
|
|
|
|
|
|
|
|
let(:destroy_opts) { {} }
|
|
|
|
|
2014-08-18 12:07:21 -04:00
|
|
|
before do
|
|
|
|
# out of sync user_stat data shouldn't break UserDestroyer
|
2020-12-22 01:30:37 -05:00
|
|
|
user.user_stat.update_attribute(:post_count, 1)
|
2014-08-18 12:07:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "successfully destroy a user"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when user has deleted posts" do
|
2020-12-22 01:30:37 -05:00
|
|
|
let!(:deleted_post) { Fabricate(:post, user: user, deleted_at: 1.hour.ago) }
|
2023-06-21 10:00:19 -04:00
|
|
|
|
2013-09-03 17:19:29 -04:00
|
|
|
it "should mark the user's deleted posts as belonging to a nuked user" do
|
2020-12-22 01:30:37 -05:00
|
|
|
expect { UserDestroyer.new(admin).destroy(user) }.to change { User.count }.by(-1)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(deleted_post.reload.user_id).to eq(nil)
|
2013-09-03 17:19:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when user has no posts" do
|
|
|
|
context "when destroy succeeds" do
|
2020-12-22 01:30:37 -05:00
|
|
|
subject(:destroy) { UserDestroyer.new(admin).destroy(user) }
|
2013-07-24 13:48:55 -04:00
|
|
|
|
2023-06-21 10:00:19 -04:00
|
|
|
let(:destroy_opts) { {} }
|
|
|
|
|
2013-07-24 13:48:55 -04:00
|
|
|
include_examples "successfully destroy a user"
|
2013-07-25 15:30:03 -04:00
|
|
|
include_examples "email block list"
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when destroy fails" do
|
2020-12-22 01:30:37 -05:00
|
|
|
subject(:destroy) { UserDestroyer.new(admin).destroy(user) }
|
2013-07-24 13:48:55 -04:00
|
|
|
|
2013-04-11 16:04:20 -04:00
|
|
|
it "should not log the action" do
|
2020-12-22 01:30:37 -05:00
|
|
|
user.stubs(:destroy).returns(false)
|
2013-07-23 17:58:26 -04:00
|
|
|
StaffActionLogger.any_instance.expects(:log_user_deletion).never
|
2013-04-11 16:04:20 -04:00
|
|
|
destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-08-14 11:05:53 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when user has posts with links" do
|
|
|
|
context "with external links" do
|
2013-08-14 11:05:53 -04:00
|
|
|
before do
|
2020-12-22 01:30:37 -05:00
|
|
|
@post = Fabricate(:post_with_external_links, user: user)
|
2013-08-14 11:05:53 -04:00
|
|
|
TopicLink.extract_from(@post)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't add ScreenedUrl records by default" do
|
|
|
|
ScreenedUrl.expects(:watch).never
|
2020-12-22 01:30:37 -05:00
|
|
|
UserDestroyer.new(admin).destroy(user, delete_posts: true)
|
2013-08-14 11:05:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "adds ScreenedUrl records when :block_urls is true" do
|
2013-08-22 19:04:17 -04:00
|
|
|
ScreenedUrl.expects(:watch).with(anything, anything, has_key(:ip_address)).at_least_once
|
2020-12-22 01:30:37 -05:00
|
|
|
UserDestroyer.new(admin).destroy(user, delete_posts: true, block_urls: true)
|
2013-08-14 11:05:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with internal links" do
|
2013-08-14 11:05:53 -04:00
|
|
|
before do
|
2020-12-22 01:30:37 -05:00
|
|
|
@post = Fabricate(:post_with_external_links, user: user)
|
2013-08-14 11:05:53 -04:00
|
|
|
TopicLink.extract_from(@post)
|
2021-02-01 16:57:31 -05:00
|
|
|
TopicLink.where(user: user).update_all(internal: true)
|
2013-08-14 11:05:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't add ScreenedUrl records" do
|
|
|
|
ScreenedUrl.expects(:watch).never
|
2020-12-22 01:30:37 -05:00
|
|
|
UserDestroyer.new(admin).destroy(user, delete_posts: true, block_urls: true)
|
2013-08-14 11:05:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with oneboxed links" do
|
|
|
|
before do
|
2020-12-22 01:30:37 -05:00
|
|
|
@post = Fabricate(:post_with_youtube, user: user)
|
2013-08-14 11:05:53 -04:00
|
|
|
TopicLink.extract_from(@post)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't add ScreenedUrl records" do
|
|
|
|
ScreenedUrl.expects(:watch).never
|
2020-12-22 01:30:37 -05:00
|
|
|
UserDestroyer.new(admin).destroy(user, delete_posts: true, block_urls: true)
|
2013-08-14 11:05:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-10-21 14:49:51 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with ip address screening" do
|
2013-10-21 14:49:51 -04:00
|
|
|
it "doesn't create screened_ip_address records by default" do
|
|
|
|
ScreenedIpAddress.expects(:watch).never
|
2020-12-22 01:30:37 -05:00
|
|
|
UserDestroyer.new(admin).destroy(user)
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when block_ip is true" do
|
2014-04-29 14:37:56 -04:00
|
|
|
it "creates a new screened_ip_address record" do
|
2020-12-22 01:30:37 -05:00
|
|
|
ScreenedIpAddress.expects(:watch).with(user.ip_address).returns(stub_everything)
|
|
|
|
UserDestroyer.new(admin).destroy(user, block_ip: true)
|
2014-04-29 14:37:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "creates two new screened_ip_address records when registration_ip_address is different than last ip_address" do
|
2020-12-22 01:30:37 -05:00
|
|
|
user.registration_ip_address = "12.12.12.12"
|
|
|
|
ScreenedIpAddress.expects(:watch).with(user.ip_address).returns(stub_everything)
|
|
|
|
ScreenedIpAddress
|
|
|
|
.expects(:watch)
|
|
|
|
.with(user.registration_ip_address)
|
|
|
|
.returns(stub_everything)
|
|
|
|
UserDestroyer.new(admin).destroy(user, block_ip: true)
|
2014-04-29 14:37:56 -04:00
|
|
|
end
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
end
|
2013-11-01 16:55:56 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when user created a category" do
|
2020-12-22 01:30:37 -05:00
|
|
|
let!(:category) { Fabricate(:category_with_definition, user: user) }
|
2013-11-01 16:55:56 -04:00
|
|
|
|
|
|
|
it "assigns the system user to the categories" do
|
2020-12-22 01:30:37 -05:00
|
|
|
UserDestroyer.new(admin).destroy(user, delete_posts: true)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(category.reload.user_id).to eq(Discourse.system_user.id)
|
|
|
|
expect(category.topic).to be_present
|
|
|
|
expect(category.topic.user_id).to eq(Discourse.system_user.id)
|
2013-11-01 16:55:56 -04:00
|
|
|
end
|
|
|
|
end
|
2014-03-31 14:06:25 -04:00
|
|
|
|
2020-03-11 20:16:00 -04:00
|
|
|
describe "Destroying a user with security key" do
|
|
|
|
let!(:security_key) { Fabricate(:user_security_key_with_random_credential, user: user) }
|
|
|
|
|
|
|
|
it "removes the security key" do
|
|
|
|
UserDestroyer.new(admin).destroy(user)
|
|
|
|
expect(UserSecurityKey.where(user_id: user.id).count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Destroying a user with a bookmark" do
|
|
|
|
let!(:bookmark) { Fabricate(:bookmark, user: user) }
|
|
|
|
|
|
|
|
it "removes the bookmark" do
|
|
|
|
UserDestroyer.new(admin).destroy(user)
|
|
|
|
expect(Bookmark.where(user_id: user.id).count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when user liked things" do
|
2014-03-31 14:06:25 -04:00
|
|
|
before do
|
|
|
|
@topic = Fabricate(:topic, user: Fabricate(:user))
|
|
|
|
@post = Fabricate(:post, user: @topic.user, topic: @topic)
|
2020-12-22 01:30:37 -05:00
|
|
|
PostActionCreator.like(user, @post)
|
2014-03-31 14:06:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should destroy the like" do
|
2020-12-22 01:30:37 -05:00
|
|
|
expect { UserDestroyer.new(admin).destroy(user, delete_posts: true) }.to change {
|
2014-03-31 14:06:25 -04:00
|
|
|
PostAction.count
|
|
|
|
}.by(-1)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(@post.reload.like_count).to eq(0)
|
2014-03-31 14:06:25 -04:00
|
|
|
end
|
|
|
|
end
|
2018-03-14 15:11:35 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when user belongs to groups that grant trust level" do
|
2020-07-27 12:40:10 -04:00
|
|
|
let(:group) { Fabricate(:group, grant_trust_level: 4) }
|
2018-03-14 15:11:35 -04:00
|
|
|
|
|
|
|
before { group.add(user) }
|
|
|
|
|
|
|
|
it "can delete the user" do
|
|
|
|
d = UserDestroyer.new(admin)
|
|
|
|
expect { d.destroy(user) }.to change { User.count }.by(-1)
|
|
|
|
end
|
2020-06-22 15:43:59 -04:00
|
|
|
|
2020-07-27 12:40:10 -04:00
|
|
|
it "can delete the user if they have a manual locked trust level and have no email" do
|
|
|
|
user.update(manual_locked_trust_level: 3)
|
|
|
|
|
|
|
|
UserEmail.where(user: user).delete_all
|
|
|
|
user.reload
|
|
|
|
expect { UserDestroyer.new(admin).destroy(user) }.to change { User.count }.by(-1)
|
|
|
|
end
|
|
|
|
|
2020-06-22 15:43:59 -04:00
|
|
|
it "can delete the user if they were to fall into another trust level and have no email" do
|
|
|
|
g2 = Fabricate(:group, grant_trust_level: 1)
|
|
|
|
g2.add(user)
|
|
|
|
|
|
|
|
UserEmail.where(user: user).delete_all
|
|
|
|
user.reload
|
|
|
|
expect { UserDestroyer.new(admin).destroy(user) }.to change { User.count }.by(-1)
|
|
|
|
end
|
2018-03-14 15:11:35 -04:00
|
|
|
end
|
2018-10-02 13:46:43 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when user has staff action logs" do
|
2018-10-02 13:46:43 -04:00
|
|
|
before do
|
|
|
|
logger = StaffActionLogger.new(user)
|
|
|
|
logger.log_site_setting_change(
|
|
|
|
"site_description",
|
|
|
|
"Our friendly community",
|
2022-05-11 08:39:31 -04:00
|
|
|
"My favourite community",
|
|
|
|
)
|
|
|
|
logger.log_site_setting_change(
|
|
|
|
"site_description",
|
|
|
|
"Our friendly community",
|
|
|
|
"My favourite community",
|
|
|
|
details: "existing details",
|
2018-10-02 13:46:43 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should keep the staff action log and add the username" do
|
|
|
|
username = user.username
|
2022-05-11 08:39:31 -04:00
|
|
|
ids =
|
|
|
|
UserHistory.staff_action_records(Discourse.system_user, acting_user: username).map(&:id)
|
2018-10-02 13:46:43 -04:00
|
|
|
UserDestroyer.new(admin).destroy(user, delete_posts: true)
|
2022-05-11 08:39:31 -04:00
|
|
|
details = UserHistory.where(id: ids).map(&:details)
|
|
|
|
expect(details).to contain_exactly(
|
|
|
|
"\nuser_id: #{user.id}\nusername: #{username}",
|
|
|
|
"existing details\nuser_id: #{user.id}\nusername: #{username}",
|
|
|
|
)
|
2018-10-02 13:46:43 -04:00
|
|
|
end
|
|
|
|
end
|
2021-02-22 08:07:47 -05:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when user got an email" do
|
2021-02-22 08:07:47 -05:00
|
|
|
let!(:email_log) { Fabricate(:email_log, user: user) }
|
|
|
|
|
|
|
|
it "does not delete the email log" do
|
|
|
|
expect { UserDestroyer.new(admin).destroy(user, delete_posts: true) }.to_not change {
|
|
|
|
EmailLog.count
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
end
|