test: Org.IntegrationsLive interval helpers, exclusive toggle, and validation branches

This commit is contained in:
Graham McIntire 2026-05-09 12:38:06 -05:00
parent eccdfe8e5e
commit f87dbcf933

View file

@ -463,6 +463,125 @@ defmodule ToweropsWeb.Org.IntegrationsLiveTest do
assert html =~ "Infrastructure"
end
test "renders next_sync_minutes and humanize_interval for integration with last_synced_at",
%{conn: conn, user: user, organization: org} do
now = DateTime.truncate(DateTime.utc_now(), :second)
{:ok, _} =
Towerops.Integrations.create_integration(org.id, %{
provider: "preseem",
enabled: true,
credentials: %{"api_key" => "k"},
last_sync_status: "success",
last_synced_at: now,
sync_interval_minutes: 90
})
{:ok, _view, html} =
conn
|> log_in_user(user)
|> live(~p"/orgs/#{org.slug}/settings/integrations")
# humanize_interval(90) -> "1h 30min"
assert html =~ "1h 30min"
# Next sync should appear
assert html =~ "Next in" or html =~ "Syncs every"
end
test "renders humanize_interval at exact hour boundary",
%{conn: conn, user: user, organization: org} do
{:ok, _} =
Towerops.Integrations.create_integration(org.id, %{
provider: "preseem",
enabled: true,
credentials: %{"api_key" => "k"},
last_sync_status: "success",
last_synced_at: DateTime.truncate(DateTime.utc_now(), :second),
sync_interval_minutes: 120
})
{:ok, _view, html} =
conn
|> log_in_user(user)
|> live(~p"/orgs/#{org.slug}/settings/integrations")
# humanize_interval(120) -> "2h"
assert html =~ "2h"
end
test "renders humanize_interval below hour", %{conn: conn, user: user, organization: org} do
{:ok, _} =
Towerops.Integrations.create_integration(org.id, %{
provider: "preseem",
enabled: true,
credentials: %{"api_key" => "k"},
last_sync_status: "success",
last_synced_at: DateTime.truncate(DateTime.utc_now(), :second),
sync_interval_minutes: 30
})
{:ok, _view, html} =
conn
|> log_in_user(user)
|> live(~p"/orgs/#{org.slug}/settings/integrations")
assert html =~ "30min"
end
test "test_connection without filling required fields shows error for cn_maestro",
%{conn: conn, user: user, organization: org} do
{:ok, view, _html} =
conn
|> log_in_user(user)
|> live(~p"/orgs/#{org.slug}/settings/integrations")
view |> element("#configure-cn_maestro") |> render_click()
html = render_hook(view, "test_connection", %{})
assert html =~ "Please fill in all required fields first"
end
test "test_connection without filling required fields shows error for uisp",
%{conn: conn, user: user, organization: org} do
{:ok, view, _html} =
conn
|> log_in_user(user)
|> live(~p"/orgs/#{org.slug}/settings/integrations")
view |> element("#configure-uisp") |> render_click()
html = render_hook(view, "test_connection", %{})
assert html =~ "Please fill in all required fields first"
end
test "toggle_enabled is rejected when an exclusive integration is already active",
%{conn: conn, user: user, organization: org} do
# Sonar (billing exclusive) is enabled
{:ok, _sonar} =
Towerops.Integrations.create_integration(org.id, %{
provider: "sonar",
enabled: true,
credentials: %{"instance_url" => "https://x", "api_token" => "t"}
})
# Splynx (also billing exclusive) is disabled
{:ok, _splynx} =
Towerops.Integrations.create_integration(org.id, %{
provider: "splynx",
enabled: false,
credentials: %{"instance_url" => "https://y", "api_key" => "k", "api_secret" => "s"}
})
{:ok, view, _html} =
conn
|> log_in_user(user)
|> live(~p"/orgs/#{org.slug}/settings/integrations")
html = render_hook(view, "toggle_enabled", %{"provider" => "splynx"})
# Should hit the exclusive_conflict? branch
assert html =~ "Only one" or html =~ "Disable"
end
test "clears sync status when credentials change", %{conn: conn, user: user, organization: org} do
{:ok, _} =
Towerops.Integrations.create_integration(org.id, %{