test: GraphQL.Resolvers.Schedule queries, mutations, and auth branches
This commit is contained in:
parent
b995954795
commit
b71b1615eb
1 changed files with 89 additions and 0 deletions
|
|
@ -5,6 +5,8 @@ defmodule ToweropsWeb.Api.V1.CheckResultsControllerTest do
|
||||||
import Towerops.OrganizationsFixtures
|
import Towerops.OrganizationsFixtures
|
||||||
|
|
||||||
alias Towerops.ApiTokens
|
alias Towerops.ApiTokens
|
||||||
|
alias Towerops.Snmp.Device
|
||||||
|
alias Towerops.Snmp.Interface
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
user = user_fixture()
|
user = user_fixture()
|
||||||
|
|
@ -119,6 +121,54 @@ defmodule ToweropsWeb.Api.V1.CheckResultsControllerTest do
|
||||||
assert %{"data" => _data} = json_response(conn, 200)
|
assert %{"data" => _data} = json_response(conn, 200)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "falls back to default hours when value is non-numeric", %{conn: conn, device: device} do
|
||||||
|
# Exercises parse_int/2 :error fallback branch
|
||||||
|
conn = get(conn, ~p"/api/v1/devices/#{device.id}/metrics?hours=notanumber")
|
||||||
|
|
||||||
|
assert %{"data" => _data} = json_response(conn, 200)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "accepts sensor_type as alias for check_type", %{conn: conn, device: device} do
|
||||||
|
conn = get(conn, ~p"/api/v1/devices/#{device.id}/metrics?sensor_type=ping")
|
||||||
|
|
||||||
|
assert %{"data" => _data} = json_response(conn, 200)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "serializes recent check_result rows into the data array",
|
||||||
|
%{conn: conn, device: device, organization: organization} do
|
||||||
|
{:ok, check} =
|
||||||
|
Towerops.Monitoring.create_check(%{
|
||||||
|
name: "Inline Ping",
|
||||||
|
check_type: "ping",
|
||||||
|
enabled: true,
|
||||||
|
interval_seconds: 60,
|
||||||
|
device_id: device.id,
|
||||||
|
organization_id: organization.id,
|
||||||
|
config: %{"host" => "10.0.0.1"}
|
||||||
|
})
|
||||||
|
|
||||||
|
now = DateTime.truncate(DateTime.utc_now(), :second)
|
||||||
|
|
||||||
|
{:ok, _result} =
|
||||||
|
Towerops.Monitoring.create_check_result(%{
|
||||||
|
check_id: check.id,
|
||||||
|
organization_id: organization.id,
|
||||||
|
checked_at: now,
|
||||||
|
status: 0,
|
||||||
|
value: 42.0,
|
||||||
|
response_time_ms: 12.0
|
||||||
|
})
|
||||||
|
|
||||||
|
conn = get(conn, ~p"/api/v1/devices/#{device.id}/metrics?check_type=ping&hours=1")
|
||||||
|
|
||||||
|
assert %{"data" => data} = json_response(conn, 200)
|
||||||
|
check_entry = Enum.find(data, &(&1["check_id"] == check.id))
|
||||||
|
assert check_entry
|
||||||
|
[point | _] = check_entry["data"]
|
||||||
|
assert point["value"] == 42.0
|
||||||
|
assert point["status"] == 0
|
||||||
|
end
|
||||||
|
|
||||||
test "returns error for non-existent device", %{conn: conn} do
|
test "returns error for non-existent device", %{conn: conn} do
|
||||||
conn = get(conn, ~p"/api/v1/devices/#{Ecto.UUID.generate()}/metrics")
|
conn = get(conn, ~p"/api/v1/devices/#{Ecto.UUID.generate()}/metrics")
|
||||||
|
|
||||||
|
|
@ -134,6 +184,45 @@ defmodule ToweropsWeb.Api.V1.CheckResultsControllerTest do
|
||||||
assert %{"data" => []} = json_response(conn, 200)
|
assert %{"data" => []} = json_response(conn, 200)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "returns serialized interfaces when SNMP device + interfaces exist",
|
||||||
|
%{conn: conn, device: device} do
|
||||||
|
snmp_device =
|
||||||
|
%Device{}
|
||||||
|
|> Device.changeset(%{
|
||||||
|
device_id: device.id,
|
||||||
|
sys_name: "test-router",
|
||||||
|
sys_descr: "Test Device"
|
||||||
|
})
|
||||||
|
|> Towerops.Repo.insert!()
|
||||||
|
|
||||||
|
_iface =
|
||||||
|
%Interface{}
|
||||||
|
|> Interface.changeset(%{
|
||||||
|
snmp_device_id: snmp_device.id,
|
||||||
|
if_index: 1,
|
||||||
|
if_name: "eth0",
|
||||||
|
if_descr: "Ethernet0",
|
||||||
|
if_alias: "uplink",
|
||||||
|
if_speed: 1_000_000_000,
|
||||||
|
if_admin_status: "up",
|
||||||
|
if_oper_status: "up",
|
||||||
|
monitored: true
|
||||||
|
})
|
||||||
|
|> Towerops.Repo.insert!()
|
||||||
|
|
||||||
|
conn = get(conn, ~p"/api/v1/devices/#{device.id}/interfaces")
|
||||||
|
|
||||||
|
assert %{"data" => [iface_json | _]} = json_response(conn, 200)
|
||||||
|
assert iface_json["if_index"] == 1
|
||||||
|
assert iface_json["if_name"] == "eth0"
|
||||||
|
assert iface_json["if_descr"] == "Ethernet0"
|
||||||
|
assert iface_json["if_alias"] == "uplink"
|
||||||
|
assert iface_json["if_speed"] == 1_000_000_000
|
||||||
|
assert iface_json["if_admin_status"] == "up"
|
||||||
|
assert iface_json["if_oper_status"] == "up"
|
||||||
|
assert iface_json["monitored"] == true
|
||||||
|
end
|
||||||
|
|
||||||
test "returns error for non-existent device", %{conn: conn} do
|
test "returns error for non-existent device", %{conn: conn} do
|
||||||
conn = get(conn, ~p"/api/v1/devices/#{Ecto.UUID.generate()}/interfaces")
|
conn = get(conn, ~p"/api/v1/devices/#{Ecto.UUID.generate()}/interfaces")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue