1 # frozen_string_literal: true
 
   6   class PicturesControllerTest < ActionDispatch::IntegrationTest
 
   8     # test all routes which lead to this controller
 
  11         { :path => "/user/username/traces/1/picture", :method => :get },
 
  12         { :controller => "traces/pictures", :action => "show", :display_name => "username", :trace_id => "1" }
 
  16     # Test downloading the picture for a trace
 
  18       public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
 
  20       # First with no auth, which should work since the trace is public
 
  21       get trace_picture_path(public_trace_file.user, public_trace_file)
 
  22       check_trace_picture public_trace_file
 
  24       # Now with some other user, which should work since the trace is public
 
  25       session_for(create(:user))
 
  26       get trace_picture_path(public_trace_file.user, public_trace_file)
 
  27       check_trace_picture public_trace_file
 
  29       # And finally we should be able to do it with the owner of the trace
 
  30       session_for(public_trace_file.user)
 
  31       get trace_picture_path(public_trace_file.user, public_trace_file)
 
  32       check_trace_picture public_trace_file
 
  35     # Check the picture for an anonymous trace can't be downloaded by another user
 
  37       anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
 
  40       get trace_picture_path(anon_trace_file.user, anon_trace_file)
 
  41       assert_response :forbidden
 
  43       # Now with some other user, which shouldn't work since the trace is anon
 
  44       session_for(create(:user))
 
  45       get trace_picture_path(anon_trace_file.user, anon_trace_file)
 
  46       assert_response :forbidden
 
  48       # And finally we should be able to do it with the owner of the trace
 
  49       session_for(anon_trace_file.user)
 
  50       get trace_picture_path(anon_trace_file.user, anon_trace_file)
 
  51       check_trace_picture anon_trace_file
 
  54     # Test downloading the picture for a trace that doesn't exist
 
  55     def test_show_not_found
 
  56       deleted_trace_file = create(:trace, :deleted)
 
  58       # First with a trace that has never existed
 
  59       get trace_picture_path(create(:user), 0)
 
  60       assert_response :not_found
 
  62       # Now with a trace that has been deleted
 
  63       session_for(deleted_trace_file.user)
 
  64       get trace_picture_path(deleted_trace_file.user, deleted_trace_file)
 
  65       assert_response :not_found
 
  70     def check_trace_picture(trace)
 
  73       assert_response :success
 
  74       assert_equal "image/gif", response.media_type
 
  75       assert_equal trace.large_picture, response.body