]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/traces_controller_test.rb
Merge pull request #6394 from openstreetmap/dependabot/github_actions/ruby/setup...
[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     # Test creating a trace through the api
109     def test_create
110       # Get file to use
111       fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
112       file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
113       user = create(:user)
114
115       # First with no auth
116       post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
117       assert_response :unauthorized
118
119       # Rewind the file
120       file.rewind
121
122       # Now authenticated
123       create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
124       assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
125
126       auth_header = bearer_authorization_header user
127
128       # Create trace and import tracepoints in background job
129       perform_enqueued_jobs do
130         post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }, :headers => auth_header
131       end
132
133       assert_response :success
134
135       trace = Trace.find(response.body.to_i)
136       assert_equal "a.gpx", trace.name
137       assert_equal "New Trace", trace.description
138       assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
139       assert_equal "trackable", trace.visibility
140       assert trace.inserted
141       assert_equal File.new(fixture).read, trace.file.blob.download
142
143       # Validate tracepoints
144       assert_equal 1, trace.points.size
145       tp = trace.points.first
146       assert_equal 10000000, tp.latitude
147       assert_equal 10000000, tp.longitude
148       assert_equal 3221331576, tp.tile
149       assert_equal 0, tp.trackid
150       assert_in_delta(134.0, tp.altitude)
151       assert_equal DateTime.parse("2008-10-01T10:10:10.000Z"), tp.timestamp
152
153       trace.destroy
154       assert_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
155
156       # Rewind the file
157       file.rewind
158
159       # Now authenticated, with the legacy public flag
160       assert_not_equal "public", user.preferences.find_by(:k => "gps.trace.visibility").v
161       auth_header = bearer_authorization_header user
162       post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }, :headers => auth_header
163       assert_response :success
164       trace = Trace.find(response.body.to_i)
165       assert_equal "a.gpx", trace.name
166       assert_equal "New Trace", trace.description
167       assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
168       assert_equal "public", trace.visibility
169       assert_not trace.inserted
170       assert_equal File.new(fixture).read, trace.file.blob.download
171       trace.destroy
172       assert_equal "public", user.preferences.find_by(:k => "gps.trace.visibility").v
173
174       # Rewind the file
175       file.rewind
176
177       # Now authenticated, with the legacy private flag
178       second_user = create(:user)
179       assert_nil second_user.preferences.find_by(:k => "gps.trace.visibility")
180       auth_header = bearer_authorization_header second_user
181       post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }, :headers => auth_header
182       assert_response :success
183       trace = Trace.find(response.body.to_i)
184       assert_equal "a.gpx", trace.name
185       assert_equal "New Trace", trace.description
186       assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
187       assert_equal "private", trace.visibility
188       assert_not trace.inserted
189       assert_equal File.new(fixture).read, trace.file.blob.download
190       trace.destroy
191       assert_equal "private", second_user.preferences.find_by(:k => "gps.trace.visibility").v
192     end
193
194     # Check updating a trace through the api
195     def test_update
196       public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
197       deleted_trace_file = create(:trace, :deleted)
198       anon_trace_file = create(:trace, :visibility => "private")
199
200       # First with no auth
201       put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file)
202       assert_response :unauthorized
203
204       # Now with some other user, which should fail
205       auth_header = bearer_authorization_header
206       put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file), :headers => auth_header
207       assert_response :forbidden
208
209       # Now with a trace which doesn't exist
210       auth_header = bearer_authorization_header
211       put api_trace_path(:id => 0), :params => create_trace_xml(public_trace_file), :headers => auth_header
212       assert_response :not_found
213
214       # Now with a trace which did exist but has been deleted
215       auth_header = bearer_authorization_header deleted_trace_file.user
216       put api_trace_path(deleted_trace_file), :params => create_trace_xml(deleted_trace_file), :headers => auth_header
217       assert_response :not_found
218
219       # Now try an update with the wrong ID
220       auth_header = bearer_authorization_header public_trace_file.user
221       put api_trace_path(public_trace_file), :params => create_trace_xml(anon_trace_file), :headers => auth_header
222       assert_response :bad_request,
223                       "should not be able to update a trace with a different ID from the XML"
224
225       # And finally try an update that should work
226       auth_header = bearer_authorization_header public_trace_file.user
227       t = public_trace_file
228       t.description = "Changed description"
229       t.visibility = "private"
230       put api_trace_path(t), :params => create_trace_xml(t), :headers => auth_header
231       assert_response :success
232       nt = Trace.find(t.id)
233       assert_equal nt.description, t.description
234       assert_equal nt.visibility, t.visibility
235     end
236
237     # Test that updating a trace doesn't duplicate the tags
238     def test_update_tags
239       tracetag = create(:tracetag)
240       trace = tracetag.trace
241       auth_header = bearer_authorization_header trace.user
242
243       put api_trace_path(trace), :params => create_trace_xml(trace), :headers => auth_header
244       assert_response :success
245
246       updated = Trace.find(trace.id)
247       # Ensure there's only one tag in the database after updating
248       assert_equal(1, Tracetag.count)
249       # The new tag object might have a different id, so check the string representation
250       assert_equal trace.tagstring, updated.tagstring
251     end
252
253     # Check deleting a trace through the api
254     def test_destroy
255       public_trace_file = create(:trace, :visibility => "public")
256
257       # First with no auth
258       delete api_trace_path(public_trace_file)
259       assert_response :unauthorized
260
261       # Now with some other user, which should fail
262       auth_header = bearer_authorization_header
263       delete api_trace_path(public_trace_file), :headers => auth_header
264       assert_response :forbidden
265
266       # Now with a trace which doesn't exist
267       auth_header = bearer_authorization_header
268       delete api_trace_path(:id => 0), :headers => auth_header
269       assert_response :not_found
270
271       # And finally we should be able to do it with the owner of the trace
272       auth_header = bearer_authorization_header public_trace_file.user
273       delete api_trace_path(public_trace_file), :headers => auth_header
274       assert_response :success
275
276       # Try it a second time, which should fail
277       auth_header = bearer_authorization_header public_trace_file.user
278       delete api_trace_path(public_trace_file), :headers => auth_header
279       assert_response :not_found
280     end
281
282     private
283
284     ##
285     # build XML for traces
286     # this builds a minimum viable XML for the tests in this suite
287     def create_trace_xml(trace)
288       root = XML::Document.new
289       root.root = XML::Node.new "osm"
290       trc = XML::Node.new "gpx_file"
291       trc["id"] = trace.id.to_s
292       trc["visibility"] = trace.visibility
293       trc["visible"] = trace.visible.to_s
294       desc = XML::Node.new "description"
295       desc << trace.description
296       trc << desc
297       trace.tags.each do |tag|
298         t = XML::Node.new "tag"
299         t << tag.tag
300         trc << t
301       end
302       root.root << trc
303       root.to_s
304     end
305   end
306 end