defmodule ToweropsWeb.AccountLive.MyDataTest do use ToweropsWeb.ConnCase import Phoenix.LiveViewTest import Towerops.AccountsFixtures import Towerops.OrganizationsFixtures alias Towerops.Accounts alias Towerops.Organizations setup :register_and_log_in_user_with_sudo describe "My Data page" do test "renders profile information", %{conn: conn, user: user} do {:ok, _view, html} = live(conn, ~p"/users/my-data") assert html =~ "My Data" assert html =~ "Profile Information" assert html =~ user.email end test "displays user name when set", %{conn: conn, user: user} do {:ok, _user} = Accounts.update_user_profile(user, %{first_name: "Jane", last_name: "Doe"}) {:ok, _view, html} = live(conn, ~p"/users/my-data") assert html =~ "Jane" assert html =~ "Doe" end test "shows consent records from registration", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/users/my-data") assert html =~ "Consent Records" # User fixture grants privacy_policy and terms_of_service consents assert html =~ "Privacy policy" assert html =~ "Terms of service" assert html =~ "Active" end test "shows revoke consent button for active consents", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/users/my-data") assert html =~ "Revoke Consent" end test "revoking consent updates the display", %{conn: conn, user: user} do consent = Accounts.get_active_consent(user.id, "privacy_policy") {:ok, view, _html} = live(conn, ~p"/users/my-data") html = view |> element(~s|button[phx-click=revoke_consent][phx-value-consent-id="#{consent.id}"]|) |> render_click() assert html =~ "Consent revoked successfully" assert html =~ "Revoked" end test "hides org/device/alert sections when user has no orgs", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/users/my-data") # User from fixture has no organizations, so owner-only sections are hidden refute html =~ "Organizations you own" refute html =~ "Network devices monitored" refute html =~ "Recent Alerts" end test "shows export data section with download link", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/users/my-data") assert html =~ "Export Your Data" assert html =~ "Download My Data (JSON)" assert html =~ "/api/v1/account/data" end test "shows data processing information", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/users/my-data") assert html =~ "How We Use Your Data" assert html =~ "Profile Information" assert html =~ "Email Address" assert html =~ "Privacy Policy" end test "shows navigation links to settings tabs", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/users/my-data") assert html =~ "Personal" assert html =~ "Account" assert html =~ "Sessions" assert html =~ "Security" end end describe "My Data page with organization data" do setup %{user: user} do {:ok, org} = Organizations.create_organization(%{name: "WISP Corp"}, user.id) {:ok, site} = Towerops.Sites.create_site(%{ name: "Tower Alpha", organization_id: org.id }) {:ok, device} = Towerops.Devices.create_device(%{ name: "MikroTik Router", ip_address: "10.0.0.1", site_id: site.id, organization_id: org.id, monitoring_enabled: true }) %{org: org, site: site, device: device} end test "shows organizations section for owners", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/users/my-data") assert html =~ "Organizations" assert html =~ "WISP Corp" assert html =~ "Owner" end test "shows devices section with device details", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/users/my-data") assert html =~ "Devices" assert html =~ "MikroTik Router" assert html =~ "10.0.0.1" assert html =~ "WISP Corp" end test "shows device count", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/users/my-data") assert html =~ "Total devices: 1" end test "shows alerts section with alert data", %{conn: conn, device: device} do {:ok, _alert} = Towerops.Alerts.create_alert(%{ device_id: device.id, alert_type: "device_down", triggered_at: DateTime.utc_now(), message: "Device unreachable" }) {:ok, _view, html} = live(conn, ~p"/users/my-data") assert html =~ "Recent Alerts" assert html =~ "device_down" assert html =~ "MikroTik Router" end test "shows correct alert status badges", %{conn: conn, device: device} do # Active alert {:ok, _} = Towerops.Alerts.create_alert(%{ device_id: device.id, alert_type: "device_down", triggered_at: DateTime.utc_now(), message: "Still down" }) # Resolved alert {:ok, _} = Towerops.Alerts.create_alert(%{ device_id: device.id, alert_type: "high_latency", triggered_at: DateTime.add(DateTime.utc_now(), -3600, :second), resolved_at: DateTime.utc_now(), message: "Latency normalized" }) {:ok, _view, html} = live(conn, ~p"/users/my-data") assert html =~ "Active" assert html =~ "Resolved" end test "shows no alerts message when none exist", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/users/my-data") assert html =~ "No alerts in the last 90 days" end end describe "My Data page as non-owner member" do setup %{user: user} do # Create org owned by someone else, add test user as viewer owner = user_fixture() org = organization_fixture(owner.id, %{name: "Other Org"}) membership_fixture(org.id, user.id, :viewer) %{org: org} end test "hides org/device/alert sections for non-owner members", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/users/my-data") # Non-owner members should not see org-level data refute html =~ "Organizations you own" refute html =~ "Network devices monitored" refute html =~ "Recent Alerts" end test "still shows profile and consent sections", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/users/my-data") assert html =~ "Profile Information" assert html =~ "Consent Records" assert html =~ "Export Your Data" end end describe "My Data page audit logs" do test "shows audit log section", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/users/my-data") assert html =~ "Audit Log" end test "shows audit log entries when they exist", %{conn: conn, user: user} do # The sudo_mode_granted event from setup creates an audit log {:ok, _} = Towerops.Admin.create_audit_log(%{ action: "user_data_viewed", superuser_id: user.id, target_user_id: user.id, metadata: %{} }) {:ok, _view, html} = live(conn, ~p"/users/my-data") assert html =~ "user_data_viewed" end end end