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(:id => 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 = basic_authorization_header create(:user).email, "test"
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 = basic_authorization_header create(:moderator_user).email, "test"
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_with_read_prefs_scope
81 auth_header = create_bearer_auth_header(create(:user), %w[read_prefs])
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_api_scope
87 auth_header = create_bearer_auth_header(create(:user), %w[write_api])
88 do_redact_redactable_relation(auth_header)
89 assert_response :forbidden, "should need to be moderator to redact."
92 def test_redact_relation_by_regular_with_write_redactions_scope
93 auth_header = create_bearer_auth_header(create(:user), %w[write_redactions])
94 do_redact_redactable_relation(auth_header)
95 assert_response :forbidden, "should need to be moderator to redact."
98 def test_redact_relation_by_moderator_with_read_prefs_scope
99 auth_header = create_bearer_auth_header(create(:moderator_user), %w[read_prefs])
100 do_redact_redactable_relation(auth_header)
101 assert_response :forbidden, "should need to have write_redactions scope to redact."
104 def test_redact_relation_by_moderator_with_write_api_scope
105 auth_header = create_bearer_auth_header(create(:moderator_user), %w[write_api])
106 do_redact_redactable_relation(auth_header)
107 assert_response :success, "should be OK to redact old version as moderator with write_api scope."
108 # assert_response :forbidden, "should need to have write_redactions scope to redact."
111 def test_redact_relation_by_moderator_with_write_redactions_scope
112 auth_header = create_bearer_auth_header(create(:moderator_user), %w[write_redactions])
113 do_redact_redactable_relation(auth_header)
114 assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
118 # test that redacted relations aren't visible, regardless of
119 # authorisation except as moderator...
120 def test_version_redacted
121 relation = create(:relation, :with_history, :version => 2)
122 relation_v1 = relation.old_relations.find_by(:version => 1)
123 relation_v1.redact!(create(:redaction))
125 get api_old_relation_path(:id => relation_v1.relation_id, :version => relation_v1.version)
126 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
128 # not even to a logged-in user
129 auth_header = basic_authorization_header create(:user).email, "test"
130 get api_old_relation_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
131 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API, even when logged in."
135 # test that redacted relations aren't visible in the history
136 def test_history_redacted
137 relation = create(:relation, :with_history, :version => 2)
138 relation_v1 = relation.old_relations.find_by(:version => 1)
139 relation_v1.redact!(create(:redaction))
141 get api_relation_history_path(:id => relation_v1.relation_id)
142 assert_response :success, "Redaction shouldn't have stopped history working."
143 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 0,
144 "redacted relation #{relation_v1.relation_id} version #{relation_v1.version} shouldn't be present in the history."
146 # not even to a logged-in user
147 auth_header = basic_authorization_header create(:user).email, "test"
148 get api_old_relation_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
149 get api_relation_history_path(:id => relation_v1.relation_id), :headers => auth_header
150 assert_response :success, "Redaction shouldn't have stopped history working."
151 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 0,
152 "redacted relation #{relation_v1.relation_id} version #{relation_v1.version} shouldn't be present in the history, even when logged in."
156 # test the redaction of an old version of a relation, while being
157 # authorised as a moderator.
158 def test_redact_relation_moderator
159 relation = create(:relation, :with_history, :version => 4)
160 relation_v3 = relation.old_relations.find_by(:version => 3)
162 auth_header = basic_authorization_header create(:moderator_user).email, "test"
164 do_redact_relation(relation_v3, create(:redaction), auth_header)
165 assert_response :success, "should be OK to redact old version as moderator."
167 # check moderator can still see the redacted data, when passing
168 # the appropriate flag
169 get api_old_relation_path(:id => relation_v3.relation_id, :version => relation_v3.version), :headers => auth_header
170 assert_response :forbidden, "After redaction, relation should be gone for moderator, when flag not passed."
171 get api_old_relation_path(:id => relation_v3.relation_id, :version => relation_v3.version), :params => { :show_redactions => "true" }, :headers => auth_header
172 assert_response :success, "After redaction, relation should not be gone for moderator, when flag passed."
174 # and when accessed via history
175 get api_relation_history_path(:id => relation_v3.relation_id), :headers => auth_header
176 assert_response :success, "Redaction shouldn't have stopped history working."
177 assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 0,
178 "relation #{relation_v3.relation_id} version #{relation_v3.version} should not be present in the history for moderators when not passing flag."
179 get api_relation_history_path(:id => relation_v3.relation_id), :params => { :show_redactions => "true" }, :headers => auth_header
180 assert_response :success, "Redaction shouldn't have stopped history working."
181 assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 1,
182 "relation #{relation_v3.relation_id} version #{relation_v3.version} should still be present in the history for moderators when passing flag."
185 # testing that if the moderator drops auth, he can't see the
186 # redacted stuff any more.
187 def test_redact_relation_is_redacted
188 relation = create(:relation, :with_history, :version => 4)
189 relation_v3 = relation.old_relations.find_by(:version => 3)
191 auth_header = basic_authorization_header create(:moderator_user).email, "test"
193 do_redact_relation(relation_v3, create(:redaction), auth_header)
194 assert_response :success, "should be OK to redact old version as moderator."
196 # re-auth as non-moderator
197 auth_header = basic_authorization_header create(:user).email, "test"
199 # check can't see the redacted data
200 get api_old_relation_path(:id => relation_v3.relation_id, :version => relation_v3.version), :headers => auth_header
201 assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
203 # and when accessed via history
204 get api_relation_history_path(:id => relation_v3.relation_id), :headers => auth_header
205 assert_response :success, "Redaction shouldn't have stopped history working."
206 assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 0,
207 "redacted relation #{relation_v3.relation_id} version #{relation_v3.version} shouldn't be present in the history."
211 # test the unredaction of an old version of a relation, while not being
213 def test_unredact_relation_unauthorised
214 relation = create(:relation, :with_history, :version => 2)
215 relation_v1 = relation.old_relations.find_by(:version => 1)
216 relation_v1.redact!(create(:redaction))
218 post relation_version_redact_path(:id => relation_v1.relation_id, :version => relation_v1.version)
219 assert_response :unauthorized, "should need to be authenticated to unredact."
223 # test the unredaction of an old version of a relation, while being
224 # authorised as a normal user.
225 def test_unredact_relation_normal_user
226 relation = create(:relation, :with_history, :version => 2)
227 relation_v1 = relation.old_relations.find_by(:version => 1)
228 relation_v1.redact!(create(:redaction))
230 auth_header = basic_authorization_header create(:user).email, "test"
232 post relation_version_redact_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
233 assert_response :forbidden, "should need to be moderator to unredact."
237 # test the unredaction of an old version of a relation, while being
238 # authorised as a moderator.
239 def test_unredact_relation_moderator
240 relation = create(:relation, :with_history, :version => 2)
241 relation_v1 = relation.old_relations.find_by(:version => 1)
242 relation_v1.redact!(create(:redaction))
244 auth_header = basic_authorization_header create(:moderator_user).email, "test"
246 post relation_version_redact_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
247 assert_response :success, "should be OK to unredact old version as moderator."
249 # check moderator can still see the redacted data, without passing
250 # the appropriate flag
251 get api_old_relation_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
252 assert_response :success, "After unredaction, relation should not be gone for moderator."
254 # and when accessed via history
255 get api_relation_history_path(:id => relation_v1.relation_id), :headers => auth_header
256 assert_response :success, "Redaction shouldn't have stopped history working."
257 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1,
258 "relation #{relation_v1.relation_id} version #{relation_v1.version} should still be present in the history for moderators."
260 auth_header = basic_authorization_header create(:user).email, "test"
262 # check normal user can now see the redacted data
263 get api_old_relation_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
264 assert_response :success, "After redaction, node should not be gone for normal user."
266 # and when accessed via history
267 get api_relation_history_path(:id => relation_v1.relation_id), :headers => auth_header
268 assert_response :success, "Redaction shouldn't have stopped history working."
269 assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1,
270 "relation #{relation_v1.relation_id} version #{relation_v1.version} should still be present in the history for normal users."
276 # check that the current version of a relation is equivalent to the
277 # version which we're getting from the versions call.
278 def check_current_version(relation_id)
279 # get the current version
280 current_relation = with_controller(RelationsController.new) do
281 get :show, :params => { :id => relation_id }
282 assert_response :success, "can't get current relation #{relation_id}"
283 Relation.from_xml(@response.body)
285 assert_not_nil current_relation, "getting relation #{relation_id} returned nil"
287 # get the "old" version of the relation from the version method
288 get :version, :params => { :id => relation_id, :version => current_relation.version }
289 assert_response :success, "can't get old relation #{relation_id}, v#{current_relation.version}"
290 old_relation = Relation.from_xml(@response.body)
292 # check that the relations are identical
293 assert_relations_are_equal current_relation, old_relation
297 # look at all the versions of the relation in the history and get each version from
298 # the versions call. check that they're the same.
299 def check_history_equals_versions(relation_id)
300 get :history, :params => { :id => relation_id }
301 assert_response :success, "can't get relation #{relation_id} from API"
302 history_doc = XML::Parser.string(@response.body).parse
303 assert_not_nil history_doc, "parsing relation #{relation_id} history failed"
305 history_doc.find("//osm/relation").each do |relation_doc|
306 history_relation = Relation.from_xml_node(relation_doc)
307 assert_not_nil history_relation, "parsing relation #{relation_id} version failed"
309 get :version, :params => { :id => relation_id, :version => history_relation.version }
310 assert_response :success, "couldn't get relation #{relation_id}, v#{history_relation.version}"
311 version_relation = Relation.from_xml(@response.body)
312 assert_not_nil version_relation, "failed to parse #{relation_id}, v#{history_relation.version}"
314 assert_relations_are_equal history_relation, version_relation
318 def create_bearer_auth_header(user, scopes)
319 token = create(:oauth_access_token,
320 :resource_owner_id => user.id,
322 bearer_authorization_header(token.token)
325 def do_redact_redactable_relation(headers = {})
326 relation = create(:relation, :with_history, :version => 4)
327 relation_v3 = relation.old_relations.find_by(:version => 3)
328 do_redact_relation(relation_v3, create(:redaction), headers)
331 def do_redact_relation(relation, redaction, headers = {})
332 get api_old_relation_path(:id => relation.relation_id, :version => relation.version)
333 assert_response :success, "should be able to get version #{relation.version} of relation #{relation.relation_id}."
336 post relation_version_redact_path(:id => relation.relation_id, :version => relation.version), :params => { :redaction => redaction.id }, :headers => headers