defmodule Towerops.Accounts.UserNotifierTest do
  use Towerops.DataCase, async: true

  import Swoosh.TestAssertions
  import Towerops.AccountsFixtures

  alias Towerops.Accounts.UserNotifier

  describe "deliver_update_email_instructions/2" do
    test "sends email with update instructions" do
      user = user_fixture()
      url = "https://example.com/users/settings/confirm_email/token"

      assert {:ok, email} = UserNotifier.deliver_update_email_instructions(user, url)
      assert email.to == [{"", user.email}]
      assert email.subject == "Update email instructions"
      assert email.text_body =~ "You can change your email"
      assert email.text_body =~ url
      assert_email_sent(email)
    end
  end

  describe "deliver_login_instructions/2" do
    test "sends confirmation instructions for unconfirmed user" do
      user = unconfirmed_user_fixture()
      url = "https://example.com/users/confirm/token"

      assert {:ok, email} = UserNotifier.deliver_login_instructions(user, url)
      assert email.to == [{"", user.email}]
      assert email.subject == "Confirmation instructions"
      assert email.text_body =~ "You can confirm your account"
      assert email.text_body =~ url
      assert_email_sent(email)
    end

    test "sends magic link instructions for confirmed user" do
      user = user_fixture(%{confirmed_at: DateTime.utc_now()})
      url = "https://example.com/users/log-in/token"

      assert {:ok, email} = UserNotifier.deliver_login_instructions(user, url)
      assert email.to == [{"", user.email}]
      assert email.subject == "Log in instructions"
      assert email.text_body =~ "You can log into your account"
      assert email.text_body =~ url
      assert_email_sent(email)
    end
  end
end
