2 require "old_way_controller"
4 class OldWayControllerTest < ActionController::TestCase
8 # test all routes which lead to this controller
11 { :path => "/api/0.6/way/1/history", :method => :get },
12 { :controller => "old_way", :action => "history", :id => "1" }
15 { :path => "/api/0.6/way/1/2", :method => :get },
16 { :controller => "old_way", :action => "version", :id => "1", :version => "2" }
19 { :path => "/api/0.6/way/1/2/redact", :method => :post },
20 { :controller => "old_way", :action => "redact", :id => "1", :version => "2" }
24 # -------------------------------------
25 # Test reading old ways.
26 # -------------------------------------
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
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
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
47 # check that we can retrieve versions of a way
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)
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)
61 check_current_version(way.id)
62 check_current_version(used_way.id)
63 check_current_version(way_with_versions.id)
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)
75 check_history_equals_versions(way.id)
76 check_history_equals_versions(used_way.id)
77 check_history_equals_versions(way_with_versions.id)
81 # test the redaction of an old version of a way, while not being
83 def test_redact_way_unauthorised
84 way = create(:way, :with_history, :version => 4)
85 way_v3 = way.old_ways.find_by(:version => 3)
87 do_redact_way(way_v3, create(:redaction))
88 assert_response :unauthorized, "should need to be authenticated to redact."
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)
99 do_redact_way(way_v3, create(:redaction))
100 assert_response :forbidden, "should need to be moderator to redact."
104 # test that, even as moderator, the current version of a way
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
111 do_redact_way(way_latest, create(:redaction))
112 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
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))
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."
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."
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))
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."
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."
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")
159 do_redact_way(way_v3, create(:redaction))
160 assert_response :success, "should be OK to redact old version as moderator."
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."
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."
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")
185 do_redact_way(way_v3, create(:redaction))
186 assert_response :success, "should be OK to redact old version as moderator."
188 # re-auth as non-moderator
189 basic_authorization(create(:user).email, "test")
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."
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."
202 # test the unredaction of an old version of a way, while not being
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))
209 post :redact, :id => way_v1.way_id, :version => way_v1.version
210 assert_response :unauthorized, "should need to be authenticated to unredact."
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))
221 basic_authorization(create(:user).email, "test")
223 post :redact, :id => way_v1.way_id, :version => way_v1.version
224 assert_response :forbidden, "should need to be moderator to unredact."
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))
236 basic_authorization(moderator_user.email, "test")
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."
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."
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."
251 basic_authorization(create(:user).email, "test")
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."
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."
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)
275 assert_not_nil current_way, "getting way #{way_id} returned nil"
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)
282 # check that the ways are identical
283 assert_ways_are_equal current_way, old_way
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"
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"
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}"
304 assert_ways_are_equal history_way, version_way
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}."
313 post :redact, :id => way.way_id, :version => way.version, :redaction => redaction.id
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)