require 'spec_helper'

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

  subject { page }

  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

  before(:each) do
    @mac_client = FactoryGirl.create(:artifact_update)
    UserMailer.deliveries.clear
    MaxMindManager.create_phony_database
  end

  describe "signup page" do
    before { visit signup_path }

    it "should initialize successfully" do
      should have_selector('h2.create-account-header', text: '1Create your free JamKazam account')

      # we should see these locations in the signup form already chosen
      location = GeoIpLocations.lookup('127.0.0.1')
      find('.field.country .easydropdown .selected', text:location[:country])
      find('.field.state .easydropdown .selected', text:location[:state])
      find('.field.city .easydropdown .selected', text:location[:city])
    end

    describe "with valid musician information" do
      before do
        fill_in "jam_ruby_user[first_name]",    with: "Mike"
        fill_in "jam_ruby_user[last_name]", with: "Jones"
        fill_in "jam_ruby_user[email]", with: "newuser1@jamkazam.com"
        fill_in "jam_ruby_user[password]", with: "jam123"
        fill_in "jam_ruby_user[password_confirmation]", with: "jam123"
        check("jam_ruby_user[instruments][drums][selected]")
        check("jam_ruby_user[terms_of_service]")
        click_button "CREATE ACCOUNT"
      end

      # Successful signup with no invitation tells you to go sign up
      it {
          should have_title("JamKazam | Congratulations")
          should have_content("Congratulations! Your account is ready.")
          user = User.find_by_email('newuser1@jamkazam.com')
          user.musician_instruments.length.should == 1
          location = GeoIpLocations.lookup('127.0.0.1')
          user.country.should == location[:country]
          user.state.should == location[:state]
          user.city.should == location[:city]
          # an email is sent on no-invite signup
          UserMailer.deliveries.length.should == 1
          UserMailer.deliveries[0].html_part.body.include?("To confirm this email address")== 1
          uri = URI.parse(current_url); "#{uri.path}?#{uri.query}".should == congratulations_musician_path(:type => 'Native')
      }

      describe "user can confirm email and receive welcome email" do

        before(:each) do
          UserMailer.deliveries.clear
          visit signup_confirm_path(User.find_by_email('newuser1@jamkazam.com').signup_token)
        end

        it {
          should have_title("Signup Confirmation")
          should have_selector('h1', text: "Email Confirmed")
          UserMailer.deliveries.length.should == 1
          UserMailer.deliveries[0].html_part.body.include?("Following are links to some resources") == 1
        }
      end
    end

    describe "with valid fan information" do
      before do
        fill_in "jam_ruby_user[first_name]",    with: "Mike"
        fill_in "jam_ruby_user[last_name]", with: "Jones"
        fill_in "jam_ruby_user[email]", with: "somefan@jamkazam.com"
        fill_in "jam_ruby_user[password]", with: "jam123"
        fill_in "jam_ruby_user[password_confirmation]", with: "jam123"
        choose "jam_ruby_user_musician_false"

         check("jam_ruby_user[terms_of_service]")
        click_button "CREATE ACCOUNT"
      end

      # Successful signup with no invitation tells you to go sign up
      it {
        should have_title("JamKazam | Congratulations")
        should have_selector('.flash-content', text: "Congratulations! Your account is ready.")
        User.find_by_email('somefan@jamkazam.com').musician_instruments.length.should == 0
        # an email is sent on no-invite signup
        UserMailer.deliveries.length.should == 1
        uri = URI.parse(current_url)
        "#{uri.path}?#{uri.query}".should == congratulations_fan_path(:type => 'Native')
      }
    end

    describe "with service invite" do
      before do
        @invited_user = FactoryGirl.create(:invited_user, :email => "noone@jamkazam.com")
        visit "#{signup_path}?invitation_code=#{@invited_user.invitation_code}"
        find('#jam_ruby_user_first_name')
        sleep 1 # if I don't do this, first_name and/or last name intermittently fail to fill out

        UserMailer.deliveries.clear

        fill_in "jam_ruby_user[first_name]", with: "Mike"
        fill_in "jam_ruby_user[last_name]", with: "Jones"
        fill_in "jam_ruby_user[email]", with: "newuser2@jamkazam.com"
        fill_in "jam_ruby_user[password]", with: "jam123"
        fill_in "jam_ruby_user[password_confirmation]", with: "jam123"
        check("jam_ruby_user[instruments][drums][selected]")
        check("jam_ruby_user[terms_of_service]")
        click_button "CREATE ACCOUNT"
      end

      # Successful sign-in goes to the client
      it {
        should have_title("JamKazam")
        should have_selector('.flash-content', text: "Congratulations! Your account is ready.")
        UserMailer.deliveries.length.should == 1
        uri = URI.parse(current_url)
        "#{uri.path}?#{uri.query}".should == congratulations_musician_path(:type => 'Native')
      }
    end

    describe "with user invite and autofriend" do
      before do
        @user = FactoryGirl.create(:user)
        @invited_user = FactoryGirl.create(:invited_user, :sender => @user, :autofriend => true, :email => "noone@jamkazam.com")
        visit "#{signup_path}?invitation_code=#{@invited_user.invitation_code}"
        find('#jam_ruby_user_first_name')
        sleep 1 # if I don't do this, first_name and/or last name intermittently fail to fill out

        fill_in "jam_ruby_user[first_name]",    with: "Mike"
        fill_in "jam_ruby_user[last_name]", with: "Jones"
        fill_in "jam_ruby_user[email]", with: "newuser3@jamkazam.com"
        fill_in "jam_ruby_user[password]", with: "jam123"
        fill_in "jam_ruby_user[password_confirmation]", with: "jam123"
        check("jam_ruby_user[instruments][drums][selected]")
        check("jam_ruby_user[terms_of_service]")
        click_button "CREATE ACCOUNT"
      end

      # Successful sign-in goes to the client
      it {
        should have_title("JamKazam")
        should have_selector('.flash-content', text: "Congratulations! Your account is ready.")
        @user.friends?(User.find_by_email("newuser3@jamkazam.com"))
        User.find_by_email("newuser3@jamkazam.com").friends?(@user)
        uri = URI.parse(current_url)
        "#{uri.path}?#{uri.query}".should == congratulations_musician_path(:type => 'Native')
      }
    end

    describe "can't signup to the same invite twice" do
      before do
        @invited_user = FactoryGirl.create(:invited_user, :email => "noone@jamkazam.com")
        visit "#{signup_path}?invitation_code=#{@invited_user.invitation_code}"
        find('#jam_ruby_user_first_name')
        sleep 1 # if I don't do this, first_name and/or last name intermittently fail to fill out

        fill_in "jam_ruby_user[first_name]",    with: "Mike"
        fill_in "jam_ruby_user[last_name]", with: "Jones"
        fill_in "jam_ruby_user[email]", with: "newuser4@jamkazam.com"
        fill_in "jam_ruby_user[password]", with: "jam123"
        fill_in "jam_ruby_user[password_confirmation]", with: "jam123"
        check("jam_ruby_user[instruments][drums][selected]")
        check("jam_ruby_user[terms_of_service]")
        click_button "CREATE ACCOUNT"
        page.should have_title("JamKazam")
        should have_selector('.flash-content', text: "Congratulations! Your account is ready.")
        sign_out
        visit "#{signup_path}?invitation_code=#{@invited_user.invitation_code}"
      end

      it { should have_selector('h1', text: "You have already signed up with this invitation") }

    end

    describe "signup facebook user" do
      before do
        @fb_signup = FactoryGirl.create(:facebook_signup)
        visit "#{signup_path}?facebook_signup=#{@fb_signup.lookup_id}"
        find('#jam_ruby_user_first_name')
        sleep 1 # if I don't do this, first_name and/or last name intermittently fail to fill out

        fill_in "jam_ruby_user[first_name]",    with: "Mike"
        fill_in "jam_ruby_user[last_name]", with: "Jones"
        fill_in "jam_ruby_user[email]", with: "newuser_fb@jamkazam.com"
        fill_in "jam_ruby_user[password]", with: "jam123"
        fill_in "jam_ruby_user[password_confirmation]", with: "jam123"
        check("jam_ruby_user[instruments][drums][selected]")
        check("jam_ruby_user[terms_of_service]")
        click_button "CREATE ACCOUNT"
      end

      it "success" do
        page.should have_title("JamKazam")
        should have_selector('.flash-content', text: "Congratulations! Your account is ready.")
        uri = URI.parse(current_url)
        "#{uri.path}?#{uri.query}".should == congratulations_musician_path(:type => 'Facebook')
      end
    end
    def signup_invited_user
      visit "#{signup_path}?invitation_code=#{@invited_user.invitation_code}"
      find('#jam_ruby_user_first_name')
      sleep 1 # if I don't do this, first_name and/or last name intermittently fail to fill out

      fill_in "jam_ruby_user[first_name]",    with: "Mike"
      fill_in "jam_ruby_user[last_name]", with: "Jones"
      @invited_user_email = "newuser#{rand(10000)}@jamkazam.com"
      fill_in "jam_ruby_user[email]", with: @invited_user_email
      fill_in "jam_ruby_user[password]", with: "jam123"
      fill_in "jam_ruby_user[password_confirmation]", with: "jam123"
      check("jam_ruby_user[instruments][drums][selected]")
      check("jam_ruby_user[terms_of_service]")
      click_button "CREATE ACCOUNT"
    end

    def signup_good
      should have_title("JamKazam")
      should have_selector('.flash-content', text: "Congratulations! Your account is ready.")
      @user.friends?(User.find_by_email(@invited_user_email))
      User.find_by_email(@invited_user_email).friends?(@user)
      uri = URI.parse(current_url)
      "#{uri.path}?#{uri.query}".should == congratulations_musician_path(:type => 'Native')
    end

    describe "can signup with facebook link multiple times with same invite" do
      before do
        @user = FactoryGirl.create(:user)
        @invited_user = FactoryGirl.create(:invited_user, :sender => @user, :autofriend => true, :email => nil, :invite_medium => InvitedUser::FB_MEDIUM)
      end

      # Successful sign-in goes to the client
      it {
        signup_invited_user
        signup_good
      }
      it {
        signup_invited_user
        signup_good
      }
    end

    describe "can signup with an email different than the one used to invite" do
      before do
        @invited_user = FactoryGirl.create(:invited_user, :email => "what@jamkazam.com")
        visit "#{signup_path}?invitation_code=#{@invited_user.invitation_code}"
        find('#jam_ruby_user_first_name')
        sleep 1 # if I don't do this, first_name and/or last name intermittently fail to fill out

        UserMailer.deliveries.clear

        fill_in "jam_ruby_user[first_name]",    with: "Mike"
        fill_in "jam_ruby_user[last_name]", with: "Jones"
        fill_in "jam_ruby_user[email]", with: "newuser5@jamkazam.com"
        fill_in "jam_ruby_user[password]", with: "jam123"
        fill_in "jam_ruby_user[password_confirmation]", with: "jam123"
        check("jam_ruby_user[instruments][drums][selected]")
        check("jam_ruby_user[terms_of_service]")
        click_button "CREATE ACCOUNT"
      end

      it {
        should have_title("JamKazam | Congratulations")
        should have_selector('.flash-content', text: "Congratulations! Your account is ready.")
        User.find_by_email('newuser5@jamkazam.com').musician_instruments.length.should == 1
        User.find_by_email('what@jamkazam.com').should be_nil
        # an email is sent when you invite but use a different email than the one used to invite
        UserMailer.deliveries.length.should == 1
        uri = URI.parse(current_url)
        "#{uri.path}?#{uri.query}".should == congratulations_musician_path(:type => 'Native')
        sign_out
        visit "#{signup_path}?invitation_code=#{@invited_user.invitation_code}"
        should have_selector('h1', text: "You have already signed up with this invitation")
      }
    end

    describe "signup_hints" do
      it "redirects to custom location on matched signup_hint" do

        # causes anon cookie to show
        visit '/'
        find('h3', text: 'Complete, Multi-Track Backing Tracks')

        # get a anonymous cookie set up
        anon_user_id = page.driver.cookies["user_uuid"]
        anon_user = AnonymousUser.new(anon_user_id.value, {})
        SignupHint.refresh_by_anoymous_user(anon_user, {redirect_location: '/products/jamblaster', want_jamblaster: true})

        visit signup_path

        fill_in "jam_ruby_user[first_name]",    with: "Mike"
        fill_in "jam_ruby_user[last_name]", with: "Jones"
        fill_in "jam_ruby_user[email]", with: "signup_hint_guy@jamkazam.com"
        fill_in "jam_ruby_user[password]", with: "jam123"
        fill_in "jam_ruby_user[password_confirmation]", with: "jam123"
        check("jam_ruby_user[instruments][drums][selected]")
        check("jam_ruby_user[terms_of_service]")
        click_button "CREATE ACCOUNT"

        find('h1.product-headline', text:'The JamBlaster by JamKazam')

        user = User.find_by_email('signup_hint_guy@jamkazam.com')
        user.want_jamblaster.should be_true
      end

      it "ignores expired_at signup_hint" do

        # causes anon cookie to show
        visit '/'
        find('h3', text: 'Complete, Multi-Track Backing Tracks')

        # get a anonymous cookie set up
        anon_user_id = page.driver.cookies["user_uuid"]
        anon_user = AnonymousUser.new(anon_user_id.value, {})
        hint = SignupHint.refresh_by_anoymous_user(anon_user, {redirect_location: '/products/jamblaster', want_jamblaster: true})
        hint.expires_at = 1.day.ago
        hint.save!

        visit signup_path

        fill_in "jam_ruby_user[first_name]",    with: "Mike"
        fill_in "jam_ruby_user[last_name]", with: "Jones"
        fill_in "jam_ruby_user[email]", with: "signup_hint_guy2@jamkazam.com"
        fill_in "jam_ruby_user[password]", with: "jam123"
        fill_in "jam_ruby_user[password_confirmation]", with: "jam123"
        check("jam_ruby_user[instruments][drums][selected]")
        check("jam_ruby_user[terms_of_service]")
        click_button "CREATE ACCOUNT"

        should have_title("JamKazam | Congratulations")

        user = User.find_by_email('signup_hint_guy2@jamkazam.com')
        user.want_jamblaster.should be_false
      end
    end

  end

end
