DEV: remove stubs from silencer spec

DEV: remove stubs from silencer spec

Overall stubs lead to long term instability, this helps stabilize them.

Also fixed flaky spec around plugin hooks.

It was relying you .posts on system_user which could be loaded already and invalid. Instead we now load it by hand.

diff --git a/spec/services/user_silencer_spec.rb b/spec/services/user_silencer_spec.rb
index 0e4c621..6ed9062 100644
--- a/spec/services/user_silencer_spec.rb
+++ b/spec/services/user_silencer_spec.rb
@@ -3,80 +3,72 @@
 require 'rails_helper'
 
 describe UserSilencer do
-
-  before do
-    SystemMessage.stubs(:create)
-  end
+  fab!(:user) { Fabricate(:user, trust_level: 0) }
+  fab!(:post) { Fabricate(:post, user: user) }
+  fab!(:admin) { Fabricate(:admin) }
 
   describe 'silence' do
-    fab!(:user) { Fabricate(:user) }
+
     let(:silencer) { UserSilencer.new(user) }
     subject(:silence_user) { silencer.silence }
 
-    it 'silences the user' do
-      u = Fabricate(:user)
-      expect { UserSilencer.silence(u) }.to change { u.reload.silenced? }
-    end
+    it 'silences the user correctly' do
+      expect { UserSilencer.silence(user, admin) }.to change { user.reload.silenced? }
 
-    it 'hides posts' do
-      silencer.expects(:hide_posts)
-      silence_user
-    end
+      # no need to silence as we are already silenced
+      expect { UserSilencer.silence(user) }.not_to change { Post.count }
 
-    context 'given a staff user argument' do
-      it 'sends the correct message to the silenced user' do
-        SystemMessage.unstub(:create)
-        SystemMessage.expects(:create).with(user, :silenced_by_staff, {}).returns(true)
-        UserSilencer.silence(user, Fabricate.build(:admin))
-      end
-    end
+      # post should be hidden
+      post.reload
+      expect(post.topic.visible).to eq(false)
+      expect(post.hidden).to eq(true)
 
-    context 'not given a staff user argument' do
-      it 'sends a default message to the user' do
-        SystemMessage.unstub(:create)
-        SystemMessage.expects(:create).with(user, :silenced_by_staff, {}).returns(true)
-        UserSilencer.silence(user, Fabricate.build(:admin))
-      end
-    end
+      # history should be right
+      count = UserHistory.where(
+        action: UserHistory.actions[:silence_user],
+        acting_user_id: admin.id,
+        target_user_id: user.id
+      ).count
 
-    context 'given a message option' do
-      it 'sends that message to the user' do
-        SystemMessage.unstub(:create)
-        SystemMessage.expects(:create).with(user, :the_custom_message, {}).returns(true)
-        UserSilencer.silence(user, Fabricate.build(:admin), message: :the_custom_message)
-      end
+      expect(count).to eq(1)
     end
 
-    it "doesn't send a pm if save fails" do
-      user.stubs(:save).returns(false)
-      SystemMessage.unstub(:create)
-      SystemMessage.expects(:create).never
-      silence_user
+    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)
     end
 
-    it "doesn't send a pm if the user is already silenced" do
-      user.silenced_till = 1.year.from_now
-      SystemMessage.unstub(:create)
-      SystemMessage.expects(:create).never
-      expect(silence_user).to eq(false)
+    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
 
-    it "logs it with context" do
-      SystemMessage.stubs(:create)
-      expect {
-        UserSilencer.silence(user, Fabricate(:admin))
-      }.to change { UserHistory.count }.by(1)
-      expect(UserHistory.last.context).to be_present
+    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
     end
 
     context 'with a plugin hook' do
       before do
-        SystemMessage.unstub(:create)
-
         @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
 
@@ -85,9 +77,12 @@ describe UserSilencer do
       end
 
       it 'allows the message to be overridden' do
-        UserSilencer.silence(user, Fabricate(:admin))
+        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
 
-        post = Discourse.system_user.posts.last
         expect(post.topic.title).to eq("override title")
         expect(post.raw).to eq("override raw")
       end
@@ -95,72 +90,27 @@ describe UserSilencer do
   end
 
   describe 'unsilence' do
-    let(:user)             { stub_everything(save: true) }
-    subject(:unsilence_user) { UserSilencer.unsilence(user, Fabricate.build(:admin)) }
-
-    it 'unsilences the user' do
-      u = Fabricate(:user, silenced_till: 1.year.from_now)
-      expect { UserSilencer.unsilence(u) }.to change { u.reload.silenced? }
-    end
-
-    it 'sends a message to the user' do
-      SystemMessage.unstub(:create)
-      SystemMessage.expects(:create).with(user, :unsilenced).returns(true)
-      unsilence_user
-    end
-
-    it "doesn't send a pm if save fails" do
-      user.stubs(:save).returns(false)
-      SystemMessage.unstub(:create)
-      SystemMessage.expects(:create).never
-      unsilence_user
-    end
 
-    it "logs it" do
-      expect {
-        unsilence_user
-      }.to change { UserHistory.count }.by(1)
-    end
-  end
+    it 'unsilences the user correctly' do
+      user.update!(silenced_till: 1.year.from_now)
 
-  describe 'hide_posts' do
-    fab!(:user)    { Fabricate(:user, trust_level: 0) }
-    fab!(:post)   { Fabricate(:post, user: user) }
-    subject       { UserSilencer.new(user) }
+      expect { UserSilencer.unsilence(user, admin) }.to change { user.reload.silenced? }
 
-    it "hides all the user's posts" do
-      subject.silence
-      expect(post.reload).to be_hidden
-    end
-
-    it "hides the topic if the post was the first post" do
-      subject.silence
-      expect(post.topic.reload).to_not be_visible
-    end
+      # 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)
 
-    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
+      # logs it
+      count = UserHistory.where(
+        action: UserHistory.actions[:unsilence_user],
+        acting_user_id: admin.id,
+        target_user_id: user.id
+      ).count
 
-    it "doesn't hide posts if user is TL1" do
-      user.trust_level = 1
-      subject.silence
-      expect(post.reload).to_not be_hidden
-      expect(post.topic.reload).to be_visible
+      expect(count).to eq(1)
     end
 
-    it "only hides posts from the past 24 hours" do
-      old_post = Fabricate(:post, user: user, created_at: 2.days.ago)
-      subject.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
-    end
   end
 
 end

GitHub sha: 15f7fa80

1 Like