]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/old_relations_controller_test.rb
Merge remote-tracking branch 'upstream/pull/2918' into master
[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 => "version", :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 => "version", :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(:id => 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     ##
81     # test that redacted relations aren't visible, regardless of
82     # authorisation except as moderator...
83     def test_version_redacted
84       relation = create(:relation, :with_history, :version => 2)
85       relation_v1 = relation.old_relations.find_by(:version => 1)
86       relation_v1.redact!(create(:redaction))
87
88       get relation_version_path(:id => relation_v1.relation_id, :version => relation_v1.version)
89       assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
90
91       # not even to a logged-in user
92       auth_header = basic_authorization_header create(:user).email, "test"
93       get relation_version_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
94       assert_response :forbidden, "Redacted relation shouldn't be visible via the version API, even when logged in."
95     end
96
97     ##
98     # test that redacted relations aren't visible in the history
99     def test_history_redacted
100       relation = create(:relation, :with_history, :version => 2)
101       relation_v1 = relation.old_relations.find_by(:version => 1)
102       relation_v1.redact!(create(:redaction))
103
104       get api_relation_history_path(:id => relation_v1.relation_id)
105       assert_response :success, "Redaction shouldn't have stopped history working."
106       assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 0,
107                     "redacted relation #{relation_v1.relation_id} version #{relation_v1.version} shouldn't be present in the history."
108
109       # not even to a logged-in user
110       auth_header = basic_authorization_header create(:user).email, "test"
111       get relation_version_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
112       get api_relation_history_path(:id => relation_v1.relation_id), :headers => auth_header
113       assert_response :success, "Redaction shouldn't have stopped history working."
114       assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 0,
115                     "redacted relation #{relation_v1.relation_id} version #{relation_v1.version} shouldn't be present in the history, even when logged in."
116     end
117
118     ##
119     # test the redaction of an old version of a relation, while being
120     # authorised as a moderator.
121     def test_redact_relation_moderator
122       relation = create(:relation, :with_history, :version => 4)
123       relation_v3 = relation.old_relations.find_by(:version => 3)
124
125       auth_header = basic_authorization_header create(:moderator_user).email, "test"
126
127       do_redact_relation(relation_v3, create(:redaction), auth_header)
128       assert_response :success, "should be OK to redact old version as moderator."
129
130       # check moderator can still see the redacted data, when passing
131       # the appropriate flag
132       get relation_version_path(:id => relation_v3.relation_id, :version => relation_v3.version), :headers => auth_header
133       assert_response :forbidden, "After redaction, relation should be gone for moderator, when flag not passed."
134       get relation_version_path(:id => relation_v3.relation_id, :version => relation_v3.version), :params => { :show_redactions => "true" }, :headers => auth_header
135       assert_response :success, "After redaction, relation should not be gone for moderator, when flag passed."
136
137       # and when accessed via history
138       get api_relation_history_path(:id => relation_v3.relation_id), :headers => auth_header
139       assert_response :success, "Redaction shouldn't have stopped history working."
140       assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 0,
141                     "relation #{relation_v3.relation_id} version #{relation_v3.version} should not be present in the history for moderators when not passing flag."
142       get api_relation_history_path(:id => relation_v3.relation_id), :params => { :show_redactions => "true" }, :headers => auth_header
143       assert_response :success, "Redaction shouldn't have stopped history working."
144       assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 1,
145                     "relation #{relation_v3.relation_id} version #{relation_v3.version} should still be present in the history for moderators when passing flag."
146     end
147
148     # testing that if the moderator drops auth, he can't see the
149     # redacted stuff any more.
150     def test_redact_relation_is_redacted
151       relation = create(:relation, :with_history, :version => 4)
152       relation_v3 = relation.old_relations.find_by(:version => 3)
153
154       auth_header = basic_authorization_header create(:moderator_user).email, "test"
155
156       do_redact_relation(relation_v3, create(:redaction), auth_header)
157       assert_response :success, "should be OK to redact old version as moderator."
158
159       # re-auth as non-moderator
160       auth_header = basic_authorization_header create(:user).email, "test"
161
162       # check can't see the redacted data
163       get relation_version_path(:id => relation_v3.relation_id, :version => relation_v3.version), :headers => auth_header
164       assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
165
166       # and when accessed via history
167       get api_relation_history_path(:id => relation_v3.relation_id), :headers => auth_header
168       assert_response :success, "Redaction shouldn't have stopped history working."
169       assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 0,
170                     "redacted relation #{relation_v3.relation_id} version #{relation_v3.version} shouldn't be present in the history."
171     end
172
173     ##
174     # test the unredaction of an old version of a relation, while not being
175     # authorised.
176     def test_unredact_relation_unauthorised
177       relation = create(:relation, :with_history, :version => 2)
178       relation_v1 = relation.old_relations.find_by(:version => 1)
179       relation_v1.redact!(create(:redaction))
180
181       post relation_version_redact_path(:id => relation_v1.relation_id, :version => relation_v1.version)
182       assert_response :unauthorized, "should need to be authenticated to unredact."
183     end
184
185     ##
186     # test the unredaction of an old version of a relation, while being
187     # authorised as a normal user.
188     def test_unredact_relation_normal_user
189       relation = create(:relation, :with_history, :version => 2)
190       relation_v1 = relation.old_relations.find_by(:version => 1)
191       relation_v1.redact!(create(:redaction))
192
193       auth_header = basic_authorization_header create(:user).email, "test"
194
195       post relation_version_redact_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
196       assert_response :forbidden, "should need to be moderator to unredact."
197     end
198
199     ##
200     # test the unredaction of an old version of a relation, while being
201     # authorised as a moderator.
202     def test_unredact_relation_moderator
203       relation = create(:relation, :with_history, :version => 2)
204       relation_v1 = relation.old_relations.find_by(:version => 1)
205       relation_v1.redact!(create(:redaction))
206
207       auth_header = basic_authorization_header create(:moderator_user).email, "test"
208
209       post relation_version_redact_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
210       assert_response :success, "should be OK to unredact old version as moderator."
211
212       # check moderator can still see the redacted data, without passing
213       # the appropriate flag
214       get relation_version_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
215       assert_response :success, "After unredaction, relation should not be gone for moderator."
216
217       # and when accessed via history
218       get api_relation_history_path(:id => relation_v1.relation_id), :headers => auth_header
219       assert_response :success, "Redaction shouldn't have stopped history working."
220       assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1,
221                     "relation #{relation_v1.relation_id} version #{relation_v1.version} should still be present in the history for moderators."
222
223       auth_header = basic_authorization_header create(:user).email, "test"
224
225       # check normal user can now see the redacted data
226       get relation_version_path(:id => relation_v1.relation_id, :version => relation_v1.version), :headers => auth_header
227       assert_response :success, "After redaction, node should not be gone for normal user."
228
229       # and when accessed via history
230       get api_relation_history_path(:id => relation_v1.relation_id), :headers => auth_header
231       assert_response :success, "Redaction shouldn't have stopped history working."
232       assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1,
233                     "relation #{relation_v1.relation_id} version #{relation_v1.version} should still be present in the history for normal users."
234     end
235
236     private
237
238     ##
239     # check that the current version of a relation is equivalent to the
240     # version which we're getting from the versions call.
241     def check_current_version(relation_id)
242       # get the current version
243       current_relation = with_controller(RelationsController.new) do
244         get :show, :params => { :id => relation_id }
245         assert_response :success, "can't get current relation #{relation_id}"
246         Relation.from_xml(@response.body)
247       end
248       assert_not_nil current_relation, "getting relation #{relation_id} returned nil"
249
250       # get the "old" version of the relation from the version method
251       get :version, :params => { :id => relation_id, :version => current_relation.version }
252       assert_response :success, "can't get old relation #{relation_id}, v#{current_relation.version}"
253       old_relation = Relation.from_xml(@response.body)
254
255       # check that the relations are identical
256       assert_relations_are_equal current_relation, old_relation
257     end
258
259     ##
260     # look at all the versions of the relation in the history and get each version from
261     # the versions call. check that they're the same.
262     def check_history_equals_versions(relation_id)
263       get :history, :params => { :id => relation_id }
264       assert_response :success, "can't get relation #{relation_id} from API"
265       history_doc = XML::Parser.string(@response.body).parse
266       assert_not_nil history_doc, "parsing relation #{relation_id} history failed"
267
268       history_doc.find("//osm/relation").each do |relation_doc|
269         history_relation = Relation.from_xml_node(relation_doc)
270         assert_not_nil history_relation, "parsing relation #{relation_id} version failed"
271
272         get :version, :params => { :id => relation_id, :version => history_relation.version }
273         assert_response :success, "couldn't get relation #{relation_id}, v#{history_relation.version}"
274         version_relation = Relation.from_xml(@response.body)
275         assert_not_nil version_relation, "failed to parse #{relation_id}, v#{history_relation.version}"
276
277         assert_relations_are_equal history_relation, version_relation
278       end
279     end
280
281     def do_redact_relation(relation, redaction, headers = {})
282       get relation_version_path(:id => relation.relation_id, :version => relation.version)
283       assert_response :success, "should be able to get version #{relation.version} of relation #{relation.relation_id}."
284
285       # now redact it
286       post relation_version_redact_path(:id => relation.relation_id, :version => relation.version), :params => { :redaction => redaction.id }, :headers => headers
287     end
288   end
289 end