1 # frozen_string_literal: true
 
   6   class OldWaysControllerTest < ActionDispatch::IntegrationTest
 
   8     # test all routes which lead to this controller
 
  11         { :path => "/api/0.6/way/1/history", :method => :get },
 
  12         { :controller => "api/old_ways", :action => "index", :way_id => "1" }
 
  15         { :path => "/api/0.6/way/1/history.json", :method => :get },
 
  16         { :controller => "api/old_ways", :action => "index", :way_id => "1", :format => "json" }
 
  19         { :path => "/api/0.6/way/1/2", :method => :get },
 
  20         { :controller => "api/old_ways", :action => "show", :way_id => "1", :version => "2" }
 
  23         { :path => "/api/0.6/way/1/2.json", :method => :get },
 
  24         { :controller => "api/old_ways", :action => "show", :way_id => "1", :version => "2", :format => "json" }
 
  29     # check that a visible way is returned properly
 
  31       way = create(:way, :with_history, :version => 2)
 
  33       get api_way_versions_path(way)
 
  35       assert_response :success
 
  36       assert_dom "osm:root", 1 do
 
  37         assert_dom "> way", 2 do |dom_ways|
 
  38           assert_dom dom_ways[0], "> @id", way.id.to_s
 
  39           assert_dom dom_ways[0], "> @version", "1"
 
  41           assert_dom dom_ways[1], "> @id", way.id.to_s
 
  42           assert_dom dom_ways[1], "> @version", "2"
 
  48     # check that an invisible way's history is returned properly
 
  49     def test_index_invisible
 
  50       get api_way_versions_path(create(:way, :with_history, :deleted))
 
  51       assert_response :success
 
  55     # check chat a non-existent way is not returned
 
  56     def test_index_invalid
 
  57       get api_way_versions_path(0)
 
  58       assert_response :not_found
 
  62     # test that redacted ways aren't visible in the history
 
  63     def test_index_redacted_unauthorised
 
  64       way = create(:way, :with_history, :version => 2)
 
  65       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
 
  67       get api_way_versions_path(way)
 
  69       assert_response :success, "Redaction shouldn't have stopped history working."
 
  70       assert_dom "osm way[id='#{way.id}'][version='1']", 0,
 
  71                  "redacted way #{way.id} version 1 shouldn't be present in the history."
 
  73       get api_way_versions_path(way, :show_redactions => "true")
 
  75       assert_response :success, "Redaction shouldn't have stopped history working."
 
  76       assert_dom "osm way[id='#{way.id}'][version='1']", 0,
 
  77                  "redacted way #{way.id} version 1 shouldn't be present in the history when passing flag."
 
  80     def test_index_redacted_normal_user
 
  81       way = create(:way, :with_history, :version => 2)
 
  82       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
 
  84       get api_way_versions_path(way), :headers => bearer_authorization_header
 
  86       assert_response :success, "Redaction shouldn't have stopped history working."
 
  87       assert_dom "osm way[id='#{way.id}'][version='1']", 0,
 
  88                  "redacted node #{way.id} version 1 shouldn't be present in the history, even when logged in."
 
  90       get api_way_versions_path(way, :show_redactions => "true"), :headers => bearer_authorization_header
 
  92       assert_response :success, "Redaction shouldn't have stopped history working."
 
  93       assert_dom "osm way[id='#{way.id}'][version='1']", 0,
 
  94                  "redacted node #{way.id} version 1 shouldn't be present in the history, even when logged in and passing flag."
 
  97     def test_index_redacted_moderator
 
  98       way = create(:way, :with_history, :version => 2)
 
  99       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
 
 100       auth_header = bearer_authorization_header create(:moderator_user)
 
 102       get api_way_versions_path(way), :headers => auth_header
 
 104       assert_response :success, "Redaction shouldn't have stopped history working."
 
 105       assert_dom "osm way[id='#{way.id}'][version='1']", 0,
 
 106                  "way #{way.id} version 1 should not be present in the history for moderators when not passing flag."
 
 108       get api_way_versions_path(way, :show_redactions => "true"), :headers => auth_header
 
 110       assert_response :success, "Redaction shouldn't have stopped history working."
 
 111       assert_dom "osm way[id='#{way.id}'][version='1']", 1,
 
 112                  "way #{way.id} version 1 should still be present in the history for moderators when passing flag."
 
 116       way = create(:way, :with_history, :version => 2)
 
 118       get api_way_version_path(way, 1)
 
 120       assert_response :success
 
 121       assert_dom "osm:root", 1 do
 
 122         assert_dom "> way", 1 do
 
 123           assert_dom "> @id", way.id.to_s
 
 124           assert_dom "> @version", "1"
 
 128       get api_way_version_path(way, 2)
 
 130       assert_response :success
 
 131       assert_dom "osm:root", 1 do
 
 132         assert_dom "> way", 1 do
 
 133           assert_dom "> @id", way.id.to_s
 
 134           assert_dom "> @version", "2"
 
 140     # test that redacted ways aren't visible, regardless of
 
 141     # authorisation except as moderator...
 
 142     def test_show_redacted_unauthorised
 
 143       way = create(:way, :with_history, :version => 2)
 
 144       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
 
 146       get api_way_version_path(way, 1)
 
 148       assert_response :forbidden, "Redacted way shouldn't be visible via the version API."
 
 150       get api_way_version_path(way, 1, :show_redactions => "true")
 
 152       assert_response :forbidden, "Redacted way shouldn't be visible via the version API when passing flag."
 
 155     def test_show_redacted_normal_user
 
 156       way = create(:way, :with_history, :version => 2)
 
 157       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
 
 159       get api_way_version_path(way, 1), :headers => bearer_authorization_header
 
 161       assert_response :forbidden, "Redacted way shouldn't be visible via the version API, even when logged in."
 
 163       get api_way_version_path(way, 1, :show_redactions => "true"), :headers => bearer_authorization_header
 
 165       assert_response :forbidden, "Redacted way shouldn't be visible via the version API, even when logged in and passing flag."
 
 168     def test_show_redacted_moderator
 
 169       way = create(:way, :with_history, :version => 2)
 
 170       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
 
 171       auth_header = bearer_authorization_header create(:moderator_user)
 
 173       get api_way_version_path(way, 1), :headers => auth_header
 
 175       assert_response :forbidden, "Redacted node should be gone for moderator, when flag not passed."
 
 177       get api_way_version_path(way, 1, :show_redactions => "true"), :headers => auth_header
 
 179       assert_response :success, "Redacted node should not be gone for moderator, when flag passed."
 
 183     # check that returned history is the same as getting all
 
 184     # versions of a way from the api.
 
 185     def test_history_equals_versions
 
 186       way = create(:way, :with_history)
 
 187       used_way = create(:way, :with_history)
 
 188       create(:relation_member, :member => used_way)
 
 189       way_with_versions = create(:way, :with_history, :version => 4)
 
 191       check_history_equals_versions(way.id)
 
 192       check_history_equals_versions(used_way.id)
 
 193       check_history_equals_versions(way_with_versions.id)
 
 199     # look at all the versions of the way in the history and get each version from
 
 200     # the versions call. check that they're the same.
 
 201     def check_history_equals_versions(way_id)
 
 202       get api_way_versions_path(way_id)
 
 203       assert_response :success, "can't get way #{way_id} from API"
 
 204       history_doc = XML::Parser.string(@response.body).parse
 
 205       assert_not_nil history_doc, "parsing way #{way_id} history failed"
 
 207       history_doc.find("//osm/way").each do |way_doc|
 
 208         history_way = Way.from_xml_node(way_doc)
 
 209         assert_not_nil history_way, "parsing way #{way_id} version failed"
 
 211         get api_way_version_path(way_id, history_way.version)
 
 212         assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
 
 213         version_way = Way.from_xml(@response.body)
 
 214         assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
 
 216         assert_ways_are_equal history_way, version_way