]> git.openstreetmap.org Git - rails.git/blobdiff - test/controllers/api/traces_controller_test.rb
Use resourceful routes for api trace create
[rails.git] / test / controllers / api / traces_controller_test.rb
index de5c309a49868e0425922f9c30a11a96e1f25132..0bc5a8d36ca0a05f6f865c4e22197e67a22338bd 100644 (file)
@@ -6,9 +6,13 @@ module Api
     # test all routes which lead to this controller
     def test_routes
       assert_routing(
-        { :path => "/api/0.6/gpx/create", :method => :post },
+        { :path => "/api/0.6/gpx", :method => :post },
         { :controller => "api/traces", :action => "create" }
       )
+      assert_recognizes(
+        { :controller => "api/traces", :action => "create" },
+        { :path => "/api/0.6/gpx/create", :method => :post }
+      )
       assert_routing(
         { :path => "/api/0.6/gpx/1", :method => :get },
         { :controller => "api/traces", :action => "show", :id => "1" }
@@ -186,7 +190,7 @@ module Api
       user = create(:user)
 
       # First with no auth
-      post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
+      post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
       assert_response :unauthorized
 
       # Rewind the file
@@ -195,16 +199,34 @@ module Api
       # Now authenticated
       create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
       assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
+
       auth_header = bearer_authorization_header user
-      post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }, :headers => auth_header
+
+      # Create trace and import tracepoints in background job
+      perform_enqueued_jobs do
+        post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }, :headers => auth_header
+      end
+
       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_not trace.inserted
+      assert trace.inserted
       assert_equal File.new(fixture).read, trace.file.blob.download
+
+      # Validate tracepoints
+      assert_equal 1, trace.points.size
+      tp = trace.points.first
+      assert_equal 10000000, tp.latitude
+      assert_equal 10000000, tp.longitude
+      assert_equal 3221331576, tp.tile
+      assert_equal 0, tp.trackid
+      assert_in_delta(134.0, tp.altitude)
+      assert_equal DateTime.parse("2008-10-01T10:10:10.000Z"), tp.timestamp
+
       trace.destroy
       assert_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
 
@@ -214,7 +236,7 @@ module Api
       # Now authenticated, with the legacy public flag
       assert_not_equal "public", user.preferences.find_by(:k => "gps.trace.visibility").v
       auth_header = bearer_authorization_header user
-      post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }, :headers => auth_header
+      post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }, :headers => auth_header
       assert_response :success
       trace = Trace.find(response.body.to_i)
       assert_equal "a.gpx", trace.name
@@ -233,7 +255,7 @@ module Api
       second_user = create(:user)
       assert_nil second_user.preferences.find_by(:k => "gps.trace.visibility")
       auth_header = bearer_authorization_header second_user
-      post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }, :headers => auth_header
+      post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }, :headers => auth_header
       assert_response :success
       trace = Trace.find(response.body.to_i)
       assert_equal "a.gpx", trace.name