require 'spec_helper'

# these tests avoid the use of ActiveRecord and FactoryGirl to do blackbox, non test-instrumented tests
describe UserManager do

  before(:each) do
    Rails.application.config.recaptcha_enable=false
    @user_manager = UserManager.new(:conn => @conn)
    UserMailer.deliveries.clear
    @location = {:country => "US", :state => "Arkansas", :city => "Little Rock"}
    @loca = {:country => "US", :state => "Arkansas", :city => "Texarkana"}
    @locb = {:country => "US", :state => "Texas", :city => "Longview"}
    @locc = {}
    @instruments = [{:instrument_id => "electric guitar", :proficiency_level => '1', :priority => 0}]
  end

  describe 'better signup' do
    it 'signup sets last_jam_blah of musician' do

      # puts "user manager spec loca = #{@loca.to_s}"

      user = @user_manager.signup(remote_ip: "1.2.3.4",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "userman15@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: true,
                                  location: @loca,
                                  signup_confirm_url: "http://localhost:3000/confirm")

      user.errors.any?.should be false
      user.city.should == @loca[:city]
      user.state.should == @loca[:state]
      user.country.should == @loca[:country]
      user.last_jam_addr.should == 0x01020304
      user.last_jam_locidispid.should == 17192000002
      user.last_jam_updated_reason.should == 'r'
      # d = Time.now - user.last_jam_updated_at # guessing this is seconds
      # puts "d = #{d}"
      (Time.now - user.last_jam_updated_at).should be_between(0, 10)
    end

    it 'signup does not set last_jam_blah of fan' do

      # puts "user manager spec loca = #{@loca.to_s}"

      user = @user_manager.signup(remote_ip: "1.2.3.4",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "userman16@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: false,
                                  location: @locb,
                                  signup_confirm_url: "http://localhost:3000/confirm")

      puts "user.errors #{user.errors.first}" if user.errors.any?
      user.errors.any?.should be false
      user.city.should == @locb[:city]
      user.state.should == @locb[:state]
      user.country.should == @locb[:country]
      user.last_jam_addr.should be_nil
      user.last_jam_locidispid.should be_nil
      user.last_jam_updated_reason.should be_nil
      user.last_jam_updated_at.should be_nil
    end

    it 'signup does not set fan location if location blank' do

      # puts "user manager spec loca = #{@loca.to_s}"

      user = @user_manager.signup(remote_ip: "1.2.3.4",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "userman17@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: false,
                                  location: @locc,
                                  signup_confirm_url: "http://localhost:3000/confirm")

      puts "user.errors #{user.errors.first}" if user.errors.any?
      user.errors.any?.should be false
      user.city.should be_nil
      user.state.should be_nil
      user.country.should be_nil
      user.last_jam_addr.should be_nil
      user.last_jam_locidispid.should be_nil
      user.last_jam_updated_reason.should be_nil
      user.last_jam_updated_at.should be_nil
    end

    it 'signup does not set musician location if location blank' do

      # puts "user manager spec loca = #{@loca.to_s}"

      user = @user_manager.signup(remote_ip: "1.2.3.4",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "userman18@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: true,
                                  location: @locc,
                                  signup_confirm_url: "http://localhost:3000/confirm")

      puts "user.errors #{user.errors.first}" if user.errors.any?
      user.errors.any?.should be false
      user.city.should be_nil
      user.state.should be_nil
      user.country.should be_nil
      user.last_jam_addr.should == 0x01020304
      user.last_jam_locidispid.should == 17192000002
      user.last_jam_updated_reason.should == 'r'
      (Time.now - user.last_jam_updated_at).should be_between(0, 10)
    end

    it 'signup sets fan location from remote_ip if location nil' do

      # puts "user manager spec loca = #{@loca.to_s}"

      user = @user_manager.signup(remote_ip: "1.2.3.4",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "userman19@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: false,
                                  location: nil,
                                  signup_confirm_url: "http://localhost:3000/confirm")

      puts "user.errors #{user.errors.first}" if user.errors.any?
      user.errors.any?.should be false
      user.city.should == 'Austin'
      user.state.should == 'TX'
      user.country.should == 'US'
      user.last_jam_addr.should be_nil
      user.last_jam_locidispid.should be_nil
      user.last_jam_updated_reason.should be_nil
      user.last_jam_updated_at.should be_nil
    end

    it 'signup sets musician location from remote_ip if location nil' do

      # puts "user manager spec loca = #{@loca.to_s}"

      user = @user_manager.signup(remote_ip: "1.2.3.4",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "userman20@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: true,
                                  location: nil,
                                  signup_confirm_url: "http://localhost:3000/confirm")

      puts "user.errors #{user.errors.first}" if user.errors.any?
      user.errors.any?.should be false
      user.city.should == 'Austin'
      user.state.should == 'TX'
      user.country.should == 'US'
      user.last_jam_addr.should == 0x01020304
      user.last_jam_locidispid.should == 17192000002
      user.last_jam_updated_reason.should == 'r'
      (Time.now - user.last_jam_updated_at).should be_between(0, 10)
    end
  end

  describe "signup" do
    let!(:user) { FactoryGirl.create(:user) }
    let!(:partner) {
      AffiliatePartner.create_with_web_params(user, {:partner_name => Faker::Company.name, entity_type: 'Individual'})
    }

    it "signup successfully" do
      GeoIpLocations.delete_all # prove that city/state/country will remain nil if no maxmind data

      user = @user_manager.signup(remote_ip: "127.0.0.1",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "userman1@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: true,
                                  signup_confirm_url: "http://localhost:3000/confirm",
                                  affiliate_referral_id: partner.id)

      user.errors.any?.should be false
      user.first_name.should == "bob"
      user.last_name.should == "smith"
      user.email.should == "userman1@jamkazam.com"
      user.email_confirmed.should be false
      user.city.should be_nil
      user.state.should be_nil
      user.country.should be_nil
      user.instruments.length.should == 1
      user.subscribe_email.should be true
      user.signup_token.should_not be_nil

      user.reload
      expect(user.affiliate_referral).to eq(partner)

      UserMailer.deliveries.length.should == 2
    end

    it "signup successfully with instruments" do
      user = @user_manager.signup(remote_ip: "127.0.0.1",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "userman2@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: true,
                                  signup_confirm_url: "http://localhost:3000/confirm")

      user.errors.any?.should be false
      user.instruments.length.should == 1
      musician_instrument = user.musician_instruments[0]
      musician_instrument.instrument.should == Instrument.find("electric guitar")
      musician_instrument.proficiency_level.should == 1
    end

    it "doesnt fail if ip address is nil" do
      user = @user_manager.signup(first_name: "bob",
                                  last_name: "smith",
                                  email: "userman3@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: true,
                                  signup_confirm_url: "http://localhost:3000/confirm")

      user.errors.any?.should be false
      user.city.should be_nil
      user.state.should be_nil
      user.country.should be_nil
    end

    it "sets the location properly from maxmind" do
      MaxMindManager.create_phony_database
      user = @user_manager.signup(remote_ip: "127.0.0.1",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "userman4@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: true,
                                  signup_confirm_url: "http://localhost:3000/confirm")

      user.errors.any?.should be false
      user.city.should == 'Boston'
      user.state.should == 'MA'
      user.country.should == 'US'
    end

    it "accepts location if specified" do
      MaxMindManager.create_phony_database
      user = @user_manager.signup(remote_ip: "127.0.0.1",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "userman5@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  location: @location,
                                  musician: true,
                                  signup_confirm_url: "http://localhost:3000/confirm")

      user.errors.any?.should be false
      user.city.should == 'Little Rock'
      user.state.should == 'Arkansas'
      user.country.should == 'US'
    end

    it "accepts a nil location, if specified" do
      MaxMindManager.create_phony_database
      user = @user_manager.signup(remote_ip: "127.0.0.1",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "userman6@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  location: {},
                                  musician: true,
                                  signup_confirm_url: "http://localhost:3000/confirm")

      user.errors.any?.should be false
      user.city.should be_nil
      user.state.should be_nil
      user.country.should be_nil
    end


    it "accepts birth_date if specified" do
      MaxMindManager.create_phony_database
      user = @user_manager.signup(remote_ip: "127.0.0.1",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "userman7@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  birth_date: Date.new(2001, 1, 1),
                                  musician: true,
                                  signup_confirm_url: "http://localhost:3000/confirm")

      user.errors.any?.should be false
      user.birth_date.should == Date.new(2001, 1, 1)
    end


    it "duplicate signup failure" do
      user = @user_manager.signup(remote_ip: "127.0.0.1",
                                   first_name: "bob",
                                   last_name: "smith",
                                   email: "userman8@jamkazam.com",
                                   password: "foobar",
                                   password_confirmation: "foobar",
                                   terms_of_service: true,
                                   instruments: @instruments,
                                   musician: true,
                                   signup_confirm_url: "http://localhost:3000/confirm")

      UserMailer.deliveries.length.should == 2
      user.errors.any?.should be false

      # exactly the same parameters; should dup on email, and send no email
      user = @user_manager.signup(remote_ip: "127.0.0.1",
                                   first_name: "bob",
                                   last_name: "smith",
                                   email: "userman8@jamkazam.com",
                                   password: "foobar",
                                   password_confirmation: "foobar",
                                   terms_of_service: true,
                                   instruments: @instruments,
                                   musician: true,
                                   signup_confirm_url: "http://localhost:3000/confirm")
      UserMailer.deliveries.length.should == 2
      user.errors.any?.should be true
      user.errors[:email][0].should == "has already been taken"
    end

    it "is OK with no first_name/last_name" do
      user = @user_manager.signup(remote_ip: "127.0.0.1",
                                  first_name: "",
                                  last_name: "",
                                  email: "userman10@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: true,
                                  signup_confirm_url: "http://localhost:3000/confirm")
      UserMailer.deliveries.length.should == 2
      user.errors.any?.should be false
      user.name.should eq 'Anonymous'
    end

    it "fail on no email" do
      user = @user_manager.signup(remote_ip: "127.0.0.1",
                                  first_name: "murp",
                                  last_name: "blurp",
                                  email: "",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: true,
                                  signup_confirm_url: "http://localhost:3000/confirm")

      UserMailer.deliveries.length.should == 0
      user.errors.any?.should be true
      user.errors[:email][0].should == "can't be blank"
    end

  end

  describe "signup_confirm" do
    it "fail on no username" do
      user = @user_manager.signup(remote_ip: "127.0.0.1",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "userman11@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: true,
                                  signup_confirm_url: "http://localhost:3000/confirm")
      user = @user_manager.signup_confirm(user.signup_token)
      user.email_confirmed.should be true
    end

    it "fail to confirm bogus signup_confirmnup token" do
      @user_manager.signup_confirm("murp").should be_nil
    end

    it "fail to confirm empty signup token" do
      @user_manager.signup_confirm("").should be_nil
    end

    it "fail to confirm nil signup token" do
      @user_manager.signup_confirm(nil).should be_nil
    end
  end

  it "signup successfully with due to service (not from any particular user) invitation" do

    invitation = FactoryGirl.create(:invited_user)

    UserMailer.deliveries.clear

    invitation.accepted.should be false

    user = @user_manager.signup(remote_ip: "127.0.0.1",
                                first_name: "bob",
                                last_name: "smith",
                                email: invitation.email,
                                password: "foobar",
                                password_confirmation: "foobar",
                                terms_of_service: true,
                                instruments: @instruments,
                                musician: true,
                                invited_user: invitation,
                                signup_confirm_url: "http://localhost:3000/confirm")

    user.errors.any?.should be false
    user.email_confirmed.should be true
    user.signup_token.should be_nil
    invitation.errors.any?.should be false
    invitation.accepted.should be true

    UserMailer.deliveries.length.should == 1
  end

  it "signup successfully with due to user invitation with no autofriend" do

    @some_user = FactoryGirl.create(:user)
    invitation = FactoryGirl.create(:invited_user, :sender => @some_user)
    invitation.accepted.should be false
    UserMailer.deliveries.clear


    user = @user_manager.signup(remote_ip: "127.0.0.1",
                                first_name: "bob",
                                last_name: "smith",
                                email: invitation.email,
                                password: "foobar",
                                password_confirmation: "foobar",
                                terms_of_service: true,
                                instruments: @instruments,
                                musician: true,
                                invited_user: invitation,
                                signup_confirm_url: "http://localhost:3000/confirm")

    user.errors.any?.should be false
    user.email_confirmed.should be true
    user.signup_token.should be_nil
    invitation.errors.any?.should be false
    invitation.accepted.should be true

    UserMailer.deliveries.length.should == 1
  end

  it "signup successfully with due to user invitation with autofriend" do

    @some_user = FactoryGirl.create(:user)
    invitation = FactoryGirl.create(:invited_user, :sender => @some_user, :autofriend => true)
    invitation.accepted.should be false
    UserMailer.deliveries.clear


    user = @user_manager.signup(remote_ip: "127.0.0.1",
                                first_name: "bob",
                                last_name: "smith",
                                email: invitation.email,
                                password: "foobar",
                                password_confirmation: "foobar",
                                terms_of_service: true,
                                instruments: @instruments,
                                musician: true,
                                invited_user: invitation,
                                signup_confirm_url: "http://localhost:3000/confirm")

    user.errors.any?.should be false
    user.email_confirmed.should be true
    user.signup_token.should be_nil
    invitation.errors.any?.should be false
    invitation.accepted.should be true
    user.friends?(@some_user).should be true
    user.friends?(@some_user).should be true

    UserMailer.deliveries.length.should == 1
  end

  it "signup successfully with due to user invitation with autofriend, but uses another email" do

    @some_user = FactoryGirl.create(:user)
    invitation = FactoryGirl.create(:invited_user, :sender => @some_user, :autofriend => true)
    invitation.accepted.should be false
    UserMailer.deliveries.clear


    user = @user_manager.signup(remote_ip: "127.0.0.1",
                                first_name: "bob",
                                last_name: "smith",
                                email: "userman12@jamkazam.com",
                                password: "foobar",
                                password_confirmation: "foobar",
                                terms_of_service: true,
                                instruments: @instruments,
                                musician: true,
                                invited_user: invitation,
                                signup_confirm_url: "http://localhost:3000/confirm")

    user.errors.any?.should be false
    user.email_confirmed.should be false
    user.signup_token.should_not be_nil
    invitation.errors.any?.should be false
    invitation.accepted.should be true
    user.friends?(@some_user).should be true
    user.friends?(@some_user).should be true

    UserMailer.deliveries.length.should == 2
  end

  it "signup successfully with facebook signup additional info" do
    fb_signup = FactoryGirl.create(:facebook_signup)

    UserMailer.deliveries.clear


    user = @user_manager.signup(remote_ip: "127.0.0.1",
                                first_name: "bob",
                                last_name: "smith",
                                email: fb_signup.email,
                                password: "foobar",
                                password_confirmation: "foobar",
                                terms_of_service: true,
                                instruments: @instruments,
                                musician: true,
                                fb_signup: fb_signup,
                                signup_confirm_url: "http://localhost:3000/confirm")

    user.errors.any?.should be false
    user.email_confirmed.should be true
    user.signup_token.should be_nil
    user.user_authorizations.length.should == 1
    user.user_authorizations[0].uid = fb_signup.uid
    user.user_authorizations[0].token = fb_signup.token
    user.user_authorizations[0].token_expiration = fb_signup.token_expires_at

    UserMailer.deliveries.length.should == 1
  end

  it "signup successfully with facebook signup additional info, but different email" do
    fb_signup = FactoryGirl.create(:facebook_signup)

    UserMailer.deliveries.clear


    user = @user_manager.signup(remote_ip: "127.0.0.1",
                                first_name: "bob",
                                last_name: "smith",
                                email: "userman13@jamkazam.com",
                                password: "foobar",
                                password_confirmation: "foobar",
                                terms_of_service: true,
                                instruments: @instruments,
                                musician: true,
                                fb_signup: fb_signup,
                                signup_confirm_url: "http://localhost:3000/confirm")

    user.errors.any?.should be false
    user.email_confirmed.should be false
    user.signup_token.should_not be_nil
    user.user_authorizations.length.should == 1
    user.user_authorizations[0].uid = fb_signup.uid
    user.user_authorizations[0].token = fb_signup.token
    user.user_authorizations[0].token_expiration = fb_signup.token_expires_at

    UserMailer.deliveries.length.should == 2
  end

  it "fail to signup when facebook UID already taken" do
    fb_signup = FactoryGirl.create(:facebook_signup)

    @some_user = FactoryGirl.create(:user)
    @some_user.update_fb_authorization(fb_signup)
    @some_user.save!

    UserMailer.deliveries.clear

    user = @user_manager.signup(remote_ip: "127.0.0.1",
                                first_name: "bob",
                                last_name: "smith",
                                email: "userman13@jamkazam.com",
                                password: "foobar",
                                password_confirmation: "foobar",
                                terms_of_service: true,
                                instruments: @instruments,
                                musician: true,
                                fb_signup: fb_signup,
                                signup_confirm_url: "http://localhost:3000/confirm")

    user.errors.any?.should be true
    user.errors[:user_authorizations].should == ['is invalid']

    UserMailer.deliveries.length.should == 0
  end

  describe "without nocaptcha" do
    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

    it "passes even with true recaptcha" do
      user = @user_manager.signup(remote_ip: "1.2.3.4",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "userman30@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: true,
                                  location: @loca,
                                  recaptcha_failed: true,
                                  signup_confirm_url: "http://localhost:3000/confirm")
      user.errors.any?.should be false
    end # it "passes even with true recaptcha"  
  end # describe "without nocaptcha"

  describe "with nocaptcha" do
    before(:each) do
      @old_recaptcha=Rails.application.config.recaptcha_enable
      Rails.application.config.recaptcha_enable=true
      UserMailer.deliveries.clear
    end

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

    it "fails when nocaptcha fails" do
      user = @user_manager.signup(remote_ip: "1.2.3.4",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "userman31@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: true,
                                  location: @loca,
                                  recaptcha_response: nil,
                                  signup_confirm_url: "http://localhost:3000/confirm")

      user.errors.any?.should be true
      UserMailer.deliveries.should have(0).items
    end

    it "passes when facebook signup" do
      user = @user_manager.signup(remote_ip: "1.2.3.4",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "userman31@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  fb_signup: FactoryGirl.create(:facebook_signup),
                                  recaptcha_response: nil,
                                  instruments: @instruments,
                                  musician: true,
                                  location: @loca,
                                  signup_confirm_url: "http://localhost:3000/confirm")

      user.errors.any?.should be false
    end # it "passes when facebook signup"
  end # describe "with nocaptcha"

  describe "student/teacher" do
    it "accepts student" do
      user = @user_manager.signup(remote_ip: "1.2.3.4",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "student1@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  location: @loca,
                                  student: true,
                                  signup_confirm_url: "http://localhost:3000/confirm")
      UserMailer.deliveries.length.should == 2
      user.errors.any?.should be false
      user = User.find(user.id)
      user.has_redeemable_jamtrack.should be true
      user.is_a_student.should be true
      user.is_a_teacher.should be false
      user.musician.should be true
    end

    it "accepts teacher" do
      user = @user_manager.signup(remote_ip: "1.2.3.4",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "student2@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  location: @loca,
                                  teacher: true,
                                  signup_confirm_url: "http://localhost:3000/confirm")
      UserMailer.deliveries.length.should == 2
      user.errors.any?.should be false
      user = User.find(user.id)
      user.has_redeemable_jamtrack.should be true
      user.is_a_student.should be false
      user.is_a_teacher.should be true
      user.musician.should be true
    end

    it "accepts teacher and student" do
      user = @user_manager.signup(remote_ip: "1.2.3.4",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "student3@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  location: @loca,
                                  teacher: true,
                                  signup_confirm_url: "http://localhost:3000/confirm")
      UserMailer.deliveries.length.should == 2
      user.errors.any?.should be false
      user = User.find(user.id)
      user.has_redeemable_jamtrack.should be true
      user.is_a_student.should be false
      user.is_a_teacher.should be true
      user.musician.should be true
    end

    it "lets user be a fan if nothing if musician/student/teacher is true" do
      user = @user_manager.signup(remote_ip: "1.2.3.4",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "student4@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  location: @loca,
                                  signup_confirm_url: "http://localhost:3000/confirm")
      UserMailer.deliveries.length.should == 2
      user.errors.any?.should be false
      user = User.find(user.id)
      user.has_redeemable_jamtrack.should be true
      user.is_a_student.should be false
      user.is_a_teacher.should be false
      user.musician.should be false
    end
  end
  describe "gift_card" do

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

    it "can succeed when specified" do
      user = @user_manager.signup(remote_ip: "1.2.3.4",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "giftcard1@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: true,
                                  location: @loca,
                                  signup_confirm_url: "http://localhost:3000/confirm",
                                  gift_card: gift_card.code)
      user.errors.any?.should be false
      gift_card.reload
      gift_card.user.should eq(user)
      user = User.find(user.id)
      user.has_redeemable_jamtrack.should be true
      user.gifted_jamtracks.should eq(5)
      user.gift_cards[0].should eq(gift_card)
    end

    it "will fail if invalid gift card code" do
      user = @user_manager.signup(remote_ip: "1.2.3.4",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "giftcard2@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: true,
                                  location: @loca,
                                  signup_confirm_url: "http://localhost:3000/confirm",
                                  gift_card: '')
      user.errors.any?.should be true
      user.errors["gift_card"].should eq(["not found"])
      user.gifted_jamtracks.should eq(0)
      gift_card.reload
      gift_card.user.should be_nil

      user.gift_cards.length.should eq(0)
    end

    it "will fail if used gift card" do
      gift_card.user = FactoryGirl.create(:user)

      user = @user_manager.signup(remote_ip: "1.2.3.4",
                                  first_name: "bob",
                                  last_name: "smith",
                                  email: "giftcard2@jamkazam.com",
                                  password: "foobar",
                                  password_confirmation: "foobar",
                                  terms_of_service: true,
                                  instruments: @instruments,
                                  musician: true,
                                  location: @loca,
                                  signup_confirm_url: "http://localhost:3000/confirm",
                                  gift_card: '')
      user.errors.any?.should be true
      user.errors["gift_card"].should eq(["not found"])
      user.gifted_jamtracks.should eq(0)
      gift_card.reload
      gift_card.user.should be_nil

      user.gift_cards.length.should eq(0)
    end
  end
end # test
