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