]> git.openstreetmap.org Git - rails.git/blob - test/controllers/old_relation_controller_test.rb
Use redactions factory for old_way controller tests
[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                        create(:redaction))
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     user = create(:user)
51     basic_authorization(user.email, "test")
52
53     do_redact_relation(relations(:relation_with_versions_v3),
54                        create(:redaction, :user => user))
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     moderator_user = create(:moderator_user)
63     basic_authorization(moderator_user.email, "test")
64
65     do_redact_relation(relations(:relation_with_versions_v4),
66                        create(:redaction, :user => moderator_user))
67     assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
68   end
69
70   ##
71   # test that redacted relations aren't visible, regardless of
72   # authorisation except as moderator...
73   def test_version_redacted
74     relation = relations(:relation_with_redacted_versions_v2)
75
76     get :version, :id => relation.relation_id, :version => relation.version
77     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
78
79     # not even to a logged-in user
80     basic_authorization(users(:public_user).email, "test")
81     get :version, :id => relation.relation_id, :version => relation.version
82     assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
83   end
84
85   ##
86   # test that redacted nodes aren't visible in the history
87   def test_history_redacted
88     relation = relations(:relation_with_redacted_versions_v2)
89
90     get :history, :id => relation.relation_id
91     assert_response :success, "Redaction shouldn't have stopped history working."
92     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."
93
94     # not even to a logged-in user
95     basic_authorization(users(:public_user).email, "test")
96     get :version, :id => relation.relation_id, :version => relation.version
97     get :history, :id => relation.relation_id
98     assert_response :success, "Redaction shouldn't have stopped history working."
99     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."
100   end
101
102   ##
103   # test the redaction of an old version of a relation, while being
104   # authorised as a moderator.
105   def test_redact_relation_moderator
106     moderator_user = create(:moderator_user)
107     relation = relations(:relation_with_versions_v3)
108     basic_authorization(moderator_user.email, "test")
109
110     do_redact_relation(relation, create(:redaction, :user => moderator_user))
111     assert_response :success, "should be OK to redact old version as moderator."
112
113     # check moderator can still see the redacted data, when passing
114     # the appropriate flag
115     get :version, :id => relation.relation_id, :version => relation.version
116     assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
117     get :version, :id => relation.relation_id, :version => relation.version, :show_redactions => "true"
118     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
119
120     # and when accessed via history
121     get :history, :id => relation.relation_id
122     assert_response :success, "Redaction shouldn't have stopped history working."
123     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."
124     get :history, :id => relation.relation_id, :show_redactions => "true"
125     assert_response :success, "Redaction shouldn't have stopped history working."
126     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."
127   end
128
129   # testing that if the moderator drops auth, he can't see the
130   # redacted stuff any more.
131   def test_redact_relation_is_redacted
132     moderator_user = create(:moderator_user)
133     relation = relations(:relation_with_versions_v3)
134     basic_authorization(moderator_user.email, "test")
135
136     do_redact_relation(relation, create(:redaction, :user => moderator_user))
137     assert_response :success, "should be OK to redact old version as moderator."
138
139     # re-auth as non-moderator
140     basic_authorization(users(:public_user).email, "test")
141
142     # check can't see the redacted data
143     get :version, :id => relation.relation_id, :version => relation.version
144     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
145
146     # and when accessed via history
147     get :history, :id => relation.relation_id
148     assert_response :success, "Redaction shouldn't have stopped history working."
149     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."
150   end
151
152   ##
153   # test the unredaction of an old version of a relation, while not being
154   # authorised.
155   def test_unredact_relation_unauthorised
156     relation = relations(:relation_with_redacted_versions_v3)
157
158     post :redact, :id => relation.relation_id, :version => relation.version
159     assert_response :unauthorized, "should need to be authenticated to unredact."
160   end
161
162   ##
163   # test the unredaction of an old version of a relation, while being
164   # authorised as a normal user.
165   def test_unredact_relation_normal_user
166     relation = relations(:relation_with_redacted_versions_v3)
167     basic_authorization(users(:public_user).email, "test")
168
169     post :redact, :id => relation.relation_id, :version => relation.version
170     assert_response :forbidden, "should need to be moderator to unredact."
171   end
172
173   ##
174   # test the unredaction of an old version of a relation, while being
175   # authorised as a moderator.
176   def test_unredact_relation_moderator
177     relation = relations(:relation_with_redacted_versions_v3)
178     basic_authorization(users(:moderator_user).email, "test")
179
180     post :redact, :id => relation.relation_id, :version => relation.version
181     assert_response :success, "should be OK to unredact old version as moderator."
182
183     # check moderator can still see the redacted data, without passing
184     # the appropriate flag
185     get :version, :id => relation.relation_id, :version => relation.version
186     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
187
188     # and when accessed via history
189     get :history, :id => relation.relation_id
190     assert_response :success, "Redaction shouldn't have stopped history working."
191     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."
192
193     basic_authorization(users(:normal_user).email, "test")
194
195     # check normal user can now see the redacted data
196     get :version, :id => relation.relation_id, :version => relation.version
197     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
198
199     # and when accessed via history
200     get :history, :id => relation.relation_id
201     assert_response :success, "Redaction shouldn't have stopped history working."
202     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."
203   end
204
205   private
206
207   ##
208   # check that the current version of a relation is equivalent to the
209   # version which we're getting from the versions call.
210   def check_current_version(relation_id)
211     # get the current version
212     current_relation = with_controller(RelationController.new) do
213       get :read, :id => relation_id
214       assert_response :success, "can't get current relation #{relation_id}"
215       Relation.from_xml(@response.body)
216     end
217     assert_not_nil current_relation, "getting relation #{relation_id} returned nil"
218
219     # get the "old" version of the relation from the version method
220     get :version, :id => relation_id, :version => current_relation.version
221     assert_response :success, "can't get old relation #{relation_id}, v#{current_relation.version}"
222     old_relation = Relation.from_xml(@response.body)
223
224     # check that the relations are identical
225     assert_relations_are_equal current_relation, old_relation
226   end
227
228   ##
229   # look at all the versions of the relation in the history and get each version from
230   # the versions call. check that they're the same.
231   def check_history_equals_versions(relation_id)
232     get :history, :id => relation_id
233     assert_response :success, "can't get relation #{relation_id} from API"
234     history_doc = XML::Parser.string(@response.body).parse
235     assert_not_nil history_doc, "parsing relation #{relation_id} history failed"
236
237     history_doc.find("//osm/relation").each do |relation_doc|
238       history_relation = Relation.from_xml_node(relation_doc)
239       assert_not_nil history_relation, "parsing relation #{relation_id} version failed"
240
241       get :version, :id => relation_id, :version => history_relation.version
242       assert_response :success, "couldn't get relation #{relation_id}, v#{history_relation.version}"
243       version_relation = Relation.from_xml(@response.body)
244       assert_not_nil version_relation, "failed to parse #{relation_id}, v#{history_relation.version}"
245
246       assert_relations_are_equal history_relation, version_relation
247     end
248   end
249
250   def do_redact_relation(relation, redaction)
251     get :version, :id => relation.relation_id, :version => relation.version
252     assert_response :success, "should be able to get version #{relation.version} of relation #{relation.relation_id}."
253
254     # now redact it
255     post :redact, :id => relation.relation_id, :version => relation.version, :redaction => redaction.id
256   end
257 end