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