]> git.openstreetmap.org Git - rails.git/commitdiff
Use with_settings for temporary changes to settings
authorTom Hughes <tom@compton.nu>
Thu, 1 Feb 2024 18:37:28 +0000 (18:37 +0000)
committerTom Hughes <tom@compton.nu>
Thu, 1 Feb 2024 18:37:28 +0000 (18:37 +0000)
test/controllers/diary_entries_controller_test.rb
test/controllers/messages_controller_test.rb
test/models/relation_test.rb
test/test_helper.rb

index b474468789d342a5e3a71df904518dd22ad14e79..b83e9b7a0e07e96e287514010daf76e930f891d1 100644 (file)
@@ -677,7 +677,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest
     get diary_rss_path
     assert_select "rss>channel>item", :count => 2
 
     get diary_rss_path
     assert_select "rss>channel>item", :count => 2
 
-    with_diary_feed_delay(6) do
+    with_settings(:diary_feed_delay => 6) do
       get diary_rss_path
       assert_select "rss>channel>item", :count => 1
     end
       get diary_rss_path
       assert_select "rss>channel>item", :count => 1
     end
@@ -1003,13 +1003,4 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest
       assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"
     end
   end
       assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"
     end
   end
-
-  def with_diary_feed_delay(value)
-    diary_feed_delay = Settings.diary_feed_delay
-    Settings.diary_feed_delay = value
-
-    yield
-
-    Settings.diary_feed_delay = diary_feed_delay
-  end
 end
 end
index ca17401fdce4567bb008657278fd514a64750f42..fcfc8da5e30cfb2fcf30bafac9a1e7094e3c965f 100644 (file)
@@ -214,7 +214,7 @@ class MessagesControllerTest < ActionDispatch::IntegrationTest
     # Check that sending a message fails when the message limit is hit
     assert_no_difference "ActionMailer::Base.deliveries.size" do
       assert_no_difference "Message.count" do
     # Check that sending a message fails when the message limit is hit
     assert_no_difference "ActionMailer::Base.deliveries.size" do
       assert_no_difference "Message.count" do
-        with_message_limit(0) do
+        with_settings(:max_messages_per_hour => 0) do
           perform_enqueued_jobs do
             post messages_path(:display_name => recipient_user.display_name,
                                :message => { :title => "Test Message", :body => "Test message body" })
           perform_enqueued_jobs do
             post messages_path(:display_name => recipient_user.display_name,
                                :message => { :title => "Test Message", :body => "Test message body" })
@@ -472,15 +472,4 @@ class MessagesControllerTest < ActionDispatch::IntegrationTest
     assert_response :not_found
     assert_template "no_such_message"
   end
     assert_response :not_found
     assert_template "no_such_message"
   end
-
-  private
-
-  def with_message_limit(value)
-    max_messages_per_hour = Settings.max_messages_per_hour
-    Settings.max_messages_per_hour = value
-
-    yield
-
-    Settings.max_messages_per_hour = max_messages_per_hour
-  end
 end
 end
index 193126df14408e69a0ec3666eab10b8c2d4b5165..575813ad532131ea90e45fe032c2087d4b862258 100644 (file)
@@ -235,22 +235,19 @@ class RelationTest < ActiveSupport::TestCase
   # the maximum number of members in a relation.
   def test_max_members_per_relation_limit
     # Speed up unit test by using a small relation member limit
   # the maximum number of members in a relation.
   def test_max_members_per_relation_limit
     # Speed up unit test by using a small relation member limit
-    default_limit = Settings.max_number_of_relation_members
-    Settings.max_number_of_relation_members = 20
-
-    user = create(:user)
-    changeset = create(:changeset, :user => user)
-    relation = create(:relation, :changeset => changeset)
-    node = create(:node, :longitude => 116, :latitude => 39)
-    # Create relation which exceeds the relation member limit by one
-    0.upto(Settings.max_number_of_relation_members) do |i|
-      create(:relation_member, :relation => relation, :member_type => "Node", :member_id => node.id, :sequence_id => i)
-    end
+    with_settings(:max_number_of_relation_members => 20) do
+      user = create(:user)
+      changeset = create(:changeset, :user => user)
+      relation = create(:relation, :changeset => changeset)
+      node = create(:node, :longitude => 116, :latitude => 39)
+      # Create relation which exceeds the relation member limit by one
+      0.upto(Settings.max_number_of_relation_members) do |i|
+        create(:relation_member, :relation => relation, :member_type => "Node", :member_id => node.id, :sequence_id => i)
+      end
 
 
-    assert_raise OSM::APITooManyRelationMembersError do
-      relation.create_with_history user
+      assert_raise OSM::APITooManyRelationMembersError do
+        relation.create_with_history user
+      end
     end
     end
-
-    Settings.max_number_of_relation_members = default_limit
   end
 end
   end
 end
index 5bf8a5239ecfe2c6625eb969b98c625cf5a81484..5fe1ada472605d04d25717e032a87a02837e363d 100644 (file)
@@ -384,14 +384,11 @@ module ActiveSupport
       Settings.merge!(saved_settings)
     end
 
       Settings.merge!(saved_settings)
     end
 
-    def with_user_account_deletion_delay(value)
+    def with_user_account_deletion_delay(value, &block)
       freeze_time
       freeze_time
-      default_value = Settings.user_account_deletion_delay
-      Settings.user_account_deletion_delay = value
 
 
-      yield
-
-      Settings.user_account_deletion_delay = default_value
+      with_settings(:user_account_deletion_delay => value, &block)
+    ensure
       unfreeze_time
     end
   end
       unfreeze_time
     end
   end