2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe GroupMessage do
|
2013-06-11 01:17:09 +08:00
|
|
|
let(:moderators_group) { Group[:moderators].name }
|
|
|
|
|
|
|
|
let!(:admin) { Fabricate.build(:admin, id: 999) }
|
|
|
|
let!(:user) { Fabricate.build(:user, id: 111) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Discourse.stubs(:system_user).returns(admin)
|
|
|
|
end
|
|
|
|
|
2017-11-11 01:18:08 +08:00
|
|
|
subject(:send_group_message) { GroupMessage.create(moderators_group, :user_automatically_silenced, user: user) }
|
2013-06-11 01:17:09 +08:00
|
|
|
|
|
|
|
describe 'not sent recently' do
|
|
|
|
before { GroupMessage.any_instance.stubs(:sent_recently?).returns(false) }
|
|
|
|
|
|
|
|
it 'should send a private message to the given group' do
|
|
|
|
PostCreator.expects(:create).with do |from_user, opts|
|
|
|
|
from_user.id == (admin.id) &&
|
|
|
|
opts[:target_group_names] && opts[:target_group_names].include?(Group[:moderators].name) &&
|
|
|
|
opts[:archetype] == (Archetype.private_message) &&
|
|
|
|
opts[:title].present? &&
|
|
|
|
opts[:raw].present?
|
|
|
|
end.returns(stub_everything)
|
|
|
|
send_group_message
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns whatever PostCreator returns' do
|
|
|
|
the_output = stub_everything
|
|
|
|
PostCreator.stubs(:create).returns(the_output)
|
|
|
|
expect(send_group_message).to eq(the_output)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "remembers that it was sent so it doesn't spam the group with the same message" do
|
|
|
|
PostCreator.stubs(:create).returns(stub_everything)
|
|
|
|
GroupMessage.any_instance.expects(:remember_message_sent)
|
|
|
|
send_group_message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'sent recently' do
|
|
|
|
before { GroupMessage.any_instance.stubs(:sent_recently?).returns(true) }
|
2017-11-11 01:18:08 +08:00
|
|
|
subject { GroupMessage.create(moderators_group, :user_automatically_silenced, user: user) }
|
2013-06-11 01:17:09 +08:00
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
it { is_expected.to eq(false) }
|
2013-06-11 01:17:09 +08:00
|
|
|
|
|
|
|
it 'should not send the same notification again' do
|
|
|
|
PostCreator.expects(:create).never
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'message_params' do
|
|
|
|
let(:user) { Fabricate.build(:user, id: 123123) }
|
|
|
|
shared_examples 'common message params for group messages' do
|
|
|
|
it 'returns the correct params' do
|
|
|
|
expect(subject[:username]).to eq(user.username)
|
|
|
|
expect(subject[:user_url]).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context 'with user_automatically_silenced' do
|
2017-11-11 01:18:08 +08:00
|
|
|
subject { GroupMessage.new(moderators_group, :user_automatically_silenced, user: user).message_params }
|
2013-06-11 01:17:09 +08:00
|
|
|
include_examples 'common message params for group messages'
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context 'with spam_post_blocked' do
|
2013-06-11 01:17:09 +08:00
|
|
|
subject { GroupMessage.new(moderators_group, :spam_post_blocked, user: user).message_params }
|
|
|
|
include_examples 'common message params for group messages'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'methods that use redis' do
|
|
|
|
let(:user) { Fabricate.build(:user, id: 123123) }
|
2017-11-11 01:18:08 +08:00
|
|
|
subject(:group_message) { GroupMessage.new(moderators_group, :user_automatically_silenced, user: user) }
|
2013-06-11 01:17:09 +08:00
|
|
|
before do
|
|
|
|
PostCreator.stubs(:create).returns(stub_everything)
|
|
|
|
group_message.stubs(:sent_recently_key).returns('the_key')
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'sent_recently?' do
|
|
|
|
it 'returns true if redis says so' do
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.stubs(:get).with(group_message.sent_recently_key).returns('1')
|
2014-09-25 23:44:48 +08:00
|
|
|
expect(group_message.sent_recently?).to be_truthy
|
2013-06-11 01:17:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if redis returns nil' do
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.stubs(:get).with(group_message.sent_recently_key).returns(nil)
|
2014-09-25 23:44:48 +08:00
|
|
|
expect(group_message.sent_recently?).to be_falsey
|
2013-06-11 01:17:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'always returns false if limit_once_per is false' do
|
2017-11-11 01:18:08 +08:00
|
|
|
gm = GroupMessage.new(moderators_group, :user_automatically_silenced, user: user, limit_once_per: false)
|
2013-06-11 01:17:09 +08:00
|
|
|
gm.stubs(:sent_recently_key).returns('the_key')
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.stubs(:get).with(gm.sent_recently_key).returns('1')
|
2014-09-25 23:44:48 +08:00
|
|
|
expect(gm.sent_recently?).to be_falsey
|
2013-06-11 01:17:09 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'remember_message_sent' do
|
|
|
|
it 'stores a key in redis that expires after 24 hours' do
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.expects(:setex).with(group_message.sent_recently_key, 24 * 60 * 60, anything).returns('OK')
|
2013-06-11 01:17:09 +08:00
|
|
|
group_message.remember_message_sent
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can use a given expiry time' do
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.expects(:setex).with(anything, 30 * 60, anything).returns('OK')
|
2017-11-11 01:18:08 +08:00
|
|
|
GroupMessage.new(moderators_group, :user_automatically_silenced, user: user, limit_once_per: 30.minutes).remember_message_sent
|
2013-06-11 01:17:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can be disabled' do
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.expects(:setex).never
|
2017-11-11 01:18:08 +08:00
|
|
|
GroupMessage.new(moderators_group, :user_automatically_silenced, user: user, limit_once_per: false).remember_message_sent
|
2013-06-11 01:17:09 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|