4   class OldWaysControllerTest < ActionDispatch::IntegrationTest
 
   6     # test all routes which lead to this controller
 
   9         { :path => "/api/0.6/way/1/history", :method => :get },
 
  10         { :controller => "api/old_ways", :action => "index", :way_id => "1" }
 
  13         { :path => "/api/0.6/way/1/history.json", :method => :get },
 
  14         { :controller => "api/old_ways", :action => "index", :way_id => "1", :format => "json" }
 
  17         { :path => "/api/0.6/way/1/2", :method => :get },
 
  18         { :controller => "api/old_ways", :action => "show", :way_id => "1", :version => "2" }
 
  21         { :path => "/api/0.6/way/1/2.json", :method => :get },
 
  22         { :controller => "api/old_ways", :action => "show", :way_id => "1", :version => "2", :format => "json" }
 
  27     # check that a visible way is returned properly
 
  29       way = create(:way, :with_history, :version => 2)
 
  31       get api_way_versions_path(way)
 
  33       assert_response :success
 
  34       assert_dom "osm:root", 1 do
 
  35         assert_dom "> way", 2 do |dom_ways|
 
  36           assert_dom dom_ways[0], "> @id", way.id.to_s
 
  37           assert_dom dom_ways[0], "> @version", "1"
 
  39           assert_dom dom_ways[1], "> @id", way.id.to_s
 
  40           assert_dom dom_ways[1], "> @version", "2"
 
  46     # check that an invisible way's history is returned properly
 
  47     def test_index_invisible
 
  48       get api_way_versions_path(create(:way, :with_history, :deleted))
 
  49       assert_response :success
 
  53     # check chat a non-existent way is not returned
 
  54     def test_index_invalid
 
  55       get api_way_versions_path(0)
 
  56       assert_response :not_found
 
  60     # test that redacted ways aren't visible in the history
 
  61     def test_index_redacted_unauthorised
 
  62       way = create(:way, :with_history, :version => 2)
 
  63       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
 
  65       get api_way_versions_path(way)
 
  67       assert_response :success, "Redaction shouldn't have stopped history working."
 
  68       assert_dom "osm way[id='#{way.id}'][version='1']", 0,
 
  69                  "redacted way #{way.id} version 1 shouldn't be present in the history."
 
  71       get api_way_versions_path(way, :show_redactions => "true")
 
  73       assert_response :success, "Redaction shouldn't have stopped history working."
 
  74       assert_dom "osm way[id='#{way.id}'][version='1']", 0,
 
  75                  "redacted way #{way.id} version 1 shouldn't be present in the history when passing flag."
 
  78     def test_index_redacted_normal_user
 
  79       way = create(:way, :with_history, :version => 2)
 
  80       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
 
  82       get api_way_versions_path(way), :headers => bearer_authorization_header
 
  84       assert_response :success, "Redaction shouldn't have stopped history working."
 
  85       assert_dom "osm way[id='#{way.id}'][version='1']", 0,
 
  86                  "redacted node #{way.id} version 1 shouldn't be present in the history, even when logged in."
 
  88       get api_way_versions_path(way, :show_redactions => "true"), :headers => bearer_authorization_header
 
  90       assert_response :success, "Redaction shouldn't have stopped history working."
 
  91       assert_dom "osm way[id='#{way.id}'][version='1']", 0,
 
  92                  "redacted node #{way.id} version 1 shouldn't be present in the history, even when logged in and passing flag."
 
  95     def test_index_redacted_moderator
 
  96       way = create(:way, :with_history, :version => 2)
 
  97       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
 
  98       auth_header = bearer_authorization_header create(:moderator_user)
 
 100       get api_way_versions_path(way), :headers => auth_header
 
 102       assert_response :success, "Redaction shouldn't have stopped history working."
 
 103       assert_dom "osm way[id='#{way.id}'][version='1']", 0,
 
 104                  "way #{way.id} version 1 should not be present in the history for moderators when not passing flag."
 
 106       get api_way_versions_path(way, :show_redactions => "true"), :headers => auth_header
 
 108       assert_response :success, "Redaction shouldn't have stopped history working."
 
 109       assert_dom "osm way[id='#{way.id}'][version='1']", 1,
 
 110                  "way #{way.id} version 1 should still be present in the history for moderators when passing flag."
 
 114       way = create(:way, :with_history, :version => 2)
 
 116       get api_way_version_path(way, 1)
 
 118       assert_response :success
 
 119       assert_dom "osm:root", 1 do
 
 120         assert_dom "> way", 1 do
 
 121           assert_dom "> @id", way.id.to_s
 
 122           assert_dom "> @version", "1"
 
 126       get api_way_version_path(way, 2)
 
 128       assert_response :success
 
 129       assert_dom "osm:root", 1 do
 
 130         assert_dom "> way", 1 do
 
 131           assert_dom "> @id", way.id.to_s
 
 132           assert_dom "> @version", "2"
 
 138     # test that redacted ways aren't visible, regardless of
 
 139     # authorisation except as moderator...
 
 140     def test_show_redacted_unauthorised
 
 141       way = create(:way, :with_history, :version => 2)
 
 142       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
 
 144       get api_way_version_path(way, 1)
 
 146       assert_response :forbidden, "Redacted way shouldn't be visible via the version API."
 
 148       get api_way_version_path(way, 1, :show_redactions => "true")
 
 150       assert_response :forbidden, "Redacted way shouldn't be visible via the version API when passing flag."
 
 153     def test_show_redacted_normal_user
 
 154       way = create(:way, :with_history, :version => 2)
 
 155       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
 
 157       get api_way_version_path(way, 1), :headers => bearer_authorization_header
 
 159       assert_response :forbidden, "Redacted way shouldn't be visible via the version API, even when logged in."
 
 161       get api_way_version_path(way, 1, :show_redactions => "true"), :headers => bearer_authorization_header
 
 163       assert_response :forbidden, "Redacted way shouldn't be visible via the version API, even when logged in and passing flag."
 
 166     def test_show_redacted_moderator
 
 167       way = create(:way, :with_history, :version => 2)
 
 168       way.old_ways.find_by(:version => 1).redact!(create(:redaction))
 
 169       auth_header = bearer_authorization_header create(:moderator_user)
 
 171       get api_way_version_path(way, 1), :headers => auth_header
 
 173       assert_response :forbidden, "Redacted node should be gone for moderator, when flag not passed."
 
 175       get api_way_version_path(way, 1, :show_redactions => "true"), :headers => auth_header
 
 177       assert_response :success, "Redacted node should not be gone for moderator, when flag passed."
 
 181     # check that returned history is the same as getting all
 
 182     # versions of a way from the api.
 
 183     def test_history_equals_versions
 
 184       way = create(:way, :with_history)
 
 185       used_way = create(:way, :with_history)
 
 186       create(:relation_member, :member => used_way)
 
 187       way_with_versions = create(:way, :with_history, :version => 4)
 
 189       check_history_equals_versions(way.id)
 
 190       check_history_equals_versions(used_way.id)
 
 191       check_history_equals_versions(way_with_versions.id)
 
 197     # look at all the versions of the way in the history and get each version from
 
 198     # the versions call. check that they're the same.
 
 199     def check_history_equals_versions(way_id)
 
 200       get api_way_versions_path(way_id)
 
 201       assert_response :success, "can't get way #{way_id} from API"
 
 202       history_doc = XML::Parser.string(@response.body).parse
 
 203       assert_not_nil history_doc, "parsing way #{way_id} history failed"
 
 205       history_doc.find("//osm/way").each do |way_doc|
 
 206         history_way = Way.from_xml_node(way_doc)
 
 207         assert_not_nil history_way, "parsing way #{way_id} version failed"
 
 209         get api_way_version_path(way_id, history_way.version)
 
 210         assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
 
 211         version_way = Way.from_xml(@response.body)
 
 212         assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
 
 214         assert_ways_are_equal history_way, version_way