]> git.openstreetmap.org Git - rails.git/commitdiff
Add tests & factories for UserMute feature
authorGregory Igelmund <github.com@grekko.de>
Wed, 13 Dec 2023 18:09:39 +0000 (13:09 -0500)
committerGregory Igelmund <github.com@grekko.de>
Tue, 19 Dec 2023 17:57:47 +0000 (12:57 -0500)
test/controllers/user_mutes_controller_test.rb [new file with mode: 0644]
test/factories/messages.rb
test/factories/user_mute.rb [new file with mode: 0644]
test/models/message_test.rb
test/models/user_mute_test.rb [new file with mode: 0644]
test/system/user_muting_test.rb [new file with mode: 0644]

diff --git a/test/controllers/user_mutes_controller_test.rb b/test/controllers/user_mutes_controller_test.rb
new file mode 100644 (file)
index 0000000..cc22faa
--- /dev/null
@@ -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
index 4f8ee5437fc8a86bcb1897ebbe03b4722da3e78a..906c4dea9fada803fc6e15fe9718c7100ac949d9 100644 (file)
@@ -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 (file)
index 0000000..4beaf3a
--- /dev/null
@@ -0,0 +1,6 @@
+FactoryBot.define do
+  factory :user_mute do
+    owner :factory => :user
+    subject :factory => :user
+  end
+end
index 97afad56e17f80c72ec2f71add8c9f03146fb4c3..83cc1251d02d05a8465e802cb34727c029579cdb 100644 (file)
@@ -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 (file)
index 0000000..ccc6811
--- /dev/null
@@ -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 (file)
index 0000000..ce43461
--- /dev/null
@@ -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