2.6 KiB · text History 9c0fc16
defmodule LambentExWeb.InterfacesTest do
  use LambentExWeb.ConnCase

  import Phoenix.LiveViewTest

  alias LambentEx.Meta
  alias LambentEx.Scan.NMAP

  setup do
    on_exit(fn -> Meta.put_skipped_ifaces([]) end)
    :ok
  end

  test "the devices page links to the sweep page rather than carrying it", %{conn: conn} do
    {:ok, _live, html} = live(conn, "/cfg/devices")

    assert html =~ "/cfg/devices/interfaces"
    refute html =~ "toggle_iface"
  end

  test "the sweep page lists the interfaces worth choosing about", %{conn: conn} do
    {:ok, _live, html} = live(conn, "/cfg/devices/interfaces")

    assert html =~ "toggle_iface"
    assert html =~ "of #{length(NMAP.interfaces())} swept"

    for iface <- NMAP.interfaces() do
      assert html =~ iface.name
      # never the ones dropped by prefix
      refute String.starts_with?(iface.name, "docker")
      refute String.starts_with?(iface.name, "lo")
    end
  end

  test "unchecking one is remembered", %{conn: conn} do
    {:ok, live, _html} = live(conn, "/cfg/devices/interfaces")

    # an interface this machine does not have exercises the plumbing without
    # stopping a sweep the test host is actually running
    NMAP.set_enabled("test-iface1", false)
    NMAP.interfaces()
    assert render(live) =~ "toggle_iface"
    assert "test-iface1" in Meta.get_skipped_ifaces()

    NMAP.set_enabled("test-iface1", true)
    NMAP.interfaces()
    refute "test-iface1" in Meta.get_skipped_ifaces()
  end

  test "a real interface can be toggled from the page and comes back", %{conn: conn} do
    case NMAP.interfaces() do
      [] ->
        :ok

      [iface | _rest] ->
        {:ok, live, _html} = live(conn, "/cfg/devices/interfaces")

        live
        |> element(~s([phx-click="toggle_iface"][phx-value-name="#{iface.name}"]))
        |> render_click()

        refute NMAP.interfaces() |> Enum.find(&(&1.name == iface.name)) |> Map.get(:enabled)
        assert iface.name in Meta.get_skipped_ifaces()

        live
        |> element(~s([phx-click="toggle_iface"][phx-value-name="#{iface.name}"]))
        |> render_click()

        assert NMAP.interfaces() |> Enum.find(&(&1.name == iface.name)) |> Map.get(:enabled)
    end
  end

  test "the page follows a change made elsewhere", %{conn: conn} do
    {:ok, live, _html} = live(conn, "/cfg/devices/interfaces")

    Phoenix.PubSub.broadcast(
      LambentEx.PubSub,
      "scan-ifaces",
      {:interfaces, [%{name: "somewhere-else", addr: "10.9.9.1", enabled: false}]}
    )

    html = render(live)
    assert html =~ "somewhere-else"
    assert html =~ "10.9.9.1"
    assert html =~ "0 of 1 swept"
  end
end