]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/old_relations_controller_test.rb
Merge remote-tracking branch 'upstream/pull/4717'
[rails.git] / test / controllers / api / old_relations_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class OldRelationsControllerTest < ActionDispatch::IntegrationTest
5     ##
6     # test all routes which lead to this controller
7     def test_routes
8       assert_routing(
9         { :path => "/api/0.6/relation/1/history", :method => :get },
10         { :controller => "api/old_relations", :action => "history", :id => "1" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/relation/1/2", :method => :get },
14         { :controller => "api/old_relations", :action => "show", :id => "1", :version => "2" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/relation/1/history.json", :method => :get },
18         { :controller => "api/old_relations", :action => "history", :id => "1", :format => "json" }
19       )
20       assert_routing(
21         { :path => "/api/0.6/relation/1/2.json", :method => :get },
22         { :controller => "api/old_relations", :action => "show", :id => "1", :version => "2", :format => "json" }
23       )
24       assert_routing(
25         { :path => "/api/0.6/relation/1/2/redact", :method => :post },
26         { :controller => "api/old_relations", :action => "redact", :id => "1", :version => "2" }
27       )
28     end
29
30     # -------------------------------------
31     # Test reading old relations.
32     # -------------------------------------
33     def test_history
34       # check that a visible relations is returned properly
35       get api_relation_history_path(create(:relation, :with_history))
36       assert_response :success
37
38       # check chat a non-existent relations is not returned
39       get api_relation_history_path(0)
40       assert_response :not_found
41     end
42
43     ##
44     # test the redaction of an old version of a relation, while not being
45     # authorised.
46     def test_redact_relation_unauthorised
47       relation = create(:relation, :with_history, :version => 4)
48       relation_v3 = relation.old_relations.find_by(:version => 3)
49
50       do_redact_relation(relation_v3, create(:redaction))
51       assert_response :unauthorized, "should need to be authenticated to redact."
52     end
53
54     ##
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)
60
61       auth_header = basic_authorization_header create(:user).email, "test"
62
63       do_redact_relation(relation_v3, create(:redaction), auth_header)
64       assert_response :forbidden, "should need to be moderator to redact."
65     end
66
67     ##
68     # test that, even as moderator, the current version of a relation
69     # can't be redacted.
70     def test_redact_relation_current_version
71       relation = create(:relation, :with_history, :version => 4)
72       relation_latest = relation.old_relations.last
73
74       auth_header = basic_authorization_header create(:moderator_user).email, "test"
75
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."
78     end
79
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."
84     end
85
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."
90     end
91
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."
96     end
97
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."
102     end
103
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."
109     end
110
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."
115     end
116
117     ##
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))
124
125       get api_old_relation_path(relation_v1.relation_id, relation_v1.version)
126       assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
127
128       # not even to a logged-in user
129       auth_header = basic_authorization_header create(:user).email, "test"
130       get api_old_relation_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
131       assert_response :forbidden, "Redacted relation shouldn't be visible via the version API, even when logged in."
132     end
133
134     ##
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))
140
141       get api_relation_history_path(relation)
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."
145
146       # not even to a logged-in user
147       auth_header = basic_authorization_header create(:user).email, "test"
148       get api_old_relation_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
149       get api_relation_history_path(relation), :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."
153     end
154
155     ##
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)
161
162       auth_header = basic_authorization_header create(:moderator_user).email, "test"
163
164       do_redact_relation(relation_v3, create(:redaction), auth_header)
165       assert_response :success, "should be OK to redact old version as moderator."
166
167       # check moderator can still see the redacted data, when passing
168       # the appropriate flag
169       get api_old_relation_path(relation_v3.relation_id, 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(relation_v3.relation_id, relation_v3.version, :show_redactions => "true"), :headers => auth_header
172       assert_response :success, "After redaction, relation should not be gone for moderator, when flag passed."
173
174       # and when accessed via history
175       get api_relation_history_path(relation), :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(relation, :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."
183     end
184
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)
190
191       auth_header = basic_authorization_header create(:moderator_user).email, "test"
192
193       do_redact_relation(relation_v3, create(:redaction), auth_header)
194       assert_response :success, "should be OK to redact old version as moderator."
195
196       # re-auth as non-moderator
197       auth_header = basic_authorization_header create(:user).email, "test"
198
199       # check can't see the redacted data
200       get api_old_relation_path(relation_v3.relation_id, relation_v3.version), :headers => auth_header
201       assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
202
203       # and when accessed via history
204       get api_relation_history_path(relation), :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."
208     end
209
210     ##
211     # test the unredaction of an old version of a relation, while not being
212     # authorised.
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))
217
218       post relation_version_redact_path(relation_v1.relation_id, relation_v1.version)
219       assert_response :unauthorized, "should need to be authenticated to unredact."
220     end
221
222     ##
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))
229
230       auth_header = basic_authorization_header create(:user).email, "test"
231
232       post relation_version_redact_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
233       assert_response :forbidden, "should need to be moderator to unredact."
234     end
235
236     ##
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))
243
244       auth_header = basic_authorization_header create(:moderator_user).email, "test"
245
246       post relation_version_redact_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
247       assert_response :success, "should be OK to unredact old version as moderator."
248
249       # check moderator can still see the redacted data, without passing
250       # the appropriate flag
251       get api_old_relation_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
252       assert_response :success, "After unredaction, relation should not be gone for moderator."
253
254       # and when accessed via history
255       get api_relation_history_path(relation), :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."
259
260       auth_header = basic_authorization_header create(:user).email, "test"
261
262       # check normal user can now see the redacted data
263       get api_old_relation_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
264       assert_response :success, "After redaction, node should not be gone for normal user."
265
266       # and when accessed via history
267       get api_relation_history_path(relation), :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."
271     end
272
273     private
274
275     ##
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)
284       end
285       assert_not_nil current_relation, "getting relation #{relation_id} returned nil"
286
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)
291
292       # check that the relations are identical
293       assert_relations_are_equal current_relation, old_relation
294     end
295
296     ##
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"
304
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"
308
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}"
313
314         assert_relations_are_equal history_relation, version_relation
315       end
316     end
317
318     def create_bearer_auth_header(user, scopes)
319       token = create(:oauth_access_token,
320                      :resource_owner_id => user.id,
321                      :scopes => scopes)
322       bearer_authorization_header(token.token)
323     end
324
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)
329     end
330
331     def do_redact_relation(relation, redaction, headers = {})
332       get api_old_relation_path(relation.relation_id, relation.version)
333       assert_response :success, "should be able to get version #{relation.version} of relation #{relation.relation_id}."
334
335       # now redact it
336       post relation_version_redact_path(relation.relation_id, relation.version), :params => { :redaction => redaction.id }, :headers => headers
337     end
338   end
339 end