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."
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))
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."
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."
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)
111 check_history_equals_versions(way.id)
112 check_history_equals_versions(used_way.id)
113 check_history_equals_versions(way_with_versions.id)
117 # test the redaction of an old version of a way, while not being
119 def test_redact_way_unauthorised
120 way = create(:way, :with_history, :version => 4)
121 way_v3 = way.old_ways.find_by(:version => 3)
123 do_redact_way(way_v3, create(:redaction))
124 assert_response :unauthorized, "should need to be authenticated to redact."
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)
135 do_redact_way(way_v3, create(:redaction), auth_header)
136 assert_response :forbidden, "should need to be moderator to redact."
140 # test that, even as moderator, the current version of a way
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
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."
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."
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."
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."
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."
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)
183 do_redact_way(way_v3, create(:redaction), auth_header)
184 assert_response :success, "should be OK to redact old version as moderator."
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."
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."
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)
211 do_redact_way(way_v3, create(:redaction), auth_header)
212 assert_response :success, "should be OK to redact old version as moderator."
214 # re-auth as non-moderator
215 auth_header = bearer_authorization_header
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."
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."
229 # test the unredaction of an old version of a way, while not being
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))
236 post way_version_redact_path(way_v1.way_id, way_v1.version)
237 assert_response :unauthorized, "should need to be authenticated to unredact."
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))
248 auth_header = bearer_authorization_header
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."
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))
263 auth_header = bearer_authorization_header moderator_user
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."
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."
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."
279 auth_header = bearer_authorization_header
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."
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."
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"
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"
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}"
312 assert_ways_are_equal history_way, version_way
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)
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}."
327 post way_version_redact_path(way.way_id, way.version), :params => { :redaction => redaction.id }, :headers => headers