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