proxMon/server/test/server_web/live/admin_hosts_live_test.exs

46 lines
1.3 KiB
Elixir

defmodule ServerWeb.AdminHostsLiveTest do
use ServerWeb.ConnCase, async: false
import Phoenix.LiveViewTest
alias Server.Hosts
defp auth(conn), do: Plug.Test.init_test_session(conn, %{authenticated: true})
test "lists hosts", %{conn: conn} do
{:ok, {_, _}} = Hosts.create_host("pve-01")
{:ok, _view, html} = live(auth(conn), "/admin/hosts")
assert html =~ "pve-01"
end
test "creates a new host and reveals the token", %{conn: conn} do
{:ok, view, _html} = live(auth(conn), "/admin/hosts")
html =
view
|> form("form[phx-submit=create]", host: %{name: "pve-new"})
|> render_submit()
assert html =~ "pve-new"
assert html =~ ~r/[A-Za-z0-9_\-]{40,}/
end
test "revokes token", %{conn: conn} do
{:ok, {host, _}} = Hosts.create_host("pve-01")
original_hash = host.token_hash
{:ok, view, _html} = live(auth(conn), "/admin/hosts")
_html = render_click(view, "rotate", %{"id" => to_string(host.id)})
reloaded = Server.Repo.reload!(host)
refute reloaded.token_hash == original_hash
end
test "deletes a host", %{conn: conn} do
{:ok, {host, _}} = Hosts.create_host("pve-gone")
{:ok, view, _html} = live(auth(conn), "/admin/hosts")
html = render_click(view, "delete", %{"id" => to_string(host.id)})
refute html =~ "pve-gone"
end
end