4 class OldRelationsControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/relation/1/history", :method => :get },
10 { :controller => "api/old_relations", :action => "index", :relation_id => "1" }
13 { :path => "/api/0.6/relation/1/history.json", :method => :get },
14 { :controller => "api/old_relations", :action => "index", :relation_id => "1", :format => "json" }
17 { :path => "/api/0.6/relation/1/2", :method => :get },
18 { :controller => "api/old_relations", :action => "show", :relation_id => "1", :version => "2" }
21 { :path => "/api/0.6/relation/1/2.json", :method => :get },
22 { :controller => "api/old_relations", :action => "show", :relation_id => "1", :version => "2", :format => "json" }
25 { :path => "/api/0.6/relation/1/2/redact", :method => :post },
26 { :controller => "api/old_relations", :action => "redact", :relation_id => "1", :version => "2" }
31 # check that a visible relations is returned properly
33 relation = create(:relation, :with_history, :version => 2)
35 get api_relation_versions_path(relation)
37 assert_response :success
38 assert_dom "osm:root", 1 do
39 assert_dom "> relation", 2 do |dom_relations|
40 assert_dom dom_relations[0], "> @id", relation.id.to_s
41 assert_dom dom_relations[0], "> @version", "1"
43 assert_dom dom_relations[1], "> @id", relation.id.to_s
44 assert_dom dom_relations[1], "> @version", "2"
50 # check that a non-existent relations is not returned
51 def test_index_invalid
52 get api_relation_versions_path(0)
53 assert_response :not_found
57 # test that redacted relations aren't visible in the history
58 def test_index_redacted_unauthorised
59 relation = create(:relation, :with_history, :version => 2)
60 relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
62 get api_relation_versions_path(relation)
64 assert_response :success, "Redaction shouldn't have stopped history working."
65 assert_dom "osm relation[id='#{relation.id}'][version='1']", 0,
66 "redacted relation #{relation.id} version 1 shouldn't be present in the history."
68 get api_relation_versions_path(relation, :show_redactions => "true")
70 assert_response :success, "Redaction shouldn't have stopped history working."
71 assert_dom "osm relation[id='#{relation.id}'][version='1']", 0,
72 "redacted relation #{relation.id} version 1 shouldn't be present in the history when passing flag."
75 def test_index_redacted_normal_user
76 relation = create(:relation, :with_history, :version => 2)
77 relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
79 get api_relation_versions_path(relation), :headers => bearer_authorization_header
81 assert_response :success, "Redaction shouldn't have stopped history working."
82 assert_dom "osm relation[id='#{relation.id}'][version='1']", 0,
83 "redacted relation #{relation.id} version 1 shouldn't be present in the history, even when logged in."
85 get api_relation_versions_path(relation, :show_redactions => "true"), :headers => bearer_authorization_header
87 assert_response :success, "Redaction shouldn't have stopped history working."
88 assert_dom "osm relation[id='#{relation.id}'][version='1']", 0,
89 "redacted relation #{relation.id} version 1 shouldn't be present in the history, even when logged in and passing flag."
92 def test_index_redacted_moderator
93 relation = create(:relation, :with_history, :version => 2)
94 relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
95 auth_header = bearer_authorization_header create(:moderator_user)
97 get api_relation_versions_path(relation), :headers => auth_header
99 assert_response :success, "Redaction shouldn't have stopped history working."
100 assert_dom "osm relation[id='#{relation.id}'][version='1']", 0,
101 "relation #{relation.id} version 1 should not be present in the history for moderators when not passing flag."
103 get api_relation_versions_path(relation, :show_redactions => "true"), :headers => auth_header
105 assert_response :success, "Redaction shouldn't have stopped history working."
106 assert_dom "osm relation[id='#{relation.id}'][version='1']", 1,
107 "relation #{relation.id} version 1 should still be present in the history for moderators when passing flag."
111 relation = create(:relation, :with_history, :version => 2)
112 create(:old_relation_tag, :old_relation => relation.old_relations[0], :k => "k1", :v => "v1")
113 create(:old_relation_tag, :old_relation => relation.old_relations[1], :k => "k2", :v => "v2")
115 get api_relation_version_path(relation, 1)
117 assert_response :success
118 assert_dom "osm:root", 1 do
119 assert_dom "> relation", 1 do
120 assert_dom "> @id", relation.id.to_s
121 assert_dom "> @version", "1"
122 assert_dom "> tag", 1 do
123 assert_dom "> @k", "k1"
124 assert_dom "> @v", "v1"
129 get api_relation_version_path(relation, 2)
131 assert_response :success
132 assert_dom "osm:root", 1 do
133 assert_dom "> relation", 1 do
134 assert_dom "> @id", relation.id.to_s
135 assert_dom "> @version", "2"
136 assert_dom "> tag", 1 do
137 assert_dom "> @k", "k2"
138 assert_dom "> @v", "v2"
145 # test that redacted relations aren't visible, regardless of
146 # authorisation except as moderator...
147 def test_show_redacted_unauthorised
148 relation = create(:relation, :with_history, :version => 2)
149 relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
151 get api_relation_version_path(relation, 1)
153 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
155 get api_relation_version_path(relation, 1, :show_redactions => "true")
157 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API when passing flag."
160 def test_show_redacted_normal_user
161 relation = create(:relation, :with_history, :version => 2)
162 relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
164 get api_relation_version_path(relation, 1), :headers => bearer_authorization_header
166 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API, even when logged in."
168 get api_relation_version_path(relation, 1, :show_redactions => "true"), :headers => bearer_authorization_header
170 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API, even when logged in and passing flag."
173 def test_show_redacted_moderator
174 relation = create(:relation, :with_history, :version => 2)
175 relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
176 auth_header = bearer_authorization_header create(:moderator_user)
178 get api_relation_version_path(relation, 1), :headers => auth_header
180 assert_response :forbidden, "Redacted relation should be gone for moderator, when flag not passed."
182 get api_relation_version_path(relation, 1, :show_redactions => "true"), :headers => auth_header
184 assert_response :success, "Redacted relation should not be gone for moderator, when flag passed."
188 # test the redaction of an old version of a relation, while not being
190 def test_redact_relation_unauthorised
191 relation = create(:relation, :with_history, :version => 4)
192 relation_v3 = relation.old_relations.find_by(:version => 3)
194 do_redact_relation(relation_v3, create(:redaction))
195 assert_response :unauthorized, "should need to be authenticated to redact."
199 # test the redaction of an old version of a relation, while being
200 # authorised as a normal user.
201 def test_redact_relation_normal_user
202 relation = create(:relation, :with_history, :version => 4)
203 relation_v3 = relation.old_relations.find_by(:version => 3)
205 auth_header = bearer_authorization_header
207 do_redact_relation(relation_v3, create(:redaction), auth_header)
208 assert_response :forbidden, "should need to be moderator to redact."
212 # test that, even as moderator, the current version of a relation
214 def test_redact_relation_current_version
215 relation = create(:relation, :with_history, :version => 4)
216 relation_latest = relation.old_relations.last
218 auth_header = bearer_authorization_header create(:moderator_user)
220 do_redact_relation(relation_latest, create(:redaction), auth_header)
221 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
224 def test_redact_relation_by_regular_without_write_redactions_scope
225 auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
226 do_redact_redactable_relation(auth_header)
227 assert_response :forbidden, "should need to be moderator to redact."
230 def test_redact_relation_by_regular_with_write_redactions_scope
231 auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
232 do_redact_redactable_relation(auth_header)
233 assert_response :forbidden, "should need to be moderator to redact."
236 def test_redact_relation_by_moderator_without_write_redactions_scope
237 auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
238 do_redact_redactable_relation(auth_header)
239 assert_response :forbidden, "should need to have write_redactions scope to redact."
242 def test_redact_relation_by_moderator_with_write_redactions_scope
243 auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
244 do_redact_redactable_relation(auth_header)
245 assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
249 # test the redaction of an old version of a relation, while being
250 # authorised as a moderator.
251 def test_redact_relation_moderator
252 relation = create(:relation, :with_history, :version => 4)
253 relation_v3 = relation.old_relations.find_by(:version => 3)
254 auth_header = bearer_authorization_header create(:moderator_user)
256 do_redact_relation(relation_v3, create(:redaction), auth_header)
258 assert_response :success, "should be OK to redact old version as moderator."
259 assert_predicate relation_v3.reload, :redacted?
263 # test the unredaction of an old version of a relation, while not being
265 def test_unredact_relation_unauthorised
266 relation = create(:relation, :with_history, :version => 2)
267 relation_v1 = relation.old_relations.find_by(:version => 1)
268 relation_v1.redact!(create(:redaction))
270 post relation_version_redact_path(relation_v1.relation_id, relation_v1.version)
271 assert_response :unauthorized, "should need to be authenticated to unredact."
275 # test the unredaction of an old version of a relation, while being
276 # authorised as a normal user.
277 def test_unredact_relation_normal_user
278 relation = create(:relation, :with_history, :version => 2)
279 relation_v1 = relation.old_relations.find_by(:version => 1)
280 relation_v1.redact!(create(:redaction))
282 auth_header = bearer_authorization_header
284 post relation_version_redact_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
285 assert_response :forbidden, "should need to be moderator to unredact."
289 # test the unredaction of an old version of a relation, while being
290 # authorised as a moderator.
291 def test_unredact_relation_moderator
292 relation = create(:relation, :with_history, :version => 2)
293 relation_v1 = relation.old_relations.find_by(:version => 1)
294 relation_v1.redact!(create(:redaction))
296 auth_header = bearer_authorization_header create(:moderator_user)
298 post relation_version_redact_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
299 assert_response :success, "should be OK to unredact old version as moderator."
301 # check moderator can still see the redacted data, without passing
302 # the appropriate flag
303 get api_relation_version_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
304 assert_response :success, "After unredaction, relation should not be gone for moderator."
306 # and when accessed via history
307 get api_relation_versions_path(relation), :headers => auth_header
308 assert_response :success, "Redaction shouldn't have stopped history working."
309 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1,
310 "relation #{relation_v1.relation_id} version #{relation_v1.version} should still be present in the history for moderators."
312 auth_header = bearer_authorization_header
314 # check normal user can now see the redacted data
315 get api_relation_version_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
316 assert_response :success, "After redaction, node should not be gone for normal user."
318 # and when accessed via history
319 get api_relation_versions_path(relation), :headers => auth_header
320 assert_response :success, "Redaction shouldn't have stopped history working."
321 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1,
322 "relation #{relation_v1.relation_id} version #{relation_v1.version} should still be present in the history for normal users."
327 def do_redact_redactable_relation(headers = {})
328 relation = create(:relation, :with_history, :version => 4)
329 relation_v3 = relation.old_relations.find_by(:version => 3)
330 do_redact_relation(relation_v3, create(:redaction), headers)
333 def do_redact_relation(relation, redaction, headers = {})
334 get api_relation_version_path(relation.relation_id, relation.version)
335 assert_response :success, "should be able to get version #{relation.version} of relation #{relation.relation_id}."
338 post relation_version_redact_path(relation.relation_id, relation.version), :params => { :redaction => redaction.id }, :headers => headers