]> git.openstreetmap.org Git - rails.git/blob - test/controllers/old_relations_controller_test.rb
Require terms agreement for abilities and capabilities related to api write methods
[rails.git] / test / controllers / old_relations_controller_test.rb
1 require "test_helper"
2
3 class OldRelationsControllerTest < ActionController::TestCase
4   ##
5   # test all routes which lead to this controller
6   def test_routes
7     assert_routing(
8       { :path => "/api/0.6/relation/1/history", :method => :get },
9       { :controller => "old_relations", :action => "history", :id => "1" }
10     )
11     assert_routing(
12       { :path => "/api/0.6/relation/1/2", :method => :get },
13       { :controller => "old_relations", :action => "version", :id => "1", :version => "2" }
14     )
15     assert_routing(
16       { :path => "/api/0.6/relation/1/2/redact", :method => :post },
17       { :controller => "old_relations", :action => "redact", :id => "1", :version => "2" }
18     )
19   end
20
21   # -------------------------------------
22   # Test reading old relations.
23   # -------------------------------------
24   def test_history
25     # check that a visible relations is returned properly
26     get :history, :params => { :id => create(:relation, :with_history).id }
27     assert_response :success
28
29     # check chat a non-existent relations is not returned
30     get :history, :params => { :id => 0 }
31     assert_response :not_found
32   end
33
34   ##
35   # test the redaction of an old version of a relation, while not being
36   # authorised.
37   def test_redact_relation_unauthorised
38     relation = create(:relation, :with_history, :version => 4)
39     relation_v3 = relation.old_relations.find_by(:version => 3)
40
41     do_redact_relation(relation_v3, create(:redaction))
42     assert_response :unauthorized, "should need to be authenticated to redact."
43   end
44
45   ##
46   # test the redaction of an old version of a relation, while being
47   # authorised as a normal user.
48   def test_redact_relation_normal_user
49     relation = create(:relation, :with_history, :version => 4)
50     relation_v3 = relation.old_relations.find_by(:version => 3)
51
52     basic_authorization create(:user).email, "test"
53
54     do_redact_relation(relation_v3, create(:redaction))
55     assert_response :forbidden, "should need to be moderator to redact."
56   end
57
58   ##
59   # test that, even as moderator, the current version of a relation
60   # can't be redacted.
61   def test_redact_relation_current_version
62     relation = create(:relation, :with_history, :version => 4)
63     relation_latest = relation.old_relations.last
64
65     basic_authorization create(:moderator_user).email, "test"
66
67     do_redact_relation(relation_latest, create(:redaction))
68     assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
69   end
70
71   ##
72   # test that redacted relations aren't visible, regardless of
73   # authorisation except as moderator...
74   def test_version_redacted
75     relation = create(:relation, :with_history, :version => 2)
76     relation_v1 = relation.old_relations.find_by(:version => 1)
77     relation_v1.redact!(create(:redaction))
78
79     get :version, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
80     assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
81
82     # not even to a logged-in user
83     basic_authorization create(:user).email, "test"
84     get :version, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
85     assert_response :forbidden, "Redacted relation shouldn't be visible via the version API, even when logged in."
86   end
87
88   ##
89   # test that redacted relations aren't visible in the history
90   def test_history_redacted
91     relation = create(:relation, :with_history, :version => 2)
92     relation_v1 = relation.old_relations.find_by(:version => 1)
93     relation_v1.redact!(create(:redaction))
94
95     get :history, :params => { :id => relation_v1.relation_id }
96     assert_response :success, "Redaction shouldn't have stopped history working."
97     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."
98
99     # not even to a logged-in user
100     basic_authorization create(:user).email, "test"
101     get :version, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
102     get :history, :params => { :id => relation_v1.relation_id }
103     assert_response :success, "Redaction shouldn't have stopped history working."
104     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."
105   end
106
107   ##
108   # test the redaction of an old version of a relation, while being
109   # authorised as a moderator.
110   def test_redact_relation_moderator
111     relation = create(:relation, :with_history, :version => 4)
112     relation_v3 = relation.old_relations.find_by(:version => 3)
113
114     basic_authorization create(:moderator_user).email, "test"
115
116     do_redact_relation(relation_v3, create(:redaction))
117     assert_response :success, "should be OK to redact old version as moderator."
118
119     # check moderator can still see the redacted data, when passing
120     # the appropriate flag
121     get :version, :params => { :id => relation_v3.relation_id, :version => relation_v3.version }
122     assert_response :forbidden, "After redaction, relation should be gone for moderator, when flag not passed."
123     get :version, :params => { :id => relation_v3.relation_id, :version => relation_v3.version, :show_redactions => "true" }
124     assert_response :success, "After redaction, relation should not be gone for moderator, when flag passed."
125
126     # and when accessed via history
127     get :history, :params => { :id => relation_v3.relation_id }
128     assert_response :success, "Redaction shouldn't have stopped history working."
129     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."
130     get :history, :params => { :id => relation_v3.relation_id, :show_redactions => "true" }
131     assert_response :success, "Redaction shouldn't have stopped history working."
132     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."
133   end
134
135   # testing that if the moderator drops auth, he can't see the
136   # redacted stuff any more.
137   def test_redact_relation_is_redacted
138     relation = create(:relation, :with_history, :version => 4)
139     relation_v3 = relation.old_relations.find_by(:version => 3)
140
141     basic_authorization create(:moderator_user).email, "test"
142
143     do_redact_relation(relation_v3, create(:redaction))
144     assert_response :success, "should be OK to redact old version as moderator."
145
146     # re-auth as non-moderator
147     basic_authorization create(:user).email, "test"
148
149     # check can't see the redacted data
150     get :version, :params => { :id => relation_v3.relation_id, :version => relation_v3.version }
151     assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
152
153     # and when accessed via history
154     get :history, :params => { :id => relation_v3.relation_id }
155     assert_response :success, "Redaction shouldn't have stopped history working."
156     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."
157   end
158
159   ##
160   # test the unredaction of an old version of a relation, while not being
161   # authorised.
162   def test_unredact_relation_unauthorised
163     relation = create(:relation, :with_history, :version => 2)
164     relation_v1 = relation.old_relations.find_by(:version => 1)
165     relation_v1.redact!(create(:redaction))
166
167     post :redact, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
168     assert_response :unauthorized, "should need to be authenticated to unredact."
169   end
170
171   ##
172   # test the unredaction of an old version of a relation, while being
173   # authorised as a normal user.
174   def test_unredact_relation_normal_user
175     relation = create(:relation, :with_history, :version => 2)
176     relation_v1 = relation.old_relations.find_by(:version => 1)
177     relation_v1.redact!(create(:redaction))
178
179     basic_authorization create(:user).email, "test"
180
181     post :redact, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
182     assert_response :forbidden, "should need to be moderator to unredact."
183   end
184
185   ##
186   # test the unredaction of an old version of a relation, while being
187   # authorised as a moderator.
188   def test_unredact_relation_moderator
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     basic_authorization create(:moderator_user).email, "test"
194
195     post :redact, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
196     assert_response :success, "should be OK to unredact old version as moderator."
197
198     # check moderator can still see the redacted data, without passing
199     # the appropriate flag
200     get :version, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
201     assert_response :success, "After unredaction, relation should not be gone for moderator."
202
203     # and when accessed via history
204     get :history, :params => { :id => relation_v1.relation_id }
205     assert_response :success, "Redaction shouldn't have stopped history working."
206     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."
207
208     basic_authorization create(:user).email, "test"
209
210     # check normal user can now see the redacted data
211     get :version, :params => { :id => relation_v1.relation_id, :version => relation_v1.version }
212     assert_response :success, "After redaction, node should not be gone for normal user."
213
214     # and when accessed via history
215     get :history, :params => { :id => relation_v1.relation_id }
216     assert_response :success, "Redaction shouldn't have stopped history working."
217     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."
218   end
219
220   private
221
222   ##
223   # check that the current version of a relation is equivalent to the
224   # version which we're getting from the versions call.
225   def check_current_version(relation_id)
226     # get the current version
227     current_relation = with_controller(RelationsController.new) do
228       get :read, :params => { :id => relation_id }
229       assert_response :success, "can't get current relation #{relation_id}"
230       Relation.from_xml(@response.body)
231     end
232     assert_not_nil current_relation, "getting relation #{relation_id} returned nil"
233
234     # get the "old" version of the relation from the version method
235     get :version, :params => { :id => relation_id, :version => current_relation.version }
236     assert_response :success, "can't get old relation #{relation_id}, v#{current_relation.version}"
237     old_relation = Relation.from_xml(@response.body)
238
239     # check that the relations are identical
240     assert_relations_are_equal current_relation, old_relation
241   end
242
243   ##
244   # look at all the versions of the relation in the history and get each version from
245   # the versions call. check that they're the same.
246   def check_history_equals_versions(relation_id)
247     get :history, :params => { :id => relation_id }
248     assert_response :success, "can't get relation #{relation_id} from API"
249     history_doc = XML::Parser.string(@response.body).parse
250     assert_not_nil history_doc, "parsing relation #{relation_id} history failed"
251
252     history_doc.find("//osm/relation").each do |relation_doc|
253       history_relation = Relation.from_xml_node(relation_doc)
254       assert_not_nil history_relation, "parsing relation #{relation_id} version failed"
255
256       get :version, :params => { :id => relation_id, :version => history_relation.version }
257       assert_response :success, "couldn't get relation #{relation_id}, v#{history_relation.version}"
258       version_relation = Relation.from_xml(@response.body)
259       assert_not_nil version_relation, "failed to parse #{relation_id}, v#{history_relation.version}"
260
261       assert_relations_are_equal history_relation, version_relation
262     end
263   end
264
265   def do_redact_relation(relation, redaction)
266     get :version, :params => { :id => relation.relation_id, :version => relation.version }
267     assert_response :success, "should be able to get version #{relation.version} of relation #{relation.relation_id}."
268
269     # now redact it
270     post :redact, :params => { :id => relation.relation_id, :version => relation.version, :redaction => redaction.id }
271   end
272 end