]> git.openstreetmap.org Git - rails.git/blob - test/controllers/old_way_controller_test.rb
Merge remote-tracking branch 'openstreetmap/pull/1508'
[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     basic_authorization(create(:user).email, "test")
84
85     do_redact_way(ways(:way_with_versions_v3),
86                   create(:redaction))
87     assert_response :forbidden, "should need to be moderator to redact."
88   end
89
90   ##
91   # test that, even as moderator, the current version of a way
92   # can't be redacted.
93   def test_redact_way_current_version
94     basic_authorization(create(:moderator_user).email, "test")
95
96     do_redact_way(ways(:way_with_versions_v4),
97                   create(:redaction))
98     assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
99   end
100
101   ##
102   # test that redacted ways aren't visible, regardless of
103   # authorisation except as moderator...
104   def test_version_redacted
105     way = ways(:way_with_redacted_versions_v2)
106
107     get :version, :id => way.way_id, :version => way.version
108     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
109
110     # not even to a logged-in user
111     basic_authorization(create(:user).email, "test")
112     get :version, :id => way.way_id, :version => way.version
113     assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
114   end
115
116   ##
117   # test that redacted nodes aren't visible in the history
118   def test_history_redacted
119     way = ways(:way_with_redacted_versions_v2)
120
121     get :history, :id => way.way_id
122     assert_response :success, "Redaction shouldn't have stopped history working."
123     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."
124
125     # not even to a logged-in user
126     basic_authorization(create(:user).email, "test")
127     get :version, :id => way.way_id, :version => way.version
128     get :history, :id => way.way_id
129     assert_response :success, "Redaction shouldn't have stopped history working."
130     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."
131   end
132
133   ##
134   # test the redaction of an old version of a way, while being
135   # authorised as a moderator.
136   def test_redact_way_moderator
137     way = ways(:way_with_versions_v3)
138     basic_authorization(create(:moderator_user).email, "test")
139
140     do_redact_way(way, create(:redaction))
141     assert_response :success, "should be OK to redact old version as moderator."
142
143     # check moderator can still see the redacted data, when passing
144     # the appropriate flag
145     get :version, :id => way.way_id, :version => way.version
146     assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
147     get :version, :id => way.way_id, :version => way.version, :show_redactions => "true"
148     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
149
150     # and when accessed via history
151     get :history, :id => way.way_id
152     assert_response :success, "Redaction shouldn't have stopped history working."
153     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."
154     get :history, :id => way.way_id, :show_redactions => "true"
155     assert_response :success, "Redaction shouldn't have stopped history working."
156     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."
157   end
158
159   # testing that if the moderator drops auth, he can't see the
160   # redacted stuff any more.
161   def test_redact_way_is_redacted
162     way = ways(:way_with_versions_v3)
163     basic_authorization(create(:moderator_user).email, "test")
164
165     do_redact_way(way, create(:redaction))
166     assert_response :success, "should be OK to redact old version as moderator."
167
168     # re-auth as non-moderator
169     basic_authorization(create(:user).email, "test")
170
171     # check can't see the redacted data
172     get :version, :id => way.way_id, :version => way.version
173     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
174
175     # and when accessed via history
176     get :history, :id => way.way_id
177     assert_response :success, "Redaction shouldn't have stopped history working."
178     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."
179   end
180
181   ##
182   # test the unredaction of an old version of a way, while not being
183   # authorised.
184   def test_unredact_way_unauthorised
185     way = ways(:way_with_redacted_versions_v3)
186
187     post :redact, :id => way.way_id, :version => way.version
188     assert_response :unauthorized, "should need to be authenticated to unredact."
189   end
190
191   ##
192   # test the unredaction of an old version of a way, while being
193   # authorised as a normal user.
194   def test_unredact_way_normal_user
195     way = ways(:way_with_redacted_versions_v3)
196     basic_authorization(create(:user).email, "test")
197
198     post :redact, :id => way.way_id, :version => way.version
199     assert_response :forbidden, "should need to be moderator to unredact."
200   end
201
202   ##
203   # test the unredaction of an old version of a way, while being
204   # authorised as a moderator.
205   def test_unredact_way_moderator
206     moderator_user = create(:moderator_user)
207     way = ways(:way_with_redacted_versions_v3)
208     basic_authorization(moderator_user.email, "test")
209
210     post :redact, :id => way.way_id, :version => way.version
211     assert_response :success, "should be OK to unredact old version as moderator."
212
213     # check moderator can still see the redacted data, without passing
214     # the appropriate flag
215     get :version, :id => way.way_id, :version => way.version
216     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
217
218     # and when accessed via history
219     get :history, :id => way.way_id
220     assert_response :success, "Redaction shouldn't have stopped history working."
221     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."
222
223     basic_authorization(users(:normal_user).email, "test")
224
225     # check normal user can now see the redacted data
226     get :version, :id => way.way_id, :version => way.version
227     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
228
229     # and when accessed via history
230     get :history, :id => way.way_id
231     assert_response :success, "Redaction shouldn't have stopped history working."
232     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."
233   end
234
235   private
236
237   ##
238   # check that the current version of a way is equivalent to the
239   # version which we're getting from the versions call.
240   def check_current_version(way_id)
241     # get the current version
242     current_way = with_controller(WayController.new) do
243       get :read, :id => way_id
244       assert_response :success, "can't get current way #{way_id}"
245       Way.from_xml(@response.body)
246     end
247     assert_not_nil current_way, "getting way #{way_id} returned nil"
248
249     # get the "old" version of the way from the version method
250     get :version, :id => way_id, :version => current_way.version
251     assert_response :success, "can't get old way #{way_id}, v#{current_way.version}"
252     old_way = Way.from_xml(@response.body)
253
254     # check that the ways are identical
255     assert_ways_are_equal current_way, old_way
256   end
257
258   ##
259   # look at all the versions of the way in the history and get each version from
260   # the versions call. check that they're the same.
261   def check_history_equals_versions(way_id)
262     get :history, :id => way_id
263     assert_response :success, "can't get way #{way_id} from API"
264     history_doc = XML::Parser.string(@response.body).parse
265     assert_not_nil history_doc, "parsing way #{way_id} history failed"
266
267     history_doc.find("//osm/way").each do |way_doc|
268       history_way = Way.from_xml_node(way_doc)
269       assert_not_nil history_way, "parsing way #{way_id} version failed"
270
271       get :version, :id => way_id, :version => history_way.version
272       assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
273       version_way = Way.from_xml(@response.body)
274       assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
275
276       assert_ways_are_equal history_way, version_way
277     end
278   end
279
280   def do_redact_way(way, redaction)
281     get :version, :id => way.way_id, :version => way.version
282     assert_response :success, "should be able to get version #{way.version} of way #{way.way_id}."
283
284     # now redact it
285     post :redact, :id => way.way_id, :version => way.version, :redaction => redaction.id
286   end
287
288   def propagate_tags(way, old_way)
289     way.tags.each do |k, v|
290       create(:old_way_tag, :old_way => old_way, :k => k, :v => v)
291     end
292   end
293 end