2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-10 12:18:08 -05:00
|
|
|
describe UserSilencer do
|
2020-09-13 21:47:57 -04:00
|
|
|
fab!(:user) { Fabricate(:user, trust_level: 0) }
|
|
|
|
fab!(:post) { Fabricate(:post, user: user) }
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2013-07-02 14:42:30 -04:00
|
|
|
|
2017-11-10 12:18:08 -05:00
|
|
|
describe 'silence' do
|
2020-09-13 21:47:57 -04:00
|
|
|
|
2017-11-13 13:41:36 -05:00
|
|
|
let(:silencer) { UserSilencer.new(user) }
|
2017-11-10 12:18:08 -05:00
|
|
|
subject(:silence_user) { silencer.silence }
|
2013-07-02 14:42:30 -04:00
|
|
|
|
2020-09-13 21:47:57 -04:00
|
|
|
it 'silences the user correctly' do
|
|
|
|
expect { UserSilencer.silence(user, admin) }.to change { user.reload.silenced? }
|
2013-07-02 14:42:30 -04:00
|
|
|
|
2020-09-13 21:47:57 -04:00
|
|
|
# no need to silence as we are already silenced
|
|
|
|
expect { UserSilencer.silence(user) }.not_to change { Post.count }
|
2013-07-02 14:42:30 -04:00
|
|
|
|
2020-09-13 21:47:57 -04:00
|
|
|
# post should be hidden
|
|
|
|
post.reload
|
|
|
|
expect(post.topic.visible).to eq(false)
|
|
|
|
expect(post.hidden).to eq(true)
|
2013-07-02 14:42:30 -04:00
|
|
|
|
2020-09-13 21:47:57 -04:00
|
|
|
# history should be right
|
|
|
|
count = UserHistory.where(
|
|
|
|
action: UserHistory.actions[:silence_user],
|
|
|
|
acting_user_id: admin.id,
|
|
|
|
target_user_id: user.id
|
|
|
|
).count
|
2013-07-02 14:42:30 -04:00
|
|
|
|
2020-09-13 21:47:57 -04:00
|
|
|
expect(count).to eq(1)
|
2013-07-02 14:42:30 -04:00
|
|
|
end
|
|
|
|
|
2021-03-01 18:18:09 -05:00
|
|
|
it "skips sending the email for the silence PM via post alert" do
|
|
|
|
NotificationEmailer.enable
|
|
|
|
Jobs.run_immediately!
|
|
|
|
UserSilencer.silence(user, admin)
|
|
|
|
expect(ActionMailer::Base.deliveries.size).to eq(0)
|
|
|
|
end
|
|
|
|
|
2020-09-13 21:47:57 -04:00
|
|
|
it 'does not hide posts for tl1' do
|
|
|
|
user.update!(trust_level: 1)
|
|
|
|
|
|
|
|
UserSilencer.silence(user, admin)
|
|
|
|
|
|
|
|
post.reload
|
|
|
|
expect(post.topic.visible).to eq(true)
|
|
|
|
expect(post.hidden).to eq(false)
|
2013-07-02 14:42:30 -04:00
|
|
|
end
|
2013-08-02 10:55:02 -04:00
|
|
|
|
2020-09-13 21:47:57 -04:00
|
|
|
it "allows us to silence the user for a particular post" do
|
|
|
|
expect(UserSilencer.was_silenced_for?(post)).to eq(false)
|
|
|
|
UserSilencer.new(user, Discourse.system_user, post_id: post.id).silence
|
|
|
|
expect(user).to be_silenced
|
|
|
|
expect(UserSilencer.was_silenced_for?(post)).to eq(true)
|
2013-08-02 10:55:02 -04:00
|
|
|
end
|
2017-01-11 11:41:11 -05:00
|
|
|
|
2020-09-13 21:47:57 -04:00
|
|
|
it "only hides posts from the past 24 hours" do
|
|
|
|
old_post = Fabricate(:post, user: user, created_at: 2.days.ago)
|
|
|
|
|
|
|
|
UserSilencer.new(user, Discourse.system_user, post_id: post.id).silence
|
|
|
|
|
|
|
|
expect(post.reload).to be_hidden
|
|
|
|
expect(post.topic.reload).to_not be_visible
|
|
|
|
old_post.reload
|
|
|
|
expect(old_post).to_not be_hidden
|
|
|
|
expect(old_post.topic).to be_visible
|
2017-01-11 11:41:11 -05:00
|
|
|
end
|
2020-09-01 20:25:24 -04:00
|
|
|
|
|
|
|
context 'with a plugin hook' do
|
|
|
|
before do
|
|
|
|
@override_silence_message = -> (opts) do
|
|
|
|
opts[:silence_message_params][:message_title] = "override title"
|
|
|
|
opts[:silence_message_params][:message_raw] = "override raw"
|
|
|
|
end
|
2020-09-13 21:47:57 -04:00
|
|
|
|
2020-09-01 20:25:24 -04:00
|
|
|
DiscourseEvent.on(:user_silenced, &@override_silence_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
DiscourseEvent.off(:user_silenced, &@override_silence_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows the message to be overridden' do
|
2020-09-13 21:47:57 -04:00
|
|
|
UserSilencer.silence(user, admin)
|
|
|
|
# force a reload in case instance has no posts
|
|
|
|
system_user = User.find(Discourse::SYSTEM_USER_ID)
|
|
|
|
|
|
|
|
post = system_user.posts.order('posts.id desc').first
|
2020-09-12 02:47:06 -04:00
|
|
|
|
2020-09-01 20:25:24 -04:00
|
|
|
expect(post.topic.title).to eq("override title")
|
|
|
|
expect(post.raw).to eq("override raw")
|
|
|
|
end
|
|
|
|
end
|
2013-07-02 14:42:30 -04:00
|
|
|
end
|
|
|
|
|
2017-11-10 12:18:08 -05:00
|
|
|
describe 'unsilence' do
|
2017-01-11 11:41:11 -05:00
|
|
|
|
2020-09-13 21:47:57 -04:00
|
|
|
it 'unsilences the user correctly' do
|
|
|
|
user.update!(silenced_till: 1.year.from_now)
|
2013-07-02 14:42:30 -04:00
|
|
|
|
2020-09-13 21:47:57 -04:00
|
|
|
expect { UserSilencer.unsilence(user, admin) }.to change { user.reload.silenced? }
|
2013-07-02 14:42:30 -04:00
|
|
|
|
2020-09-13 21:47:57 -04:00
|
|
|
# sends a message
|
|
|
|
pm = user.topics_allowed.order('topics.id desc').first
|
|
|
|
title = I18n.t("system_messages.unsilenced.subject_template")
|
|
|
|
expect(pm.title).to eq(title)
|
2016-09-12 11:58:10 -04:00
|
|
|
|
2020-09-13 21:47:57 -04:00
|
|
|
# logs it
|
|
|
|
count = UserHistory.where(
|
|
|
|
action: UserHistory.actions[:unsilence_user],
|
|
|
|
acting_user_id: admin.id,
|
|
|
|
target_user_id: user.id
|
|
|
|
).count
|
2019-02-07 13:46:05 -05:00
|
|
|
|
2020-09-13 21:47:57 -04:00
|
|
|
expect(count).to eq(1)
|
2016-09-12 11:58:10 -04:00
|
|
|
end
|
2017-01-19 15:56:14 -05:00
|
|
|
|
2013-07-02 14:42:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|