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