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