4 class OldWaysControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/way/1/history", :method => :get },
10 { :controller => "api/old_ways", :action => "index", :way_id => "1" }
13 { :path => "/api/0.6/way/1/history.json", :method => :get },
14 { :controller => "api/old_ways", :action => "index", :way_id => "1", :format => "json" }
17 { :path => "/api/0.6/way/1/2", :method => :get },
18 { :controller => "api/old_ways", :action => "show", :way_id => "1", :version => "2" }
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" }
25 { :path => "/api/0.6/way/1/2/redact", :method => :post },
26 { :controller => "api/old_ways", :action => "redact", :way_id => "1", :version => "2" }
31 # check that a visible way is returned properly
33 way = create(:way, :with_history, :version => 2)
35 get api_way_versions_path(way)
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"
43 assert_dom dom_ways[1], "> @id", way.id.to_s
44 assert_dom dom_ways[1], "> @version", "2"
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
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
64 # test that redacted ways aren't visible in the history
65 def test_index_redacted_unauthorised
66 way = create(:way, :with_history, :version => 2)
67 way.old_ways.find_by(:version => 1).redact!(create(:redaction))
69 get api_way_versions_path(way)
71 assert_response :success, "Redaction shouldn't have stopped history working."
72 assert_dom "osm way[id='#{way.id}'][version='1']", 0,
73 "redacted way #{way.id} version 1 shouldn't be present in the history."
75 get api_way_versions_path(way, :show_redactions => "true")
77 assert_response :success, "Redaction shouldn't have stopped history working."
78 assert_dom "osm way[id='#{way.id}'][version='1']", 0,
79 "redacted way #{way.id} version 1 shouldn't be present in the history when passing flag."
82 def test_index_redacted_normal_user
83 way = create(:way, :with_history, :version => 2)
84 way.old_ways.find_by(:version => 1).redact!(create(:redaction))
86 get api_way_versions_path(way), :headers => bearer_authorization_header
88 assert_response :success, "Redaction shouldn't have stopped history working."
89 assert_dom "osm way[id='#{way.id}'][version='1']", 0,
90 "redacted node #{way.id} version 1 shouldn't be present in the history, even when logged in."
92 get api_way_versions_path(way, :show_redactions => "true"), :headers => bearer_authorization_header
94 assert_response :success, "Redaction shouldn't have stopped history working."
95 assert_dom "osm way[id='#{way.id}'][version='1']", 0,
96 "redacted node #{way.id} version 1 shouldn't be present in the history, even when logged in and passing flag."
99 def test_index_redacted_moderator
100 way = create(:way, :with_history, :version => 2)
101 way.old_ways.find_by(:version => 1).redact!(create(:redaction))
102 auth_header = bearer_authorization_header create(:moderator_user)
104 get api_way_versions_path(way), :headers => auth_header
106 assert_response :success, "Redaction shouldn't have stopped history working."
107 assert_dom "osm way[id='#{way.id}'][version='1']", 0,
108 "way #{way.id} version 1 should not be present in the history for moderators when not passing flag."
110 get api_way_versions_path(way, :show_redactions => "true"), :headers => auth_header
112 assert_response :success, "Redaction shouldn't have stopped history working."
113 assert_dom "osm way[id='#{way.id}'][version='1']", 1,
114 "way #{way.id} version 1 should still be present in the history for moderators when passing flag."
118 way = create(:way, :with_history, :version => 2)
120 get api_way_version_path(way, 1)
122 assert_response :success
123 assert_dom "osm:root", 1 do
124 assert_dom "> way", 1 do
125 assert_dom "> @id", way.id.to_s
126 assert_dom "> @version", "1"
130 get api_way_version_path(way, 2)
132 assert_response :success
133 assert_dom "osm:root", 1 do
134 assert_dom "> way", 1 do
135 assert_dom "> @id", way.id.to_s
136 assert_dom "> @version", "2"
142 # test that redacted ways aren't visible, regardless of
143 # authorisation except as moderator...
144 def test_show_redacted_unauthorised
145 way = create(:way, :with_history, :version => 2)
146 way.old_ways.find_by(:version => 1).redact!(create(:redaction))
148 get api_way_version_path(way, 1)
150 assert_response :forbidden, "Redacted way shouldn't be visible via the version API."
153 def test_show_redacted_normal_user
154 way = create(:way, :with_history, :version => 2)
155 way.old_ways.find_by(:version => 1).redact!(create(:redaction))
157 get api_way_version_path(way, 1), :headers => bearer_authorization_header
159 assert_response :forbidden, "Redacted way shouldn't be visible via the version API, even when logged in."
163 # check that returned history is the same as getting all
164 # versions of a way from the api.
165 def test_history_equals_versions
166 way = create(:way, :with_history)
167 used_way = create(:way, :with_history)
168 create(:relation_member, :member => used_way)
169 way_with_versions = create(:way, :with_history, :version => 4)
171 check_history_equals_versions(way.id)
172 check_history_equals_versions(used_way.id)
173 check_history_equals_versions(way_with_versions.id)
177 # test the redaction of an old version of a way, while not being
179 def test_redact_way_unauthorised
180 way = create(:way, :with_history, :version => 4)
181 way_v3 = way.old_ways.find_by(:version => 3)
183 do_redact_way(way_v3, create(:redaction))
184 assert_response :unauthorized, "should need to be authenticated to redact."
188 # test the redaction of an old version of a way, while being
189 # authorised as a normal user.
190 def test_redact_way_normal_user
191 auth_header = bearer_authorization_header
192 way = create(:way, :with_history, :version => 4)
193 way_v3 = way.old_ways.find_by(:version => 3)
195 do_redact_way(way_v3, create(:redaction), auth_header)
196 assert_response :forbidden, "should need to be moderator to redact."
200 # test that, even as moderator, the current version of a way
202 def test_redact_way_current_version
203 auth_header = bearer_authorization_header create(:moderator_user)
204 way = create(:way, :with_history, :version => 4)
205 way_latest = way.old_ways.last
207 do_redact_way(way_latest, create(:redaction), auth_header)
208 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
211 def test_redact_way_by_regular_without_write_redactions_scope
212 auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
213 do_redact_redactable_way(auth_header)
214 assert_response :forbidden, "should need to be moderator to redact."
217 def test_redact_way_by_regular_with_write_redactions_scope
218 auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
219 do_redact_redactable_way(auth_header)
220 assert_response :forbidden, "should need to be moderator to redact."
223 def test_redact_way_by_moderator_without_write_redactions_scope
224 auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
225 do_redact_redactable_way(auth_header)
226 assert_response :forbidden, "should need to have write_redactions scope to redact."
229 def test_redact_way_by_moderator_with_write_redactions_scope
230 auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
231 do_redact_redactable_way(auth_header)
232 assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
236 # test the redaction of an old version of a way, while being
237 # authorised as a moderator.
238 def test_redact_way_moderator
239 way = create(:way, :with_history, :version => 4)
240 way_v3 = way.old_ways.find_by(:version => 3)
241 auth_header = bearer_authorization_header create(:moderator_user)
243 do_redact_way(way_v3, create(:redaction), auth_header)
245 assert_response :success, "should be OK to redact old version as moderator."
246 assert_predicate way_v3.reload, :redacted?
248 # check moderator can still see the redacted data, when passing
249 # the appropriate flag
250 get api_way_version_path(way_v3.way_id, way_v3.version), :headers => auth_header
251 assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
252 get api_way_version_path(way_v3.way_id, way_v3.version, :show_redactions => "true"), :headers => auth_header
253 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
256 # testing that if the moderator drops auth, he can't see the
257 # redacted stuff any more.
258 def test_redact_way_is_redacted
259 way = create(:way, :with_history, :version => 4)
260 way_v3 = way.old_ways.find_by(:version => 3)
261 auth_header = bearer_authorization_header create(:moderator_user)
263 do_redact_way(way_v3, create(:redaction), auth_header)
264 assert_response :success, "should be OK to redact old version as moderator."
266 # re-auth as non-moderator
267 auth_header = bearer_authorization_header
269 # check can't see the redacted data
270 get api_way_version_path(way_v3.way_id, way_v3.version), :headers => auth_header
271 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
275 # test the unredaction of an old version of a way, while not being
277 def test_unredact_way_unauthorised
278 way = create(:way, :with_history, :version => 2)
279 way_v1 = way.old_ways.find_by(:version => 1)
280 way_v1.redact!(create(:redaction))
282 post way_version_redact_path(way_v1.way_id, way_v1.version)
283 assert_response :unauthorized, "should need to be authenticated to unredact."
287 # test the unredaction of an old version of a way, while being
288 # authorised as a normal user.
289 def test_unredact_way_normal_user
290 way = create(:way, :with_history, :version => 2)
291 way_v1 = way.old_ways.find_by(:version => 1)
292 way_v1.redact!(create(:redaction))
294 auth_header = bearer_authorization_header
296 post way_version_redact_path(way_v1.way_id, way_v1.version), :headers => auth_header
297 assert_response :forbidden, "should need to be moderator to unredact."
301 # test the unredaction of an old version of a way, while being
302 # authorised as a moderator.
303 def test_unredact_way_moderator
304 moderator_user = create(:moderator_user)
305 way = create(:way, :with_history, :version => 2)
306 way_v1 = way.old_ways.find_by(:version => 1)
307 way_v1.redact!(create(:redaction))
309 auth_header = bearer_authorization_header moderator_user
311 post way_version_redact_path(way_v1.way_id, way_v1.version), :headers => auth_header
312 assert_response :success, "should be OK to unredact old version as moderator."
314 # check moderator can still see the unredacted data, without passing
315 # the appropriate flag
316 get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
317 assert_response :success, "After unredaction, node should not be gone for moderator."
319 # and when accessed via history
320 get api_way_versions_path(way), :headers => auth_header
321 assert_response :success, "Unredaction shouldn't have stopped history working."
322 assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1,
323 "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for moderators."
325 auth_header = bearer_authorization_header
327 # check normal user can now see the unredacted data
328 get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
329 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
331 # and when accessed via history
332 get api_way_versions_path(way), :headers => auth_header
333 assert_response :success, "Redaction shouldn't have stopped history working."
334 assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1,
335 "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for normal users."
341 # look at all the versions of the way in the history and get each version from
342 # the versions call. check that they're the same.
343 def check_history_equals_versions(way_id)
344 get api_way_versions_path(way_id)
345 assert_response :success, "can't get way #{way_id} from API"
346 history_doc = XML::Parser.string(@response.body).parse
347 assert_not_nil history_doc, "parsing way #{way_id} history failed"
349 history_doc.find("//osm/way").each do |way_doc|
350 history_way = Way.from_xml_node(way_doc)
351 assert_not_nil history_way, "parsing way #{way_id} version failed"
353 get api_way_version_path(way_id, history_way.version)
354 assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
355 version_way = Way.from_xml(@response.body)
356 assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
358 assert_ways_are_equal history_way, version_way
362 def do_redact_redactable_way(headers = {})
363 way = create(:way, :with_history, :version => 4)
364 way_v3 = way.old_ways.find_by(:version => 3)
365 do_redact_way(way_v3, create(:redaction), headers)
368 def do_redact_way(way, redaction, headers = {})
369 get api_way_version_path(way.way_id, way.version)
370 assert_response :success, "should be able to get version #{way.version} of way #{way.way_id}."
373 post way_version_redact_path(way.way_id, way.version), :params => { :redaction => redaction.id }, :headers => headers