From d0ec5d143726daf3f98b0ca8d4f5aab24c897023 Mon Sep 17 00:00:00 2001 From: Gregory Igelmund Date: Wed, 13 Dec 2023 13:09:39 -0500 Subject: [PATCH] Add tests & factories for UserMute feature --- .../controllers/user_mutes_controller_test.rb | 60 +++++++++++++++++++ test/factories/messages.rb | 4 ++ test/factories/user_mute.rb | 6 ++ test/models/message_test.rb | 20 +++++++ test/models/user_mute_test.rb | 24 ++++++++ test/system/user_muting_test.rb | 44 ++++++++++++++ 6 files changed, 158 insertions(+) create mode 100644 test/controllers/user_mutes_controller_test.rb create mode 100644 test/factories/user_mute.rb create mode 100644 test/models/user_mute_test.rb create mode 100644 test/system/user_muting_test.rb diff --git a/test/controllers/user_mutes_controller_test.rb b/test/controllers/user_mutes_controller_test.rb new file mode 100644 index 000000000..cc22faaaa --- /dev/null +++ b/test/controllers/user_mutes_controller_test.rb @@ -0,0 +1,60 @@ +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 diff --git a/test/factories/messages.rb b/test/factories/messages.rb index 4f8ee5437..906c4dea9 100644 --- a/test/factories/messages.rb +++ b/test/factories/messages.rb @@ -14,5 +14,9 @@ FactoryBot.define do trait :read do message_read { true } end + + trait :muted do + muted { true } + end end end diff --git a/test/factories/user_mute.rb b/test/factories/user_mute.rb new file mode 100644 index 000000000..4beaf3a23 --- /dev/null +++ b/test/factories/user_mute.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :user_mute do + owner :factory => :user + subject :factory => :user + end +end diff --git a/test/models/message_test.rb b/test/models/message_test.rb index 97afad56e..83cc1251d 100644 --- a/test/models/message_test.rb +++ b/test/models/message_test.rb @@ -175,6 +175,26 @@ class MessageTest < ActiveSupport::TestCase 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) diff --git a/test/models/user_mute_test.rb b/test/models/user_mute_test.rb new file mode 100644 index 000000000..ccc68110a --- /dev/null +++ b/test/models/user_mute_test.rb @@ -0,0 +1,24 @@ +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 diff --git a/test/system/user_muting_test.rb b/test/system/user_muting_test.rb new file mode 100644 index 000000000..ce4346115 --- /dev/null +++ b/test/system/user_muting_test.rb @@ -0,0 +1,44 @@ +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 -- 2.39.5