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
66 way = create(:way, :with_history, :version => 2)
67 way_v1 = way.old_ways.find_by(:version => 1)
68 way_v1.redact!(create(:redaction))
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."
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."
84 way = create(:way, :with_history, :version => 2)
86 get api_way_version_path(way, 1)
88 assert_response :success
89 assert_dom "osm:root", 1 do
90 assert_dom "> way", 1 do
91 assert_dom "> @id", way.id.to_s
92 assert_dom "> @version", "1"
96 get api_way_version_path(way, 2)
98 assert_response :success
99 assert_dom "osm:root", 1 do
100 assert_dom "> way", 1 do
101 assert_dom "> @id", way.id.to_s
102 assert_dom "> @version", "2"
108 # test that redacted ways aren't visible, regardless of
109 # authorisation except as moderator...
110 def test_show_redacted
111 way = create(:way, :with_history, :version => 2)
112 way_v1 = way.old_ways.find_by(:version => 1)
113 way_v1.redact!(create(:redaction))
115 get api_way_version_path(way_v1.way_id, way_v1.version)
116 assert_response :forbidden, "Redacted way shouldn't be visible via the version API."
118 # not even to a logged-in user
119 auth_header = bearer_authorization_header
120 get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
121 assert_response :forbidden, "Redacted way shouldn't be visible via the version API, even when logged in."
125 # check that returned history is the same as getting all
126 # versions of a way from the api.
127 def test_history_equals_versions
128 way = create(:way, :with_history)
129 used_way = create(:way, :with_history)
130 create(:relation_member, :member => used_way)
131 way_with_versions = create(:way, :with_history, :version => 4)
133 check_history_equals_versions(way.id)
134 check_history_equals_versions(used_way.id)
135 check_history_equals_versions(way_with_versions.id)
139 # test the redaction of an old version of a way, while not being
141 def test_redact_way_unauthorised
142 way = create(:way, :with_history, :version => 4)
143 way_v3 = way.old_ways.find_by(:version => 3)
145 do_redact_way(way_v3, create(:redaction))
146 assert_response :unauthorized, "should need to be authenticated to redact."
150 # test the redaction of an old version of a way, while being
151 # authorised as a normal user.
152 def test_redact_way_normal_user
153 auth_header = bearer_authorization_header
154 way = create(:way, :with_history, :version => 4)
155 way_v3 = way.old_ways.find_by(:version => 3)
157 do_redact_way(way_v3, create(:redaction), auth_header)
158 assert_response :forbidden, "should need to be moderator to redact."
162 # test that, even as moderator, the current version of a way
164 def test_redact_way_current_version
165 auth_header = bearer_authorization_header create(:moderator_user)
166 way = create(:way, :with_history, :version => 4)
167 way_latest = way.old_ways.last
169 do_redact_way(way_latest, create(:redaction), auth_header)
170 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
173 def test_redact_way_by_regular_without_write_redactions_scope
174 auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
175 do_redact_redactable_way(auth_header)
176 assert_response :forbidden, "should need to be moderator to redact."
179 def test_redact_way_by_regular_with_write_redactions_scope
180 auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
181 do_redact_redactable_way(auth_header)
182 assert_response :forbidden, "should need to be moderator to redact."
185 def test_redact_way_by_moderator_without_write_redactions_scope
186 auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
187 do_redact_redactable_way(auth_header)
188 assert_response :forbidden, "should need to have write_redactions scope to redact."
191 def test_redact_way_by_moderator_with_write_redactions_scope
192 auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
193 do_redact_redactable_way(auth_header)
194 assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
198 # test the redaction of an old version of a way, while being
199 # authorised as a moderator.
200 def test_redact_way_moderator
201 way = create(:way, :with_history, :version => 4)
202 way_v3 = way.old_ways.find_by(:version => 3)
203 auth_header = bearer_authorization_header create(:moderator_user)
205 do_redact_way(way_v3, create(:redaction), auth_header)
206 assert_response :success, "should be OK to redact old version as moderator."
208 # check moderator can still see the redacted data, when passing
209 # the appropriate flag
210 get api_way_version_path(way_v3.way_id, way_v3.version), :headers => auth_header
211 assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
212 get api_way_version_path(way_v3.way_id, way_v3.version, :show_redactions => "true"), :headers => auth_header
213 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
215 # and when accessed via history
216 get api_way_versions_path(way), :headers => auth_header
217 assert_response :success, "Redaction shouldn't have stopped history working."
218 assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 0,
219 "way #{way_v3.way_id} version #{way_v3.version} should not be present in the history for moderators when not passing flag."
220 get api_way_versions_path(way, :show_redactions => "true"), :headers => auth_header
221 assert_response :success, "Redaction shouldn't have stopped history working."
222 assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 1,
223 "way #{way_v3.way_id} version #{way_v3.version} should still be present in the history for moderators when passing flag."
226 # testing that if the moderator drops auth, he can't see the
227 # redacted stuff any more.
228 def test_redact_way_is_redacted
229 way = create(:way, :with_history, :version => 4)
230 way_v3 = way.old_ways.find_by(:version => 3)
231 auth_header = bearer_authorization_header create(:moderator_user)
233 do_redact_way(way_v3, create(:redaction), auth_header)
234 assert_response :success, "should be OK to redact old version as moderator."
236 # re-auth as non-moderator
237 auth_header = bearer_authorization_header
239 # check can't see the redacted data
240 get api_way_version_path(way_v3.way_id, way_v3.version), :headers => auth_header
241 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
243 # and when accessed via history
244 get api_way_versions_path(way), :headers => auth_header
245 assert_response :success, "Redaction shouldn't have stopped history working."
246 assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 0,
247 "redacted way #{way_v3.way_id} version #{way_v3.version} shouldn't be present in the history."
251 # test the unredaction of an old version of a way, while not being
253 def test_unredact_way_unauthorised
254 way = create(:way, :with_history, :version => 2)
255 way_v1 = way.old_ways.find_by(:version => 1)
256 way_v1.redact!(create(:redaction))
258 post way_version_redact_path(way_v1.way_id, way_v1.version)
259 assert_response :unauthorized, "should need to be authenticated to unredact."
263 # test the unredaction of an old version of a way, while being
264 # authorised as a normal user.
265 def test_unredact_way_normal_user
266 way = create(:way, :with_history, :version => 2)
267 way_v1 = way.old_ways.find_by(:version => 1)
268 way_v1.redact!(create(:redaction))
270 auth_header = bearer_authorization_header
272 post way_version_redact_path(way_v1.way_id, way_v1.version), :headers => auth_header
273 assert_response :forbidden, "should need to be moderator to unredact."
277 # test the unredaction of an old version of a way, while being
278 # authorised as a moderator.
279 def test_unredact_way_moderator
280 moderator_user = create(:moderator_user)
281 way = create(:way, :with_history, :version => 2)
282 way_v1 = way.old_ways.find_by(:version => 1)
283 way_v1.redact!(create(:redaction))
285 auth_header = bearer_authorization_header moderator_user
287 post way_version_redact_path(way_v1.way_id, way_v1.version), :headers => auth_header
288 assert_response :success, "should be OK to unredact old version as moderator."
290 # check moderator can still see the unredacted data, without passing
291 # the appropriate flag
292 get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
293 assert_response :success, "After unredaction, node should not be gone for moderator."
295 # and when accessed via history
296 get api_way_versions_path(way), :headers => auth_header
297 assert_response :success, "Unredaction shouldn't have stopped history working."
298 assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1,
299 "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for moderators."
301 auth_header = bearer_authorization_header
303 # check normal user can now see the unredacted data
304 get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
305 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
307 # and when accessed via history
308 get api_way_versions_path(way), :headers => auth_header
309 assert_response :success, "Redaction shouldn't have stopped history working."
310 assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1,
311 "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for normal users."
317 # look at all the versions of the way in the history and get each version from
318 # the versions call. check that they're the same.
319 def check_history_equals_versions(way_id)
320 get api_way_versions_path(way_id)
321 assert_response :success, "can't get way #{way_id} from API"
322 history_doc = XML::Parser.string(@response.body).parse
323 assert_not_nil history_doc, "parsing way #{way_id} history failed"
325 history_doc.find("//osm/way").each do |way_doc|
326 history_way = Way.from_xml_node(way_doc)
327 assert_not_nil history_way, "parsing way #{way_id} version failed"
329 get api_way_version_path(way_id, history_way.version)
330 assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
331 version_way = Way.from_xml(@response.body)
332 assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
334 assert_ways_are_equal history_way, version_way
338 def do_redact_redactable_way(headers = {})
339 way = create(:way, :with_history, :version => 4)
340 way_v3 = way.old_ways.find_by(:version => 3)
341 do_redact_way(way_v3, create(:redaction), headers)
344 def do_redact_way(way, redaction, headers = {})
345 get api_way_version_path(way.way_id, way.version)
346 assert_response :success, "should be able to get version #{way.version} of way #{way.way_id}."
349 post way_version_redact_path(way.way_id, way.version), :params => { :redaction => redaction.id }, :headers => headers