require "application_system_test_case"

class ElementOldVersionTest < ApplicationSystemTestCase
  test "shows a node with one version" do
    node = create(:node, :with_history, :lat => 60, :lon => 30)

    visit old_node_path(node, 1)

    within_sidebar do
      assert_css "h2", :text => "Node: #{node.id}"
      assert_css "h4", :text => "Version #1"
      assert_text(/Location: 60\.\d+, 30\.\d+/)

      assert_link "Download XML", :href => api_node_version_path(node, 1)
      assert_link "View Details", :href => node_path(node)
      assert_link "View History", :href => node_history_path(node)
    end
  end

  test "shows a node with two versions" do
    node = create(:node, :with_history, :version => 2, :lat => 60, :lon => 30)
    node.old_nodes.find_by(:version => 1).update(:lat => 59, :lon => 29)

    visit old_node_path(node, 1)

    within_sidebar do
      assert_css "h2", :text => "Node: #{node.id}"
      assert_css "h4", :text => "Version #1"
      assert_text(/Location: 59\.\d+, 29\.\d+/)

      assert_link "Download XML", :href => api_node_version_path(node, 1)
      assert_link "View Details", :href => node_path(node)
      assert_link "View History", :href => node_history_path(node)

      click_on "Version #2"

      assert_css "h2", :text => "Node: #{node.id}"
      assert_css "h4", :text => "Version #2"
      assert_text(/Location: 60\.\d+, 30\.\d+/)

      assert_link "Download XML", :href => api_node_version_path(node, 2)
      assert_link "View Details", :href => node_path(node)
      assert_link "View History", :href => node_history_path(node)
    end
  end

  test "show a redacted node version" do
    node = create_redacted_node

    visit old_node_path(node, 1)

    within_sidebar do
      assert_css "h2", :text => "Node: #{node.id}"
      assert_text "Version 1 of this node cannot be shown"
      assert_no_text "Location"
      assert_no_text "TOP SECRET"

      assert_no_link "Download XML"
      assert_no_link "View Redacted Data"
      assert_link "View Details", :href => node_path(node)
      assert_link "View History", :href => node_history_path(node)
    end
  end

  test "show a redacted node version to a regular user" do
    node = create_redacted_node

    sign_in_as(create(:user))
    visit old_node_path(node, 1)

    within_sidebar do
      assert_css "h2", :text => "Node: #{node.id}"
      assert_text "Version 1 of this node cannot be shown"
      assert_no_text "Location"
      assert_no_text "TOP SECRET"

      assert_no_link "Download XML"
      assert_no_link "View Redacted Data"
      assert_link "View Details", :href => node_path(node)
      assert_link "View History", :href => node_history_path(node)
    end
  end

  test "show a redacted node version to a moderator" do
    node = create_redacted_node

    sign_in_as(create(:moderator_user))
    visit old_node_path(node, 1)

    within_sidebar do
      assert_css "h2", :text => "Node: #{node.id}"
      assert_text "Version 1 of this node cannot be shown"
      assert_no_text "Location"
      assert_no_text "TOP SECRET"

      assert_no_link "Download XML"
      assert_link "View Redacted Data"
      assert_no_link "View Redaction Message"
      assert_link "View Details", :href => node_path(node)
      assert_link "View History", :href => node_history_path(node)

      click_on "View Redacted Data"

      assert_css "h2", :text => "Node: #{node.id}"
      assert_css "h4", :text => "Redacted Version #1"
      assert_text(/Location: 59\.\d+, 29\.\d+/)
      assert_text "TOP SECRET"

      assert_no_link "Download XML"
      assert_no_link "View Redacted Data"
      assert_link "View Redaction Message"
      assert_link "View Details", :href => node_path(node)
      assert_link "View History", :href => node_history_path(node)

      click_on "View Redaction Message"

      assert_text "Version 1 of this node cannot be shown"
    end
  end

  private

  def create_redacted_node
    create(:node, :with_history, :version => 2, :lat => 60, :lon => 30) do |node|
      node_v1 = node.old_nodes.find_by(:version => 1)
      node_v1.update(:lat => 59, :lon => 29)
      create(:old_node_tag, :old_node => node_v1, :k => "name", :v => "TOP SECRET")
      node_v1.redact!(create(:redaction))
    end
  end
end
