require 'spec_helper'

describe "Accept Friend Request", :js => true, :type => :feature, :capybara_feature => true do

  before(:all) do
    User.delete_all # we delete all users due to the use of find_musician() helper method, which scrolls through all users
  end

  let (:friend_request) { FactoryGirl.create(:friend_request, user: @user2, friend: @user1) }

  before(:each) do
    stub_const("APP_CONFIG", web_config)
    @user1 = FactoryGirl.create(:user)
    @user2 = FactoryGirl.create(:user, first_name: 'bone_crusher')
    sign_in_poltergeist(@user1)
  end

  describe "dialog behavior" do

    describe "launch states" do

      it "happy path" do
        # users are not friends yet, and this request has not been dealt with
        visit '/'
        should_be_at_root
        visit Nav.accept_friend_request_dialog(friend_request.id)

        find('h1', text: 'friend request')
        find('#accept-friend-request-dialog .btn-accept-friend-request', text: 'ACCEPT').trigger(:click)
        page.should_not have_selector('h1', text: 'friend request')
        friend_request.reload
        friend_request.status.should == 'accept'

        # make sure the friend list is refreshed
        find("[layout-id=\"panelFriends\"] .friend-name[user-id=\"#{@user2.id}\"]", visible: false)
      end

      it "already accepted" do
        # users are not friends yet, and this request has not been dealt with
        friend_request.status = 'accept'
        friend_request.save!
        visit '/'
        should_be_at_root
        visit Nav.accept_friend_request_dialog(friend_request.id)

        find('h1', text: 'friend request')
        find('.accept-friend-msg', text: "This friend request from #{@user2.name} is no longer valid.")
        find('#accept-friend-request-dialog .btn-close-dialog', text: 'CLOSE').trigger(:click)
        page.should_not have_selector('h1', text: 'friend request')
      end

      it "already friends" do
        FactoryGirl.create(:friendship, user: @user1, friend: @user2)
        FactoryGirl.create(:friendship, user: @user2, friend: @user1)

        visit '/'
        should_be_at_root
        visit Nav.accept_friend_request_dialog(friend_request.id)

        find('h1', text: 'friend request')
        find('.accept-friend-msg', text: "You are already friends with #{@user2.name}.")
        find('#accept-friend-request-dialog .btn-close-dialog', text: 'CLOSE').trigger(:click)
        page.should_not have_selector('h1', text: 'friend request')
      end

      it "same user seeing own friend request" do
        user3 = FactoryGirl.create(:user)
        friend_request.friend = @user2
        friend_request.user = @user1
        friend_request.save!

        visit '/'
        should_be_at_root
        visit Nav.accept_friend_request_dialog(friend_request.id)

        find('h1', text: 'friend request')
        find('.generic-error-msg', 'You can\'t become friends with yourself.')
        find('#accept-friend-request-dialog .btn-close-dialog', text: 'CLOSE').trigger(:click)
        page.should_not have_selector('h1', text: 'friend request')
      end

      it "no longer exists" do
        visit '/'
        should_be_at_root
        visit Nav.accept_friend_request_dialog('junk')

        find('h1', text: 'friend request')
        find('.generic-error-msg', 'This friend request no longer exists.')
        find('#accept-friend-request-dialog .btn-close-dialog', text: 'CLOSE').trigger(:click)
        page.should_not have_selector('h1', text: 'friend request')
      end

      it "no permission" do
        user3 = FactoryGirl.create(:user)
        friend_request.friend = user3
        friend_request.save!

        visit '/'
        should_be_at_root
        visit Nav.accept_friend_request_dialog(friend_request.id)

        find('h1', text: 'friend request')
        find('.generic-error-msg', 'You do not have permission to access this information.')
        find('#accept-friend-request-dialog .btn-close-dialog', text: 'CLOSE').trigger(:click)
        page.should_not have_selector('h1', text: 'friend request')
      end
    end
  end
end
