]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/traces_controller_test.rb
Merge remote-tracking branch 'upstream/pull/4764'
[rails.git] / test / controllers / api / traces_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class TracesControllerTest < ActionDispatch::IntegrationTest
5     ##
6     # test all routes which lead to this controller
7     def test_routes
8       assert_routing(
9         { :path => "/api/0.6/gpx/create", :method => :post },
10         { :controller => "api/traces", :action => "create" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/gpx/1", :method => :get },
14         { :controller => "api/traces", :action => "show", :id => "1" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/gpx/1", :method => :put },
18         { :controller => "api/traces", :action => "update", :id => "1" }
19       )
20       assert_routing(
21         { :path => "/api/0.6/gpx/1", :method => :delete },
22         { :controller => "api/traces", :action => "destroy", :id => "1" }
23       )
24       assert_recognizes(
25         { :controller => "api/traces", :action => "show", :id => "1" },
26         { :path => "/api/0.6/gpx/1/details", :method => :get }
27       )
28       assert_routing(
29         { :path => "/api/0.6/gpx/1/data", :method => :get },
30         { :controller => "api/traces", :action => "data", :id => "1" }
31       )
32       assert_routing(
33         { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
34         { :controller => "api/traces", :action => "data", :id => "1", :format => "xml" }
35       )
36     end
37
38     # Check getting a specific trace through the api
39     def test_show
40       public_trace_file = create(:trace, :visibility => "public")
41
42       # First with no auth
43       get api_trace_path(public_trace_file)
44       assert_response :unauthorized
45
46       # Now with some other user, which should work since the trace is public
47       auth_header = basic_authorization_header create(:user).display_name, "test"
48       get api_trace_path(public_trace_file), :headers => auth_header
49       assert_response :success
50
51       # And finally we should be able to do it with the owner of the trace
52       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
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
56     end
57
58     # Check an anonymous trace can't be specifically fetched by another user
59     def test_show_anon
60       anon_trace_file = create(:trace, :visibility => "private")
61
62       # First with no auth
63       get api_trace_path(anon_trace_file)
64       assert_response :unauthorized
65
66       # Now try with another user, which shouldn't work since the trace is anon
67       auth_header = basic_authorization_header create(:user).display_name, "test"
68       get api_trace_path(anon_trace_file), :headers => auth_header
69       assert_response :forbidden
70
71       # And finally we should be able to get the trace details with the trace owner
72       auth_header = basic_authorization_header anon_trace_file.user.display_name, "test"
73       get api_trace_path(anon_trace_file), :headers => auth_header
74       assert_response :success
75     end
76
77     # Check the api details for a trace that doesn't exist
78     def test_show_not_found
79       deleted_trace_file = create(:trace, :deleted)
80
81       # Try first with no auth, as it should require it
82       get api_trace_path(:id => 0)
83       assert_response :unauthorized
84
85       # Login, and try again
86       auth_header = basic_authorization_header deleted_trace_file.user.display_name, "test"
87       get api_trace_path(:id => 0), :headers => auth_header
88       assert_response :not_found
89
90       # Now try a trace which did exist but has been deleted
91       auth_header = basic_authorization_header deleted_trace_file.user.display_name, "test"
92       get api_trace_path(deleted_trace_file), :headers => auth_header
93       assert_response :not_found
94     end
95
96     # Test downloading a trace through the api
97     def test_data
98       public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
99
100       # First with no auth
101       get api_trace_data_path(public_trace_file)
102       assert_response :unauthorized
103
104       # Now with some other user, which should work since the trace is public
105       auth_header = basic_authorization_header create(:user).display_name, "test"
106       get api_trace_data_path(public_trace_file), :headers => auth_header
107       follow_redirect!
108       follow_redirect!
109       check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
110
111       # And finally we should be able to do it with the owner of the trace
112       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
113       get api_trace_data_path(public_trace_file), :headers => auth_header
114       follow_redirect!
115       follow_redirect!
116       check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
117     end
118
119     # Test downloading a compressed trace through the api
120     def test_data_compressed
121       identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
122
123       # Authenticate as the owner of the trace we will be using
124       auth_header = basic_authorization_header identifiable_trace_file.user.display_name, "test"
125
126       # First get the data as is
127       get api_trace_data_path(identifiable_trace_file), :headers => auth_header
128       follow_redirect!
129       follow_redirect!
130       check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/gzip", "gpx.gz"
131
132       # Now ask explicitly for XML format
133       get api_trace_data_path(identifiable_trace_file, :format => "xml"), :headers => auth_header
134       check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
135
136       # Now ask explicitly for GPX format
137       get api_trace_data_path(identifiable_trace_file, :format => "gpx"), :headers => auth_header
138       check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
139     end
140
141     # Check an anonymous trace can't be downloaded by another user through the api
142     def test_data_anon
143       anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
144
145       # First with no auth
146       get api_trace_data_path(anon_trace_file)
147       assert_response :unauthorized
148
149       # Now with some other user, which shouldn't work since the trace is anon
150       auth_header = basic_authorization_header create(:user).display_name, "test"
151       get api_trace_data_path(anon_trace_file), :headers => auth_header
152       assert_response :forbidden
153
154       # And finally we should be able to do it with the owner of the trace
155       auth_header = basic_authorization_header anon_trace_file.user.display_name, "test"
156       get api_trace_data_path(anon_trace_file), :headers => auth_header
157       follow_redirect!
158       follow_redirect!
159       check_trace_data anon_trace_file, "db4cb5ed2d7d2b627b3b504296c4f701"
160     end
161
162     # Test downloading a trace that doesn't exist through the api
163     def test_data_not_found
164       deleted_trace_file = create(:trace, :deleted)
165
166       # Try first with no auth, as it should require it
167       get api_trace_data_path(:id => 0)
168       assert_response :unauthorized
169
170       # Login, and try again
171       auth_header = basic_authorization_header create(:user).display_name, "test"
172       get api_trace_data_path(:id => 0), :headers => auth_header
173       assert_response :not_found
174
175       # Now try a trace which did exist but has been deleted
176       auth_header = basic_authorization_header deleted_trace_file.user.display_name, "test"
177       get api_trace_data_path(deleted_trace_file), :headers => auth_header
178       assert_response :not_found
179     end
180
181     # Test creating a trace through the api
182     def test_create
183       # Get file to use
184       fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
185       file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
186       user = create(:user)
187
188       # First with no auth
189       post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
190       assert_response :unauthorized
191
192       # Rewind the file
193       file.rewind
194
195       # Now authenticated
196       create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
197       assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
198       auth_header = basic_authorization_header user.display_name, "test"
199       post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }, :headers => auth_header
200       assert_response :success
201       trace = Trace.find(response.body.to_i)
202       assert_equal "a.gpx", trace.name
203       assert_equal "New Trace", trace.description
204       assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
205       assert_equal "trackable", trace.visibility
206       assert_not trace.inserted
207       assert_equal File.new(fixture).read, trace.file.blob.download
208       trace.destroy
209       assert_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
210
211       # Rewind the file
212       file.rewind
213
214       # Now authenticated, with the legacy public flag
215       assert_not_equal "public", user.preferences.find_by(:k => "gps.trace.visibility").v
216       auth_header = basic_authorization_header user.display_name, "test"
217       post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }, :headers => auth_header
218       assert_response :success
219       trace = Trace.find(response.body.to_i)
220       assert_equal "a.gpx", trace.name
221       assert_equal "New Trace", trace.description
222       assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
223       assert_equal "public", trace.visibility
224       assert_not trace.inserted
225       assert_equal File.new(fixture).read, trace.file.blob.download
226       trace.destroy
227       assert_equal "public", user.preferences.find_by(:k => "gps.trace.visibility").v
228
229       # Rewind the file
230       file.rewind
231
232       # Now authenticated, with the legacy private flag
233       second_user = create(:user)
234       assert_nil second_user.preferences.find_by(:k => "gps.trace.visibility")
235       auth_header = basic_authorization_header second_user.display_name, "test"
236       post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }, :headers => auth_header
237       assert_response :success
238       trace = Trace.find(response.body.to_i)
239       assert_equal "a.gpx", trace.name
240       assert_equal "New Trace", trace.description
241       assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
242       assert_equal "private", trace.visibility
243       assert_not trace.inserted
244       assert_equal File.new(fixture).read, trace.file.blob.download
245       trace.destroy
246       assert_equal "private", second_user.preferences.find_by(:k => "gps.trace.visibility").v
247     end
248
249     # Check updating a trace through the api
250     def test_update
251       public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
252       deleted_trace_file = create(:trace, :deleted)
253       anon_trace_file = create(:trace, :visibility => "private")
254
255       # First with no auth
256       put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file)
257       assert_response :unauthorized
258
259       # Now with some other user, which should fail
260       auth_header = basic_authorization_header create(:user).display_name, "test"
261       put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file), :headers => auth_header
262       assert_response :forbidden
263
264       # Now with a trace which doesn't exist
265       auth_header = basic_authorization_header create(:user).display_name, "test"
266       put api_trace_path(:id => 0), :params => create_trace_xml(public_trace_file), :headers => auth_header
267       assert_response :not_found
268
269       # Now with a trace which did exist but has been deleted
270       auth_header = basic_authorization_header deleted_trace_file.user.display_name, "test"
271       put api_trace_path(deleted_trace_file), :params => create_trace_xml(deleted_trace_file), :headers => auth_header
272       assert_response :not_found
273
274       # Now try an update with the wrong ID
275       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
276       put api_trace_path(public_trace_file), :params => create_trace_xml(anon_trace_file), :headers => auth_header
277       assert_response :bad_request,
278                       "should not be able to update a trace with a different ID from the XML"
279
280       # And finally try an update that should work
281       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
282       t = public_trace_file
283       t.description = "Changed description"
284       t.visibility = "private"
285       put api_trace_path(t), :params => create_trace_xml(t), :headers => auth_header
286       assert_response :success
287       nt = Trace.find(t.id)
288       assert_equal nt.description, t.description
289       assert_equal nt.visibility, t.visibility
290     end
291
292     # Test that updating a trace doesn't duplicate the tags
293     def test_update_tags
294       tracetag = create(:tracetag)
295       trace = tracetag.trace
296       auth_header = basic_authorization_header trace.user.display_name, "test"
297
298       put api_trace_path(trace), :params => create_trace_xml(trace), :headers => auth_header
299       assert_response :success
300
301       updated = Trace.find(trace.id)
302       # Ensure there's only one tag in the database after updating
303       assert_equal(1, Tracetag.count)
304       # The new tag object might have a different id, so check the string representation
305       assert_equal trace.tagstring, updated.tagstring
306     end
307
308     # Check deleting a trace through the api
309     def test_destroy
310       public_trace_file = create(:trace, :visibility => "public")
311
312       # First with no auth
313       delete api_trace_path(public_trace_file)
314       assert_response :unauthorized
315
316       # Now with some other user, which should fail
317       auth_header = basic_authorization_header create(:user).display_name, "test"
318       delete api_trace_path(public_trace_file), :headers => auth_header
319       assert_response :forbidden
320
321       # Now with a trace which doesn't exist
322       auth_header = basic_authorization_header create(:user).display_name, "test"
323       delete api_trace_path(:id => 0), :headers => auth_header
324       assert_response :not_found
325
326       # And finally we should be able to do it with the owner of the trace
327       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
328       delete api_trace_path(public_trace_file), :headers => auth_header
329       assert_response :success
330
331       # Try it a second time, which should fail
332       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
333       delete api_trace_path(public_trace_file), :headers => auth_header
334       assert_response :not_found
335     end
336
337     private
338
339     def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
340       assert_response :success
341       assert_equal digest, Digest::MD5.hexdigest(response.body)
342       assert_equal content_type, response.media_type
343       assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"; filename*=UTF-8''#{trace.id}.#{extension}", @response.header["Content-Disposition"]
344     end
345
346     ##
347     # build XML for traces
348     # this builds a minimum viable XML for the tests in this suite
349     def create_trace_xml(trace)
350       root = XML::Document.new
351       root.root = XML::Node.new "osm"
352       trc = XML::Node.new "gpx_file"
353       trc["id"] = trace.id.to_s
354       trc["visibility"] = trace.visibility
355       trc["visible"] = trace.visible.to_s
356       desc = XML::Node.new "description"
357       desc << trace.description
358       trc << desc
359       trace.tags.each do |tag|
360         t = XML::Node.new "tag"
361         t << tag.tag
362         trc << t
363       end
364       root.root << trc
365       root.to_s
366     end
367   end
368 end