4 class TracesControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/gpx", :method => :post },
10 { :controller => "api/traces", :action => "create" }
13 { :controller => "api/traces", :action => "create" },
14 { :path => "/api/0.6/gpx/create", :method => :post }
17 { :path => "/api/0.6/gpx/1", :method => :get },
18 { :controller => "api/traces", :action => "show", :id => "1" }
21 { :path => "/api/0.6/gpx/1.json", :method => :get },
22 { :controller => "api/traces", :action => "show", :id => "1", :format => "json" }
25 { :path => "/api/0.6/gpx/1", :method => :put },
26 { :controller => "api/traces", :action => "update", :id => "1" }
29 { :path => "/api/0.6/gpx/1", :method => :delete },
30 { :controller => "api/traces", :action => "destroy", :id => "1" }
33 { :controller => "api/traces", :action => "show", :id => "1" },
34 { :path => "/api/0.6/gpx/1/details", :method => :get }
38 # Check getting a specific trace through the api
40 public_trace_file = create(:trace, :visibility => "public")
43 get api_trace_path(public_trace_file)
44 assert_response :unauthorized
46 # Now with some other user, which should work since the trace is public
47 auth_header = bearer_authorization_header
48 get api_trace_path(public_trace_file), :headers => auth_header
49 assert_response :success
51 # We should be able to do it with the owner of the trace
52 auth_header = bearer_authorization_header public_trace_file.user
53 get api_trace_path(public_trace_file), :headers => auth_header
54 assert_response :success
55 assert_select "gpx_file[id='#{public_trace_file.id}'][uid='#{public_trace_file.user.id}']", 1
57 # We should be able to do it with the owner of the trace with json format
58 auth_header = bearer_authorization_header public_trace_file.user
59 get api_trace_path(public_trace_file, :format => "json"), :headers => auth_header
60 assert_response :success
61 assert_equal "application/json", response.media_type
62 js = ActiveSupport::JSON.decode(@response.body)
64 assert_equal public_trace_file.id, js["trace"]["id"]
65 assert_equal public_trace_file.user.id, js["trace"]["uid"]
68 # Check an anonymous trace can't be specifically fetched by another user
70 anon_trace_file = create(:trace, :visibility => "private")
73 get api_trace_path(anon_trace_file)
74 assert_response :unauthorized
76 # Now try with another user, which shouldn't work since the trace is anon
77 auth_header = bearer_authorization_header
78 get api_trace_path(anon_trace_file), :headers => auth_header
79 assert_response :forbidden
81 # And finally we should be able to get the trace details with the trace owner
82 auth_header = bearer_authorization_header anon_trace_file.user
83 get api_trace_path(anon_trace_file), :headers => auth_header
84 assert_response :success
87 # Check the api details for a trace that doesn't exist
88 def test_show_not_found
89 deleted_trace_file = create(:trace, :deleted)
91 # Try first with no auth, as it should require it
92 get api_trace_path(:id => 0)
93 assert_response :unauthorized
95 # Login, and try again
96 auth_header = bearer_authorization_header deleted_trace_file.user
97 get api_trace_path(:id => 0), :headers => auth_header
98 assert_response :not_found
100 # Now try a trace which did exist but has been deleted
101 auth_header = bearer_authorization_header deleted_trace_file.user
102 get api_trace_path(deleted_trace_file), :headers => auth_header
103 assert_response :not_found
106 # Test creating a trace through the api
109 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
110 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
114 post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
115 assert_response :unauthorized
121 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
122 assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
124 auth_header = bearer_authorization_header user
126 # Create trace and import tracepoints in background job
127 perform_enqueued_jobs do
128 post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }, :headers => auth_header
131 assert_response :success
133 trace = Trace.find(response.body.to_i)
134 assert_equal "a.gpx", trace.name
135 assert_equal "New Trace", trace.description
136 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
137 assert_equal "trackable", trace.visibility
138 assert trace.inserted
139 assert_equal File.new(fixture).read, trace.file.blob.download
141 # Validate tracepoints
142 assert_equal 1, trace.points.size
143 tp = trace.points.first
144 assert_equal 10000000, tp.latitude
145 assert_equal 10000000, tp.longitude
146 assert_equal 3221331576, tp.tile
147 assert_equal 0, tp.trackid
148 assert_in_delta(134.0, tp.altitude)
149 assert_equal DateTime.parse("2008-10-01T10:10:10.000Z"), tp.timestamp
152 assert_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
157 # Now authenticated, with the legacy public flag
158 assert_not_equal "public", user.preferences.find_by(:k => "gps.trace.visibility").v
159 auth_header = bearer_authorization_header user
160 post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }, :headers => auth_header
161 assert_response :success
162 trace = Trace.find(response.body.to_i)
163 assert_equal "a.gpx", trace.name
164 assert_equal "New Trace", trace.description
165 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
166 assert_equal "public", trace.visibility
167 assert_not trace.inserted
168 assert_equal File.new(fixture).read, trace.file.blob.download
170 assert_equal "public", user.preferences.find_by(:k => "gps.trace.visibility").v
175 # Now authenticated, with the legacy private flag
176 second_user = create(:user)
177 assert_nil second_user.preferences.find_by(:k => "gps.trace.visibility")
178 auth_header = bearer_authorization_header second_user
179 post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }, :headers => auth_header
180 assert_response :success
181 trace = Trace.find(response.body.to_i)
182 assert_equal "a.gpx", trace.name
183 assert_equal "New Trace", trace.description
184 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
185 assert_equal "private", trace.visibility
186 assert_not trace.inserted
187 assert_equal File.new(fixture).read, trace.file.blob.download
189 assert_equal "private", second_user.preferences.find_by(:k => "gps.trace.visibility").v
192 # Check updating a trace through the api
194 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
195 deleted_trace_file = create(:trace, :deleted)
196 anon_trace_file = create(:trace, :visibility => "private")
199 put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file)
200 assert_response :unauthorized
202 # Now with some other user, which should fail
203 auth_header = bearer_authorization_header
204 put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file), :headers => auth_header
205 assert_response :forbidden
207 # Now with a trace which doesn't exist
208 auth_header = bearer_authorization_header
209 put api_trace_path(:id => 0), :params => create_trace_xml(public_trace_file), :headers => auth_header
210 assert_response :not_found
212 # Now with a trace which did exist but has been deleted
213 auth_header = bearer_authorization_header deleted_trace_file.user
214 put api_trace_path(deleted_trace_file), :params => create_trace_xml(deleted_trace_file), :headers => auth_header
215 assert_response :not_found
217 # Now try an update with the wrong ID
218 auth_header = bearer_authorization_header public_trace_file.user
219 put api_trace_path(public_trace_file), :params => create_trace_xml(anon_trace_file), :headers => auth_header
220 assert_response :bad_request,
221 "should not be able to update a trace with a different ID from the XML"
223 # And finally try an update that should work
224 auth_header = bearer_authorization_header public_trace_file.user
225 t = public_trace_file
226 t.description = "Changed description"
227 t.visibility = "private"
228 put api_trace_path(t), :params => create_trace_xml(t), :headers => auth_header
229 assert_response :success
230 nt = Trace.find(t.id)
231 assert_equal nt.description, t.description
232 assert_equal nt.visibility, t.visibility
235 # Test that updating a trace doesn't duplicate the tags
237 tracetag = create(:tracetag)
238 trace = tracetag.trace
239 auth_header = bearer_authorization_header trace.user
241 put api_trace_path(trace), :params => create_trace_xml(trace), :headers => auth_header
242 assert_response :success
244 updated = Trace.find(trace.id)
245 # Ensure there's only one tag in the database after updating
246 assert_equal(1, Tracetag.count)
247 # The new tag object might have a different id, so check the string representation
248 assert_equal trace.tagstring, updated.tagstring
251 # Check deleting a trace through the api
253 public_trace_file = create(:trace, :visibility => "public")
256 delete api_trace_path(public_trace_file)
257 assert_response :unauthorized
259 # Now with some other user, which should fail
260 auth_header = bearer_authorization_header
261 delete api_trace_path(public_trace_file), :headers => auth_header
262 assert_response :forbidden
264 # Now with a trace which doesn't exist
265 auth_header = bearer_authorization_header
266 delete api_trace_path(:id => 0), :headers => auth_header
267 assert_response :not_found
269 # And finally we should be able to do it with the owner of the trace
270 auth_header = bearer_authorization_header public_trace_file.user
271 delete api_trace_path(public_trace_file), :headers => auth_header
272 assert_response :success
274 # Try it a second time, which should fail
275 auth_header = bearer_authorization_header public_trace_file.user
276 delete api_trace_path(public_trace_file), :headers => auth_header
277 assert_response :not_found
283 # build XML for traces
284 # this builds a minimum viable XML for the tests in this suite
285 def create_trace_xml(trace)
286 root = XML::Document.new
287 root.root = XML::Node.new "osm"
288 trc = XML::Node.new "gpx_file"
289 trc["id"] = trace.id.to_s
290 trc["visibility"] = trace.visibility
291 trc["visible"] = trace.visible.to_s
292 desc = XML::Node.new "description"
293 desc << trace.description
295 trace.tags.each do |tag|
296 t = XML::Node.new "tag"