require 'spec_helper'

describe Friendship do

  let(:user1) { FactoryGirl.create(:user) }
  let(:user2) { FactoryGirl.create(:user) }

  before(:each) do
    Friendship.save_using_models(user1, user2)
  end

  it "can create two-way friendship" do
    user1.friends?(user2).should be_true
    user2.friends?(user1).should be_true
  end

  it "should track user progression" do
    user1.first_friended_at.should_not be_nil
    user2.first_friended_at.should_not be_nil
  end

  it "should accept a single request" do
    user1 = FactoryGirl.create(:user)
    user2 = FactoryGirl.create(:user)

    req1 = FriendRequest.new
    req1.user = user1
    req1.friend = user2
    req1.save!

    FriendRequest.save(req1.id, user1.id, user2.id, 'accept', nil)

    requests = FriendRequest.where('user_id = ? AND friend_id = ? AND status = ?', user1.id, user2.id, 'accept')
    requests.count.should == 1
  end

  it "should update duplicate requests" do
    user1 = FactoryGirl.create(:user)
    user2 = FactoryGirl.create(:user)

    req1 = FriendRequest.new
    req1.user = user1
    req1.friend = user2
    req1.save!

    req2 = FriendRequest.new
    req2.user = user1
    req2.friend = user2
    req2.save!

    FriendRequest.save(req1.id, user1.id, user2.id, 'accept', nil)

    requests = FriendRequest.where('user_id = ? AND friend_id = ? AND status = ?', user1.id, user2.id, 'accept')
    requests.count.should == 2
  end
end
