+ # 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, "848caa72f2f456d1bd6a0fdf228aa1b9"
+
+ # 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, "848caa72f2f456d1bd6a0fdf228aa1b9"
+ 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, "c6422a3d8750faae49ed70e7e8a51b93", "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, "abd6675fdf3024a84fc0a1deac147c0d", "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, "abd6675fdf3024a84fc0a1deac147c0d"
+ 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, "66179ca44f1e93d8df62e2b88cbea732"
+ 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 }
+ assert_response :unauthorized
+
+ # Login, and try again
+ basic_authorization create(:user).display_name, "test"
+ get :api_data, :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_data, :params => { :id => deleted_trace_file.id }
+ assert_response :not_found
+ end
+
+ # Test creating a trace through the api
+ def test_api_create
+ # Get file to use
+ fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
+ file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
+ user = create(:user)
+
+ # First with no auth
+ post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
+ assert_response :unauthorized
+
+ # Rewind the file
+ file.rewind
+
+ # Now authenticated
+ create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
+ assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
+ basic_authorization user.display_name, "test"
+ post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
+ assert_response :success
+ trace = Trace.find(response.body.to_i)
+ assert_equal "a.gpx", trace.name
+ 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_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
+
+ # Rewind the file
+ file.rewind
+
+ # Now authenticated, with the legacy public flag
+ assert_not_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
+ basic_authorization user.display_name, "test"
+ post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }
+ assert_response :success
+ trace = Trace.find(response.body.to_i)
+ assert_equal "a.gpx", trace.name
+ assert_equal "New Trace", trace.description
+ assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
+ assert_equal "public", trace.visibility
+ assert_equal false, trace.inserted
+ assert_equal File.new(fixture).read, File.new(trace.trace_name).read
+ trace.destroy
+ assert_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
+
+ # Rewind the file
+ file.rewind
+
+ # Now authenticated, with the legacy private flag
+ second_user = create(:user)
+ assert_nil second_user.preferences.where(:k => "gps.trace.visibility").first
+ basic_authorization second_user.display_name, "test"
+ post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }
+ assert_response :success
+ trace = Trace.find(response.body.to_i)
+ assert_equal "a.gpx", trace.name
+ assert_equal "New Trace", trace.description
+ assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
+ assert_equal "private", trace.visibility
+ assert_equal false, trace.inserted
+ assert_equal File.new(fixture).read, File.new(trace.trace_name).read
+ trace.destroy
+ assert_equal "private", second_user.preferences.where(:k => "gps.trace.visibility").first.v
+ end
+