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."
69 def test_index_redacted_normal_user
70 relation = create(:relation, :with_history, :version => 2)
71 relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
73 get api_relation_versions_path(relation), :headers => bearer_authorization_header
75 assert_response :success, "Redaction shouldn't have stopped history working."
76 assert_dom "osm relation[id='#{relation.id}'][version='1']", 0,
77 "redacted relation #{relation.id} version 1 shouldn't be present in the history, even when logged in."
81 relation = create(:relation, :with_history, :version => 2)
82 create(:old_relation_tag, :old_relation => relation.old_relations[0], :k => "k1", :v => "v1")
83 create(:old_relation_tag, :old_relation => relation.old_relations[1], :k => "k2", :v => "v2")
85 get api_relation_version_path(relation, 1)
87 assert_response :success
88 assert_dom "osm:root", 1 do
89 assert_dom "> relation", 1 do
90 assert_dom "> @id", relation.id.to_s
91 assert_dom "> @version", "1"
92 assert_dom "> tag", 1 do
93 assert_dom "> @k", "k1"
94 assert_dom "> @v", "v1"
99 get api_relation_version_path(relation, 2)
101 assert_response :success
102 assert_dom "osm:root", 1 do
103 assert_dom "> relation", 1 do
104 assert_dom "> @id", relation.id.to_s
105 assert_dom "> @version", "2"
106 assert_dom "> tag", 1 do
107 assert_dom "> @k", "k2"
108 assert_dom "> @v", "v2"
115 # test that redacted relations aren't visible, regardless of
116 # authorisation except as moderator...
117 def test_show_redacted_unauthorised
118 relation = create(:relation, :with_history, :version => 2)
119 relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
121 get api_relation_version_path(relation, 1)
123 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
126 def test_show_redacted_normal_user
127 relation = create(:relation, :with_history, :version => 2)
128 relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
130 get api_relation_version_path(relation, 1), :headers => bearer_authorization_header
132 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API, even when logged in."
136 # test the redaction of an old version of a relation, while not being
138 def test_redact_relation_unauthorised
139 relation = create(:relation, :with_history, :version => 4)
140 relation_v3 = relation.old_relations.find_by(:version => 3)
142 do_redact_relation(relation_v3, create(:redaction))
143 assert_response :unauthorized, "should need to be authenticated to redact."
147 # test the redaction of an old version of a relation, while being
148 # authorised as a normal user.
149 def test_redact_relation_normal_user
150 relation = create(:relation, :with_history, :version => 4)
151 relation_v3 = relation.old_relations.find_by(:version => 3)
153 auth_header = bearer_authorization_header
155 do_redact_relation(relation_v3, create(:redaction), auth_header)
156 assert_response :forbidden, "should need to be moderator to redact."
160 # test that, even as moderator, the current version of a relation
162 def test_redact_relation_current_version
163 relation = create(:relation, :with_history, :version => 4)
164 relation_latest = relation.old_relations.last
166 auth_header = bearer_authorization_header create(:moderator_user)
168 do_redact_relation(relation_latest, create(:redaction), auth_header)
169 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
172 def test_redact_relation_by_regular_without_write_redactions_scope
173 auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
174 do_redact_redactable_relation(auth_header)
175 assert_response :forbidden, "should need to be moderator to redact."
178 def test_redact_relation_by_regular_with_write_redactions_scope
179 auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
180 do_redact_redactable_relation(auth_header)
181 assert_response :forbidden, "should need to be moderator to redact."
184 def test_redact_relation_by_moderator_without_write_redactions_scope
185 auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
186 do_redact_redactable_relation(auth_header)
187 assert_response :forbidden, "should need to have write_redactions scope to redact."
190 def test_redact_relation_by_moderator_with_write_redactions_scope
191 auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
192 do_redact_redactable_relation(auth_header)
193 assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
197 # test the redaction of an old version of a relation, while being
198 # authorised as a moderator.
199 def test_redact_relation_moderator
200 relation = create(:relation, :with_history, :version => 4)
201 relation_v3 = relation.old_relations.find_by(:version => 3)
203 auth_header = bearer_authorization_header create(:moderator_user)
205 do_redact_relation(relation_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_relation_version_path(relation_v3.relation_id, relation_v3.version), :headers => auth_header
211 assert_response :forbidden, "After redaction, relation should be gone for moderator, when flag not passed."
212 get api_relation_version_path(relation_v3.relation_id, relation_v3.version, :show_redactions => "true"), :headers => auth_header
213 assert_response :success, "After redaction, relation should not be gone for moderator, when flag passed."
215 # and when accessed via history
216 get api_relation_versions_path(relation), :headers => auth_header
217 assert_response :success, "Redaction shouldn't have stopped history working."
218 assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 0,
219 "relation #{relation_v3.relation_id} version #{relation_v3.version} should not be present in the history for moderators when not passing flag."
220 get api_relation_versions_path(relation, :show_redactions => "true"), :headers => auth_header
221 assert_response :success, "Redaction shouldn't have stopped history working."
222 assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 1,
223 "relation #{relation_v3.relation_id} version #{relation_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_relation_is_redacted
229 relation = create(:relation, :with_history, :version => 4)
230 relation_v3 = relation.old_relations.find_by(:version => 3)
232 auth_header = bearer_authorization_header create(:moderator_user)
234 do_redact_relation(relation_v3, create(:redaction), auth_header)
235 assert_response :success, "should be OK to redact old version as moderator."
237 # re-auth as non-moderator
238 auth_header = bearer_authorization_header
240 # check can't see the redacted data
241 get api_relation_version_path(relation_v3.relation_id, relation_v3.version), :headers => auth_header
242 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
244 # and when accessed via history
245 get api_relation_versions_path(relation), :headers => auth_header
246 assert_response :success, "Redaction shouldn't have stopped history working."
247 assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 0,
248 "redacted relation #{relation_v3.relation_id} version #{relation_v3.version} shouldn't be present in the history."
252 # test the unredaction of an old version of a relation, while not being
254 def test_unredact_relation_unauthorised
255 relation = create(:relation, :with_history, :version => 2)
256 relation_v1 = relation.old_relations.find_by(:version => 1)
257 relation_v1.redact!(create(:redaction))
259 post relation_version_redact_path(relation_v1.relation_id, relation_v1.version)
260 assert_response :unauthorized, "should need to be authenticated to unredact."
264 # test the unredaction of an old version of a relation, while being
265 # authorised as a normal user.
266 def test_unredact_relation_normal_user
267 relation = create(:relation, :with_history, :version => 2)
268 relation_v1 = relation.old_relations.find_by(:version => 1)
269 relation_v1.redact!(create(:redaction))
271 auth_header = bearer_authorization_header
273 post relation_version_redact_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
274 assert_response :forbidden, "should need to be moderator to unredact."
278 # test the unredaction of an old version of a relation, while being
279 # authorised as a moderator.
280 def test_unredact_relation_moderator
281 relation = create(:relation, :with_history, :version => 2)
282 relation_v1 = relation.old_relations.find_by(:version => 1)
283 relation_v1.redact!(create(:redaction))
285 auth_header = bearer_authorization_header create(:moderator_user)
287 post relation_version_redact_path(relation_v1.relation_id, relation_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 redacted data, without passing
291 # the appropriate flag
292 get api_relation_version_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
293 assert_response :success, "After unredaction, relation should not be gone for moderator."
295 # and when accessed via history
296 get api_relation_versions_path(relation), :headers => auth_header
297 assert_response :success, "Redaction shouldn't have stopped history working."
298 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1,
299 "relation #{relation_v1.relation_id} version #{relation_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 redacted data
304 get api_relation_version_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
305 assert_response :success, "After redaction, node should not be gone for normal user."
307 # and when accessed via history
308 get api_relation_versions_path(relation), :headers => auth_header
309 assert_response :success, "Redaction shouldn't have stopped history working."
310 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1,
311 "relation #{relation_v1.relation_id} version #{relation_v1.version} should still be present in the history for normal users."
316 def do_redact_redactable_relation(headers = {})
317 relation = create(:relation, :with_history, :version => 4)
318 relation_v3 = relation.old_relations.find_by(:version => 3)
319 do_redact_relation(relation_v3, create(:redaction), headers)
322 def do_redact_relation(relation, redaction, headers = {})
323 get api_relation_version_path(relation.relation_id, relation.version)
324 assert_response :success, "should be able to get version #{relation.version} of relation #{relation.relation_id}."
327 post relation_version_redact_path(relation.relation_id, relation.version), :params => { :redaction => redaction.id }, :headers => headers