]> git.openstreetmap.org Git - rails.git/blob - test/controllers/old_way_controller_test.rb
Refactor putway method tests to use factories
[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 => create(:way, :with_history).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 => create(:way, :with_history, :deleted).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     way = create(:way, :with_history)
50     used_way = create(:way, :with_history)
51     create(:relation_member, :member => used_way)
52     way_with_versions = create(:way, :with_history, :version => 4)
53
54     create(:way_tag, :way => way)
55     create(:way_tag, :way => used_way)
56     create(:way_tag, :way => way_with_versions)
57     propagate_tags(way, way.old_ways.last)
58     propagate_tags(used_way, used_way.old_ways.last)
59     propagate_tags(way_with_versions, way_with_versions.old_ways.last)
60
61     check_current_version(way.id)
62     check_current_version(used_way.id)
63     check_current_version(way_with_versions.id)
64   end
65
66   ##
67   # check that returned history is the same as getting all
68   # versions of a way from the api.
69   def test_history_equals_versions
70     way = create(:way, :with_history)
71     used_way = create(:way, :with_history)
72     create(:relation_member, :member => used_way)
73     way_with_versions = create(:way, :with_history, :version => 4)
74
75     check_history_equals_versions(way.id)
76     check_history_equals_versions(used_way.id)
77     check_history_equals_versions(way_with_versions.id)
78   end
79
80   ##
81   # test the redaction of an old version of a way, while not being
82   # authorised.
83   def test_redact_way_unauthorised
84     way = create(:way, :with_history, :version => 4)
85     way_v3 = way.old_ways.find_by(:version => 3)
86
87     do_redact_way(way_v3, create(:redaction))
88     assert_response :unauthorized, "should need to be authenticated to redact."
89   end
90
91   ##
92   # test the redaction of an old version of a way, while being
93   # authorised as a normal user.
94   def test_redact_way_normal_user
95     basic_authorization(create(:user).email, "test")
96     way = create(:way, :with_history, :version => 4)
97     way_v3 = way.old_ways.find_by(:version => 3)
98
99     do_redact_way(way_v3, create(:redaction))
100     assert_response :forbidden, "should need to be moderator to redact."
101   end
102
103   ##
104   # test that, even as moderator, the current version of a way
105   # can't be redacted.
106   def test_redact_way_current_version
107     basic_authorization(create(:moderator_user).email, "test")
108     way = create(:way, :with_history, :version => 4)
109     way_latest = way.old_ways.last
110
111     do_redact_way(way_latest, create(:redaction))
112     assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
113   end
114
115   ##
116   # test that redacted ways aren't visible, regardless of
117   # authorisation except as moderator...
118   def test_version_redacted
119     way = create(:way, :with_history, :version => 2)
120     way_v1 = way.old_ways.find_by(:version => 1)
121     way_v1.redact!(create(:redaction))
122
123     get :version, :id => way_v1.way_id, :version => way_v1.version
124     assert_response :forbidden, "Redacted way shouldn't be visible via the version API."
125
126     # not even to a logged-in user
127     basic_authorization(create(:user).email, "test")
128     get :version, :id => way_v1.way_id, :version => way_v1.version
129     assert_response :forbidden, "Redacted way shouldn't be visible via the version API, even when logged in."
130   end
131
132   ##
133   # test that redacted ways aren't visible in the history
134   def test_history_redacted
135     way = create(:way, :with_history, :version => 2)
136     way_v1 = way.old_ways.find_by(:version => 1)
137     way_v1.redact!(create(:redaction))
138
139     get :history, :id => way_v1.way_id
140     assert_response :success, "Redaction shouldn't have stopped history working."
141     assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 0, "redacted way #{way_v1.way_id} version #{way_v1.version} shouldn't be present in the history."
142
143     # not even to a logged-in user
144     basic_authorization(create(:user).email, "test")
145     get :version, :id => way_v1.way_id, :version => way_v1.version
146     get :history, :id => way_v1.way_id
147     assert_response :success, "Redaction shouldn't have stopped history working."
148     assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 0, "redacted node #{way_v1.way_id} version #{way_v1.version} shouldn't be present in the history, even when logged in."
149   end
150
151   ##
152   # test the redaction of an old version of a way, while being
153   # authorised as a moderator.
154   def test_redact_way_moderator
155     way = create(:way, :with_history, :version => 4)
156     way_v3 = way.old_ways.find_by(:version => 3)
157     basic_authorization(create(:moderator_user).email, "test")
158
159     do_redact_way(way_v3, create(:redaction))
160     assert_response :success, "should be OK to redact old version as moderator."
161
162     # check moderator can still see the redacted data, when passing
163     # the appropriate flag
164     get :version, :id => way_v3.way_id, :version => way_v3.version
165     assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
166     get :version, :id => way_v3.way_id, :version => way_v3.version, :show_redactions => "true"
167     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
168
169     # and when accessed via history
170     get :history, :id => way_v3.way_id
171     assert_response :success, "Redaction shouldn't have stopped history working."
172     assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 0, "way #{way_v3.way_id} version #{way_v3.version} should not be present in the history for moderators when not passing flag."
173     get :history, :id => way_v3.way_id, :show_redactions => "true"
174     assert_response :success, "Redaction shouldn't have stopped history working."
175     assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 1, "way #{way_v3.way_id} version #{way_v3.version} should still be present in the history for moderators when passing flag."
176   end
177
178   # testing that if the moderator drops auth, he can't see the
179   # redacted stuff any more.
180   def test_redact_way_is_redacted
181     way = create(:way, :with_history, :version => 4)
182     way_v3 = way.old_ways.find_by(:version => 3)
183     basic_authorization(create(:moderator_user).email, "test")
184
185     do_redact_way(way_v3, create(:redaction))
186     assert_response :success, "should be OK to redact old version as moderator."
187
188     # re-auth as non-moderator
189     basic_authorization(create(:user).email, "test")
190
191     # check can't see the redacted data
192     get :version, :id => way_v3.way_id, :version => way_v3.version
193     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
194
195     # and when accessed via history
196     get :history, :id => way_v3.way_id
197     assert_response :success, "Redaction shouldn't have stopped history working."
198     assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 0, "redacted way #{way_v3.way_id} version #{way_v3.version} shouldn't be present in the history."
199   end
200
201   ##
202   # test the unredaction of an old version of a way, while not being
203   # authorised.
204   def test_unredact_way_unauthorised
205     way = create(:way, :with_history, :version => 2)
206     way_v1 = way.old_ways.find_by(:version => 1)
207     way_v1.redact!(create(:redaction))
208
209     post :redact, :id => way_v1.way_id, :version => way_v1.version
210     assert_response :unauthorized, "should need to be authenticated to unredact."
211   end
212
213   ##
214   # test the unredaction of an old version of a way, while being
215   # authorised as a normal user.
216   def test_unredact_way_normal_user
217     way = create(:way, :with_history, :version => 2)
218     way_v1 = way.old_ways.find_by(:version => 1)
219     way_v1.redact!(create(:redaction))
220
221     basic_authorization(create(:user).email, "test")
222
223     post :redact, :id => way_v1.way_id, :version => way_v1.version
224     assert_response :forbidden, "should need to be moderator to unredact."
225   end
226
227   ##
228   # test the unredaction of an old version of a way, while being
229   # authorised as a moderator.
230   def test_unredact_way_moderator
231     moderator_user = create(:moderator_user)
232     way = create(:way, :with_history, :version => 2)
233     way_v1 = way.old_ways.find_by(:version => 1)
234     way_v1.redact!(create(:redaction))
235
236     basic_authorization(moderator_user.email, "test")
237
238     post :redact, :id => way_v1.way_id, :version => way_v1.version
239     assert_response :success, "should be OK to unredact old version as moderator."
240
241     # check moderator can still see the unredacted data, without passing
242     # the appropriate flag
243     get :version, :id => way_v1.way_id, :version => way_v1.version
244     assert_response :success, "After unredaction, node should not be gone for moderator."
245
246     # and when accessed via history
247     get :history, :id => way_v1.way_id
248     assert_response :success, "Unredaction shouldn't have stopped history working."
249     assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1, "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for moderators."
250
251     basic_authorization(create(:user).email, "test")
252
253     # check normal user can now see the unredacted data
254     get :version, :id => way_v1.way_id, :version => way_v1.version
255     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
256
257     # and when accessed via history
258     get :history, :id => way_v1.way_id
259     assert_response :success, "Redaction shouldn't have stopped history working."
260     assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1, "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for normal users."
261   end
262
263   private
264
265   ##
266   # check that the current version of a way is equivalent to the
267   # version which we're getting from the versions call.
268   def check_current_version(way_id)
269     # get the current version
270     current_way = with_controller(WayController.new) do
271       get :read, :id => way_id
272       assert_response :success, "can't get current way #{way_id}"
273       Way.from_xml(@response.body)
274     end
275     assert_not_nil current_way, "getting way #{way_id} returned nil"
276
277     # get the "old" version of the way from the version method
278     get :version, :id => way_id, :version => current_way.version
279     assert_response :success, "can't get old way #{way_id}, v#{current_way.version}"
280     old_way = Way.from_xml(@response.body)
281
282     # check that the ways are identical
283     assert_ways_are_equal current_way, old_way
284   end
285
286   ##
287   # look at all the versions of the way in the history and get each version from
288   # the versions call. check that they're the same.
289   def check_history_equals_versions(way_id)
290     get :history, :id => way_id
291     assert_response :success, "can't get way #{way_id} from API"
292     history_doc = XML::Parser.string(@response.body).parse
293     assert_not_nil history_doc, "parsing way #{way_id} history failed"
294
295     history_doc.find("//osm/way").each do |way_doc|
296       history_way = Way.from_xml_node(way_doc)
297       assert_not_nil history_way, "parsing way #{way_id} version failed"
298
299       get :version, :id => way_id, :version => history_way.version
300       assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
301       version_way = Way.from_xml(@response.body)
302       assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
303
304       assert_ways_are_equal history_way, version_way
305     end
306   end
307
308   def do_redact_way(way, redaction)
309     get :version, :id => way.way_id, :version => way.version
310     assert_response :success, "should be able to get version #{way.version} of way #{way.way_id}."
311
312     # now redact it
313     post :redact, :id => way.way_id, :version => way.version, :redaction => redaction.id
314   end
315
316   def propagate_tags(way, old_way)
317     way.tags.each do |k, v|
318       create(:old_way_tag, :old_way => old_way, :k => k, :v => v)
319     end
320   end
321 end