--- /dev/null
+require "test_helper"
+
+class UserMutesControllerTest < ActionDispatch::IntegrationTest
+ def test_routes
+ assert_routing(
+ { :path => "/user/username/mute", :method => :post },
+ { :controller => "user_mutes", :action => "create", :display_name => "username" }
+ )
+ assert_routing(
+ { :path => "/user/username/mute", :method => :delete },
+ { :controller => "user_mutes", :action => "destroy", :display_name => "username" }
+ )
+ assert_routing(
+ { :path => "/user_mutes", :method => :get },
+ { :controller => "user_mutes", :action => "index" }
+ )
+ end
+
+ def test_index
+ user = create(:user)
+ user.mutes.create(:subject => create(:user))
+ session_for(user)
+
+ get user_mutes_path
+ assert_match "You have muted 1 User", @response.body
+ end
+
+ def test_create
+ user = create(:user)
+ session_for(user)
+
+ assert_equal 0, user.muted_users.count
+ subject = create(:user, :display_name => "Bob")
+ post user_mute_path(subject)
+ assert_match "You muted Bob", flash[:notice]
+
+ assert_equal 1, user.muted_users.count
+ assert_equal subject, user.muted_users.first
+
+ post user_mute_path(subject)
+ assert_match "Bob could not be muted. Is already muted", flash[:error]
+ assert_equal 1, user.muted_users.count
+ end
+
+ def test_destroy
+ user = create(:user)
+ session_for(user)
+
+ subject = create(:user, :display_name => "Bob")
+ user.mutes.create(:subject => subject)
+ assert_equal 1, user.muted_users.count
+
+ delete user_mute_path(subject)
+ assert_match "You unmuted Bob", flash[:notice]
+ assert_equal 0, user.muted_users.count
+
+ delete user_mute_path(subject)
+ assert_response :not_found
+ end
+end
trait :read do
message_read { true }
end
+
+ trait :muted do
+ muted { true }
+ end
end
end
--- /dev/null
+FactoryBot.define do
+ factory :user_mute do
+ owner :factory => :user
+ subject :factory => :user
+ end
+end
assert_equal "text", message.body_format
end
+ def test_notify_recipient
+ message = create(:message)
+ assert_not_predicate message, :muted?
+ assert_predicate message, :notify_recipient?
+ end
+
+ def test_notify_recipient_for_muted_messages
+ message = create(:message, :muted)
+ assert_predicate message, :muted?
+ assert_not_predicate message, :notify_recipient?
+ end
+
+ def test_unmuting_a_muted_message
+ message = create(:message, :muted)
+ assert_predicate message, :muted?
+
+ message.unmute
+ assert_not_predicate message, :muted?
+ end
+
private
def make_message(char, count)
--- /dev/null
+require "test_helper"
+
+class UserMuteTest < ActiveSupport::TestCase
+ def test_messages_by_muted_users_are_muted
+ user = create(:user)
+ muted_user = create(:user)
+ create(:user_mute, :owner => user, :subject => muted_user)
+
+ message = create(:message, :sender => muted_user, :recipient => user)
+ assert_predicate message, :muted?
+ end
+
+ def test_messages_by_admins_or_moderators_are_never_muted
+ user = create(:user)
+
+ [create(:administrator_user), create(:moderator_user)].each do |admin_or_moderator|
+ create(:user_mute, :owner => user, :subject => admin_or_moderator)
+
+ message = create(:message, :sender => admin_or_moderator, :recipient => user)
+
+ assert_not_predicate message, :muted?
+ end
+ end
+end
--- /dev/null
+require "application_system_test_case"
+
+class UserMutingTest < ApplicationSystemTestCase
+ # NB: loads helpers to verify mailer-related behaviour e.g. via assert_no_emails
+ include ActionMailer::TestHelper
+
+ test "users can mute and unmute other users" do
+ user = create(:user)
+ other_user = create(:user)
+ sign_in_as(user)
+
+ visit user_path(other_user)
+ click_link "Mute this User"
+ assert_content "You muted #{other_user.display_name}"
+
+ visit edit_account_path
+ assert_content "Muted Users"
+ click_link "Muted Users"
+ assert_content "You have muted 1 User"
+ click_link "Unmute"
+
+ assert_content "You unmuted #{other_user.display_name}"
+ refute_content "Muted Users"
+ assert_current_path edit_account_path
+ end
+
+ test "messages sent by muted users are set `muted` and do not cause notification emails" do
+ user = create(:user)
+ muted_user = create(:user)
+ create(:user_mute, :owner => user, :subject => muted_user)
+ sign_in_as(muted_user)
+
+ visit new_message_path(user)
+ fill_in "Subject", :with => "Hey Hey"
+ fill_in "Body", :with => "some message"
+
+ assert_no_emails do
+ click_button "Send"
+ end
+
+ message = Message.find_by(:sender => muted_user, :recipient => user)
+ assert_predicate message, :muted?
+ end
+end