]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/old_ways_controller_test.rb
141f9cfa338b91cb8b05a17283eb174e730afaf8
[rails.git] / test / controllers / api / old_ways_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class OldWaysControllerTest < ActionDispatch::IntegrationTest
5     ##
6     # test all routes which lead to this controller
7     def test_routes
8       assert_routing(
9         { :path => "/api/0.6/way/1/history", :method => :get },
10         { :controller => "api/old_ways", :action => "index", :way_id => "1" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/way/1/history.json", :method => :get },
14         { :controller => "api/old_ways", :action => "index", :way_id => "1", :format => "json" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/way/1/2", :method => :get },
18         { :controller => "api/old_ways", :action => "show", :way_id => "1", :version => "2" }
19       )
20       assert_routing(
21         { :path => "/api/0.6/way/1/2.json", :method => :get },
22         { :controller => "api/old_ways", :action => "show", :way_id => "1", :version => "2", :format => "json" }
23       )
24       assert_routing(
25         { :path => "/api/0.6/way/1/2/redact", :method => :post },
26         { :controller => "api/old_ways", :action => "redact", :way_id => "1", :version => "2" }
27       )
28     end
29
30     ##
31     # check that a visible way is returned properly
32     def test_index
33       way = create(:way, :with_history, :version => 2)
34
35       get api_way_versions_path(way)
36
37       assert_response :success
38       assert_dom "osm:root", 1 do
39         assert_dom "> way", 2 do |dom_ways|
40           assert_dom dom_ways[0], "> @id", way.id.to_s
41           assert_dom dom_ways[0], "> @version", "1"
42
43           assert_dom dom_ways[1], "> @id", way.id.to_s
44           assert_dom dom_ways[1], "> @version", "2"
45         end
46       end
47     end
48
49     ##
50     # check that an invisible way's history is returned properly
51     def test_index_invisible
52       get api_way_versions_path(create(:way, :with_history, :deleted))
53       assert_response :success
54     end
55
56     ##
57     # check chat a non-existent way is not returned
58     def test_index_invalid
59       get api_way_versions_path(0)
60       assert_response :not_found
61     end
62
63     ##
64     # test that redacted ways aren't visible in the history
65     def test_index_redacted
66       way = create(:way, :with_history, :version => 2)
67       way_v1 = way.old_ways.find_by(:version => 1)
68       way_v1.redact!(create(:redaction))
69
70       get api_way_versions_path(way)
71       assert_response :success, "Redaction shouldn't have stopped history working."
72       assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 0,
73                     "redacted way #{way_v1.way_id} version #{way_v1.version} shouldn't be present in the history."
74
75       # not even to a logged-in user
76       auth_header = bearer_authorization_header
77       get api_way_versions_path(way), :headers => auth_header
78       assert_response :success, "Redaction shouldn't have stopped history working."
79       assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 0,
80                     "redacted node #{way_v1.way_id} version #{way_v1.version} shouldn't be present in the history, even when logged in."
81     end
82
83     # TODO: test_show
84
85     ##
86     # test that redacted ways aren't visible, regardless of
87     # authorisation except as moderator...
88     def test_show_redacted
89       way = create(:way, :with_history, :version => 2)
90       way_v1 = way.old_ways.find_by(:version => 1)
91       way_v1.redact!(create(:redaction))
92
93       get api_way_version_path(way_v1.way_id, way_v1.version)
94       assert_response :forbidden, "Redacted way shouldn't be visible via the version API."
95
96       # not even to a logged-in user
97       auth_header = bearer_authorization_header
98       get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
99       assert_response :forbidden, "Redacted way shouldn't be visible via the version API, even when logged in."
100     end
101
102     ##
103     # check that returned history is the same as getting all
104     # versions of a way from the api.
105     def test_history_equals_versions
106       way = create(:way, :with_history)
107       used_way = create(:way, :with_history)
108       create(:relation_member, :member => used_way)
109       way_with_versions = create(:way, :with_history, :version => 4)
110
111       check_history_equals_versions(way.id)
112       check_history_equals_versions(used_way.id)
113       check_history_equals_versions(way_with_versions.id)
114     end
115
116     ##
117     # test the redaction of an old version of a way, while not being
118     # authorised.
119     def test_redact_way_unauthorised
120       way = create(:way, :with_history, :version => 4)
121       way_v3 = way.old_ways.find_by(:version => 3)
122
123       do_redact_way(way_v3, create(:redaction))
124       assert_response :unauthorized, "should need to be authenticated to redact."
125     end
126
127     ##
128     # test the redaction of an old version of a way, while being
129     # authorised as a normal user.
130     def test_redact_way_normal_user
131       auth_header = bearer_authorization_header
132       way = create(:way, :with_history, :version => 4)
133       way_v3 = way.old_ways.find_by(:version => 3)
134
135       do_redact_way(way_v3, create(:redaction), auth_header)
136       assert_response :forbidden, "should need to be moderator to redact."
137     end
138
139     ##
140     # test that, even as moderator, the current version of a way
141     # can't be redacted.
142     def test_redact_way_current_version
143       auth_header = bearer_authorization_header create(:moderator_user)
144       way = create(:way, :with_history, :version => 4)
145       way_latest = way.old_ways.last
146
147       do_redact_way(way_latest, create(:redaction), auth_header)
148       assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
149     end
150
151     def test_redact_way_by_regular_without_write_redactions_scope
152       auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
153       do_redact_redactable_way(auth_header)
154       assert_response :forbidden, "should need to be moderator to redact."
155     end
156
157     def test_redact_way_by_regular_with_write_redactions_scope
158       auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
159       do_redact_redactable_way(auth_header)
160       assert_response :forbidden, "should need to be moderator to redact."
161     end
162
163     def test_redact_way_by_moderator_without_write_redactions_scope
164       auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
165       do_redact_redactable_way(auth_header)
166       assert_response :forbidden, "should need to have write_redactions scope to redact."
167     end
168
169     def test_redact_way_by_moderator_with_write_redactions_scope
170       auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
171       do_redact_redactable_way(auth_header)
172       assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
173     end
174
175     ##
176     # test the redaction of an old version of a way, while being
177     # authorised as a moderator.
178     def test_redact_way_moderator
179       way = create(:way, :with_history, :version => 4)
180       way_v3 = way.old_ways.find_by(:version => 3)
181       auth_header = bearer_authorization_header create(:moderator_user)
182
183       do_redact_way(way_v3, create(:redaction), auth_header)
184       assert_response :success, "should be OK to redact old version as moderator."
185
186       # check moderator can still see the redacted data, when passing
187       # the appropriate flag
188       get api_way_version_path(way_v3.way_id, way_v3.version), :headers => auth_header
189       assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
190       get api_way_version_path(way_v3.way_id, way_v3.version, :show_redactions => "true"), :headers => auth_header
191       assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
192
193       # and when accessed via history
194       get api_way_versions_path(way), :headers => auth_header
195       assert_response :success, "Redaction shouldn't have stopped history working."
196       assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 0,
197                     "way #{way_v3.way_id} version #{way_v3.version} should not be present in the history for moderators when not passing flag."
198       get api_way_versions_path(way, :show_redactions => "true"), :headers => auth_header
199       assert_response :success, "Redaction shouldn't have stopped history working."
200       assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 1,
201                     "way #{way_v3.way_id} version #{way_v3.version} should still be present in the history for moderators when passing flag."
202     end
203
204     # testing that if the moderator drops auth, he can't see the
205     # redacted stuff any more.
206     def test_redact_way_is_redacted
207       way = create(:way, :with_history, :version => 4)
208       way_v3 = way.old_ways.find_by(:version => 3)
209       auth_header = bearer_authorization_header create(:moderator_user)
210
211       do_redact_way(way_v3, create(:redaction), auth_header)
212       assert_response :success, "should be OK to redact old version as moderator."
213
214       # re-auth as non-moderator
215       auth_header = bearer_authorization_header
216
217       # check can't see the redacted data
218       get api_way_version_path(way_v3.way_id, way_v3.version), :headers => auth_header
219       assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
220
221       # and when accessed via history
222       get api_way_versions_path(way), :headers => auth_header
223       assert_response :success, "Redaction shouldn't have stopped history working."
224       assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 0,
225                     "redacted way #{way_v3.way_id} version #{way_v3.version} shouldn't be present in the history."
226     end
227
228     ##
229     # test the unredaction of an old version of a way, while not being
230     # authorised.
231     def test_unredact_way_unauthorised
232       way = create(:way, :with_history, :version => 2)
233       way_v1 = way.old_ways.find_by(:version => 1)
234       way_v1.redact!(create(:redaction))
235
236       post way_version_redact_path(way_v1.way_id, way_v1.version)
237       assert_response :unauthorized, "should need to be authenticated to unredact."
238     end
239
240     ##
241     # test the unredaction of an old version of a way, while being
242     # authorised as a normal user.
243     def test_unredact_way_normal_user
244       way = create(:way, :with_history, :version => 2)
245       way_v1 = way.old_ways.find_by(:version => 1)
246       way_v1.redact!(create(:redaction))
247
248       auth_header = bearer_authorization_header
249
250       post way_version_redact_path(way_v1.way_id, way_v1.version), :headers => auth_header
251       assert_response :forbidden, "should need to be moderator to unredact."
252     end
253
254     ##
255     # test the unredaction of an old version of a way, while being
256     # authorised as a moderator.
257     def test_unredact_way_moderator
258       moderator_user = create(:moderator_user)
259       way = create(:way, :with_history, :version => 2)
260       way_v1 = way.old_ways.find_by(:version => 1)
261       way_v1.redact!(create(:redaction))
262
263       auth_header = bearer_authorization_header moderator_user
264
265       post way_version_redact_path(way_v1.way_id, way_v1.version), :headers => auth_header
266       assert_response :success, "should be OK to unredact old version as moderator."
267
268       # check moderator can still see the unredacted data, without passing
269       # the appropriate flag
270       get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
271       assert_response :success, "After unredaction, node should not be gone for moderator."
272
273       # and when accessed via history
274       get api_way_versions_path(way), :headers => auth_header
275       assert_response :success, "Unredaction shouldn't have stopped history working."
276       assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1,
277                     "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for moderators."
278
279       auth_header = bearer_authorization_header
280
281       # check normal user can now see the unredacted data
282       get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
283       assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
284
285       # and when accessed via history
286       get api_way_versions_path(way), :headers => auth_header
287       assert_response :success, "Redaction shouldn't have stopped history working."
288       assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1,
289                     "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for normal users."
290     end
291
292     private
293
294     ##
295     # look at all the versions of the way in the history and get each version from
296     # the versions call. check that they're the same.
297     def check_history_equals_versions(way_id)
298       get api_way_versions_path(way_id)
299       assert_response :success, "can't get way #{way_id} from API"
300       history_doc = XML::Parser.string(@response.body).parse
301       assert_not_nil history_doc, "parsing way #{way_id} history failed"
302
303       history_doc.find("//osm/way").each do |way_doc|
304         history_way = Way.from_xml_node(way_doc)
305         assert_not_nil history_way, "parsing way #{way_id} version failed"
306
307         get api_way_version_path(way_id, history_way.version)
308         assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
309         version_way = Way.from_xml(@response.body)
310         assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
311
312         assert_ways_are_equal history_way, version_way
313       end
314     end
315
316     def do_redact_redactable_way(headers = {})
317       way = create(:way, :with_history, :version => 4)
318       way_v3 = way.old_ways.find_by(:version => 3)
319       do_redact_way(way_v3, create(:redaction), headers)
320     end
321
322     def do_redact_way(way, redaction, headers = {})
323       get api_way_version_path(way.way_id, way.version)
324       assert_response :success, "should be able to get version #{way.version} of way #{way.way_id}."
325
326       # now redact it
327       post way_version_redact_path(way.way_id, way.version), :params => { :redaction => redaction.id }, :headers => headers
328     end
329   end
330 end