require 'spec_helper'

describe User do

  before(:each) do
    @user = FactoryGirl.create(:user, first_name: "Example", last_name: "User", email: "user@example.com",
                     password: "foobar", password_confirmation: "foobar", musician: true, email_confirmed: true,
                     city: "Apex", state: "NC", country: "USA")
  end

  it "should allow search of one user" do
    ws = User.search("Example User")
    ws.length.should == 1
    user_result = ws[0]
    user_result.first_name.should == @user.first_name
    user_result.last_name.should == @user.last_name
    user_result.id.should == @user.id
    user_result.location.should == @user.location
    user_result.musician.should == true
  end

  it "should delete user" do
    ws = User.search("Example User")
    ws.length.should == 1
    user_result = ws[0]
    user_result.id.should == @user.id

    @user.destroy

    ws = User.search("Example User")
    ws.length.should == 0
  end

  it "should update user" do
    ws = User.search("Example User")
    ws.length.should == 1
    user_result = ws[0]
    user_result.id.should == @user.id

    @user.first_name = "bonus-junk"
    @user.last_name = "more-junk"
    @user.save

    ws = User.search("Example User")
    ws.length.should == 0

    ws = User.search("Bonus")
    ws.length.should == 1
    user_result = ws[0]
    user_result.id.should == @user.id
    user_result.first_name.should == "bonus-junk"
  end

  it "should tokenize correctly" do
    @user2 = FactoryGirl.create(:user, first_name: "peaches", last_name: "test", email: "peach@example.com",
                     password: "foobar", password_confirmation: "foobar", musician: true, email_confirmed: true,
                     city: "Apex", state: "NC", country: "USA")   
    ws = User.search("pea")
    ws.length.should == 1
    user_result = ws[0]
    user_result.id.should == @user2.id
  end

  it "users who have signed up, but not confirmed should show up in search index due to VRFS-378" do
    @user3 = FactoryGirl.create(:user, first_name: "unconfirmed", last_name: "unconfirmed", email: "unconfirmed@example.com",
                     password: "foobar", password_confirmation: "foobar", musician: true, email_confirmed: false,
                     city: "Apex", state: "NC", country: "USA")
    ws = User.search("unconfirmed")
    ws.length.should == 1

    # Ok, confirm the user, and see them show up
    @user3.email_confirmed = true
    @user3.save

    ws = User.search("unconfirmed")
    ws.length.should == 1
    user_result = ws[0]
    user_result.id.should == @user3.id
   end
end