defmodule FirehoseWeb.UserRegistrationControllerTest do
  use FirehoseWeb.ConnCase, async: true

  import Firehose.AccountsFixtures

  describe "GET /users/register" do
    test "renders registration page", %{conn: conn} do
      response = conn |> get(~p"/users/register") |> html_response(200)
      assert response =~ "Register"
      assert response =~ ~p"/users/log-in"
      assert response =~ ~p"/users/register"
    end

    test "redirects if already logged in", %{conn: conn} do
      conn = conn |> log_in_user(user_fixture()) |> get(~p"/users/register")

      assert redirected_to(conn) == ~p"/"
    end
  end

  describe "POST /users/register" do
    @tag :capture_log
    test "creates account but does not log in", %{conn: conn} do
      email = unique_user_email()
      Application.put_env(:firehose, :allowed_registration_email, email)
      on_exit(fn -> Application.delete_env(:firehose, :allowed_registration_email) end)

      response =
        conn
        |> post(~p"/users/register", %{
          "user" => valid_user_attributes(email: email)
        })

      refute get_session(response, :user_token)
      assert redirected_to(response) == ~p"/users/log-in"

      assert response.assigns.flash["info"] =~
               ~r/An email was sent to .*, please access it to confirm your account/
    end

    test "render errors for invalid data", %{conn: conn} do
      Application.put_env(:firehose, :allowed_registration_email, "with spaces")
      on_exit(fn -> Application.delete_env(:firehose, :allowed_registration_email) end)

      response =
        conn
        |> post(~p"/users/register", %{
          "user" => %{"email" => "with spaces"}
        })
        |> html_response(200)

      assert response =~ "Register"
      assert response =~ "must have the @ sign and no spaces"
    end
  end

  describe "POST /users/register with email gating" do
    test "succeeds when email matches ALLOWED_REGISTRATION_EMAIL", %{conn: conn} do
      Application.put_env(:firehose, :allowed_registration_email, "allowed@example.com")
      on_exit(fn -> Application.delete_env(:firehose, :allowed_registration_email) end)

      response =
        conn |> post(~p"/users/register", %{"user" => %{"email" => "allowed@example.com"}})

      assert Phoenix.Flash.get(response.assigns.flash, :info) =~ "email was sent"
    end

    test "fails with invite-only message when email doesn't match", %{conn: conn} do
      Application.put_env(:firehose, :allowed_registration_email, "allowed@example.com")
      on_exit(fn -> Application.delete_env(:firehose, :allowed_registration_email) end)

      assert conn
             |> post(~p"/users/register", %{"user" => %{"email" => "other@example.com"}})
             |> html_response(200) =~ "registration is invite only"
    end

    test "fails with invite-only message when env var is unset", %{conn: conn} do
      Application.delete_env(:firehose, :allowed_registration_email)

      assert conn
             |> post(~p"/users/register", %{"user" => %{"email" => "anyone@example.com"}})
             |> html_response(200) =~ "registration is invite only"
    end
  end
end
