towerops/test/towerops_web/controllers/api/v1/activity_controller_test.exs
Graham McIntire d1403c8069 Fix failing tests and clean up code
- Fix doctests for Accounts, Agents.Stats, Snmp to match actual behavior
- Fix dynamic_extra_test vendor post-processing tests to seed sensor data
- Fix activity_controller_test to seed devices for feed data
- Fix session_manager_test to create browser session for test
- Fix topology_test link creation for connection test
- Fix device_monitor/driver_worker tests for unique job constraints
- Fix accounts_test expired_tokens assertion (magic link token is expired)
- Fix happy_path_test and show_events_test to seed monitor data
- Fix admin user_live_test user.name -> user.email (no name field)
- Fix schema_test to seed activity data
- Fix mobile_qr_live_test to match actual template text
- Fix SnmpKit.MIB doctests and tests for enriched return values
- Fix onboarding_live, mobile_controller, mib_test weak assertions
- Remove dead code and fix credo warnings
2026-06-16 14:54:34 -05:00

88 lines
2.5 KiB
Elixir

defmodule ToweropsWeb.Api.V1.ActivityControllerTest do
use ToweropsWeb.ConnCase, async: true
import Towerops.AccountsFixtures
import Towerops.DevicesFixtures
import Towerops.OrganizationsFixtures
alias Towerops.ApiTokens
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"
})
# Seed a device so the activity feed has data
device_fixture(%{organization_id: organization.id})
conn =
build_conn()
|> put_req_header("authorization", "Bearer #{raw_token}")
|> put_req_header("accept", "application/json")
%{conn: conn, user: user, organization: organization}
end
describe "index/2" do
test "returns activity feed data", %{conn: conn} do
conn = get(conn, ~p"/api/v1/activity")
assert %{"data" => data} = json_response(conn, 200)
assert is_list(data) and data != []
end
test "respects limit parameter", %{conn: conn} do
conn = get(conn, ~p"/api/v1/activity?limit=5")
assert %{"data" => data} = json_response(conn, 200)
assert length(data) <= 5
end
test "handles invalid limit gracefully (uses default)", %{conn: conn} do
conn = get(conn, ~p"/api/v1/activity?limit=notanumber")
assert %{"data" => _data} = json_response(conn, 200)
end
test "filters by types parameter", %{conn: conn} do
conn = get(conn, ~p"/api/v1/activity?types=device_added")
assert %{"data" => data} = json_response(conn, 200)
assert is_list(data) and data != []
end
test "handles invalid types gracefully", %{conn: conn} do
conn = get(conn, ~p"/api/v1/activity?types=nonexistent_atom_xyz_12345")
assert %{"data" => data} = json_response(conn, 200)
assert data == []
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/activity")
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/activity")
assert %{"error" => _message} = json_response(conn, 401)
end
end
end