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