]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/old_relations_controller_test.rb
Merge remote-tracking branch 'upstream/pull/5645'
[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 => "index", :relation_id => "1" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/relation/1/history.json", :method => :get },
14         { :controller => "api/old_relations", :action => "index", :relation_id => "1", :format => "json" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/relation/1/2", :method => :get },
18         { :controller => "api/old_relations", :action => "show", :relation_id => "1", :version => "2" }
19       )
20       assert_routing(
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" }
23       )
24       assert_routing(
25         { :path => "/api/0.6/relation/1/2/redact", :method => :post },
26         { :controller => "api/old_relations", :action => "redact", :relation_id => "1", :version => "2" }
27       )
28     end
29
30     ##
31     # check that a visible relations is returned properly
32     def test_index
33       relation = create(:relation, :with_history, :version => 2)
34
35       get api_relation_versions_path(relation)
36
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"
42
43           assert_dom dom_relations[1], "> @id", relation.id.to_s
44           assert_dom dom_relations[1], "> @version", "2"
45         end
46       end
47     end
48
49     ##
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
54     end
55
56     ##
57     # test that redacted relations aren't visible in the history
58     def test_index_redacted
59       relation = create(:relation, :with_history, :version => 2)
60       relation_v1 = relation.old_relations.find_by(:version => 1)
61       relation_v1.redact!(create(:redaction))
62
63       get api_relation_versions_path(relation)
64       assert_response :success, "Redaction shouldn't have stopped history working."
65       assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 0,
66                     "redacted relation #{relation_v1.relation_id} version #{relation_v1.version} shouldn't be present in the history."
67
68       # not even to a logged-in user
69       auth_header = bearer_authorization_header
70       get api_relation_versions_path(relation), :headers => auth_header
71       assert_response :success, "Redaction shouldn't have stopped history working."
72       assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 0,
73                     "redacted relation #{relation_v1.relation_id} version #{relation_v1.version} shouldn't be present in the history, even when logged in."
74     end
75
76     def test_show
77       relation = create(:relation, :with_history, :version => 2)
78       create(:old_relation_tag, :old_relation => relation.old_relations[0], :k => "k1", :v => "v1")
79       create(:old_relation_tag, :old_relation => relation.old_relations[1], :k => "k2", :v => "v2")
80
81       get api_relation_version_path(relation, 1)
82
83       assert_response :success
84       assert_dom "osm:root", 1 do
85         assert_dom "> relation", 1 do
86           assert_dom "> @id", relation.id.to_s
87           assert_dom "> @version", "1"
88           assert_dom "> tag", 1 do
89             assert_dom "> @k", "k1"
90             assert_dom "> @v", "v1"
91           end
92         end
93       end
94
95       get api_relation_version_path(relation, 2)
96
97       assert_response :success
98       assert_dom "osm:root", 1 do
99         assert_dom "> relation", 1 do
100           assert_dom "> @id", relation.id.to_s
101           assert_dom "> @version", "2"
102           assert_dom "> tag", 1 do
103             assert_dom "> @k", "k2"
104             assert_dom "> @v", "v2"
105           end
106         end
107       end
108     end
109
110     ##
111     # test that redacted relations aren't visible, regardless of
112     # authorisation except as moderator...
113     def test_show_redacted
114       relation = create(:relation, :with_history, :version => 2)
115       relation_v1 = relation.old_relations.find_by(:version => 1)
116       relation_v1.redact!(create(:redaction))
117
118       get api_relation_version_path(relation_v1.relation_id, relation_v1.version)
119       assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
120
121       # not even to a logged-in user
122       auth_header = bearer_authorization_header
123       get api_relation_version_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
124       assert_response :forbidden, "Redacted relation shouldn't be visible via the version API, even when logged in."
125     end
126
127     ##
128     # test the redaction of an old version of a relation, while not being
129     # authorised.
130     def test_redact_relation_unauthorised
131       relation = create(:relation, :with_history, :version => 4)
132       relation_v3 = relation.old_relations.find_by(:version => 3)
133
134       do_redact_relation(relation_v3, create(:redaction))
135       assert_response :unauthorized, "should need to be authenticated to redact."
136     end
137
138     ##
139     # test the redaction of an old version of a relation, while being
140     # authorised as a normal user.
141     def test_redact_relation_normal_user
142       relation = create(:relation, :with_history, :version => 4)
143       relation_v3 = relation.old_relations.find_by(:version => 3)
144
145       auth_header = bearer_authorization_header
146
147       do_redact_relation(relation_v3, create(:redaction), auth_header)
148       assert_response :forbidden, "should need to be moderator to redact."
149     end
150
151     ##
152     # test that, even as moderator, the current version of a relation
153     # can't be redacted.
154     def test_redact_relation_current_version
155       relation = create(:relation, :with_history, :version => 4)
156       relation_latest = relation.old_relations.last
157
158       auth_header = bearer_authorization_header create(:moderator_user)
159
160       do_redact_relation(relation_latest, create(:redaction), auth_header)
161       assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
162     end
163
164     def test_redact_relation_by_regular_without_write_redactions_scope
165       auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
166       do_redact_redactable_relation(auth_header)
167       assert_response :forbidden, "should need to be moderator to redact."
168     end
169
170     def test_redact_relation_by_regular_with_write_redactions_scope
171       auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
172       do_redact_redactable_relation(auth_header)
173       assert_response :forbidden, "should need to be moderator to redact."
174     end
175
176     def test_redact_relation_by_moderator_without_write_redactions_scope
177       auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
178       do_redact_redactable_relation(auth_header)
179       assert_response :forbidden, "should need to have write_redactions scope to redact."
180     end
181
182     def test_redact_relation_by_moderator_with_write_redactions_scope
183       auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
184       do_redact_redactable_relation(auth_header)
185       assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
186     end
187
188     ##
189     # test the redaction of an old version of a relation, while being
190     # authorised as a moderator.
191     def test_redact_relation_moderator
192       relation = create(:relation, :with_history, :version => 4)
193       relation_v3 = relation.old_relations.find_by(:version => 3)
194
195       auth_header = bearer_authorization_header create(:moderator_user)
196
197       do_redact_relation(relation_v3, create(:redaction), auth_header)
198       assert_response :success, "should be OK to redact old version as moderator."
199
200       # check moderator can still see the redacted data, when passing
201       # the appropriate flag
202       get api_relation_version_path(relation_v3.relation_id, relation_v3.version), :headers => auth_header
203       assert_response :forbidden, "After redaction, relation should be gone for moderator, when flag not passed."
204       get api_relation_version_path(relation_v3.relation_id, relation_v3.version, :show_redactions => "true"), :headers => auth_header
205       assert_response :success, "After redaction, relation should not be gone for moderator, when flag passed."
206
207       # and when accessed via history
208       get api_relation_versions_path(relation), :headers => auth_header
209       assert_response :success, "Redaction shouldn't have stopped history working."
210       assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 0,
211                     "relation #{relation_v3.relation_id} version #{relation_v3.version} should not be present in the history for moderators when not passing flag."
212       get api_relation_versions_path(relation, :show_redactions => "true"), :headers => auth_header
213       assert_response :success, "Redaction shouldn't have stopped history working."
214       assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 1,
215                     "relation #{relation_v3.relation_id} version #{relation_v3.version} should still be present in the history for moderators when passing flag."
216     end
217
218     # testing that if the moderator drops auth, he can't see the
219     # redacted stuff any more.
220     def test_redact_relation_is_redacted
221       relation = create(:relation, :with_history, :version => 4)
222       relation_v3 = relation.old_relations.find_by(:version => 3)
223
224       auth_header = bearer_authorization_header create(:moderator_user)
225
226       do_redact_relation(relation_v3, create(:redaction), auth_header)
227       assert_response :success, "should be OK to redact old version as moderator."
228
229       # re-auth as non-moderator
230       auth_header = bearer_authorization_header
231
232       # check can't see the redacted data
233       get api_relation_version_path(relation_v3.relation_id, relation_v3.version), :headers => auth_header
234       assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
235
236       # and when accessed via history
237       get api_relation_versions_path(relation), :headers => auth_header
238       assert_response :success, "Redaction shouldn't have stopped history working."
239       assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 0,
240                     "redacted relation #{relation_v3.relation_id} version #{relation_v3.version} shouldn't be present in the history."
241     end
242
243     ##
244     # test the unredaction of an old version of a relation, while not being
245     # authorised.
246     def test_unredact_relation_unauthorised
247       relation = create(:relation, :with_history, :version => 2)
248       relation_v1 = relation.old_relations.find_by(:version => 1)
249       relation_v1.redact!(create(:redaction))
250
251       post relation_version_redact_path(relation_v1.relation_id, relation_v1.version)
252       assert_response :unauthorized, "should need to be authenticated to unredact."
253     end
254
255     ##
256     # test the unredaction of an old version of a relation, while being
257     # authorised as a normal user.
258     def test_unredact_relation_normal_user
259       relation = create(:relation, :with_history, :version => 2)
260       relation_v1 = relation.old_relations.find_by(:version => 1)
261       relation_v1.redact!(create(:redaction))
262
263       auth_header = bearer_authorization_header
264
265       post relation_version_redact_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
266       assert_response :forbidden, "should need to be moderator to unredact."
267     end
268
269     ##
270     # test the unredaction of an old version of a relation, while being
271     # authorised as a moderator.
272     def test_unredact_relation_moderator
273       relation = create(:relation, :with_history, :version => 2)
274       relation_v1 = relation.old_relations.find_by(:version => 1)
275       relation_v1.redact!(create(:redaction))
276
277       auth_header = bearer_authorization_header create(:moderator_user)
278
279       post relation_version_redact_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
280       assert_response :success, "should be OK to unredact old version as moderator."
281
282       # check moderator can still see the redacted data, without passing
283       # the appropriate flag
284       get api_relation_version_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
285       assert_response :success, "After unredaction, relation should not be gone for moderator."
286
287       # and when accessed via history
288       get api_relation_versions_path(relation), :headers => auth_header
289       assert_response :success, "Redaction shouldn't have stopped history working."
290       assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1,
291                     "relation #{relation_v1.relation_id} version #{relation_v1.version} should still be present in the history for moderators."
292
293       auth_header = bearer_authorization_header
294
295       # check normal user can now see the redacted data
296       get api_relation_version_path(relation_v1.relation_id, relation_v1.version), :headers => auth_header
297       assert_response :success, "After redaction, node should not be gone for normal user."
298
299       # and when accessed via history
300       get api_relation_versions_path(relation), :headers => auth_header
301       assert_response :success, "Redaction shouldn't have stopped history working."
302       assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1,
303                     "relation #{relation_v1.relation_id} version #{relation_v1.version} should still be present in the history for normal users."
304     end
305
306     private
307
308     def do_redact_redactable_relation(headers = {})
309       relation = create(:relation, :with_history, :version => 4)
310       relation_v3 = relation.old_relations.find_by(:version => 3)
311       do_redact_relation(relation_v3, create(:redaction), headers)
312     end
313
314     def do_redact_relation(relation, redaction, headers = {})
315       get api_relation_version_path(relation.relation_id, relation.version)
316       assert_response :success, "should be able to get version #{relation.version} of relation #{relation.relation_id}."
317
318       # now redact it
319       post relation_version_redact_path(relation.relation_id, relation.version), :params => { :redaction => redaction.id }, :headers => headers
320     end
321   end
322 end