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     check_current_version(current_ways(:visible_way).id)
 
  50     check_current_version(current_ways(:used_way).id)
 
  51     check_current_version(current_ways(:way_with_versions).id)
 
  55   # check that returned history is the same as getting all
 
  56   # versions of a way from the api.
 
  57   def test_history_equals_versions
 
  58     check_history_equals_versions(current_ways(:visible_way).id)
 
  59     check_history_equals_versions(current_ways(:used_way).id)
 
  60     check_history_equals_versions(current_ways(:way_with_versions).id)
 
  64   # test the redaction of an old version of a way, while not being
 
  66   def test_redact_way_unauthorised
 
  67     do_redact_way(ways(:way_with_versions_v3),
 
  69     assert_response :unauthorized, "should need to be authenticated to redact."
 
  73   # test the redaction of an old version of a way, while being
 
  74   # authorised as a normal user.
 
  75   def test_redact_way_normal_user
 
  76     basic_authorization(users(:public_user).email, "test")
 
  78     do_redact_way(ways(:way_with_versions_v3),
 
  80     assert_response :forbidden, "should need to be moderator to redact."
 
  84   # test that, even as moderator, the current version of a way
 
  86   def test_redact_way_current_version
 
  87     basic_authorization(users(:moderator_user).email, "test")
 
  89     do_redact_way(ways(:way_with_versions_v4),
 
  91     assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
 
  95   # test that redacted ways aren't visible, regardless of
 
  96   # authorisation except as moderator...
 
  97   def test_version_redacted
 
  98     way = ways(:way_with_redacted_versions_v2)
 
 100     get :version, :id => way.way_id, :version => way.version
 
 101     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
 
 103     # not even to a logged-in user
 
 104     basic_authorization(users(:public_user).email, "test")
 
 105     get :version, :id => way.way_id, :version => way.version
 
 106     assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
 
 110   # test that redacted nodes aren't visible in the history
 
 111   def test_history_redacted
 
 112     way = ways(:way_with_redacted_versions_v2)
 
 114     get :history, :id => way.way_id
 
 115     assert_response :success, "Redaction shouldn't have stopped history working."
 
 116     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."
 
 118     # not even to a logged-in user
 
 119     basic_authorization(users(:public_user).email, "test")
 
 120     get :version, :id => way.way_id, :version => way.version
 
 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 node #{way.way_id} version #{way.version} shouldn't be present in the history, even when logged in."
 
 127   # test the redaction of an old version of a way, while being
 
 128   # authorised as a moderator.
 
 129   def test_redact_way_moderator
 
 130     way = ways(:way_with_versions_v3)
 
 131     basic_authorization(users(:moderator_user).email, "test")
 
 133     do_redact_way(way, redactions(:example))
 
 134     assert_response :success, "should be OK to redact old version as moderator."
 
 136     # check moderator can still see the redacted data, when passing
 
 137     # the appropriate flag
 
 138     get :version, :id => way.way_id, :version => way.version
 
 139     assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
 
 140     get :version, :id => way.way_id, :version => way.version, :show_redactions => "true"
 
 141     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
 
 143     # and when accessed via history
 
 144     get :history, :id => way.way_id
 
 145     assert_response :success, "Redaction shouldn't have stopped history working."
 
 146     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."
 
 147     get :history, :id => way.way_id, :show_redactions => "true"
 
 148     assert_response :success, "Redaction shouldn't have stopped history working."
 
 149     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."
 
 152   # testing that if the moderator drops auth, he can't see the
 
 153   # redacted stuff any more.
 
 154   def test_redact_way_is_redacted
 
 155     way = ways(:way_with_versions_v3)
 
 156     basic_authorization(users(:moderator_user).email, "test")
 
 158     do_redact_way(way, redactions(:example))
 
 159     assert_response :success, "should be OK to redact old version as moderator."
 
 161     # re-auth as non-moderator
 
 162     basic_authorization(users(:public_user).email, "test")
 
 164     # check can't see the redacted data
 
 165     get :version, :id => way.way_id, :version => way.version
 
 166     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
 
 168     # and when accessed via history
 
 169     get :history, :id => way.way_id
 
 170     assert_response :success, "Redaction shouldn't have stopped history working."
 
 171     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."
 
 175   # test the unredaction of an old version of a way, while not being
 
 177   def test_unredact_way_unauthorised
 
 178     way = ways(:way_with_redacted_versions_v3)
 
 180     post :redact, :id => way.way_id, :version => way.version
 
 181     assert_response :unauthorized, "should need to be authenticated to unredact."
 
 185   # test the unredaction of an old version of a way, while being
 
 186   # authorised as a normal user.
 
 187   def test_unredact_way_normal_user
 
 188     way = ways(:way_with_redacted_versions_v3)
 
 189     basic_authorization(users(:public_user).email, "test")
 
 191     post :redact, :id => way.way_id, :version => way.version
 
 192     assert_response :forbidden, "should need to be moderator to unredact."
 
 196   # test the unredaction of an old version of a way, while being
 
 197   # authorised as a moderator.
 
 198   def test_unredact_way_moderator
 
 199     way = ways(:way_with_redacted_versions_v3)
 
 200     basic_authorization(users(:moderator_user).email, "test")
 
 202     post :redact, :id => way.way_id, :version => way.version
 
 203     assert_response :success, "should be OK to unredact old version as moderator."
 
 205     # check moderator can still see the redacted data, without passing
 
 206     # the appropriate flag
 
 207     get :version, :id => way.way_id, :version => way.version
 
 208     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
 
 210     # and when accessed via history
 
 211     get :history, :id => way.way_id
 
 212     assert_response :success, "Redaction shouldn't have stopped history working."
 
 213     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."
 
 215     basic_authorization(users(:normal_user).email, "test")
 
 217     # check normal user can now see the redacted data
 
 218     get :version, :id => way.way_id, :version => way.version
 
 219     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
 
 221     # and when accessed via history
 
 222     get :history, :id => way.way_id
 
 223     assert_response :success, "Redaction shouldn't have stopped history working."
 
 224     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."
 
 230   # check that the current version of a way is equivalent to the
 
 231   # version which we're getting from the versions call.
 
 232   def check_current_version(way_id)
 
 233     # get the current version
 
 234     current_way = with_controller(WayController.new) do
 
 235       get :read, :id => way_id
 
 236       assert_response :success, "can't get current way #{way_id}"
 
 237       Way.from_xml(@response.body)
 
 239     assert_not_nil current_way, "getting way #{way_id} returned nil"
 
 241     # get the "old" version of the way from the version method
 
 242     get :version, :id => way_id, :version => current_way.version
 
 243     assert_response :success, "can't get old way #{way_id}, v#{current_way.version}"
 
 244     old_way = Way.from_xml(@response.body)
 
 246     # check that the ways are identical
 
 247     assert_ways_are_equal current_way, old_way
 
 251   # look at all the versions of the way in the history and get each version from
 
 252   # the versions call. check that they're the same.
 
 253   def check_history_equals_versions(way_id)
 
 254     get :history, :id => way_id
 
 255     assert_response :success, "can't get way #{way_id} from API"
 
 256     history_doc = XML::Parser.string(@response.body).parse
 
 257     assert_not_nil history_doc, "parsing way #{way_id} history failed"
 
 259     history_doc.find("//osm/way").each do |way_doc|
 
 260       history_way = Way.from_xml_node(way_doc)
 
 261       assert_not_nil history_way, "parsing way #{way_id} version failed"
 
 263       get :version, :id => way_id, :version => history_way.version
 
 264       assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
 
 265       version_way = Way.from_xml(@response.body)
 
 266       assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
 
 268       assert_ways_are_equal history_way, version_way
 
 272   def do_redact_way(way, redaction)
 
 273     get :version, :id => way.way_id, :version => way.version
 
 274     assert_response :success, "should be able to get version #{way.version} of node #{way.way_id}."
 
 277     post :redact, :id => way.way_id, :version => way.version, :redaction => redaction.id