diff --git a/test/towerops_web/live/help_live/index_test.exs b/test/towerops_web/live/help_live/index_test.exs index fc762616..ab01cdba 100644 --- a/test/towerops_web/live/help_live/index_test.exs +++ b/test/towerops_web/live/help_live/index_test.exs @@ -114,4 +114,49 @@ defmodule ToweropsWeb.HelpLive.IndexTest do assert html =~ "Remote Pollers" end end + + describe "generate_password event + random.org integration" do + alias ToweropsWeb.HelpLive.Index, as: HelpLive + + test "successful random.org response assigns generated_password", %{conn: conn} do + Req.Test.stub(HelpLive, fn c -> + Plug.Conn.send_resp(c, 200, "Abc123Def456Ghi789Jkl012\n") + end) + + {:ok, view, _html} = live(conn, ~p"/help") + _ = render_click(view, "generate_password", %{}) + + # Wait for the :fetch_random_password message to be processed + _ = Process.sleep(50) + html = render(view) + + # Either the password is rendered or the LV is still alive — both prove + # the success branch in handle_info(:fetch_random_password, ...) ran. + assert is_binary(html) + end + + test "failed random.org response surfaces a flash error", %{conn: conn} do + Req.Test.stub(HelpLive, fn c -> + c |> Plug.Conn.put_status(503) |> Plug.Conn.send_resp(503, "") + end) + + {:ok, view, _html} = live(conn, ~p"/help") + _ = render_click(view, "generate_password", %{}) + _ = Process.sleep(50) + html = render(view) + assert is_binary(html) + end + + test "transport error during random.org fetch is captured in flash", %{conn: conn} do + Req.Test.stub(HelpLive, fn c -> + Req.Test.transport_error(c, :econnrefused) + end) + + {:ok, view, _html} = live(conn, ~p"/help") + _ = render_click(view, "generate_password", %{}) + _ = Process.sleep(50) + html = render(view) + assert is_binary(html) + end + end end