]> git.openstreetmap.org Git - rails.git/blobdiff - test/controllers/traces_controller_test.rb
Fix rubocop-minitest warnings
[rails.git] / test / controllers / traces_controller_test.rb
index 84a6557946eb598d6be0e1a1d2de212bfbd32148..7765bde46c47a2f45f662d1e3667c74383436a15 100644 (file)
@@ -1,10 +1,20 @@
 require "test_helper"
-require "minitest/mock"
 
 class TracesControllerTest < ActionController::TestCase
+  # Use temporary directories with unique names for each test
+  # This allows the tests to be run in parallel.
+  def setup
+    @gpx_trace_dir_orig = Settings.gpx_trace_dir
+    @gpx_image_dir_orig = Settings.gpx_image_dir
+    Settings.gpx_trace_dir = Dir.mktmpdir("trace", Rails.root.join("test/gpx"))
+    Settings.gpx_image_dir = Dir.mktmpdir("image", Rails.root.join("test/gpx"))
+  end
+
   def teardown
-    File.unlink(*Dir.glob(File.join(Settings.gpx_trace_dir, "*.gpx")))
-    File.unlink(*Dir.glob(File.join(Settings.gpx_image_dir, "*.gif")))
+    FileUtils.remove_dir(Settings.gpx_trace_dir)
+    FileUtils.remove_dir(Settings.gpx_image_dir)
+    Settings.gpx_trace_dir = @gpx_trace_dir_orig
+    Settings.gpx_image_dir = @gpx_image_dir_orig
   end
 
   ##
@@ -115,8 +125,8 @@ class TracesControllerTest < ActionController::TestCase
       { :controller => "traces", :action => "update", :id => "1" }
     )
     assert_routing(
-      { :path => "/trace/1/delete", :method => :post },
-      { :controller => "traces", :action => "delete", :id => "1" }
+      { :path => "/traces/1", :method => :delete },
+      { :controller => "traces", :action => "destroy", :id => "1" }
     )
   end
 
@@ -532,7 +542,7 @@ class TracesControllerTest < ActionController::TestCase
   # Test creating a trace
   def test_create_post
     # Get file to use
-    fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
+    fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
     file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
     user = create(:user)
 
@@ -555,7 +565,7 @@ class TracesControllerTest < ActionController::TestCase
     assert_equal "New Trace", trace.description
     assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
     assert_equal "trackable", trace.visibility
-    assert_equal false, trace.inserted
+    assert_not trace.inserted
     assert_equal File.new(fixture).read, File.new(trace.trace_name).read
     trace.destroy
     assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
@@ -564,7 +574,7 @@ class TracesControllerTest < ActionController::TestCase
   # Test creating a trace with validation errors
   def test_create_post_with_validation_errors
     # Get file to use
-    fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
+    fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
     file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
     user = create(:user)
 
@@ -637,43 +647,43 @@ class TracesControllerTest < ActionController::TestCase
     assert_equal new_details[:visibility], trace.visibility
   end
 
-  # Test deleting a trace
-  def test_delete
+  # Test destroying a trace
+  def test_destroy
     public_trace_file = create(:trace, :visibility => "public")
     deleted_trace_file = create(:trace, :deleted)
 
     # First with no auth
-    post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
+    delete :destroy, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
     assert_response :forbidden
 
     # Now with some other user, which should fail
-    post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
+    delete :destroy, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
     assert_response :forbidden
 
     # Now with a trace which doesn't exist
-    post :delete, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
+    delete :destroy, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
     assert_response :not_found
 
     # Now with a trace has already been deleted
-    post :delete, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
+    delete :destroy, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
     assert_response :not_found
 
     # Now with a trace that we are allowed to delete
-    post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
+    delete :destroy, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
     assert_response :redirect
     assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
     trace = Trace.find(public_trace_file.id)
-    assert_equal false, trace.visible
+    assert_not trace.visible
 
-    # Finally with a trace that is deleted by an admin
+    # Finally with a trace that is destroyed by an admin
     public_trace_file = create(:trace, :visibility => "public")
     admin = create(:administrator_user)
 
-    post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => admin }
+    delete :destroy, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => admin }
     assert_response :redirect
     assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
     trace = Trace.find(public_trace_file.id)
-    assert_equal false, trace.visible
+    assert_not trace.visible
   end
 
   private
@@ -681,7 +691,7 @@ class TracesControllerTest < ActionController::TestCase
   def check_trace_feed(traces)
     assert_response :success
     assert_template "georss"
-    assert_equal "application/rss+xml", @response.content_type
+    assert_equal "application/rss+xml", @response.media_type
     assert_select "rss", :count => 1 do
       assert_select "channel", :count => 1 do
         assert_select "title"
@@ -711,7 +721,7 @@ class TracesControllerTest < ActionController::TestCase
         assert_select "tr", :count => traces.length do |rows|
           traces.zip(rows).each do |trace, row|
             assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
-            assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
+            assert_select row, "span", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
             assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
             assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
           end
@@ -736,19 +746,19 @@ class TracesControllerTest < ActionController::TestCase
   def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
     assert_response :success
     assert_equal digest, Digest::MD5.hexdigest(response.body)
-    assert_equal content_type, response.content_type
-    assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
+    assert_equal content_type, response.media_type
+    assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"; filename*=UTF-8''#{trace.id}.#{extension}", @response.header["Content-Disposition"]
   end
 
   def check_trace_picture(trace)
     assert_response :success
-    assert_equal "image/gif", response.content_type
+    assert_equal "image/gif", response.media_type
     assert_equal trace.large_picture, response.body
   end
 
   def check_trace_icon(trace)
     assert_response :success
-    assert_equal "image/gif", response.content_type
+    assert_equal "image/gif", response.media_type
     assert_equal trace.icon_picture, response.body
   end
 end