+ deleted_trace_file = create(:trace, :deleted)
+
+ # Try first with no auth, as it should require it
+ get :api_read, :params => { :id => 0 }
+ assert_response :unauthorized
+
+ # Login, and try again
+ basic_authorization(deleted_trace_file.user.display_name, "test")
+ get :api_read, :params => { :id => 0 }
+ assert_response :not_found
+
+ # Now try a trace which did exist but has been deleted
+ basic_authorization(deleted_trace_file.user.display_name, "test")
+ get :api_read, :params => { :id => deleted_trace_file.id }
+ assert_response :not_found
+ end
+
+ # Test downloading a trace through the api
+ def test_api_data
+ public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
+
+ # First with no auth
+ get :api_data, :params => { :id => public_trace_file.id }
+ assert_response :unauthorized
+
+ # Now with some other user, which should work since the trace is public
+ basic_authorization(create(:user).display_name, "test")
+ get :api_data, :params => { :id => public_trace_file.id }
+ check_trace_data public_trace_file
+
+ # And finally we should be able to do it with the owner of the trace
+ basic_authorization(public_trace_file.user.display_name, "test")
+ get :api_data, :params => { :id => public_trace_file.id }
+ check_trace_data public_trace_file
+ end
+
+ # Test downloading a compressed trace through the api
+ def test_api_data_compressed
+ identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
+
+ # Authenticate as the owner of the trace we will be using
+ basic_authorization(identifiable_trace_file.user.display_name, "test")
+
+ # First get the data as is
+ get :api_data, :params => { :id => identifiable_trace_file.id }
+ check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
+
+ # Now ask explicitly for XML format
+ get :api_data, :params => { :id => identifiable_trace_file.id, :format => "xml" }
+ check_trace_data identifiable_trace_file, "application/xml", "xml"
+
+ # Now ask explicitly for GPX format
+ get :api_data, :params => { :id => identifiable_trace_file.id, :format => "gpx" }
+ check_trace_data identifiable_trace_file
+ end
+
+ # Check an anonymous trace can't be downloaded by another user through the api
+ def test_api_data_anon
+ anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
+
+ # First with no auth
+ get :api_data, :params => { :id => anon_trace_file.id }
+ assert_response :unauthorized
+
+ # Now with some other user, which shouldn't work since the trace is anon
+ basic_authorization(create(:user).display_name, "test")
+ get :api_data, :params => { :id => anon_trace_file.id }
+ assert_response :forbidden
+
+ # And finally we should be able to do it with the owner of the trace
+ basic_authorization(anon_trace_file.user.display_name, "test")
+ get :api_data, :params => { :id => anon_trace_file.id }
+ check_trace_data anon_trace_file
+ end
+
+ # Test downloading a trace that doesn't exist through the api
+ def test_api_data_not_found
+ deleted_trace_file = create(:trace, :deleted)
+
+ # Try first with no auth, as it should require it
+ get :api_data, :params => { :id => 0 }