require "spec_helper"

###########################################################
# We test just the mailer templating here.                #
# In other words, make sure there are no glaring oopsies, #
# such as broken templates, or sending to wrong address   #
###########################################################


describe UserMailer do

  let(:user) { FactoryGirl.create(:user) }

  before(:each) do
    #stub_const("APP_CONFIG", app_config)
    UserMailer.deliveries.clear
  end

  describe "should send confirm email" do

    let (:mail) { UserMailer.deliveries[0] }
    let (:signup_confirmation_url) { "/confirm" }
    let (:signup_confirmation_url_with_token  ) { "#{signup_confirmation_url}/#{user.signup_token}" }

    before(:each) do
      UserMailer.confirm_email(user, signup_confirmation_url_with_token).deliver_now
    end

    it { UserMailer.deliveries.length.should == 1 }
    it { mail['from'].to_s.should == UserMailer::DEFAULT_SENDER }
    it { mail['to'].to_s.should == user.email }
    it { mail.multipart?.should == true } # because we send plain + html

    # verify that the messages are correctly configured
    it { mail.html_part.body.include?("delighted").should be_true }
    it { mail.html_part.body.include?(signup_confirmation_url_with_token).should be_true }
    it { mail.text_part.body.include?("delighted").should be_true  }
    it { mail.text_part.body.include?(signup_confirmation_url_with_token).should be_true }
  end

  describe "should send welcome email" do

    let (:mail) { UserMailer.deliveries[0] }

    before(:each) do
      UserMailer.welcome_message(user).deliver_now
    end


    it { UserMailer.deliveries.length.should == 1 }
    it { mail['from'].to_s.should == UserMailer::DEFAULT_SENDER }
    it { mail['to'].to_s.should == user.email }
    it { mail.multipart?.should == true } # because we send plain + html

    # verify that the messages are correctly configured
    it { mail.html_part.body.include?("delighted").should be_true }
    it { mail.text_part.body.include?("delighted").should be_true  }
  end

  describe "should send reset password" do

    let(:mail) { UserMailer.deliveries[0] }
    before(:each) do
      UserMailer.password_reset(user, '/reset_password').deliver_now
    end


    it { UserMailer.deliveries.length.should == 1 }

    it { mail['from'].to_s.should == UserMailer::DEFAULT_SENDER }
    it { mail['to'].to_s.should == user.email }
    it { mail.multipart?.should == true } # because we send plain + html

    # verify that the messages are correctly configured
    it { mail.html_part.body.include?("Reset").should be_true }
    it { mail.text_part.body.include?("change your JamKazam password").should be_true }
  end

  describe "should send change password confirmation" do

    let(:mail) { UserMailer.deliveries[0] }

    before(:each) do
      UserMailer.password_changed(user).deliver_now
    end

    it { UserMailer.deliveries.length.should == 1 }

    it { mail['from'].to_s.should == UserMailer::DEFAULT_SENDER }
    it { mail['to'].to_s.should == user.email }
    it { mail.multipart?.should == true } # because we send plain + html

    # verify that the messages are correctly configured
    it { mail.html_part.body.include?("changed your password").should be_true }
    it { mail.text_part.body.include?("changed your password").should be_true }
  end

  describe "should send update email confirmation" do

    let(:mail) { UserMailer.deliveries[0] }

    before(:each) do
      UserMailer.updated_email(user).deliver_now
    end

    it { UserMailer.deliveries.length.should == 1 }

    it { mail['from'].to_s.should == UserMailer::DEFAULT_SENDER }
    it { mail['to'].to_s.should == user.email }
    it { mail.multipart?.should == true } # because we send plain + html

    # verify that the messages are correctly configured
    it { mail.html_part.body.include?("<b>#{user.email}</b> has been confirmed as your new email address.").should be_true }
    it { mail.text_part.body.include?("#{user.email} has been confirmed as your new email address.").should be_true }
  end

  describe "should send updating email" do

    let(:mail) { UserMailer.deliveries[0] }

    before(:each) do
      user.update_email = "my_new_email@jamkazam.com"
      UserMailer.updating_email(user).deliver_now
    end

    it { UserMailer.deliveries.length.should == 1 }

    it { mail['from'].to_s.should == UserMailer::DEFAULT_SENDER }
    it { mail['to'].to_s.should == user.update_email }
    it { mail.multipart?.should == true } # because we send plain + html

    # verify that the messages are correctly configured
    it { mail.html_part.body.include?("to confirm your change in email").should be_true }
    it { mail.text_part.body.include?("to confirm your change in email").should be_true }

    # verify can unsubscribe from bulk emails

    it {mail.html_part.body.include?('here to unsubscribe').should be_true}
    it {mail.html_part.body.include?("https://www.jamkazam.com/unsubscribe/#{user.unsubscribe_token}")}
  end

  describe "notifications" do

    let(:mail) { UserMailer.deliveries[0] }
    let(:music_session) { FactoryGirl.create(:music_session) }

    it "should send upcoming email" do      
      user.update_email = "my_new_email@jamkazam.com"
      UserMailer.scheduled_session_reminder_upcoming(music_session.creator, music_session).deliver_now
      UserMailer.deliveries.length.should == 1

      mail['from'].to_s.should == UserMailer::DEFAULT_SENDER
      mail['to'].to_s.should == music_session.creator.email# rsvp_requests.first.user.email
      mail.multipart?.should == true  # because we send plain + htm

      # verify that the messages are correctly configured
      mail.html_part.body.include?("This is a reminder that your JamKazam session").should be_true
      mail.text_part.body.include?("This is a reminder that your JamKazam session").should be_true
      mail.html_part.body.include?("starts in 1 hour").should be_true
      mail.text_part.body.include?("starts in 1 hour").should be_true

      
    end

    it "should send 1-day reminder" do      
      user.update_email = "my_new_email@jamkazam.com"
      UserMailer.scheduled_session_reminder_day(music_session.creator, music_session).deliver_now
      UserMailer.deliveries.length.should == 1

      mail['from'].to_s.should == UserMailer::DEFAULT_SENDER
      mail['to'].to_s.should == music_session.creator.email# rsvp_requests.first.user.email
      mail.multipart?.should == true  # because we send plain + htm

      # verify that the messages are correctly configured
      mail.html_part.body.include?("This is a reminder that your JamKazam session").should be_true
      mail.text_part.body.include?("This is a reminder that your JamKazam session").should be_true
      mail.html_part.body.include?("is scheduled for tomorrow").should be_true
      mail.text_part.body.include?("is scheduled for tomorrow").should be_true
      
    end
  end

  describe "sends new musicians email" do
    let(:mail) { UserMailer.deliveries[0] }
    before(:each) do
      UserMailer.new_musicians_match(user, []).deliver_now
    end
    it { UserMailer.deliveries.length.should == 1 }
    it { mail['from'].to_s.should == UserMailer::DEFAULT_SENDER }
    it { mail['to'].to_s.should == user.email }
    it { mail.multipart?.should == true }  # because we send plain + html
  end

  # describe "sends new musicians email" do

  #   let(:mail) { UserMailer.deliveries[0] }

  #   before(:each) do
  #     UserMailer.new_musicians(user, User.musicians).deliver_now
  #   end

  #   it { UserMailer.deliveries.length.should == 1 }

  #   it { mail['from'].to_s.should == UserMailer::DEFAULT_SENDER }
  #   it { mail['to'].to_s.should == user.email }
  #   it { mail.multipart?.should == true } # because we send plain + html

  #   # verify that the messages are correctly configured
  #   it { mail.html_part.body.include?("New JamKazam Musicians in your Area").should be_true }
  #   it { mail.text_part.body.include?("New JamKazam Musicians in your Area").should be_true }
  # end

  describe "sends group session reminder 1", focus: true do
    let(:mail) { ActionMailer::Base.deliveries.last }

    before(:each) do
      ActionMailer::Base.deliveries.clear
      UserMailer.group_session_reminder1(user).deliver_now
    end

    it "sends exactly one email" do
      expect(ActionMailer::Base.deliveries.length).to eq(1)
    end

    it "has the correct from address" do
      #expect(mail.from).to eq([UserMailer::DEFAULT_SENDER])
      mail['from'].to_s.should == UserMailer::DEFAULT_SENDER
    end

    it "has the correct recipient" do
      expect(mail.to).to eq([user.email])
    end

    it "is multipart" do
      expect(mail).to be_multipart
    end

    it "has the expected subject" do
      expect(mail.subject).to include("It’s time to start playing music with others online!")
    end

    it "includes the correct content in the HTML part" do
      expect(mail.html_part.body.to_s).to include("Now that you have set up your gear and checked it out in a solo session on your own")
    end

    it "includes the correct content in the text part" do
      expect(mail.text_part.body.to_s).to include("Now that you have set up your gear and checked it out in a solo session on your own")
    end
  end

  describe "sends group session reminder 2", focus: true do
    let(:mail) { ActionMailer::Base.deliveries.last }

    before(:each) do
      ActionMailer::Base.deliveries.clear
      UserMailer.group_session_reminder2(user).deliver_now
    end

    it "sends exactly one email" do
      expect(ActionMailer::Base.deliveries.length).to eq(1)
    end

    it "has the correct from address" do
      mail['from'].to_s.should == UserMailer::DEFAULT_SENDER
    end

    it "has the correct recipient" do
      expect(mail.to).to eq([user.email])
    end

    it "is multipart" do
      expect(mail).to be_multipart
    end

    it "has the expected subject" do
      expect(mail.subject).to include("Take the last step to play online with others now, and experience the joy")
    end

    it "includes the correct content in the HTML part" do
      expect(mail.html_part.body.to_s).to include("If you signed up for JamKazam to play music with others online")
    end

    it "includes the correct content in the text part" do
      expect(mail.text_part.body.to_s).to include("If you signed up for JamKazam to play music with others online")
    end
  end

  describe "sends group session reminder 3", focus: true do
    let(:mail) { ActionMailer::Base.deliveries.last }

    before(:each) do
      ActionMailer::Base.deliveries.clear
      UserMailer.group_session_reminder3(user).deliver_now
    end

    it "sends exactly one email" do
      expect(ActionMailer::Base.deliveries.length).to eq(1)
    end

    it "has the correct from address" do
      mail['from'].to_s.should == UserMailer::DEFAULT_SENDER
    end

    it "has the correct recipient" do
      expect(mail.to).to eq([user.email])
    end

    it "is multipart" do
      expect(mail).to be_multipart
    end

    it "has the expected subject" do
      expect(mail.subject).to include("Don’t waste your free 30-day premium gold plan–get in a session with others now!")
    end

    it "includes the correct content in the HTML part" do
      expect(mail.html_part.body.to_s).to include("When you sign up for JamKazam, we give you a free 30-day premium")
    end

    it "includes the correct content in the text part" do
      expect(mail.text_part.body.to_s).to include("When you sign up for JamKazam, we give you a free 30-day premium")
    end
  end

  describe "sends trial expires reminder 1", focus: true do
    let(:mail) { ActionMailer::Base.deliveries.last }

    before(:each) do
      ActionMailer::Base.deliveries.clear
      UserMailer.trial_expires_reminder1(user).deliver_now
    end

    it "sends exactly one email" do
      expect(ActionMailer::Base.deliveries.length).to eq(1)
    end

    it "has the correct from address" do
      mail['from'].to_s.should == UserMailer::DEFAULT_SENDER
    end

    it "has the correct recipient" do
      expect(mail.to).to eq([user.email])
    end

    it "is multipart" do
      expect(mail).to be_multipart
    end

    it "has the expected subject" do
      expect(mail.subject).to include("Your free gold trial has expired, but you have great options to keep playing!")
    end

    it "includes the correct content in the HTML part" do
      expect(mail.html_part.body.to_s).to include("We hope you’ve enjoyed your 30-day free gold trial with JamKazam")
    end

    it "includes the correct content in the text part" do
      expect(mail.text_part.body.to_s).to include("We hope you’ve enjoyed your 30-day free gold trial with JamKazam")
    end
  end

  describe "sends trial expires reminder 2", focus: true do
    let(:mail) { ActionMailer::Base.deliveries.last }

    before(:each) do
      ActionMailer::Base.deliveries.clear
      UserMailer.trial_expires_reminder2(user).deliver_now
    end

    it "sends exactly one email" do
      expect(ActionMailer::Base.deliveries.length).to eq(1)
    end

    it "has the correct from address" do
      mail['from'].to_s.should == UserMailer::DEFAULT_SENDER
    end

    it "has the correct recipient" do
      expect(mail.to).to eq([user.email])
    end

    it "is multipart" do
      expect(mail).to be_multipart
    end

    it "has the expected subject" do
      expect(mail.subject).to include("Don’t forget to check your options to keep playing")
    end

    it "includes the correct content in the HTML part" do
      expect(mail.html_part.body.to_s).to include("Your 30-day free gold trial with JamKazam has expired")
    end

    it "includes the correct content in the text part" do
      expect(mail.text_part.body.to_s).to include("Your 30-day free gold trial with JamKazam has expired")
    end
  end

  describe "sends trial expires reminder 3", focus: true do
    let(:mail) { ActionMailer::Base.deliveries.last }

    before(:each) do
      ActionMailer::Base.deliveries.clear
      UserMailer.trial_expires_reminder3(user).deliver_now
    end

    it "sends exactly one email" do
      expect(ActionMailer::Base.deliveries.length).to eq(1)
    end

    it "has the correct from address" do
      mail['from'].to_s.should == UserMailer::DEFAULT_SENDER
    end

    it "has the correct recipient" do
      expect(mail.to).to eq([user.email])
    end

    it "is multipart" do
      expect(mail).to be_multipart
    end

    it "has the expected subject" do
      expect(mail.subject).to include("One last reminder!")
    end

    it "includes the correct content in the HTML part" do
      expect(mail.html_part.body.to_s).to include("Your 30-day free gold trial with JamKazam has expired")
    end

    it "includes the correct content in the text part" do
      expect(mail.text_part.body.to_s).to include("Your 30-day free gold trial with JamKazam has expired")
    end
  end

end
