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 => ways(:visible_way).way_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 => ways(:invisible_way).way_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 propogate_tags(current_ways(:visible_way), ways(:visible_way))
50 propogate_tags(current_ways(:used_way), ways(:used_way))
51 propogate_tags(current_ways(:way_with_versions), ways(:way_with_versions_v4))
53 check_current_version(current_ways(:visible_way).id)
54 check_current_version(current_ways(:used_way).id)
55 check_current_version(current_ways(:way_with_versions).id)
59 # check that returned history is the same as getting all
60 # versions of a way from the api.
61 def test_history_equals_versions
62 check_history_equals_versions(current_ways(:visible_way).id)
63 check_history_equals_versions(current_ways(:used_way).id)
64 check_history_equals_versions(current_ways(:way_with_versions).id)
68 # test the redaction of an old version of a way, while not being
70 def test_redact_way_unauthorised
71 do_redact_way(ways(:way_with_versions_v3),
73 assert_response :unauthorized, "should need to be authenticated to redact."
77 # test the redaction of an old version of a way, while being
78 # authorised as a normal user.
79 def test_redact_way_normal_user
80 basic_authorization(users(:public_user).email, "test")
82 do_redact_way(ways(:way_with_versions_v3),
84 assert_response :forbidden, "should need to be moderator to redact."
88 # test that, even as moderator, the current version of a way
90 def test_redact_way_current_version
91 basic_authorization(users(:moderator_user).email, "test")
93 do_redact_way(ways(:way_with_versions_v4),
95 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
99 # test that redacted ways aren't visible, regardless of
100 # authorisation except as moderator...
101 def test_version_redacted
102 way = ways(:way_with_redacted_versions_v2)
104 get :version, :id => way.way_id, :version => way.version
105 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
107 # not even to a logged-in user
108 basic_authorization(users(:public_user).email, "test")
109 get :version, :id => way.way_id, :version => way.version
110 assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
114 # test that redacted nodes aren't visible in the history
115 def test_history_redacted
116 way = ways(:way_with_redacted_versions_v2)
118 get :history, :id => way.way_id
119 assert_response :success, "Redaction shouldn't have stopped history working."
120 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."
122 # not even to a logged-in user
123 basic_authorization(users(:public_user).email, "test")
124 get :version, :id => way.way_id, :version => way.version
125 get :history, :id => way.way_id
126 assert_response :success, "Redaction shouldn't have stopped history working."
127 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 # test the redaction of an old version of a way, while being
132 # authorised as a moderator.
133 def test_redact_way_moderator
134 way = ways(:way_with_versions_v3)
135 basic_authorization(users(:moderator_user).email, "test")
137 do_redact_way(way, redactions(:example))
138 assert_response :success, "should be OK to redact old version as moderator."
140 # check moderator can still see the redacted data, when passing
141 # the appropriate flag
142 get :version, :id => way.way_id, :version => way.version
143 assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
144 get :version, :id => way.way_id, :version => way.version, :show_redactions => "true"
145 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
147 # and when accessed via history
148 get :history, :id => way.way_id
149 assert_response :success, "Redaction shouldn't have stopped history working."
150 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."
151 get :history, :id => way.way_id, :show_redactions => "true"
152 assert_response :success, "Redaction shouldn't have stopped history working."
153 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."
156 # testing that if the moderator drops auth, he can't see the
157 # redacted stuff any more.
158 def test_redact_way_is_redacted
159 way = ways(:way_with_versions_v3)
160 basic_authorization(users(:moderator_user).email, "test")
162 do_redact_way(way, redactions(:example))
163 assert_response :success, "should be OK to redact old version as moderator."
165 # re-auth as non-moderator
166 basic_authorization(users(:public_user).email, "test")
168 # check can't see the redacted data
169 get :version, :id => way.way_id, :version => way.version
170 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
172 # and when accessed via history
173 get :history, :id => way.way_id
174 assert_response :success, "Redaction shouldn't have stopped history working."
175 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 # test the unredaction of an old version of a way, while not being
181 def test_unredact_way_unauthorised
182 way = ways(:way_with_redacted_versions_v3)
184 post :redact, :id => way.way_id, :version => way.version
185 assert_response :unauthorized, "should need to be authenticated to unredact."
189 # test the unredaction of an old version of a way, while being
190 # authorised as a normal user.
191 def test_unredact_way_normal_user
192 way = ways(:way_with_redacted_versions_v3)
193 basic_authorization(users(:public_user).email, "test")
195 post :redact, :id => way.way_id, :version => way.version
196 assert_response :forbidden, "should need to be moderator to unredact."
200 # test the unredaction of an old version of a way, while being
201 # authorised as a moderator.
202 def test_unredact_way_moderator
203 way = ways(:way_with_redacted_versions_v3)
204 basic_authorization(users(:moderator_user).email, "test")
206 post :redact, :id => way.way_id, :version => way.version
207 assert_response :success, "should be OK to unredact old version as moderator."
209 # check moderator can still see the redacted data, without passing
210 # the appropriate flag
211 get :version, :id => way.way_id, :version => way.version
212 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
214 # and when accessed via history
215 get :history, :id => way.way_id
216 assert_response :success, "Redaction shouldn't have stopped history working."
217 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."
219 basic_authorization(users(:normal_user).email, "test")
221 # check normal user can now see the redacted data
222 get :version, :id => way.way_id, :version => way.version
223 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
225 # and when accessed via history
226 get :history, :id => way.way_id
227 assert_response :success, "Redaction shouldn't have stopped history working."
228 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."
234 # check that the current version of a way is equivalent to the
235 # version which we're getting from the versions call.
236 def check_current_version(way_id)
237 # get the current version
238 current_way = with_controller(WayController.new) do
239 get :read, :id => way_id
240 assert_response :success, "can't get current way #{way_id}"
241 Way.from_xml(@response.body)
243 assert_not_nil current_way, "getting way #{way_id} returned nil"
245 # get the "old" version of the way from the version method
246 get :version, :id => way_id, :version => current_way.version
247 assert_response :success, "can't get old way #{way_id}, v#{current_way.version}"
248 old_way = Way.from_xml(@response.body)
250 # check that the ways are identical
251 assert_ways_are_equal current_way, old_way
255 # look at all the versions of the way in the history and get each version from
256 # the versions call. check that they're the same.
257 def check_history_equals_versions(way_id)
258 get :history, :id => way_id
259 assert_response :success, "can't get way #{way_id} from API"
260 history_doc = XML::Parser.string(@response.body).parse
261 assert_not_nil history_doc, "parsing way #{way_id} history failed"
263 history_doc.find("//osm/way").each do |way_doc|
264 history_way = Way.from_xml_node(way_doc)
265 assert_not_nil history_way, "parsing way #{way_id} version failed"
267 get :version, :id => way_id, :version => history_way.version
268 assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
269 version_way = Way.from_xml(@response.body)
270 assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
272 assert_ways_are_equal history_way, version_way
276 def do_redact_way(way, redaction)
277 get :version, :id => way.way_id, :version => way.version
278 assert_response :success, "should be able to get version #{way.version} of node #{way.way_id}."
281 post :redact, :id => way.way_id, :version => way.version, :redaction => redaction.id
284 def propogate_tags(way, old_way)
285 way.tags.each do |k, v|
286 create(:old_way_tag, :old_way => old_way, :k => k, :v => v)