146 lines
3.8 KiB
Elixir
146 lines
3.8 KiB
Elixir
defmodule Towerops.StatusPagesTest do
|
|
use Towerops.DataCase, async: false
|
|
|
|
alias Towerops.Organizations.Organization
|
|
alias Towerops.StatusPages
|
|
|
|
describe "config" do
|
|
test "create and fetch by slug" do
|
|
org = insert_org()
|
|
|
|
{:ok, config} =
|
|
StatusPages.create_config(%{
|
|
slug: "test-isp",
|
|
company_name: "Test ISP",
|
|
organization_id: org.id,
|
|
enabled: true
|
|
})
|
|
|
|
assert config.slug == "test-isp"
|
|
assert config.company_name == "Test ISP"
|
|
|
|
found = StatusPages.get_config_by_slug("test-isp")
|
|
assert found.id == config.id
|
|
end
|
|
|
|
test "disabled config not returned by slug" do
|
|
org = insert_org()
|
|
|
|
{:ok, _} =
|
|
StatusPages.create_config(%{
|
|
slug: "disabled-isp",
|
|
organization_id: org.id,
|
|
enabled: false
|
|
})
|
|
|
|
assert StatusPages.get_config_by_slug("disabled-isp") == nil
|
|
end
|
|
|
|
test "slug uniqueness enforced" do
|
|
org1 = insert_org()
|
|
org2 = insert_org()
|
|
|
|
{:ok, _} =
|
|
StatusPages.create_config(%{slug: "unique-slug", organization_id: org1.id})
|
|
|
|
{:error, changeset} =
|
|
StatusPages.create_config(%{slug: "unique-slug", organization_id: org2.id})
|
|
|
|
assert errors_on(changeset).slug
|
|
end
|
|
end
|
|
|
|
describe "components" do
|
|
test "CRUD operations" do
|
|
org = insert_org()
|
|
{:ok, config} = StatusPages.create_config(%{slug: "comp-test", organization_id: org.id})
|
|
|
|
{:ok, component} =
|
|
StatusPages.create_component(%{
|
|
name: "Internet Service",
|
|
status: "operational",
|
|
status_page_config_id: config.id
|
|
})
|
|
|
|
assert component.name == "Internet Service"
|
|
|
|
{:ok, updated} = StatusPages.update_component(component, %{status: "degraded"})
|
|
assert updated.status == "degraded"
|
|
|
|
components = StatusPages.list_components(config.id)
|
|
assert length(components) == 1
|
|
|
|
{:ok, _} = StatusPages.delete_component(component)
|
|
assert StatusPages.list_components(config.id) == []
|
|
end
|
|
end
|
|
|
|
describe "incidents" do
|
|
test "create and list" do
|
|
org = insert_org()
|
|
{:ok, config} = StatusPages.create_config(%{slug: "inc-test", organization_id: org.id})
|
|
|
|
{:ok, incident} =
|
|
StatusPages.create_incident(%{
|
|
title: "Network Outage",
|
|
severity: "major",
|
|
status: "investigating",
|
|
started_at: Towerops.Time.now(),
|
|
status_page_config_id: config.id
|
|
})
|
|
|
|
assert incident.title == "Network Outage"
|
|
|
|
active = StatusPages.list_active_incidents(config.id)
|
|
assert length(active) == 1
|
|
|
|
{:ok, resolved} = StatusPages.resolve_incident(incident)
|
|
assert resolved.status == "resolved"
|
|
assert resolved.resolved_at
|
|
|
|
active_after = StatusPages.list_active_incidents(config.id)
|
|
assert active_after == []
|
|
end
|
|
end
|
|
|
|
describe "overall_status/1" do
|
|
test "operational when all components operational" do
|
|
components = [
|
|
%{status: "operational"},
|
|
%{status: "operational"}
|
|
]
|
|
|
|
assert StatusPages.overall_status(components) == :operational
|
|
end
|
|
|
|
test "major_outage takes priority" do
|
|
components = [
|
|
%{status: "operational"},
|
|
%{status: "major_outage"}
|
|
]
|
|
|
|
assert StatusPages.overall_status(components) == :major_outage
|
|
end
|
|
|
|
test "degraded when no outages" do
|
|
components = [
|
|
%{status: "operational"},
|
|
%{status: "degraded"}
|
|
]
|
|
|
|
assert StatusPages.overall_status(components) == :degraded
|
|
end
|
|
end
|
|
|
|
defp insert_org do
|
|
{:ok, org} =
|
|
%Organization{}
|
|
|> Organization.changeset(%{
|
|
name: "Test Org #{System.unique_integer([:positive])}",
|
|
slug: "test-org-#{System.unique_integer([:positive])}"
|
|
})
|
|
|> Towerops.Repo.insert()
|
|
|
|
org
|
|
end
|
|
end
|