require 'spec_helper'

describe OnlinePresence do
  
  shared_examples_for :online_presence_specs do
    describe "index" do

      before(:all) do
        OnlinePresence.delete_all

        player1_presence1 = OnlinePresence.new({:player_id => player1.id, :username => "myonlineusername", :service_type => "facebook"})
        player1_presence1.save!

        player1_presence2 = OnlinePresence.new({:player_id => player1.id, :username => "myonlineusername", :service_type => "twitter"})
        player1_presence2.save!

        player2_presence1 = OnlinePresence.new({:player_id => player2.id, :username => "myonlineusername", :service_type => "soundcloud"})
        player2_presence1.save!
      end

      context "when request is valid" do
        it "should return all records for user" do
          presence = OnlinePresence.index({:id => player1.id})
          presence.count.should == 2

          presence = OnlinePresence.index({:id => player2.id})
          presence.count.should == 1
        end
      end

      context "when request is invalid" do
        it "should raise error when options are missing" do
          lambda{OnlinePresence.index}.should raise_error(StateError)
        end

        it "should raise error when user id is missing" do
          lambda{OnlinePresence.index({:id => ""})}.should raise_error(StateError)
        end
      end
    end

    describe "create" do

      before(:all) do
        OnlinePresence.delete_all
      end

      context "when request is valid" do
        it "should save successfully" do
          OnlinePresence.create(player1, {:player_id => player1.id, :service_type => "soundcloud", :username => "soundcloudplayer1"})

          # make sure we can save a second OnlinePresence for same user and type
          OnlinePresence.create(player1, {:player_id => player1.id, :service_type => "soundcloud", :username => "soundcloudplayer2"})

          OnlinePresence.index({:id => player1.id}).count.should == 2
        end
      end

      context "when request is not valid" do
        it "should raise JamPermissionError if requester id does not match id in request" do
          lambda{OnlinePresence.create(player1, {:player_id => player2.id, :service_type => "soundcloud", :username => "soundcloudplayer2"})}.should raise_error(JamPermissionError)
        end

        it "should raise error if service type is missing" do
          lambda{OnlinePresence.create(player1, {:player_id => player1.id, :username => "soundcloudplayer2"})}.should raise_error(StateError)
        end

        it "should raise error if username is missing" do
          lambda{OnlinePresence.create(player1, {:player_id => player1.id, :service_type => "soundcloud"})}.should raise_error(StateError)
        end

        it "should not allow duplicates of the same username / service type combination" do
          OnlinePresence.create(player1, {:player_id => player1.id, :service_type => "soundcloud", :username => "soundcloudplayer1"})
          OnlinePresence.index({:id => player1.id}).count.should == 1

          lambda{OnlinePresence.create(player1, {:player_id => player1.id, :service_type => "soundcloud", :username => "soundcloudplayer1"})}.should raise_error(ConflictError)
          OnlinePresence.index({:id => player1.id}).count.should == 1
        end
      end
    end

    describe "update" do

      before(:all) do
        OnlinePresence.delete_all
        @online_presence = OnlinePresence.new(:player_id => player1.id, :service_type => "soundcloud", :username => "soundcloudplayer1")
        @online_presence.save!
      end

      context "when request is valid" do
        it "should save successfully" do

          up_list = OnlinePresence.index({:id => player1.id})
          up_list.count.should == 1
          up_list.first.service_type.should == "soundcloud"
          up_list.first.username.should == "soundcloudplayer1"

          OnlinePresence.update(player1, {:id => @online_presence.id, :player_id => player1.id, :service_type => "soundcloud", :username => "soundcloudplayer2"})

          up_list = OnlinePresence.index({:id => player1.id})
          up_list.count.should == 1
          up_list.first.service_type.should == "soundcloud"
          up_list.first.username.should == "soundcloudplayer2"
        end
      end

      context "when request is not valid" do
        it "should raise JamPermissionError if requester id does not match id in request" do
          lambda{OnlinePresence.update(player1, {:player_id => player2.id, :id => @online_presence.id, :service_type => "soundcloud", :username => "soundcloudplayer2"})}.should raise_error(JamPermissionError)
        end

        it "should raise error if type is missing" do
          lambda{OnlinePresence.update(player1, {:player_id => player1.id, :id => @online_presence.id, :username => "soundcloudplayer2"})}.should raise_error(StateError)
        end

        it "should raise error if username is missing" do
          lambda{OnlinePresence.update(player1, {:player_id => player1.id, :id => @online_presence.id, :service_type => "soundcloud"})}.should raise_error(StateError)
        end

        it "should raise error if player presence id is missing" do
          lambda{OnlinePresence.update(player1, {:player_id => player1.id, :username => "soundcloudplayer2", :service_type => "soundcloud"})}.should raise_error(StateError)
        end
      end
    end

    describe "destroy" do

      before(:all) do
        OnlinePresence.delete_all
        @online_presence = OnlinePresence.new(:player_id => player1.id, :service_type => "soundcloud", :username => "soundcloudplayer1")
        @online_presence.save!
      end

      context "when request is valid" do
        it "should destroy successfully" do
          up_list = OnlinePresence.index({:id => player1.id})
          up_list.count.should == 1
          up_list.first.service_type.should == "soundcloud"
          up_list.first.username.should == "soundcloudplayer1"

          OnlinePresence.delete(player1, {:player_id => player1.id, :id => @online_presence.id})

          up_list = OnlinePresence.index({:id => player1.id})
          up_list.count.should == 0
        end
      end

      context "when request is not valid" do
        it "should raise JamPermissionError if requester id does not match id in request" do
          lambda{OnlinePresence.delete(player2, {:player_id => player1.id, :id => @online_presence.id})}.should raise_error(JamPermissionError)
        end

        it "should raise error if player presence id is missing" do
          lambda{OnlinePresence.delete(player1, {:player_id => player1.id})}.should raise_error(StateError)
        end
      end
    end
  end # shared

  describe "with a user" do
    it_should_behave_like :online_presence_specs do
      let(:player1) { FactoryGirl.create(:user) }
      let(:player2) { FactoryGirl.create(:user) }
    end

    after(:all) {
      Band.delete_all
    }
  end

  describe "with a band" do
    it_should_behave_like :online_presence_specs do
      let(:player1) { FactoryGirl.create(:band) }
      let(:player2) { FactoryGirl.create(:band) }
    end

    after(:all) {
      Band.delete_all
    }

  end
end