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