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 => "history", :id => "1" }
13 { :path => "/api/0.6/relation/1/2", :method => :get },
14 { :controller => "api/old_relations", :action => "show", :id => "1", :version => "2" }
17 { :path => "/api/0.6/relation/1/history.json", :method => :get },
18 { :controller => "api/old_relations", :action => "history", :id => "1", :format => "json" }
21 { :path => "/api/0.6/relation/1/2.json", :method => :get },
22 { :controller => "api/old_relations", :action => "show", :id => "1", :version => "2", :format => "json" }
25 { :path => "/api/0.6/relation/1/2/redact", :method => :post },
26 { :controller => "api/old_relations", :action => "redact", :id => "1", :version => "2" }
30 # -------------------------------------
31 # Test reading old relations.
32 # -------------------------------------
34 # check that a visible relations is returned properly
35 get api_relation_history_path(create(:relation, :with_history))
36 assert_response :success
38 # check chat a non-existent relations is not returned
39 get api_relation_history_path(0)
40 assert_response :not_found
44 # test the redaction of an old version of a relation, while not being
46 def test_redact_relation_unauthorised
47 relation = create(:relation, :with_history, :version => 4)
48 relation_v3 = relation.old_relations.find_by(:version => 3)
50 do_redact_relation(relation_v3, create(:redaction))
51 assert_response :unauthorized, "should need to be authenticated to redact."
55 # test the redaction of an old version of a relation, while being
56 # authorised as a normal user.
57 def test_redact_relation_normal_user
58 relation = create(:relation, :with_history, :version => 4)
59 relation_v3 = relation.old_relations.find_by(:version => 3)
61 auth_header = bearer_authorization_header
63 do_redact_relation(relation_v3, create(:redaction), auth_header)
64 assert_response :forbidden, "should need to be moderator to redact."
68 # test that, even as moderator, the current version of a relation
70 def test_redact_relation_current_version
71 relation = create(:relation, :with_history, :version => 4)
72 relation_latest = relation.old_relations.last
74 auth_header = bearer_authorization_header create(:moderator_user)
76 do_redact_relation(relation_latest, create(:redaction), auth_header)
77 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
80 def test_redact_relation_by_regular_without_write_redactions_scope
81 auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
82 do_redact_redactable_relation(auth_header)
83 assert_response :forbidden, "should need to be moderator to redact."
86 def test_redact_relation_by_regular_with_write_redactions_scope
87 auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
88 do_redact_redactable_relation(auth_header)
89 assert_response :forbidden, "should need to be moderator to redact."
92 def test_redact_relation_by_moderator_without_write_redactions_scope
93 auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
94 do_redact_redactable_relation(auth_header)
95 assert_response :forbidden, "should need to have write_redactions scope to redact."
98 def test_redact_relation_by_moderator_with_write_redactions_scope
99 auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
100 do_redact_redactable_relation(auth_header)
101 assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
105 # test that redacted relations aren't visible, regardless of
106 # authorisation except as moderator...
107 def test_version_redacted
108 relation = create(:relation, :with_history, :version => 2)
109 relation_v1 = relation.old_relations.find_by(:version => 1)
110 relation_v1.redact!(create(:redaction))
112 get api_old_relation_path(relation_v1.relation_id, relation_v1.version)
113 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
115 # not even to a logged-in user
116 auth_header = bearer_authorization_header
117 get api_old_relation_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
118 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API, even when logged in."
122 # test that redacted relations aren't visible in the history
123 def test_history_redacted
124 relation = create(:relation, :with_history, :version => 2)
125 relation_v1 = relation.old_relations.find_by(:version => 1)
126 relation_v1.redact!(create(:redaction))
128 get api_relation_history_path(relation)
129 assert_response :success, "Redaction shouldn't have stopped history working."
130 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 0,
131 "redacted relation #{relation_v1.relation_id} version #{relation_v1.version} shouldn't be present in the history."
133 # not even to a logged-in user
134 auth_header = bearer_authorization_header
135 get api_old_relation_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
136 get api_relation_history_path(relation), :headers => auth_header
137 assert_response :success, "Redaction shouldn't have stopped history working."
138 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 0,
139 "redacted relation #{relation_v1.relation_id} version #{relation_v1.version} shouldn't be present in the history, even when logged in."
143 # test the redaction of an old version of a relation, while being
144 # authorised as a moderator.
145 def test_redact_relation_moderator
146 relation = create(:relation, :with_history, :version => 4)
147 relation_v3 = relation.old_relations.find_by(:version => 3)
149 auth_header = bearer_authorization_header create(:moderator_user)
151 do_redact_relation(relation_v3, create(:redaction), auth_header)
152 assert_response :success, "should be OK to redact old version as moderator."
154 # check moderator can still see the redacted data, when passing
155 # the appropriate flag
156 get api_old_relation_path(relation_v3.relation_id, relation_v3.version), :headers => auth_header
157 assert_response :forbidden, "After redaction, relation should be gone for moderator, when flag not passed."
158 get api_old_relation_path(relation_v3.relation_id, relation_v3.version, :show_redactions => "true"), :headers => auth_header
159 assert_response :success, "After redaction, relation should not be gone for moderator, when flag passed."
161 # and when accessed via history
162 get api_relation_history_path(relation), :headers => auth_header
163 assert_response :success, "Redaction shouldn't have stopped history working."
164 assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 0,
165 "relation #{relation_v3.relation_id} version #{relation_v3.version} should not be present in the history for moderators when not passing flag."
166 get api_relation_history_path(relation, :show_redactions => "true"), :headers => auth_header
167 assert_response :success, "Redaction shouldn't have stopped history working."
168 assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 1,
169 "relation #{relation_v3.relation_id} version #{relation_v3.version} should still be present in the history for moderators when passing flag."
172 # testing that if the moderator drops auth, he can't see the
173 # redacted stuff any more.
174 def test_redact_relation_is_redacted
175 relation = create(:relation, :with_history, :version => 4)
176 relation_v3 = relation.old_relations.find_by(:version => 3)
178 auth_header = bearer_authorization_header create(:moderator_user)
180 do_redact_relation(relation_v3, create(:redaction), auth_header)
181 assert_response :success, "should be OK to redact old version as moderator."
183 # re-auth as non-moderator
184 auth_header = bearer_authorization_header
186 # check can't see the redacted data
187 get api_old_relation_path(relation_v3.relation_id, relation_v3.version), :headers => auth_header
188 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
190 # and when accessed via history
191 get api_relation_history_path(relation), :headers => auth_header
192 assert_response :success, "Redaction shouldn't have stopped history working."
193 assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 0,
194 "redacted relation #{relation_v3.relation_id} version #{relation_v3.version} shouldn't be present in the history."
198 # test the unredaction of an old version of a relation, while not being
200 def test_unredact_relation_unauthorised
201 relation = create(:relation, :with_history, :version => 2)
202 relation_v1 = relation.old_relations.find_by(:version => 1)
203 relation_v1.redact!(create(:redaction))
205 post relation_version_redact_path(relation_v1.relation_id, relation_v1.version)
206 assert_response :unauthorized, "should need to be authenticated to unredact."
210 # test the unredaction of an old version of a relation, while being
211 # authorised as a normal user.
212 def test_unredact_relation_normal_user
213 relation = create(:relation, :with_history, :version => 2)
214 relation_v1 = relation.old_relations.find_by(:version => 1)
215 relation_v1.redact!(create(:redaction))
217 auth_header = bearer_authorization_header
219 post relation_version_redact_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
220 assert_response :forbidden, "should need to be moderator to unredact."
224 # test the unredaction of an old version of a relation, while being
225 # authorised as a moderator.
226 def test_unredact_relation_moderator
227 relation = create(:relation, :with_history, :version => 2)
228 relation_v1 = relation.old_relations.find_by(:version => 1)
229 relation_v1.redact!(create(:redaction))
231 auth_header = bearer_authorization_header create(:moderator_user)
233 post relation_version_redact_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
234 assert_response :success, "should be OK to unredact old version as moderator."
236 # check moderator can still see the redacted data, without passing
237 # the appropriate flag
238 get api_old_relation_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
239 assert_response :success, "After unredaction, relation should not be gone for moderator."
241 # and when accessed via history
242 get api_relation_history_path(relation), :headers => auth_header
243 assert_response :success, "Redaction shouldn't have stopped history working."
244 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1,
245 "relation #{relation_v1.relation_id} version #{relation_v1.version} should still be present in the history for moderators."
247 auth_header = bearer_authorization_header
249 # check normal user can now see the redacted data
250 get api_old_relation_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
251 assert_response :success, "After redaction, node should not be gone for normal user."
253 # and when accessed via history
254 get api_relation_history_path(relation), :headers => auth_header
255 assert_response :success, "Redaction shouldn't have stopped history working."
256 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1,
257 "relation #{relation_v1.relation_id} version #{relation_v1.version} should still be present in the history for normal users."
263 # check that the current version of a relation is equivalent to the
264 # version which we're getting from the versions call.
265 def check_current_version(relation_id)
266 # get the current version
267 current_relation = with_controller(RelationsController.new) do
268 get :show, :params => { :id => relation_id }
269 assert_response :success, "can't get current relation #{relation_id}"
270 Relation.from_xml(@response.body)
272 assert_not_nil current_relation, "getting relation #{relation_id} returned nil"
274 # get the "old" version of the relation from the version method
275 get :version, :params => { :id => relation_id, :version => current_relation.version }
276 assert_response :success, "can't get old relation #{relation_id}, v#{current_relation.version}"
277 old_relation = Relation.from_xml(@response.body)
279 # check that the relations are identical
280 assert_relations_are_equal current_relation, old_relation
284 # look at all the versions of the relation in the history and get each version from
285 # the versions call. check that they're the same.
286 def check_history_equals_versions(relation_id)
287 get :history, :params => { :id => relation_id }
288 assert_response :success, "can't get relation #{relation_id} from API"
289 history_doc = XML::Parser.string(@response.body).parse
290 assert_not_nil history_doc, "parsing relation #{relation_id} history failed"
292 history_doc.find("//osm/relation").each do |relation_doc|
293 history_relation = Relation.from_xml_node(relation_doc)
294 assert_not_nil history_relation, "parsing relation #{relation_id} version failed"
296 get :version, :params => { :id => relation_id, :version => history_relation.version }
297 assert_response :success, "couldn't get relation #{relation_id}, v#{history_relation.version}"
298 version_relation = Relation.from_xml(@response.body)
299 assert_not_nil version_relation, "failed to parse #{relation_id}, v#{history_relation.version}"
301 assert_relations_are_equal history_relation, version_relation
305 def do_redact_redactable_relation(headers = {})
306 relation = create(:relation, :with_history, :version => 4)
307 relation_v3 = relation.old_relations.find_by(:version => 3)
308 do_redact_relation(relation_v3, create(:redaction), headers)
311 def do_redact_relation(relation, redaction, headers = {})
312 get api_old_relation_path(relation.relation_id, relation.version)
313 assert_response :success, "should be able to get version #{relation.version} of relation #{relation.relation_id}."
316 post relation_version_redact_path(relation.relation_id, relation.version), :params => { :redaction => redaction.id }, :headers => headers