2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe GroupMessage do
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:send_group_message) do
|
|
|
|
GroupMessage.create(moderators_group, :user_automatically_silenced, user: user)
|
|
|
|
end
|
|
|
|
|
2013-06-10 13:17:09 -04:00
|
|
|
let(:moderators_group) { Group[:moderators].name }
|
|
|
|
|
2024-05-16 11:11:24 -04:00
|
|
|
fab!(:admin)
|
|
|
|
fab!(:user)
|
2013-06-10 13:17:09 -04:00
|
|
|
|
|
|
|
before { Discourse.stubs(:system_user).returns(admin) }
|
|
|
|
|
|
|
|
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
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:group_message) do
|
|
|
|
GroupMessage.create(moderators_group, :user_automatically_silenced, user: user)
|
|
|
|
end
|
|
|
|
|
2013-06-10 13:17:09 -04:00
|
|
|
before { GroupMessage.any_instance.stubs(:sent_recently?).returns(true) }
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
it { is_expected.to eq(false) }
|
2013-06-10 13:17:09 -04:00
|
|
|
|
|
|
|
it "should not send the same notification again" do
|
|
|
|
PostCreator.expects(:create).never
|
2023-06-21 10:00:19 -04:00
|
|
|
group_message
|
2013-06-10 13:17:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "message_params" do
|
|
|
|
shared_examples "common message params for group messages" do
|
|
|
|
it "returns the correct params" do
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(message_params[:username]).to eq(user.username)
|
2024-05-16 11:11:24 -04:00
|
|
|
expect(message_params[:user_url]).to eq("/u/#{user.username}")
|
2013-06-10 13:17:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with user_automatically_silenced" do
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:message_params) do
|
2017-11-10 12:18:08 -05:00
|
|
|
GroupMessage.new(moderators_group, :user_automatically_silenced, user: user).message_params
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2023-06-21 10:00:19 -04:00
|
|
|
|
2013-06-10 13:17:09 -04:00
|
|
|
include_examples "common message params for group messages"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with spam_post_blocked" do
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:message_params) do
|
|
|
|
GroupMessage.new(moderators_group, :spam_post_blocked, user: user).message_params
|
|
|
|
end
|
|
|
|
|
2013-06-10 13:17:09 -04:00
|
|
|
include_examples "common message params for group messages"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "methods that use redis" do
|
2017-11-10 12:18:08 -05:00
|
|
|
subject(:group_message) do
|
|
|
|
GroupMessage.new(moderators_group, :user_automatically_silenced, user: user)
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2023-06-21 10:00:19 -04:00
|
|
|
|
2013-06-10 13:17:09 -04: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 04:05:53 -05:00
|
|
|
Discourse.redis.stubs(:get).with(group_message.sent_recently_key).returns("1")
|
2014-09-25 11:44:48 -04:00
|
|
|
expect(group_message.sent_recently?).to be_truthy
|
2013-06-10 13:17:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if redis returns nil" do
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.stubs(:get).with(group_message.sent_recently_key).returns(nil)
|
2014-09-25 11:44:48 -04:00
|
|
|
expect(group_message.sent_recently?).to be_falsey
|
2013-06-10 13:17:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "always returns false if limit_once_per is false" do
|
2017-11-10 12:18:08 -05:00
|
|
|
gm =
|
|
|
|
GroupMessage.new(
|
|
|
|
moderators_group,
|
|
|
|
:user_automatically_silenced,
|
|
|
|
user: user,
|
|
|
|
limit_once_per: false,
|
|
|
|
)
|
2013-06-10 13:17:09 -04:00
|
|
|
gm.stubs(:sent_recently_key).returns("the_key")
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.stubs(:get).with(gm.sent_recently_key).returns("1")
|
2014-09-25 11:44:48 -04:00
|
|
|
expect(gm.sent_recently?).to be_falsey
|
2013-06-10 13:17:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "remember_message_sent" do
|
|
|
|
it "stores a key in redis that expires after 24 hours" do
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse
|
|
|
|
.redis
|
|
|
|
.expects(:setex)
|
|
|
|
.with(group_message.sent_recently_key, 24 * 60 * 60, anything)
|
|
|
|
.returns("OK")
|
2013-06-10 13:17:09 -04:00
|
|
|
group_message.remember_message_sent
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can use a given expiry time" do
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.expects(:setex).with(anything, 30 * 60, anything).returns("OK")
|
2017-11-10 12:18:08 -05:00
|
|
|
GroupMessage.new(
|
|
|
|
moderators_group,
|
|
|
|
:user_automatically_silenced,
|
|
|
|
user: user,
|
|
|
|
limit_once_per: 30.minutes,
|
|
|
|
).remember_message_sent
|
2013-06-10 13:17:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can be disabled" do
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.expects(:setex).never
|
2017-11-10 12:18:08 -05:00
|
|
|
GroupMessage.new(
|
|
|
|
moderators_group,
|
|
|
|
:user_automatically_silenced,
|
|
|
|
user: user,
|
|
|
|
limit_once_per: false,
|
|
|
|
).remember_message_sent
|
2013-06-10 13:17:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|