require 'spec_helper'

describe JoinRequest do

  it 'can create a join request' do
    user1 = FactoryGirl.create(:user)
    music_session = FactoryGirl.create(:active_music_session, :creator => user1)
    music_session_member1 = FactoryGirl.create(:connection, :user => user1, :music_session => music_session)
    join_request = JoinRequest.new(:user => user1, :music_session => music_session.music_session, :text => "Let me join yo")

    join_request.save.should be_true

    join_requests = JoinRequest.index(user1)
    join_requests.length.should == 1
    join_requests[0].id.should == join_request.id
  end

  it 'fans cant create a join request' do
    user1 = FactoryGirl.create(:user, :musician => true)
    user2 = FactoryGirl.create(:user, :musician => false)
    music_session = FactoryGirl.create(:active_music_session, :creator => user1)
    music_session_member1 = FactoryGirl.create(:connection, :user => user1, :music_session => music_session)
    join_request = JoinRequest.new(:user => user2, :music_session => music_session.music_session, :text => "Let me join yo")

    join_request.save.should be_false
    join_request.errors.size.should == 1
    join_request.errors.get(:user).should == [JoinRequest::REQUESTOR_MUST_BE_A_MUSICIAN]
  end

  it 'cant create a dup join_request' do
    user1 = FactoryGirl.create(:user)
    music_session = FactoryGirl.create(:active_music_session, :creator => user1)
    music_session_member1 = FactoryGirl.create(:connection, :user => user1, :music_session => music_session)
    join_request = JoinRequest.new(:user => user1, :music_session => music_session.music_session, :text => "Let me join yo")
    join_request.save.should be_true

    join_request2 = JoinRequest.new(:user => user1, :music_session => music_session.music_session, :text => "Let me join yo")

    join_request2.save.should be_false
    join_request2.errors.get(:user_id) == ["has already been taken"]
  end

  it "cant contain profanity in the text" do
    user1 = FactoryGirl.create(:user)
    music_session = FactoryGirl.create(:active_music_session, :creator => user1)
    music_session_member1 = FactoryGirl.create(:connection, :user => user1, :music_session => music_session)
    join_request = JoinRequest.new(:user => user1, :music_session => music_session.music_session, :text => "fuck you")
    join_request.save
    join_request.valid?.should be_false
  end
end
