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