2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-07-03 02:42:30 +08:00
|
|
|
|
2017-11-11 01:18:08 +08:00
|
|
|
describe UserSilencer do
|
2013-07-03 02:42:30 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
SystemMessage.stubs(:create)
|
|
|
|
end
|
|
|
|
|
2017-11-11 01:18:08 +08:00
|
|
|
describe 'silence' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2017-11-14 02:41:36 +08:00
|
|
|
let(:silencer) { UserSilencer.new(user) }
|
2017-11-11 01:18:08 +08:00
|
|
|
subject(:silence_user) { silencer.silence }
|
2013-07-03 02:42:30 +08:00
|
|
|
|
2017-11-11 01:18:08 +08:00
|
|
|
it 'silences the user' do
|
2013-07-03 02:42:30 +08:00
|
|
|
u = Fabricate(:user)
|
2017-11-11 01:18:08 +08:00
|
|
|
expect { UserSilencer.silence(u) }.to change { u.reload.silenced? }
|
2013-07-03 02:42:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'hides posts' do
|
2017-11-11 01:18:08 +08:00
|
|
|
silencer.expects(:hide_posts)
|
|
|
|
silence_user
|
2013-07-03 02:42:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'given a staff user argument' do
|
2017-11-11 01:18:08 +08:00
|
|
|
it 'sends the correct message to the silenced user' do
|
2013-07-03 02:42:30 +08:00
|
|
|
SystemMessage.unstub(:create)
|
2020-09-02 08:25:24 +08:00
|
|
|
SystemMessage.expects(:create).with(user, :silenced_by_staff, {}).returns(true)
|
2017-11-11 01:18:08 +08:00
|
|
|
UserSilencer.silence(user, Fabricate.build(:admin))
|
2013-07-03 02:42:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'not given a staff user argument' do
|
|
|
|
it 'sends a default message to the user' do
|
|
|
|
SystemMessage.unstub(:create)
|
2020-09-02 08:25:24 +08:00
|
|
|
SystemMessage.expects(:create).with(user, :silenced_by_staff, {}).returns(true)
|
2017-11-11 01:18:08 +08:00
|
|
|
UserSilencer.silence(user, Fabricate.build(:admin))
|
2013-07-03 02:42:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'given a message option' do
|
|
|
|
it 'sends that message to the user' do
|
|
|
|
SystemMessage.unstub(:create)
|
2020-09-02 08:25:24 +08:00
|
|
|
SystemMessage.expects(:create).with(user, :the_custom_message, {}).returns(true)
|
2017-11-11 01:18:08 +08:00
|
|
|
UserSilencer.silence(user, Fabricate.build(:admin), message: :the_custom_message)
|
2013-07-03 02:42:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't send a pm if save fails" do
|
|
|
|
user.stubs(:save).returns(false)
|
|
|
|
SystemMessage.unstub(:create)
|
|
|
|
SystemMessage.expects(:create).never
|
2017-11-11 01:18:08 +08:00
|
|
|
silence_user
|
2013-07-03 02:42:30 +08:00
|
|
|
end
|
2013-08-02 22:55:02 +08:00
|
|
|
|
2017-11-11 01:18:08 +08:00
|
|
|
it "doesn't send a pm if the user is already silenced" do
|
2017-11-14 02:41:36 +08:00
|
|
|
user.silenced_till = 1.year.from_now
|
2013-08-02 22:55:02 +08:00
|
|
|
SystemMessage.unstub(:create)
|
|
|
|
SystemMessage.expects(:create).never
|
2017-11-11 01:18:08 +08:00
|
|
|
expect(silence_user).to eq(false)
|
2013-08-02 22:55:02 +08:00
|
|
|
end
|
2017-01-12 00:41:11 +08:00
|
|
|
|
|
|
|
it "logs it with context" do
|
2018-02-14 04:33:15 +08:00
|
|
|
SystemMessage.stubs(:create)
|
2017-01-12 00:41:11 +08:00
|
|
|
expect {
|
2017-11-11 01:18:08 +08:00
|
|
|
UserSilencer.silence(user, Fabricate(:admin))
|
2017-01-12 00:41:11 +08:00
|
|
|
}.to change { UserHistory.count }.by(1)
|
|
|
|
expect(UserHistory.last.context).to be_present
|
|
|
|
end
|
2020-09-02 08:25:24 +08: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
|
|
|
|
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
|
|
|
|
SystemMessage.unstub(:create)
|
|
|
|
UserSilencer.silence(user, Fabricate.build(:admin))
|
|
|
|
post = Discourse.system_user.posts.last
|
|
|
|
expect(post.topic.title).to eq("override title")
|
|
|
|
expect(post.raw).to eq("override raw")
|
|
|
|
end
|
|
|
|
end
|
2013-07-03 02:42:30 +08:00
|
|
|
end
|
|
|
|
|
2017-11-11 01:18:08 +08:00
|
|
|
describe 'unsilence' do
|
2013-07-03 02:42:30 +08:00
|
|
|
let(:user) { stub_everything(save: true) }
|
2017-11-11 01:18:08 +08:00
|
|
|
subject(:unsilence_user) { UserSilencer.unsilence(user, Fabricate.build(:admin)) }
|
2013-07-03 02:42:30 +08:00
|
|
|
|
2017-11-11 01:18:08 +08:00
|
|
|
it 'unsilences the user' do
|
2017-11-14 02:41:36 +08:00
|
|
|
u = Fabricate(:user, silenced_till: 1.year.from_now)
|
2017-11-11 01:18:08 +08:00
|
|
|
expect { UserSilencer.unsilence(u) }.to change { u.reload.silenced? }
|
2013-07-03 02:42:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sends a message to the user' do
|
|
|
|
SystemMessage.unstub(:create)
|
2017-11-11 01:18:08 +08:00
|
|
|
SystemMessage.expects(:create).with(user, :unsilenced).returns(true)
|
|
|
|
unsilence_user
|
2013-07-03 02:42:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't send a pm if save fails" do
|
|
|
|
user.stubs(:save).returns(false)
|
|
|
|
SystemMessage.unstub(:create)
|
|
|
|
SystemMessage.expects(:create).never
|
2017-11-11 01:18:08 +08:00
|
|
|
unsilence_user
|
2013-07-03 02:42:30 +08:00
|
|
|
end
|
2017-01-12 00:41:11 +08:00
|
|
|
|
|
|
|
it "logs it" do
|
|
|
|
expect {
|
2017-11-11 01:18:08 +08:00
|
|
|
unsilence_user
|
2017-01-12 00:41:11 +08:00
|
|
|
}.to change { UserHistory.count }.by(1)
|
|
|
|
end
|
2013-07-03 02:42:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'hide_posts' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user, trust_level: 0) }
|
|
|
|
fab!(:post) { Fabricate(:post, user: user) }
|
2017-11-11 01:18:08 +08:00
|
|
|
subject { UserSilencer.new(user) }
|
2013-07-03 02:42:30 +08:00
|
|
|
|
|
|
|
it "hides all the user's posts" do
|
2017-11-11 01:18:08 +08:00
|
|
|
subject.silence
|
2013-07-03 02:42:30 +08:00
|
|
|
expect(post.reload).to be_hidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it "hides the topic if the post was the first post" do
|
2017-11-11 01:18:08 +08:00
|
|
|
subject.silence
|
2013-07-03 02:42:30 +08:00
|
|
|
expect(post.topic.reload).to_not be_visible
|
|
|
|
end
|
2016-09-12 23:58:10 +08:00
|
|
|
|
2019-02-08 02:46:05 +08: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)
|
|
|
|
end
|
|
|
|
|
2016-09-12 23:58:10 +08:00
|
|
|
it "doesn't hide posts if user is TL1" do
|
|
|
|
user.trust_level = 1
|
2017-11-11 01:18:08 +08:00
|
|
|
subject.silence
|
2016-09-12 23:58:10 +08:00
|
|
|
expect(post.reload).to_not be_hidden
|
|
|
|
expect(post.topic.reload).to be_visible
|
|
|
|
end
|
2017-01-20 04:56:14 +08:00
|
|
|
|
|
|
|
it "only hides posts from the past 24 hours" do
|
|
|
|
old_post = Fabricate(:post, user: user, created_at: 2.days.ago)
|
2017-11-11 01:18:08 +08:00
|
|
|
subject.silence
|
2017-01-20 04:56:14 +08:00
|
|
|
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
|
|
|
|
end
|
2013-07-03 02:42:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|