require "test_helper"

class InvitationMailerTest < ActionMailer::TestCase
  test "invite sends email with correct details" do
    user = users(:pending_invitation)
    mail = InvitationMailer.invite(user)

    assert_equal "You've been invited to join Sanasto Wiki", mail.subject
    assert_equal [ user.email ], mail.to
    assert_match user.name, mail.body.encoded
    assert_match user.email, mail.body.encoded
    assert_match user.role.titleize, mail.body.encoded
  end

  test "invite includes invitation link" do
    user = users(:pending_invitation)
    mail = InvitationMailer.invite(user)

    assert_match "invitations/#{user.invitation_token}", mail.body.encoded
  end

  test "invite includes expiry date" do
    user = users(:pending_invitation)
    mail = InvitationMailer.invite(user)

    expires_at = user.invitation_sent_at + User::INVITATION_TOKEN_EXPIRY
    assert_match expires_at.strftime("%B"), mail.body.encoded
  end

  test "invite has both HTML and text parts" do
    user = users(:pending_invitation)
    mail = InvitationMailer.invite(user)

    assert_equal 2, mail.parts.size
    assert_equal "text/plain", mail.text_part.content_type.split(";").first
    assert_equal "text/html", mail.html_part.content_type.split(";").first
  end
end
