towerops/test/towerops_web/live/org_live_test.exs
Graham McIntire c8237cbcdc
fix: update tests for alert_type string migration and LiveView changes
- Add normalize_alert_type helpers for backward compatibility
- Update alert_type assertions from atoms to strings
- Fix org selection test to use form submission instead of phx-click
- Skip brute force protection test (feature disabled)
- Normalize alert_type in create_alert for atom inputs

Fixes test failures from alert schema changes.
2026-03-05 09:48:07 -06:00

131 lines
3.8 KiB
Elixir

defmodule ToweropsWeb.OrgLiveTest do
use ToweropsWeb.ConnCase
import Phoenix.LiveViewTest
setup :register_and_log_in_user
describe "Index" do
test "lists all user organizations", %{conn: conn, user: user} do
{:ok, organization} =
Towerops.Organizations.create_organization(%{name: "Test Org"}, user.id)
{:ok, _view, html} = live(conn, ~p"/orgs")
assert html =~ "Organizations"
assert html =~ organization.name
end
test "displays empty state when user has no organizations", %{conn: conn} do
{:ok, _view, html} = live(conn, ~p"/orgs")
assert html =~ "No organizations"
end
test "has link to create new organization", %{conn: conn} do
{:ok, _view, html} = live(conn, ~p"/orgs")
assert html =~ "New Organization"
end
test "navigates to organization dashboard when clicking organization card", %{
conn: conn,
user: user
} do
{:ok, organization} =
Towerops.Organizations.create_organization(%{name: "Test Org"}, user.id)
{:ok, view, html} = live(conn, ~p"/orgs")
# Verify organization card is rendered
assert html =~ "Test Org"
# The organization cards use form submission (POST to /orgs/switch) rather than LiveView events
# Submit the form to switch organizations
{:ok, _, html} =
view
|> form("form[action='/orgs/switch']", %{org_id: organization.id})
|> render_submit()
|> follow_redirect(conn, ~p"/dashboard")
assert html =~ organization.name
end
test "requires authentication" do
conn = build_conn()
{:error, redirect} = live(conn, ~p"/orgs")
assert {:redirect, %{to: path}} = redirect
assert path == ~p"/users/log-in"
end
end
describe "New" do
test "renders new organization form", %{conn: conn} do
{:ok, _view, html} = live(conn, ~p"/orgs/new")
assert html =~ "New Organization"
end
test "creates new organization", %{conn: conn, user: user} do
{:ok, view, _html} = live(conn, ~p"/orgs/new")
view
|> form("#organization-form",
organization: %{
name: "New Organization"
}
)
|> render_submit()
# Verify organization was created
organizations = Towerops.Organizations.list_user_organizations(user.id)
assert Enum.any?(organizations, &(&1.name == "New Organization"))
end
test "validates required fields", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/orgs/new")
html =
view
|> form("#organization-form", organization: %{name: ""})
|> render_submit()
assert html =~ "can't be blank"
end
test "allows duplicate organization names with different slugs", %{conn: conn, user: user} do
# Make user a superuser to bypass free org limits
user = Towerops.Repo.update!(Ecto.Changeset.change(user, is_superuser: true))
{:ok, _organization} =
Towerops.Organizations.create_organization(%{name: "Existing Org"}, user.id, bypass_limits: true)
# Need to reconnect with updated user
conn = log_in_user(conn, user)
{:ok, view, _html} = live(conn, ~p"/orgs/new")
view
|> form("#organization-form",
organization: %{
name: "Existing Org"
}
)
|> render_submit()
# Verify both organizations exist with same name but different slugs
organizations = Towerops.Organizations.list_user_organizations(user.id)
existing_orgs = Enum.filter(organizations, &(&1.name == "Existing Org"))
assert length(existing_orgs) == 2
assert length(Enum.uniq_by(existing_orgs, & &1.slug)) == 2
end
test "requires authentication" do
conn = build_conn()
{:error, redirect} = live(conn, ~p"/orgs/new")
assert {:redirect, %{to: path}} = redirect
assert path == ~p"/users/log-in"
end
end
end