4   class OldNodesControllerTest < ActionDispatch::IntegrationTest
 
   6     # test all routes which lead to this controller
 
   9         { :path => "/api/0.6/node/1/history", :method => :get },
 
  10         { :controller => "api/old_nodes", :action => "index", :node_id => "1" }
 
  13         { :path => "/api/0.6/node/1/history.json", :method => :get },
 
  14         { :controller => "api/old_nodes", :action => "index", :node_id => "1", :format => "json" }
 
  17         { :path => "/api/0.6/node/1/2", :method => :get },
 
  18         { :controller => "api/old_nodes", :action => "show", :node_id => "1", :version => "2" }
 
  21         { :path => "/api/0.6/node/1/2.json", :method => :get },
 
  22         { :controller => "api/old_nodes", :action => "show", :node_id => "1", :version => "2", :format => "json" }
 
  27       node = create(:node, :version => 2)
 
  28       create(:old_node, :node_id => node.id, :version => 1, :latitude => 60 * OldNode::SCALE, :longitude => 30 * OldNode::SCALE)
 
  29       create(:old_node, :node_id => node.id, :version => 2, :latitude => 61 * OldNode::SCALE, :longitude => 31 * OldNode::SCALE)
 
  31       get api_node_versions_path(node)
 
  33       assert_response :success
 
  34       assert_dom "osm:root", 1 do
 
  35         assert_dom "> node", 2 do |dom_nodes|
 
  36           assert_dom dom_nodes[0], "> @id", node.id.to_s
 
  37           assert_dom dom_nodes[0], "> @version", "1"
 
  38           assert_dom dom_nodes[0], "> @lat", "60.0000000"
 
  39           assert_dom dom_nodes[0], "> @lon", "30.0000000"
 
  41           assert_dom dom_nodes[1], "> @id", node.id.to_s
 
  42           assert_dom dom_nodes[1], "> @version", "2"
 
  43           assert_dom dom_nodes[1], "> @lat", "61.0000000"
 
  44           assert_dom dom_nodes[1], "> @lon", "31.0000000"
 
  50     # test that redacted nodes aren't visible in the history
 
  51     def test_index_redacted_unauthorised
 
  52       node = create(:node, :with_history, :version => 2)
 
  53       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
 
  55       get api_node_versions_path(node)
 
  57       assert_response :success, "Redaction shouldn't have stopped history working."
 
  58       assert_dom "osm node[id='#{node.id}'][version='1']", 0,
 
  59                  "redacted node #{node.id} version 1 shouldn't be present in the history."
 
  61       get api_node_versions_path(node, :show_redactions => "true")
 
  63       assert_response :success, "Redaction shouldn't have stopped history working."
 
  64       assert_dom "osm node[id='#{node.id}'][version='1']", 0,
 
  65                  "redacted node #{node.id} version 1 shouldn't be present in the history when passing flag."
 
  68     def test_index_redacted_normal_user
 
  69       node = create(:node, :with_history, :version => 2)
 
  70       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
 
  72       get api_node_versions_path(node), :headers => bearer_authorization_header
 
  74       assert_response :success, "Redaction shouldn't have stopped history working."
 
  75       assert_dom "osm node[id='#{node.id}'][version='1']", 0,
 
  76                  "redacted node #{node.id} version 1 shouldn't be present in the history, even when logged in."
 
  78       get api_node_versions_path(node, :show_redactions => "true"), :headers => bearer_authorization_header
 
  80       assert_response :success, "Redaction shouldn't have stopped history working."
 
  81       assert_dom "osm node[id='#{node.id}'][version='1']", 0,
 
  82                  "redacted node #{node.id} version 1 shouldn't be present in the history, even when logged in and passing flag."
 
  85     def test_index_redacted_moderator
 
  86       node = create(:node, :with_history, :version => 2)
 
  87       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
 
  88       auth_header = bearer_authorization_header create(:moderator_user)
 
  90       get api_node_versions_path(node), :headers => auth_header
 
  92       assert_response :success, "Redaction shouldn't have stopped history working."
 
  93       assert_dom "osm node[id='#{node.id}'][version='1']", 0,
 
  94                  "node #{node.id} version 1 should not be present in the history for moderators when not passing flag."
 
  96       get api_node_versions_path(node, :show_redactions => "true"), :headers => auth_header
 
  98       assert_response :success, "Redaction shouldn't have stopped history working."
 
  99       assert_dom "osm node[id='#{node.id}'][version='1']", 1,
 
 100                  "node #{node.id} version 1 should still be present in the history for moderators when passing flag."
 
 104       node = create(:node, :version => 2)
 
 105       create(:old_node, :node_id => node.id, :version => 1, :latitude => 60 * OldNode::SCALE, :longitude => 30 * OldNode::SCALE)
 
 106       create(:old_node, :node_id => node.id, :version => 2, :latitude => 61 * OldNode::SCALE, :longitude => 31 * OldNode::SCALE)
 
 108       get api_node_version_path(node, 1)
 
 110       assert_response :success
 
 111       assert_dom "osm:root", 1 do
 
 112         assert_dom "> node", 1 do
 
 113           assert_dom "> @id", node.id.to_s
 
 114           assert_dom "> @version", "1"
 
 115           assert_dom "> @lat", "60.0000000"
 
 116           assert_dom "> @lon", "30.0000000"
 
 120       get api_node_version_path(node, 2)
 
 122       assert_response :success
 
 123       assert_dom "osm:root", 1 do
 
 124         assert_dom "> node", 1 do
 
 125           assert_dom "> @id", node.id.to_s
 
 126           assert_dom "> @version", "2"
 
 127           assert_dom "> @lat", "61.0000000"
 
 128           assert_dom "> @lon", "31.0000000"
 
 133     def test_show_not_found
 
 134       check_not_found_id_version(70000, 312344)
 
 135       check_not_found_id_version(-1, -13)
 
 136       check_not_found_id_version(create(:node).id, 24354)
 
 137       check_not_found_id_version(24356, create(:node).version)
 
 141     # test that redacted nodes aren't visible, regardless of
 
 142     # authorisation except as moderator...
 
 143     def test_show_redacted_unauthorised
 
 144       node = create(:node, :with_history, :version => 2)
 
 145       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
 
 147       get api_node_version_path(node, 1)
 
 149       assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
 
 151       get api_node_version_path(node, 1, :show_redactions => "true")
 
 153       assert_response :forbidden, "Redacted node shouldn't be visible via the version API when passing flag."
 
 156     def test_show_redacted_normal_user
 
 157       node = create(:node, :with_history, :version => 2)
 
 158       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
 
 160       get api_node_version_path(node, 1), :headers => bearer_authorization_header
 
 162       assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
 
 164       get api_node_version_path(node, 1, :show_redactions => "true"), :headers => bearer_authorization_header
 
 166       assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in and passing flag."
 
 169     def test_show_redacted_moderator
 
 170       node = create(:node, :with_history, :version => 2)
 
 171       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
 
 172       auth_header = bearer_authorization_header create(:moderator_user)
 
 174       get api_node_version_path(node, 1), :headers => auth_header
 
 176       assert_response :forbidden, "Redacted node should be gone for moderator, when flag not passed."
 
 178       get api_node_version_path(node, 1, :show_redactions => "true"), :headers => auth_header
 
 180       assert_response :success, "Redacted node should not be gone for moderator, when flag passed."
 
 183     # Ensure the lat/lon is formatted as a decimal e.g. not 4.0e-05
 
 184     def test_lat_lon_xml_format
 
 185       old_node = create(:old_node, :latitude => (0.00004 * OldNode::SCALE).to_i, :longitude => (0.00008 * OldNode::SCALE).to_i)
 
 187       get api_node_versions_path(old_node.node_id)
 
 188       assert_match(/lat="0.0000400"/, response.body)
 
 189       assert_match(/lon="0.0000800"/, response.body)
 
 194     def check_not_found_id_version(id, version)
 
 195       get api_node_version_path(id, version)
 
 196       assert_response :not_found
 
 197     rescue ActionController::UrlGenerationError => e
 
 198       assert_match(/No route matches/, e.to_s)