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