]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/traces_controller_test.rb
Localisation updates from https://translatewiki.net.
[rails.git] / test / controllers / api / traces_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 module Api
6   class TracesControllerTest < ActionDispatch::IntegrationTest
7     ##
8     # test all routes which lead to this controller
9     def test_routes
10       assert_routing(
11         { :path => "/api/0.6/gpx", :method => :post },
12         { :controller => "api/traces", :action => "create" }
13       )
14       assert_recognizes(
15         { :controller => "api/traces", :action => "create" },
16         { :path => "/api/0.6/gpx/create", :method => :post }
17       )
18       assert_routing(
19         { :path => "/api/0.6/gpx/1", :method => :get },
20         { :controller => "api/traces", :action => "show", :id => "1" }
21       )
22       assert_routing(
23         { :path => "/api/0.6/gpx/1.json", :method => :get },
24         { :controller => "api/traces", :action => "show", :id => "1", :format => "json" }
25       )
26       assert_routing(
27         { :path => "/api/0.6/gpx/1", :method => :put },
28         { :controller => "api/traces", :action => "update", :id => "1" }
29       )
30       assert_routing(
31         { :path => "/api/0.6/gpx/1", :method => :delete },
32         { :controller => "api/traces", :action => "destroy", :id => "1" }
33       )
34       assert_recognizes(
35         { :controller => "api/traces", :action => "show", :id => "1" },
36         { :path => "/api/0.6/gpx/1/details", :method => :get }
37       )
38     end
39
40     # Check getting a specific trace through the api
41     def test_show
42       public_trace_file = create(:trace, :visibility => "public")
43
44       # First with no auth
45       get api_trace_path(public_trace_file)
46       assert_response :unauthorized
47
48       # Now with some other user, which should work since the trace is public
49       auth_header = bearer_authorization_header
50       get api_trace_path(public_trace_file), :headers => auth_header
51       assert_response :success
52
53       # We should be able to do it with the owner of the trace
54       auth_header = bearer_authorization_header public_trace_file.user
55       get api_trace_path(public_trace_file), :headers => auth_header
56       assert_response :success
57       assert_select "gpx_file[id='#{public_trace_file.id}'][uid='#{public_trace_file.user.id}']", 1
58
59       # We should be able to do it with the owner of the trace with json format
60       auth_header = bearer_authorization_header public_trace_file.user
61       get api_trace_path(public_trace_file, :format => "json"), :headers => auth_header
62       assert_response :success
63       assert_equal "application/json", response.media_type
64       js = ActiveSupport::JSON.decode(@response.body)
65       assert_not_nil js
66       assert_equal public_trace_file.id, js["trace"]["id"]
67       assert_equal public_trace_file.user.id, js["trace"]["uid"]
68     end
69
70     # Check an anonymous trace can't be specifically fetched by another user
71     def test_show_anon
72       anon_trace_file = create(:trace, :visibility => "private")
73
74       # First with no auth
75       get api_trace_path(anon_trace_file)
76       assert_response :unauthorized
77
78       # Now try with another user, which shouldn't work since the trace is anon
79       auth_header = bearer_authorization_header
80       get api_trace_path(anon_trace_file), :headers => auth_header
81       assert_response :forbidden
82
83       # And finally we should be able to get the trace details with the trace owner
84       auth_header = bearer_authorization_header anon_trace_file.user
85       get api_trace_path(anon_trace_file), :headers => auth_header
86       assert_response :success
87     end
88
89     # Check the api details for a trace that doesn't exist
90     def test_show_not_found
91       deleted_trace_file = create(:trace, :deleted)
92
93       # Try first with no auth, as it should require it
94       get api_trace_path(:id => 0)
95       assert_response :unauthorized
96
97       # Login, and try again
98       auth_header = bearer_authorization_header deleted_trace_file.user
99       get api_trace_path(:id => 0), :headers => auth_header
100       assert_response :not_found
101
102       # Now try a trace which did exist but has been deleted
103       auth_header = bearer_authorization_header deleted_trace_file.user
104       get api_trace_path(deleted_trace_file), :headers => auth_header
105       assert_response :not_found
106     end
107
108     # Check that getting a trace through the api is not possible when the traces feature is disabled
109     def test_show_disabled
110       public_trace_file = create(:trace, :visibility => "public")
111       auth_header = bearer_authorization_header public_trace_file.user
112
113       with_settings(:traces_disabled => true) do
114         get api_trace_path(public_trace_file), :headers => auth_header
115         assert_response :not_found
116       end
117     end
118
119     # Test creating a trace through the api
120     def test_create
121       # Get file to use
122       fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
123       file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
124       user = create(:user)
125
126       # First with no auth
127       post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
128       assert_response :unauthorized
129
130       # Rewind the file
131       file.rewind
132
133       # Now authenticated
134       create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
135       assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
136
137       auth_header = bearer_authorization_header user
138
139       # Create trace and import tracepoints in background job
140       perform_enqueued_jobs do
141         post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }, :headers => auth_header
142       end
143
144       assert_response :success
145
146       trace = Trace.find(response.body.to_i)
147       assert_equal "a.gpx", trace.name
148       assert_equal "New Trace", trace.description
149       assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
150       assert_equal "trackable", trace.visibility
151       assert trace.inserted
152       assert_equal File.new(fixture).read, trace.file.blob.download
153
154       # Validate tracepoints
155       assert_equal 1, trace.points.size
156       tp = trace.points.order(:timestamp).first
157       assert_equal 10000000, tp.latitude
158       assert_equal 10000000, tp.longitude
159       assert_equal 3221331576, tp.tile
160       assert_equal 0, tp.trackid
161       assert_in_delta(134.0, tp.altitude)
162       assert_equal DateTime.parse("2008-10-01T10:10:10.000Z"), tp.timestamp
163
164       trace.destroy
165       assert_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
166
167       # Rewind the file
168       file.rewind
169
170       # Now authenticated, with the legacy public flag
171       assert_not_equal "public", user.preferences.find_by(:k => "gps.trace.visibility").v
172       auth_header = bearer_authorization_header user
173       post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }, :headers => auth_header
174       assert_response :success
175       trace = Trace.find(response.body.to_i)
176       assert_equal "a.gpx", trace.name
177       assert_equal "New Trace", trace.description
178       assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
179       assert_equal "public", trace.visibility
180       assert_not trace.inserted
181       assert_equal File.new(fixture).read, trace.file.blob.download
182       trace.destroy
183       assert_equal "public", user.preferences.find_by(:k => "gps.trace.visibility").v
184
185       # Rewind the file
186       file.rewind
187
188       # Now authenticated, with the legacy private flag
189       second_user = create(:user)
190       assert_nil second_user.preferences.find_by(:k => "gps.trace.visibility")
191       auth_header = bearer_authorization_header second_user
192       post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }, :headers => auth_header
193       assert_response :success
194       trace = Trace.find(response.body.to_i)
195       assert_equal "a.gpx", trace.name
196       assert_equal "New Trace", trace.description
197       assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
198       assert_equal "private", trace.visibility
199       assert_not trace.inserted
200       assert_equal File.new(fixture).read, trace.file.blob.download
201       trace.destroy
202       assert_equal "private", second_user.preferences.find_by(:k => "gps.trace.visibility").v
203     end
204
205     # Check updating a trace through the api
206     def test_update
207       public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
208       deleted_trace_file = create(:trace, :deleted)
209       anon_trace_file = create(:trace, :visibility => "private")
210
211       # First with no auth
212       put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file)
213       assert_response :unauthorized
214
215       # Now with some other user, which should fail
216       auth_header = bearer_authorization_header
217       put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file), :headers => auth_header
218       assert_response :forbidden
219
220       # Now with a trace which doesn't exist
221       auth_header = bearer_authorization_header
222       put api_trace_path(:id => 0), :params => create_trace_xml(public_trace_file), :headers => auth_header
223       assert_response :not_found
224
225       # Now with a trace which did exist but has been deleted
226       auth_header = bearer_authorization_header deleted_trace_file.user
227       put api_trace_path(deleted_trace_file), :params => create_trace_xml(deleted_trace_file), :headers => auth_header
228       assert_response :not_found
229
230       # Now try an update with the wrong ID
231       auth_header = bearer_authorization_header public_trace_file.user
232       put api_trace_path(public_trace_file), :params => create_trace_xml(anon_trace_file), :headers => auth_header
233       assert_response :bad_request,
234                       "should not be able to update a trace with a different ID from the XML"
235
236       # And finally try an update that should work
237       auth_header = bearer_authorization_header public_trace_file.user
238       t = public_trace_file
239       t.description = "Changed description"
240       t.visibility = "private"
241       put api_trace_path(t), :params => create_trace_xml(t), :headers => auth_header
242       assert_response :success
243       nt = Trace.find(t.id)
244       assert_equal nt.description, t.description
245       assert_equal nt.visibility, t.visibility
246     end
247
248     # Test that updating a trace doesn't duplicate the tags
249     def test_update_tags
250       tracetag = create(:tracetag)
251       trace = tracetag.trace
252       auth_header = bearer_authorization_header trace.user
253
254       put api_trace_path(trace), :params => create_trace_xml(trace), :headers => auth_header
255       assert_response :success
256
257       updated = Trace.find(trace.id)
258       # Ensure there's only one tag in the database after updating
259       assert_equal(1, Tracetag.count)
260       # The new tag object might have a different id, so check the string representation
261       assert_equal trace.tagstring, updated.tagstring
262     end
263
264     # Check deleting a trace through the api
265     def test_destroy
266       public_trace_file = create(:trace, :visibility => "public")
267
268       # First with no auth
269       delete api_trace_path(public_trace_file)
270       assert_response :unauthorized
271
272       # Now with some other user, which should fail
273       auth_header = bearer_authorization_header
274       delete api_trace_path(public_trace_file), :headers => auth_header
275       assert_response :forbidden
276
277       # Now with a trace which doesn't exist
278       auth_header = bearer_authorization_header
279       delete api_trace_path(:id => 0), :headers => auth_header
280       assert_response :not_found
281
282       # And finally we should be able to do it with the owner of the trace
283       auth_header = bearer_authorization_header public_trace_file.user
284       delete api_trace_path(public_trace_file), :headers => auth_header
285       assert_response :success
286
287       # Try it a second time, which should fail
288       auth_header = bearer_authorization_header public_trace_file.user
289       delete api_trace_path(public_trace_file), :headers => auth_header
290       assert_response :not_found
291     end
292
293     private
294
295     ##
296     # build XML for traces
297     # this builds a minimum viable XML for the tests in this suite
298     def create_trace_xml(trace)
299       root = XML::Document.new
300       root.root = XML::Node.new "osm"
301       trc = XML::Node.new "gpx_file"
302       trc["id"] = trace.id.to_s
303       trc["visibility"] = trace.visibility
304       trc["visible"] = trace.visible.to_s
305       desc = XML::Node.new "description"
306       desc << trace.description
307       trc << desc
308       trace.tags.each do |tag|
309         t = XML::Node.new "tag"
310         t << tag.tag
311         trc << t
312       end
313       root.root << trc
314       root.to_s
315     end
316   end
317 end