require 'spec_helper'

describe "Affiliate Program", :js => true, :type => :feature, :capybara_feature => true do

  subject { page }

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

  before(:each) do
    AffiliateQuarterlyPayment.delete_all
    AffiliateMonthlyPayment.delete_all
    AffiliateTrafficTotal.delete_all
    AffiliatePartner.delete_all
  end

  before(:all) do
    @old_recaptcha=Rails.application.config.recaptcha_enable
    Rails.application.config.recaptcha_enable=false
  end

  after(:all) do
    Rails.application.config.recaptcha_enable=@old_recaptcha
  end

  describe "Affiliate Program signup page" do
    it "logged in user creates affiliate" do
      fast_signin user, '/affiliateProgram'

      find('input#entity_individual').click

      find('.agree-button').click

      find('h1', text: 'congratulations')
      find('.button-orange', text: 'GO TO AFFILIATE PAGE').click

      find('.tab-account', text: 'So please provide this data, and be sure to keep it current!')

      partner = AffiliatePartner.order('created_at desc').first
      partner.partner_user.should eq(user)
      partner.entity_type.should eq('Individual')
    end

    it "logged in user creates entity affiliate" do
      fast_signin user, '/affiliateProgram'

      find('input#entity_entity').click
      fill_in('entity-name', with: 'Mr. Bubbles')
      select('Sole Proprietor', from:'entity-type')

      find('.agree-button').click

      find('h1', text: 'congratulations')
      find('.button-orange', text: 'GO TO AFFILIATE PAGE').click

      find('.tab-account', text: 'So please provide this data, and be sure to keep it current!')

      partner = AffiliatePartner.order('created_at desc').first
      partner.partner_user.should eq(user)
      partner.entity_type.should eq('Sole Proprietor')
    end

    it "new user creates individual affiliate" do
      visit '/affiliateProgram'

      find('input#entity_individual').click

      find('.agree-button').click

      find('h1', text: 'congratulations')
      find('.button-orange', text: 'GO SIGNUP').click

      fill_in "jam_ruby_user[first_name]", with: "Affiliate1"
      fill_in "jam_ruby_user[last_name]", with: "Someone"
      fill_in "jam_ruby_user[email]", with: "affiliate1@jamkazam.com"
      fill_in "jam_ruby_user[password]", with: "jam123"
      fill_in "jam_ruby_user[password_confirmation]", with: "jam123"
      check("jam_ruby_user[terms_of_service]")
      click_button "CREATE ACCOUNT"

      should have_title("JamKazam | Congratulations")

      found_user = User.order('created_at desc').first
      partner = AffiliatePartner.order('created_at desc').first
      partner.partner_user.should eq(found_user)
      partner.entity_type.should eq('Individual')
    end
  end
end
