towerops/test/towerops_web/live/agent_live_test.exs

88 lines
2.6 KiB
Elixir

defmodule ToweropsWeb.AgentLiveTest do
use ToweropsWeb.ConnCase
import Phoenix.LiveViewTest
alias Towerops.Agents
setup :register_and_log_in_user
setup %{user: user} do
{:ok, organization} = Towerops.Organizations.create_organization(%{name: "Test Org"}, user.id)
%{organization: organization}
end
describe "Index" do
test "lists all agent tokens", %{conn: conn, organization: organization} do
{:ok, _agent_token, _token} = Agents.create_agent_token(organization.id, "Agent 1")
{:ok, _view, html} = live(conn, ~p"/orgs/#{organization.slug}/agents")
assert html =~ "Remote Agents"
assert html =~ "Agent 1"
end
test "displays empty state when no agents", %{conn: conn, organization: organization} do
{:ok, _view, html} = live(conn, ~p"/orgs/#{organization.slug}/agents")
assert html =~ "No agents"
assert html =~ "Get started by creating your first remote agent"
end
test "creates new agent and shows token modal", %{conn: conn, organization: organization} do
{:ok, view, _html} = live(conn, ~p"/orgs/#{organization.slug}/agents")
html =
view
|> form("#new-agent-form form", %{name: "Test Agent"})
|> render_submit()
assert html =~ "Agent Created Successfully"
assert html =~ "Test Agent"
assert html =~ "Copy this token now"
end
test "revokes agent", %{conn: conn, organization: organization} do
{:ok, agent_token, _token} = Agents.create_agent_token(organization.id, "Agent 1")
{:ok, view, _html} = live(conn, ~p"/orgs/#{organization.slug}/agents")
html =
view
|> element("button", "Revoke")
|> render_click()
assert html =~ "Revoked"
# Verify agent was actually revoked
updated_token = Towerops.Repo.get!(Agents.AgentToken, agent_token.id)
refute updated_token.enabled
end
test "closes token modal", %{conn: conn, organization: organization} do
{:ok, view, _html} = live(conn, ~p"/orgs/#{organization.slug}/agents")
# Create agent to show modal
view
|> form("#new-agent-form form", %{name: "Test Agent"})
|> render_submit()
# Close modal
html =
view
|> element("button", "I've Saved the Token")
|> render_click()
refute html =~ "Agent Created Successfully"
end
test "requires authentication", %{organization: organization} do
conn = build_conn()
{:error, redirect} = live(conn, ~p"/orgs/#{organization.slug}/agents")
assert {:redirect, %{to: path}} = redirect
assert path == ~p"/users/log-in"
end
end
end