require 'spec_helper'

describe InvitedUser do

  before(:each) do
    UserMailer.deliveries.clear
  end

  describe 'spammer' do

    after {Timecop.return}


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

    it 'max_invites_ever_per_sender' do

      Timecop.travel(Date.new(2016, 3, 20))

      APP_CONFIG.max_invites_ever_per_sender.times do
        invited_user = FactoryGirl.create(:invited_user, :sender => user1)
      end

      invited_user = FactoryGirl.build(:invited_user, :sender => user1)
      invited_user.save
      invited_user.should_not be_valid

      # but move time forward...

      Timecop.travel(Date.new(2016, 3, 22))

      # this should never 'heal'; once you hit max, you are done for good
      invited_user = FactoryGirl.build(:invited_user, :sender => user1)
      invited_user.save
      invited_user.should_not be_valid
    end

    it 'max_invites_per_day_per_sender' do

      Timecop.travel(Date.new(2016, 3, 20))

      APP_CONFIG.max_invites_per_day_per_sender.times do
        invited_user = FactoryGirl.create(:invited_user, :sender => user1)
      end

      invited_user = FactoryGirl.build(:invited_user, :sender => user1)
      invited_user.save
      invited_user.should_not be_valid

      # but move time forward...

      Timecop.travel(Date.new(2016, 3, 22))

      # this should work because we moved forward by 2 days
      invited_user = FactoryGirl.create(:invited_user, :sender => user1)
    end


    it 'max_invites_to_receiver_per_day' do

      Timecop.travel(Date.new(2016, 3, 20))

      APP_CONFIG.max_invites_to_receiver_per_day.times do
        invited_user = FactoryGirl.create(:invited_user, :email => 'seth@jamkazam.com')
      end

      invited_user = FactoryGirl.build(:invited_user, :email => 'seth@jamkazam.com')
      invited_user.save
      invited_user.should_not be_valid

      # but move time forward...

      Timecop.travel(Date.new(2016, 3, 22))

      # this should work because we moved forward by 2 days
      invited_user = FactoryGirl.create(:invited_user, :email => 'seth@jamkazam.com')
    end


  end

  it 'create an invitation from end-user' do

    # create an end user
    user1 = FactoryGirl.create(:user)

    # create the invitation from the end-user
    invited_user = FactoryGirl.create(:invited_user, :sender => user1)

    invited_user.email.should_not be_nil
    invited_user.sender.should_not be_nil
    invited_user.note.should be_nil
    invited_user.invited_by_administrator?.should be_false
    #invited_user.sender.reload
    invited_user.sender.first_invited_at.should_not be_nil
  end

  it 'create an invitation from admin-user' do

    # create an admin user
    user1 = FactoryGirl.create(:admin)

    # create the invitation from the end-user
    invited_user = FactoryGirl.create(:invited_user, :sender => user1)

    invited_user.email.should_not be_nil
    invited_user.sender.should_not be_nil
    invited_user.note.should be_nil
    invited_user.invited_by_administrator?.should be_true
  end

  it 'create an invitation from no one in particular' do
    # create the invitation from the end-user
    invited_user = FactoryGirl.build(:invited_user)

    invited_user.invited_by_administrator?.should be_true
  end

  it 'email is sent automatically by virtue of observer' do
    # create an admin user
    user1 = FactoryGirl.create(:admin)

    # create the invitation from the end-user
    invited_user = FactoryGirl.create(:invited_user, :sender => user1)

    InvitedUserMailer.deliveries.length.should == 1
  end

  it 'accept an invitation' do
    # create an admin user
    user1 = FactoryGirl.create(:admin)

    # create the invitation from the end-user
    invited_user = FactoryGirl.create(:invited_user, :sender => user1)

    invited_user.accepted.should be_false

    invited_user.accept!
    invited_user.save

    invited_user.accepted.should be_true
  end

  it 'checks can invite' do
    # create an admin user
    user1 = FactoryGirl.create(:user)
    user1.can_invite = false
    user1.save

    # create the invitation from the end-user
    invited_user = FactoryGirl.build(:invited_user, :sender => user1)
    invited_user.save
    invited_user.errors.any?.should be_true
  end

  it 'list invites for a user' do
    # user to create an invite with
    user1 = FactoryGirl.create(:user)

    InvitedUser.index(user1).length.should == 0

    # create the invitation from the end-user
    invited_user = FactoryGirl.create(:invited_user, :sender => user1)
    invited_users = InvitedUser.index(user1)
    invited_users.length.should == 1
    invited_users[0].should == invited_user
  end

  it 'should not allow note to have profanity' do

    user1 = FactoryGirl.create(:user)

    # create the invitation from the end-user
    invited_user = FactoryGirl.create(:invited_user, :sender => user1)
    invited_user.note = 'fuck you'
    invited_user.save
    invited_user.valid?.should be_false
  end

  it 'accepts empty emails' do
    # we only support email as a medium; FB is ignored
    user1 = FactoryGirl.create(:user)
    invited_user = FactoryGirl.create(:invited_user, :sender_id => user1.id, :invite_medium => InvitedUser::FB_MEDIUM, :email => '')
    expect(invited_user.valid?).to eq(false)
  end

end
