1 # frozen_string_literal: true
 
   6   class OldNodesControllerTest < ActionDispatch::IntegrationTest
 
   8     # test all routes which lead to this controller
 
  11         { :path => "/api/0.6/node/1/history", :method => :get },
 
  12         { :controller => "api/old_nodes", :action => "index", :node_id => "1" }
 
  15         { :path => "/api/0.6/node/1/history.json", :method => :get },
 
  16         { :controller => "api/old_nodes", :action => "index", :node_id => "1", :format => "json" }
 
  19         { :path => "/api/0.6/node/1/2", :method => :get },
 
  20         { :controller => "api/old_nodes", :action => "show", :node_id => "1", :version => "2" }
 
  23         { :path => "/api/0.6/node/1/2.json", :method => :get },
 
  24         { :controller => "api/old_nodes", :action => "show", :node_id => "1", :version => "2", :format => "json" }
 
  29       node = create(:node, :version => 2)
 
  30       create(:old_node, :node_id => node.id, :version => 1, :latitude => 60 * OldNode::SCALE, :longitude => 30 * OldNode::SCALE)
 
  31       create(:old_node, :node_id => node.id, :version => 2, :latitude => 61 * OldNode::SCALE, :longitude => 31 * OldNode::SCALE)
 
  33       get api_node_versions_path(node)
 
  35       assert_response :success
 
  36       assert_dom "osm:root", 1 do
 
  37         assert_dom "> node", 2 do |dom_nodes|
 
  38           assert_dom dom_nodes[0], "> @id", node.id.to_s
 
  39           assert_dom dom_nodes[0], "> @version", "1"
 
  40           assert_dom dom_nodes[0], "> @lat", "60.0000000"
 
  41           assert_dom dom_nodes[0], "> @lon", "30.0000000"
 
  43           assert_dom dom_nodes[1], "> @id", node.id.to_s
 
  44           assert_dom dom_nodes[1], "> @version", "2"
 
  45           assert_dom dom_nodes[1], "> @lat", "61.0000000"
 
  46           assert_dom dom_nodes[1], "> @lon", "31.0000000"
 
  52     # test that redacted nodes aren't visible in the history
 
  53     def test_index_redacted_unauthorised
 
  54       node = create(:node, :with_history, :version => 2)
 
  55       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
 
  57       get api_node_versions_path(node)
 
  59       assert_response :success, "Redaction shouldn't have stopped history working."
 
  60       assert_dom "osm node[id='#{node.id}'][version='1']", 0,
 
  61                  "redacted node #{node.id} version 1 shouldn't be present in the history."
 
  63       get api_node_versions_path(node, :show_redactions => "true")
 
  65       assert_response :success, "Redaction shouldn't have stopped history working."
 
  66       assert_dom "osm node[id='#{node.id}'][version='1']", 0,
 
  67                  "redacted node #{node.id} version 1 shouldn't be present in the history when passing flag."
 
  70     def test_index_redacted_normal_user
 
  71       node = create(:node, :with_history, :version => 2)
 
  72       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
 
  74       get api_node_versions_path(node), :headers => bearer_authorization_header
 
  76       assert_response :success, "Redaction shouldn't have stopped history working."
 
  77       assert_dom "osm node[id='#{node.id}'][version='1']", 0,
 
  78                  "redacted node #{node.id} version 1 shouldn't be present in the history, even when logged in."
 
  80       get api_node_versions_path(node, :show_redactions => "true"), :headers => bearer_authorization_header
 
  82       assert_response :success, "Redaction shouldn't have stopped history working."
 
  83       assert_dom "osm node[id='#{node.id}'][version='1']", 0,
 
  84                  "redacted node #{node.id} version 1 shouldn't be present in the history, even when logged in and passing flag."
 
  87     def test_index_redacted_moderator
 
  88       node = create(:node, :with_history, :version => 2)
 
  89       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
 
  90       auth_header = bearer_authorization_header create(:moderator_user)
 
  92       get api_node_versions_path(node), :headers => auth_header
 
  94       assert_response :success, "Redaction shouldn't have stopped history working."
 
  95       assert_dom "osm node[id='#{node.id}'][version='1']", 0,
 
  96                  "node #{node.id} version 1 should not be present in the history for moderators when not passing flag."
 
  98       get api_node_versions_path(node, :show_redactions => "true"), :headers => auth_header
 
 100       assert_response :success, "Redaction shouldn't have stopped history working."
 
 101       assert_dom "osm node[id='#{node.id}'][version='1']", 1,
 
 102                  "node #{node.id} version 1 should still be present in the history for moderators when passing flag."
 
 106       node = create(:node, :version => 2)
 
 107       create(:old_node, :node_id => node.id, :version => 1, :latitude => 60 * OldNode::SCALE, :longitude => 30 * OldNode::SCALE)
 
 108       create(:old_node, :node_id => node.id, :version => 2, :latitude => 61 * OldNode::SCALE, :longitude => 31 * OldNode::SCALE)
 
 110       get api_node_version_path(node, 1)
 
 112       assert_response :success
 
 113       assert_dom "osm:root", 1 do
 
 114         assert_dom "> node", 1 do
 
 115           assert_dom "> @id", node.id.to_s
 
 116           assert_dom "> @version", "1"
 
 117           assert_dom "> @lat", "60.0000000"
 
 118           assert_dom "> @lon", "30.0000000"
 
 122       get api_node_version_path(node, 2)
 
 124       assert_response :success
 
 125       assert_dom "osm:root", 1 do
 
 126         assert_dom "> node", 1 do
 
 127           assert_dom "> @id", node.id.to_s
 
 128           assert_dom "> @version", "2"
 
 129           assert_dom "> @lat", "61.0000000"
 
 130           assert_dom "> @lon", "31.0000000"
 
 135     def test_show_not_found
 
 136       check_not_found_id_version(70000, 312344)
 
 137       check_not_found_id_version(-1, -13)
 
 138       check_not_found_id_version(create(:node).id, 24354)
 
 139       check_not_found_id_version(24356, create(:node).version)
 
 143     # test that redacted nodes aren't visible, regardless of
 
 144     # authorisation except as moderator...
 
 145     def test_show_redacted_unauthorised
 
 146       node = create(:node, :with_history, :version => 2)
 
 147       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
 
 149       get api_node_version_path(node, 1)
 
 151       assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
 
 153       get api_node_version_path(node, 1, :show_redactions => "true")
 
 155       assert_response :forbidden, "Redacted node shouldn't be visible via the version API when passing flag."
 
 158     def test_show_redacted_normal_user
 
 159       node = create(:node, :with_history, :version => 2)
 
 160       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
 
 162       get api_node_version_path(node, 1), :headers => bearer_authorization_header
 
 164       assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
 
 166       get api_node_version_path(node, 1, :show_redactions => "true"), :headers => bearer_authorization_header
 
 168       assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in and passing flag."
 
 171     def test_show_redacted_moderator
 
 172       node = create(:node, :with_history, :version => 2)
 
 173       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
 
 174       auth_header = bearer_authorization_header create(:moderator_user)
 
 176       get api_node_version_path(node, 1), :headers => auth_header
 
 178       assert_response :forbidden, "Redacted node should be gone for moderator, when flag not passed."
 
 180       get api_node_version_path(node, 1, :show_redactions => "true"), :headers => auth_header
 
 182       assert_response :success, "Redacted node should not be gone for moderator, when flag passed."
 
 185     # Ensure the lat/lon is formatted as a decimal e.g. not 4.0e-05
 
 186     def test_lat_lon_xml_format
 
 187       old_node = create(:old_node, :latitude => (0.00004 * OldNode::SCALE).to_i, :longitude => (0.00008 * OldNode::SCALE).to_i)
 
 189       get api_node_versions_path(old_node.node_id)
 
 190       assert_match(/lat="0.0000400"/, response.body)
 
 191       assert_match(/lon="0.0000800"/, response.body)
 
 196     def check_not_found_id_version(id, version)
 
 197       get api_node_version_path(id, version)
 
 198       assert_response :not_found
 
 199     rescue ActionController::UrlGenerationError => e
 
 200       assert_match(/No route matches/, e.to_s)