defmodule MicrowavepropWeb.UserResetPasswordControllerTest do
  use MicrowavepropWeb.ConnCase, async: true

  import Microwaveprop.AccountsFixtures

  alias Microwaveprop.Accounts
  alias Microwaveprop.Accounts.UserToken
  alias Microwaveprop.Repo

  setup do
    %{user: user_fixture()}
  end

  describe "GET /users/reset-password" do
    test "renders the request form", %{conn: conn} do
      conn = get(conn, ~p"/users/reset-password")
      response = html_response(conn, 200)
      assert response =~ "Reset your password"
      assert response =~ "Email"
    end
  end

  describe "POST /users/reset-password" do
    @tag :capture_log
    test "sends a new reset-password token for an existing email", %{conn: conn, user: user} do
      conn = post(conn, ~p"/users/reset-password", %{"user" => %{"email" => user.email}})

      assert redirected_to(conn) == ~p"/users/log-in"

      assert Phoenix.Flash.get(conn.assigns.flash, :info) =~
               ~r/If your email is in our system/

      assert Repo.get_by(UserToken, user_id: user.id, context: "reset_password")
    end

    test "does not leak account existence for an unknown email", %{conn: conn} do
      conn =
        post(conn, ~p"/users/reset-password", %{
          "user" => %{"email" => "ghost@example.com"}
        })

      assert redirected_to(conn) == ~p"/users/log-in"

      assert Phoenix.Flash.get(conn.assigns.flash, :info) =~
               ~r/If your email is in our system/

      refute Repo.get_by(UserToken, context: "reset_password")
    end
  end

  describe "GET /users/reset-password/:token" do
    setup %{user: user} do
      token =
        extract_user_token(fn url ->
          Accounts.deliver_user_reset_password_instructions(user, url)
        end)

      %{token: token}
    end

    test "renders the new-password form for a valid token", %{conn: conn, token: token} do
      conn = get(conn, ~p"/users/reset-password/#{token}")
      response = html_response(conn, 200)
      assert response =~ "Reset your password"
      assert response =~ "New password"
      assert response =~ "Confirm new password"
    end

    test "redirects with flash error when the token is invalid", %{conn: conn} do
      conn = get(conn, ~p"/users/reset-password/oops")
      assert redirected_to(conn) == ~p"/users/reset-password"

      assert Phoenix.Flash.get(conn.assigns.flash, :error) =~
               "Reset link is invalid or it has expired"
    end
  end

  describe "PUT /users/reset-password/:token" do
    setup %{user: user} do
      token =
        extract_user_token(fn url ->
          Accounts.deliver_user_reset_password_instructions(user, url)
        end)

      %{token: token}
    end

    test "resets the password for a valid token", %{conn: conn, user: user, token: token} do
      conn =
        put(conn, ~p"/users/reset-password/#{token}", %{
          "user" => %{
            "password" => "new valid password",
            "password_confirmation" => "new valid password"
          }
        })

      assert redirected_to(conn) == ~p"/users/log-in"

      assert Phoenix.Flash.get(conn.assigns.flash, :info) =~
               "Password reset successfully"

      assert Accounts.get_user_by_email_and_password(user.email, "new valid password")
      refute Repo.get_by(UserToken, user_id: user.id)
    end

    test "renders errors when the password is invalid", %{conn: conn, token: token} do
      conn =
        put(conn, ~p"/users/reset-password/#{token}", %{
          "user" => %{"password" => "short", "password_confirmation" => "short"}
        })

      response = html_response(conn, 200)
      assert response =~ "Reset your password"
      assert response =~ "should be at least 8"
    end

    test "redirects with flash error when the token is invalid", %{conn: conn} do
      conn =
        put(conn, ~p"/users/reset-password/oops", %{
          "user" => %{
            "password" => "new valid password",
            "password_confirmation" => "new valid password"
          }
        })

      assert redirected_to(conn) == ~p"/users/reset-password"

      assert Phoenix.Flash.get(conn.assigns.flash, :error) =~
               "Reset link is invalid or it has expired"
    end
  end
end
