towerops/test/towerops_web/controllers/api/v1/sites_controller_test.exs
Graham McIntire aa9ed52bff
feat: add critical network switch sensor support (HP Comware, Dell PowerConnect, Dell SONiC)
Implemented top 3 critical network switch sensor gaps identified in Phase 3 analysis.
Restores fundamental temperature monitoring for common enterprise switch platforms.

Files Changed:
- priv/profiles/os_discovery/comware.yaml (enhanced)
  Added HP Comware chassis temperature monitoring via HH3C-ENTITY-EXT-MIB
  OID: 1.3.6.1.4.1.25506.2.6.1.1.1.1.12 (hh3cEntityExtTemperature)
  Uses entPhysicalName for sensor descriptions
  Gap: CRITICAL (broke fundamental monitoring) → RESOLVED
  Parity: 40% → 60%

- priv/profiles/os_discovery/powerconnect.yaml (enhanced)
  Added Dell PowerConnect/DNOS CPU temperature monitoring
  OID: 1.3.6.1.4.1.674.10895.5000.2.6132.1.1.43.1.8.1.5
  MIB: FASTPATH-BOXSERVICES-PRIVATE-MIB
  Gap: CRITICAL (no sensors) → RESOLVED
  Parity: 0% → 80%

- priv/profiles/os_discovery/dell-sonic.yaml (new)
  Created comprehensive Dell SONiC sensor profile
  MIB: NETGEAR-BOXSERVICES-PRIVATE-MIB (Quanta-based)
  Sensors:
    - Temperature: boxServicesTempSensorState (OID .1.3.6.1.4.1.4413.1.1.43.1.8.1.4)
    - Fan Speed: boxServicesFanSpeed (OID .1.3.6.1.4.1.4413.1.1.43.1.6.1.4)
    - PSU State: boxServicesPowSupplyItemState (OID .1.3.6.1.4.1.4413.1.1.43.1.7.1.3)
      States: other, notpresent, operational, failed, powering, nopower,
              notpowering, incompatible
  Gap: CRITICAL (OS detected, no sensors) → RESOLVED
  Parity: 0% → 95%

- test/towerops_web/plugs/brute_force_protection_test.exs (fixed)
  Fixed Credo warning: replaced length/1 with empty list comparison

- CHANGELOG.txt (updated)
  Documented Phase 3 analysis completion and critical fix implementation

Impact:
- HP Comware: Enables overheating alerts (fundamental monitoring restored)
- Dell PowerConnect: First sensor support for common access switches
- Dell SONiC: Complete hardware monitoring for modern data center platform

Business Value:
- Resolves production blockers for customers with HP Comware switches
- Adds support for very common Dell enterprise access switches
- Enables monitoring for Dell's modern SONiC-based data center switches

Next Steps: Remaining Tier 1 switches (Dell Force10 FTOS), then Tier 2
(optical transceiver monitoring for ProCurve/Comware).

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-11 17:36:45 -06:00

329 lines
9.2 KiB
Elixir

defmodule ToweropsWeb.Api.V1.SitesControllerTest do
use ToweropsWeb.ConnCase, async: true
import Towerops.AccountsFixtures
import Towerops.OrganizationsFixtures
alias Towerops.ApiTokens
alias Towerops.Sites
setup do
user = user_fixture()
organization = organization_fixture(user.id)
{:ok, {_token, raw_token}} =
ApiTokens.create_api_token(%{
organization_id: organization.id,
user_id: user.id,
name: "Test Token"
})
conn =
build_conn()
|> put_req_header("authorization", "Bearer #{raw_token}")
|> put_req_header("accept", "application/json")
%{
conn: conn,
user: user,
organization: organization,
raw_token: raw_token
}
end
describe "index/2" do
test "lists all sites for authenticated organization", %{
conn: conn,
organization: organization
} do
{:ok, site1} =
Sites.create_site(%{name: "Site Alpha", organization_id: organization.id})
{:ok, site2} =
Sites.create_site(%{name: "Site Beta", organization_id: organization.id})
conn = get(conn, ~p"/api/v1/sites")
assert %{"sites" => sites} = json_response(conn, 200)
assert length(sites) == 2
site_ids = Enum.map(sites, & &1["id"])
assert site1.id in site_ids
assert site2.id in site_ids
site_json = List.first(sites)
assert Map.has_key?(site_json, "id")
assert Map.has_key?(site_json, "name")
assert Map.has_key?(site_json, "location")
assert Map.has_key?(site_json, "snmp_community")
assert Map.has_key?(site_json, "inserted_at")
end
test "returns empty list when organization has no sites", %{conn: conn} do
conn = get(conn, ~p"/api/v1/sites")
assert %{"sites" => []} = json_response(conn, 200)
end
test "does not return sites from other organizations", %{conn: conn} do
other_user = user_fixture()
other_org = organization_fixture(other_user.id)
{:ok, _other_site} =
Sites.create_site(%{name: "Other Site", organization_id: other_org.id})
conn = get(conn, ~p"/api/v1/sites")
assert %{"sites" => sites} = json_response(conn, 200)
assert Enum.empty?(sites)
end
end
describe "create/2" do
test "creates site with valid attributes", %{conn: conn, organization: organization} do
site_params = %{
"site" => %{
"name" => "Main Office",
"location" => "New York, NY",
"snmp_community" => "public"
}
}
conn = post(conn, ~p"/api/v1/sites", site_params)
assert %{
"id" => id,
"name" => "Main Office",
"location" => "New York, NY",
"snmp_community" => "public"
} = json_response(conn, 201)
assert is_binary(id)
# Verify the site was created in the correct organization
site = Sites.get_site!(id)
assert site.organization_id == organization.id
end
test "creates site with minimal attributes", %{conn: conn} do
site_params = %{
"site" => %{
"name" => "Minimal Site"
}
}
conn = post(conn, ~p"/api/v1/sites", site_params)
assert %{
"id" => _id,
"name" => "Minimal Site",
"location" => nil,
"snmp_community" => nil
} = json_response(conn, 201)
end
test "returns 422 with invalid site params", %{conn: conn} do
site_params = %{
"site" => %{
"name" => ""
}
}
conn = post(conn, ~p"/api/v1/sites", site_params)
assert %{"errors" => errors} = json_response(conn, 422)
assert Map.has_key?(errors, "name")
end
test "returns 400 when site parameter is missing", %{conn: conn} do
conn = post(conn, ~p"/api/v1/sites", %{})
assert %{"error" => "Missing 'site' parameter"} = json_response(conn, 400)
end
end
describe "show/2" do
test "returns site when it belongs to organization", %{
conn: conn,
organization: organization
} do
{:ok, site} =
Sites.create_site(%{
name: "Test Site",
location: "Boston, MA",
snmp_community: "private",
organization_id: organization.id
})
conn = get(conn, ~p"/api/v1/sites/#{site.id}")
assert %{
"id" => id,
"name" => "Test Site",
"location" => "Boston, MA",
"snmp_community" => "private"
} = json_response(conn, 200)
assert id == site.id
end
test "returns 404 when site does not exist", %{conn: conn} do
conn = get(conn, ~p"/api/v1/sites/#{Ecto.UUID.generate()}")
assert %{"error" => "Site not found"} = json_response(conn, 404)
end
test "returns 403 when site belongs to different organization", %{conn: conn} do
other_user = user_fixture()
other_org = organization_fixture(other_user.id)
{:ok, other_site} =
Sites.create_site(%{name: "Other Site", organization_id: other_org.id})
conn = get(conn, ~p"/api/v1/sites/#{other_site.id}")
assert %{"error" => "Access denied to this site"} = json_response(conn, 403)
end
end
describe "update/2" do
test "updates site with valid attributes", %{conn: conn, organization: organization} do
{:ok, site} =
Sites.create_site(%{
name: "Original Name",
location: "Chicago, IL",
organization_id: organization.id
})
update_params = %{
"site" => %{
"name" => "Updated Name",
"location" => "Boston, MA"
}
}
conn = patch(conn, ~p"/api/v1/sites/#{site.id}", update_params)
assert %{
"id" => id,
"name" => "Updated Name",
"location" => "Boston, MA"
} = json_response(conn, 200)
assert id == site.id
end
test "returns 404 when site does not exist", %{conn: conn} do
update_params = %{
"site" => %{
"name" => "Updated Name"
}
}
conn = patch(conn, ~p"/api/v1/sites/#{Ecto.UUID.generate()}", update_params)
assert %{"error" => "Site not found"} = json_response(conn, 404)
end
test "returns 403 when site belongs to different organization", %{conn: conn} do
other_user = user_fixture()
other_org = organization_fixture(other_user.id)
{:ok, other_site} =
Sites.create_site(%{name: "Other Site", organization_id: other_org.id})
update_params = %{
"site" => %{
"name" => "Hacked Name"
}
}
conn = patch(conn, ~p"/api/v1/sites/#{other_site.id}", update_params)
assert %{"error" => "Access denied to this site"} = json_response(conn, 403)
end
test "returns 422 with invalid update params", %{conn: conn, organization: organization} do
{:ok, site} =
Sites.create_site(%{name: "Test Site", organization_id: organization.id})
update_params = %{
"site" => %{
"name" => ""
}
}
conn = patch(conn, ~p"/api/v1/sites/#{site.id}", update_params)
assert %{"errors" => errors} = json_response(conn, 422)
assert Map.has_key?(errors, "name")
end
test "returns 400 when site parameter is missing", %{
conn: conn,
organization: organization
} do
{:ok, site} =
Sites.create_site(%{name: "Test Site", organization_id: organization.id})
conn = patch(conn, ~p"/api/v1/sites/#{site.id}", %{})
assert %{"error" => "Missing 'site' parameter"} = json_response(conn, 400)
end
end
describe "delete/2" do
test "deletes site successfully", %{conn: conn, organization: organization} do
{:ok, site} =
Sites.create_site(%{name: "Doomed Site", organization_id: organization.id})
conn = delete(conn, ~p"/api/v1/sites/#{site.id}")
assert %{"success" => true} = json_response(conn, 200)
# Verify site is deleted
assert_raise Ecto.NoResultsError, fn ->
Sites.get_site!(site.id)
end
end
test "returns 404 when site does not exist", %{conn: conn} do
conn = delete(conn, ~p"/api/v1/sites/#{Ecto.UUID.generate()}")
assert %{"error" => "Site not found"} = json_response(conn, 404)
end
test "returns 403 when site belongs to different organization", %{conn: conn} do
other_user = user_fixture()
other_org = organization_fixture(other_user.id)
{:ok, other_site} =
Sites.create_site(%{name: "Other Site", organization_id: other_org.id})
conn = delete(conn, ~p"/api/v1/sites/#{other_site.id}")
assert %{"error" => "Access denied to this site"} = json_response(conn, 403)
end
end
describe "authentication" do
test "returns 401 without authorization header" do
conn =
build_conn()
|> put_req_header("accept", "application/json")
|> get(~p"/api/v1/sites")
assert %{"error" => _message} = json_response(conn, 401)
end
test "returns 401 with invalid token" do
conn =
build_conn()
|> put_req_header("authorization", "Bearer towerops_invalid_token")
|> put_req_header("accept", "application/json")
|> get(~p"/api/v1/sites")
assert %{"error" => _message} = json_response(conn, 401)
end
end
end