require 'spec_helper'

describe ActiveMusicSession do

  it 'cant create invitation to non-friend' do

    user1 = FactoryGirl.create(:user)  # in the jam session
    user2 = FactoryGirl.create(:user)  # in the jam session

    music_session = FactoryGirl.create(:active_music_session, :creator => user1)

    music_session_member1 = FactoryGirl.create(:connection, :user => user1, :music_session => music_session, :ip_address => "1.1.1.1", :client_id => "1")
    music_session_member2 = FactoryGirl.create(:connection, :user => user2, :music_session => music_session, :ip_address => "2.2.2.2", :client_id => "2")

    invitation = Invitation.new(:sender => user1, :receiver => user2, :music_session => music_session.music_session)

    invitation.save
    invitation.save.should be_false
    invitation.errors.size.should == 1
    invitation.errors.get(:receiver).should == [Invitation::FRIENDSHIP_REQUIRED_VALIDATION_ERROR]
  end

  it 'can create invitation to friend' do

    user1 = FactoryGirl.create(:user)  # in the jam session
    user2 = FactoryGirl.create(:user)  # in the jam session

    music_session = FactoryGirl.create(:active_music_session, :creator => user1)

    music_session_member1 = FactoryGirl.create(:connection, :user => user1, :music_session => music_session, :ip_address => "1.1.1.1", :client_id => "1")
    music_session_member2 = FactoryGirl.create(:connection, :user => user2, :music_session => music_session, :ip_address => "2.2.2.2", :client_id => "2")

    FactoryGirl.create(:friendship, :user => user1, :friend => user2)
    FactoryGirl.create(:friendship, :user => user2, :friend => user1)

    invitation = Invitation.new(:sender => user1, :receiver => user2, :music_session => music_session.music_session)

    invitation.save.should be_true
  end

  it 'can create invitation to a user who made a join_request' do
    user1 = FactoryGirl.create(:user)  # in the jam session
    user2 = FactoryGirl.create(:user)  # in the jam session

    music_session = FactoryGirl.create(:active_music_session, :creator => user1)

    music_session_member1 = FactoryGirl.create(:connection, :user => user1, :music_session => music_session, :ip_address => "1.1.1.1", :client_id => "1")
    connection2 = FactoryGirl.create(:connection, :user => user2, :ip_address => "2.2.2.2", :client_id => "2")

    join_request = FactoryGirl.create(:join_request, :user => user2, :music_session => music_session.music_session)

    invitation = Invitation.new(:sender => user1, :receiver => user2, :music_session => music_session.music_session, :join_request => join_request)

    invitation.save.should be_true
  end

  it 'cant create invitation to a user who did not make a join_request and is not a friend' do
    user1 = FactoryGirl.create(:user)  # in the jam session
    user2 = FactoryGirl.create(:user)  # in the jam session

    music_session = FactoryGirl.create(:active_music_session, :creator => user1)
    music_session2 = FactoryGirl.create(:active_music_session, :creator => user1)

    music_session_member1 = FactoryGirl.create(:connection, :user => user1, :music_session => music_session, :ip_address => "1.1.1.1", :client_id => "1")
    connection2 = FactoryGirl.create(:connection, :user => user2, :ip_address => "2.2.2.2", :client_id => "2")

    join_request = FactoryGirl.create(:join_request, :user => user2, :music_session => music_session2.music_session)

    invitation = Invitation.new(:sender => user1, :receiver => user2, :music_session => music_session.music_session, :join_request => join_request)

    invitation.save.should be_false
    invitation.errors.get(:join_request).should == [Invitation::JOIN_REQUEST_IS_NOT_FOR_RECEIVER_AND_MUSIC_SESSION ]
  end
end
