]> git.openstreetmap.org Git - rails.git/blob - test/controllers/old_way_controller_test.rb
Use redactions factory for old_way controller tests
[rails.git] / test / controllers / old_way_controller_test.rb
1 require "test_helper"
2 require "old_way_controller"
3
4 class OldWayControllerTest < 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/way/1/history", :method => :get },
12       { :controller => "old_way", :action => "history", :id => "1" }
13     )
14     assert_routing(
15       { :path => "/api/0.6/way/1/2", :method => :get },
16       { :controller => "old_way", :action => "version", :id => "1", :version => "2" }
17     )
18     assert_routing(
19       { :path => "/api/0.6/way/1/2/redact", :method => :post },
20       { :controller => "old_way", :action => "redact", :id => "1", :version => "2" }
21     )
22   end
23
24   # -------------------------------------
25   # Test reading old ways.
26   # -------------------------------------
27
28   def test_history_visible
29     # check that a visible way is returned properly
30     get :history, :id => ways(:visible_way).way_id
31     assert_response :success
32   end
33
34   def test_history_invisible
35     # check that an invisible way's history is returned properly
36     get :history, :id => ways(:invisible_way).way_id
37     assert_response :success
38   end
39
40   def test_history_invalid
41     # check chat a non-existent way is not returned
42     get :history, :id => 0
43     assert_response :not_found
44   end
45
46   ##
47   # check that we can retrieve versions of a way
48   def test_version
49     create(:way_tag, :way => current_ways(:visible_way))
50     create(:way_tag, :way => current_ways(:used_way))
51     create(:way_tag, :way => current_ways(:way_with_versions))
52     propagate_tags(current_ways(:visible_way), ways(:visible_way))
53     propagate_tags(current_ways(:used_way), ways(:used_way))
54     propagate_tags(current_ways(:way_with_versions), ways(:way_with_versions_v4))
55
56     check_current_version(current_ways(:visible_way).id)
57     check_current_version(current_ways(:used_way).id)
58     check_current_version(current_ways(:way_with_versions).id)
59   end
60
61   ##
62   # check that returned history is the same as getting all
63   # versions of a way from the api.
64   def test_history_equals_versions
65     check_history_equals_versions(current_ways(:visible_way).id)
66     check_history_equals_versions(current_ways(:used_way).id)
67     check_history_equals_versions(current_ways(:way_with_versions).id)
68   end
69
70   ##
71   # test the redaction of an old version of a way, while not being
72   # authorised.
73   def test_redact_way_unauthorised
74     do_redact_way(ways(:way_with_versions_v3),
75                   create(:redaction))
76     assert_response :unauthorized, "should need to be authenticated to redact."
77   end
78
79   ##
80   # test the redaction of an old version of a way, while being
81   # authorised as a normal user.
82   def test_redact_way_normal_user
83     user = create(:user)
84     basic_authorization(user.email, "test")
85
86     do_redact_way(ways(:way_with_versions_v3),
87                   create(:redaction, :user => user))
88     assert_response :forbidden, "should need to be moderator to redact."
89   end
90
91   ##
92   # test that, even as moderator, the current version of a way
93   # can't be redacted.
94   def test_redact_way_current_version
95     moderator_user = create(:moderator_user)
96     basic_authorization(users(:moderator_user).email, "test")
97
98     do_redact_way(ways(:way_with_versions_v4),
99                   create(:redaction, :user => moderator_user))
100     assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
101   end
102
103   ##
104   # test that redacted ways aren't visible, regardless of
105   # authorisation except as moderator...
106   def test_version_redacted
107     way = ways(:way_with_redacted_versions_v2)
108
109     get :version, :id => way.way_id, :version => way.version
110     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
111
112     # not even to a logged-in user
113     basic_authorization(create(:user).email, "test")
114     get :version, :id => way.way_id, :version => way.version
115     assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
116   end
117
118   ##
119   # test that redacted nodes aren't visible in the history
120   def test_history_redacted
121     way = ways(:way_with_redacted_versions_v2)
122
123     get :history, :id => way.way_id
124     assert_response :success, "Redaction shouldn't have stopped history working."
125     assert_select "osm way[id='#{way.way_id}'][version='#{way.version}']", 0, "redacted way #{way.way_id} version #{way.version} shouldn't be present in the history."
126
127     # not even to a logged-in user
128     basic_authorization(create(:user).email, "test")
129     get :version, :id => way.way_id, :version => way.version
130     get :history, :id => way.way_id
131     assert_response :success, "Redaction shouldn't have stopped history working."
132     assert_select "osm way[id='#{way.way_id}'][version='#{way.version}']", 0, "redacted node #{way.way_id} version #{way.version} shouldn't be present in the history, even when logged in."
133   end
134
135   ##
136   # test the redaction of an old version of a way, while being
137   # authorised as a moderator.
138   def test_redact_way_moderator
139     moderator_user = create(:moderator_user)
140     way = ways(:way_with_versions_v3)
141     basic_authorization(moderator_user.email, "test")
142
143     do_redact_way(way, create(:redaction, :user => moderator_user))
144     assert_response :success, "should be OK to redact old version as moderator."
145
146     # check moderator can still see the redacted data, when passing
147     # the appropriate flag
148     get :version, :id => way.way_id, :version => way.version
149     assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
150     get :version, :id => way.way_id, :version => way.version, :show_redactions => "true"
151     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
152
153     # and when accessed via history
154     get :history, :id => way.way_id
155     assert_response :success, "Redaction shouldn't have stopped history working."
156     assert_select "osm way[id='#{way.way_id}'][version='#{way.version}']", 0, "way #{way.way_id} version #{way.version} should not be present in the history for moderators when not passing flag."
157     get :history, :id => way.way_id, :show_redactions => "true"
158     assert_response :success, "Redaction shouldn't have stopped history working."
159     assert_select "osm way[id='#{way.way_id}'][version='#{way.version}']", 1, "way #{way.way_id} version #{way.version} should still be present in the history for moderators when passing flag."
160   end
161
162   # testing that if the moderator drops auth, he can't see the
163   # redacted stuff any more.
164   def test_redact_way_is_redacted
165     moderator_user = create(:moderator_user)
166     way = ways(:way_with_versions_v3)
167     basic_authorization(moderator_user.email, "test")
168
169     do_redact_way(way, create(:redaction, :user => moderator_user))
170     assert_response :success, "should be OK to redact old version as moderator."
171
172     # re-auth as non-moderator
173     basic_authorization(create(:user).email, "test")
174
175     # check can't see the redacted data
176     get :version, :id => way.way_id, :version => way.version
177     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
178
179     # and when accessed via history
180     get :history, :id => way.way_id
181     assert_response :success, "Redaction shouldn't have stopped history working."
182     assert_select "osm way[id='#{way.way_id}'][version='#{way.version}']", 0, "redacted way #{way.way_id} version #{way.version} shouldn't be present in the history."
183   end
184
185   ##
186   # test the unredaction of an old version of a way, while not being
187   # authorised.
188   def test_unredact_way_unauthorised
189     way = ways(:way_with_redacted_versions_v3)
190
191     post :redact, :id => way.way_id, :version => way.version
192     assert_response :unauthorized, "should need to be authenticated to unredact."
193   end
194
195   ##
196   # test the unredaction of an old version of a way, while being
197   # authorised as a normal user.
198   def test_unredact_way_normal_user
199     way = ways(:way_with_redacted_versions_v3)
200     basic_authorization(create(:user).email, "test")
201
202     post :redact, :id => way.way_id, :version => way.version
203     assert_response :forbidden, "should need to be moderator to unredact."
204   end
205
206   ##
207   # test the unredaction of an old version of a way, while being
208   # authorised as a moderator.
209   def test_unredact_way_moderator
210     moderator_user = create(:moderator_user)
211     way = ways(:way_with_redacted_versions_v3)
212     basic_authorization(moderator_user.email, "test")
213
214     post :redact, :id => way.way_id, :version => way.version
215     assert_response :success, "should be OK to unredact old version as moderator."
216
217     # check moderator can still see the redacted data, without passing
218     # the appropriate flag
219     get :version, :id => way.way_id, :version => way.version
220     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
221
222     # and when accessed via history
223     get :history, :id => way.way_id
224     assert_response :success, "Redaction shouldn't have stopped history working."
225     assert_select "osm way[id='#{way.way_id}'][version='#{way.version}']", 1, "way #{way.way_id} version #{way.version} should still be present in the history for moderators when passing flag."
226
227     basic_authorization(users(:normal_user).email, "test")
228
229     # check normal user can now see the redacted data
230     get :version, :id => way.way_id, :version => way.version
231     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
232
233     # and when accessed via history
234     get :history, :id => way.way_id
235     assert_response :success, "Redaction shouldn't have stopped history working."
236     assert_select "osm way[id='#{way.way_id}'][version='#{way.version}']", 1, "way #{way.way_id} version #{way.version} should still be present in the history for moderators when passing flag."
237   end
238
239   private
240
241   ##
242   # check that the current version of a way is equivalent to the
243   # version which we're getting from the versions call.
244   def check_current_version(way_id)
245     # get the current version
246     current_way = with_controller(WayController.new) do
247       get :read, :id => way_id
248       assert_response :success, "can't get current way #{way_id}"
249       Way.from_xml(@response.body)
250     end
251     assert_not_nil current_way, "getting way #{way_id} returned nil"
252
253     # get the "old" version of the way from the version method
254     get :version, :id => way_id, :version => current_way.version
255     assert_response :success, "can't get old way #{way_id}, v#{current_way.version}"
256     old_way = Way.from_xml(@response.body)
257
258     # check that the ways are identical
259     assert_ways_are_equal current_way, old_way
260   end
261
262   ##
263   # look at all the versions of the way in the history and get each version from
264   # the versions call. check that they're the same.
265   def check_history_equals_versions(way_id)
266     get :history, :id => way_id
267     assert_response :success, "can't get way #{way_id} from API"
268     history_doc = XML::Parser.string(@response.body).parse
269     assert_not_nil history_doc, "parsing way #{way_id} history failed"
270
271     history_doc.find("//osm/way").each do |way_doc|
272       history_way = Way.from_xml_node(way_doc)
273       assert_not_nil history_way, "parsing way #{way_id} version failed"
274
275       get :version, :id => way_id, :version => history_way.version
276       assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
277       version_way = Way.from_xml(@response.body)
278       assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
279
280       assert_ways_are_equal history_way, version_way
281     end
282   end
283
284   def do_redact_way(way, redaction)
285     get :version, :id => way.way_id, :version => way.version
286     assert_response :success, "should be able to get version #{way.version} of way #{way.way_id}."
287
288     # now redact it
289     post :redact, :id => way.way_id, :version => way.version, :redaction => redaction.id
290   end
291
292   def propagate_tags(way, old_way)
293     way.tags.each do |k, v|
294       create(:old_way_tag, :old_way => old_way, :k => k, :v => v)
295     end
296   end
297 end