require 'spec_helper'
describe ApiJamblastersController, type: :controller do
  render_views

  let(:user) { FactoryGirl.create(:user) }
  let(:jamblaster) { FactoryGirl.create(:jamblaster, user: user) }

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

  before(:each) do
    JamblasterUser.delete_all
    JamblasterPairingRequest.delete_all
    Jamblaster.delete_all
  end

  describe "can_pair" do
    it "works" do
      connection1 = FactoryGirl.create(:connection, :user => user, client_id: 'abc1')

      get :can_pair, {:format => 'json', jbid: connection1.client_id, user_id: user.id}
      response.status.should == 200
    end
  end

  describe "is_allowed" do
    describe "already associated" do
      before(:each) do
        user.jamblasters << jamblaster
        user.save!
      end

      it "works" do
        get :is_allowed, {:format => 'json', jbid: jamblaster.client_id, user_id: user.id}
        response.status.should == 200
      end


      #it "disallows different user" do
      #  user2 = FactoryGirl.create(:user)
      #  get :is_allowed, {:format => 'json', jbid: jamblaster.client_id, user_id: user2.id}
      #  response.status.should == 403
      #end
    end

    describe "not already associated" do
      it "works" do
        get :is_allowed, {:format => 'json', jbid: jamblaster.client_id, user_id: user.id}
        response.status.should == 200
      end
    end
  end

  describe "auth_users" do
    describe "already associated" do
      before(:each) do
        user.jamblasters << jamblaster
        user.save!
      end

      it "works" do
        get :auth_users, {:format => 'json', jbid: jamblaster.client_id, serial_no: jamblaster.serial_no}
        response.status.should == 200
        json = JSON.parse(response.body)
        json["pairing"].should be nil
        json["users"].length.should eq(1)
        json["users"][0]["id"].should eql user.id

        pairing1 = FactoryGirl.create(:jamblaster_pairing_request, user: user, jamblaster: jamblaster, vtoken:'token2', sibling_key:nil)

        get :auth_users, {:format => 'json', jbid: jamblaster.client_id, serial_no: jamblaster.serial_no}
        response.status.should == 200
        json = JSON.parse(response.body)
        json["pairing"].should be nil

        pairing1.activate('abc')
        pairing1.errors.any?.should be false

        get :auth_users, {:format => 'json', jbid: jamblaster.client_id, serial_no: jamblaster.serial_no}
        response.status.should == 200
        json = JSON.parse(response.body)
        json["pairing"]["key"].should eql 'abc'
      end
    end

    describe "not already associated" do
      it "works" do
        get :auth_users, {:format => 'json', jbid: jamblaster.client_id, serial_no: jamblaster.serial_no}
        response.status.should == 200
        json = JSON.parse(response.body)
        json["pairing"].should be nil
        json["users"].length.should eq(0)
      end
    end

  end

  describe "get_tokens" do
    before(:each) {
      controller.current_user = user
    }

    it "works" do
      get :get_tokens, {:format => 'json'}
      response.status.should == 200
      json = JSON.parse(response.body)
      json.length.should eq(0)

      # associate Jamblaster
      jamblaster = FactoryGirl.create(:jamblaster, user: user)

      user.jamblasters << jamblaster
      user.save!

      get :get_tokens, {:format => 'json'}
      response.status.should == 200
      json = JSON.parse(response.body)
      json.length.should eq(1)
    end
  end

  describe "start_pairing" do

    before(:each) do
      controller.current_user = user
    end
    describe "already associated" do
      before(:each) do

        user.jamblasters << jamblaster
        user.save!
      end

      it "works" do
        post :start_pairing, {:format => 'json', jbid: jamblaster.client_id, vtoken: 'vtoken'}
        response.status.should == 200
        json = JSON.parse(response.body)

        request = JamblasterPairingRequest.where(jamblaster_id: jamblaster.id).first
        request.should_not be_nil
        request.user.should eql(user)
        request.vtoken.should eq 'vtoken'

      end

      it "returns 422 if bogus jamblaster" do
        post :start_pairing, {:format => 'json', jbid: 'nada', vtoken: 'vtoken'}
        json = JSON.parse(response.body)
        response.status.should == 422
        json = JSON.parse(response.body)
      end

      it "returns 422 if restart pairing" do

      end
    end

    describe "not already associated" do

      it "works" do
        post :start_pairing, {:format => 'json', jbid: jamblaster.client_id, vtoken: 'vtoken'}
        response.status.should == 200
        json = JSON.parse(response.body)

        request = JamblasterPairingRequest.where(jamblaster_id: jamblaster.id).first
        request.should_not be_nil
        request.user.should eql(user)
        request.vtoken.should eq 'vtoken'

      end

      it "returns 422 if bogus jamblaster" do
        post :start_pairing, {:format => 'json', jbid: 'nada', vtoken: 'vtoken'}
        json = JSON.parse(response.body)
        response.status.should == 422
        json = JSON.parse(response.body)
      end

      it "returns 422 if restart pairing" do

      end
    end

  end

  describe "login" do

    before(:each) do
      controller.current_user = user
    end

    describe "already associated" do
      before(:each) do

        user.jamblasters << jamblaster
        user.save!
      end

      it "works" do
        post :start_pairing, {:format => 'json', jbid: jamblaster.client_id, vtoken: 'vtoken2'}
        response.status.should == 200

        request = JamblasterPairingRequest.where(jamblaster_client_id: jamblaster.client_id, vtoken: 'vtoken2', user_id: user.id).first
        request.should_not be_nil
        request.user.should eql(user)
        request.vtoken.should eq 'vtoken2'
        request.jamblaster_client_id.should eq jamblaster.client_id

        post :login, {:format => 'json', jbid: jamblaster.client_id, serial_no: jamblaster.serial_no, user_id: user.id, vtoken: 'vtoken2'}
        json = JSON.parse(response.body)
        response.status.should == 200
      end
    end

    describe "not already associated" do
      it "works" do
        post :start_pairing, {:format => 'json', jbid: jamblaster.client_id, vtoken: 'vtoken2'}
        response.status.should == 200

        request = JamblasterPairingRequest.where(jamblaster_client_id: jamblaster.client_id, vtoken: 'vtoken2', user_id: user.id).first
        request.should_not be_nil
        request.user.should eql(user)
        request.vtoken.should eq 'vtoken2'
        request.jamblaster_client_id.should eq jamblaster.client_id

        post :login, {:format => 'json', jbid: jamblaster.client_id, serial_no: jamblaster.serial_no, user_id: user.id, vtoken: 'vtoken2'}
        json = JSON.parse(response.body)
        response.status.should == 200
      end
    end


  end

  describe "pair" do
    before(:each) do
      controller.current_user = user
    end

    describe "already associated" do
      before(:each) do

        user.jamblasters << jamblaster
        user.save!
      end

      it "works" do
        post :start_pairing, {:format => 'json', jbid: jamblaster.client_id, vtoken: 'vtoken4'}
        response.status.should == 200

        request = JamblasterPairingRequest.where(jamblaster_client_id: jamblaster.client_id, vtoken: 'vtoken4', user_id: user.id).first
        request.should_not be_nil
        request.user.should eql(user)
        request.vtoken.should eq 'vtoken4'
        request.jamblaster_client_id.should eq jamblaster.client_id

        post :login, {:format => 'json', jbid: jamblaster.client_id, serial_no: jamblaster.serial_no, user_id: user.id, vtoken: 'vtoken4'}
        json = JSON.parse(response.body)
        response.status.should == 200

        post :pair, {:format => 'json', vtoken: 'vtoken4', user_id: user.id, jbid: jamblaster.client_id, key: 'abc'}
        response.status.should == 200
        json = JSON.parse(response.body)
        json["id"].should eq jamblaster.id

        get :get_tokens, {:format => 'json'}
        response.status.should == 200
        json = JSON.parse(response.body)
        json.length.should eq(1)
        key = json[0]["pairing"]["key"]
        key.should eq("abc")
      end
    end

    describe "not already associated" do
      it "works" do
        post :start_pairing, {:format => 'json', jbid: jamblaster.client_id, vtoken: 'vtoken4'}
        response.status.should == 200

        request = JamblasterPairingRequest.where(jamblaster_client_id: jamblaster.client_id, vtoken: 'vtoken4', user_id: user.id).first
        request.should_not be_nil
        request.user.should eql(user)
        request.vtoken.should eq 'vtoken4'
        request.jamblaster_client_id.should eq jamblaster.client_id

        post :login, {:format => 'json', jbid: jamblaster.client_id, serial_no: jamblaster.serial_no, user_id: user.id, vtoken: 'vtoken4'}
        json = JSON.parse(response.body)
        response.status.should == 200

        jamblaster.reload
        jamblaster.users.include?(user).should be false

        post :pair, {:format => 'json', vtoken: 'vtoken4', user_id: user.id, jbid: jamblaster.client_id, key: 'abc'}
        response.status.should == 200
        json = JSON.parse(response.body)
        json["id"].should eq jamblaster.id

        get :get_tokens, {:format => 'json'}
        response.status.should == 200
        json = JSON.parse(response.body)
        json.length.should eq(1)
        key = json[0]["pairing"]["key"]
        key.should eq("abc")

        jamblaster.reload
        jamblaster.users.include?(user).should be true
      end

      it "across two users" do
        post :start_pairing, {:format => 'json', jbid: jamblaster.client_id, vtoken: 'vtoken4'}
        response.status.should == 200

        request = JamblasterPairingRequest.where(jamblaster_client_id: jamblaster.client_id, vtoken: 'vtoken4', user_id: user.id).first
        request.should_not be_nil
        request.user.should eql(user)
        request.vtoken.should eq 'vtoken4'
        request.jamblaster_client_id.should eq jamblaster.client_id

        post :login, {:format => 'json', jbid: jamblaster.client_id, serial_no: jamblaster.serial_no, user_id: user.id, vtoken: 'vtoken4'}
        json = JSON.parse(response.body)
        response.status.should == 200

        jamblaster.reload
        jamblaster.users.include?(user).should be false

        post :pair, {:format => 'json', vtoken: 'vtoken4', user_id: user.id, jbid: jamblaster.client_id, key: 'abc'}
        response.status.should == 200
        json = JSON.parse(response.body)
        json["id"].should eq jamblaster.id

        get :get_tokens, {:format => 'json'}
        response.status.should == 200
        json = JSON.parse(response.body)
        json.length.should eq(1)
        key = json[0]["pairing"]["key"]
        key.should eq("abc")

        jamblaster.reload
        jamblaster.users.include?(user).should be true


        # now try with second user!
        controller.current_user = user2


        post :start_pairing, {:format => 'json', jbid: jamblaster.client_id, vtoken: 'vtoken5'}
        response.status.should == 200

        puts "JSON.parse(response.body) #{JSON.parse(response.body)}"
        request = JamblasterPairingRequest.where(jamblaster_client_id: jamblaster.client_id, vtoken: 'vtoken5', user_id: user2.id).first
        request.should_not be_nil
        request.user.should eql(user2)
        request.vtoken.should eq 'vtoken5'
        request.jamblaster_client_id.should eq jamblaster.client_id

        post :login, {:format => 'json', jbid: jamblaster.client_id, serial_no: jamblaster.serial_no, user_id: user2.id, vtoken: 'vtoken5'}
        json = JSON.parse(response.body)
        response.status.should == 200

        jamblaster.reload
        jamblaster.users.include?(user2).should be false

        post :pair, {:format => 'json', vtoken: 'vtoken5', user_id: user2.id, jbid: jamblaster.client_id, key: 'abcd'}
        response.status.should == 200
        json = JSON.parse(response.body)
        json["id"].should eq jamblaster.id

        get :get_tokens, {:format => 'json'}
        response.status.should == 200
        json = JSON.parse(response.body)
        json.length.should eq(1)
        key = json[0]["pairing"]["key"]
        key.should eq("abcd")

        jamblaster.reload
        jamblaster.users.include?(user).should be true
        jamblaster.users.include?(user2).should be true
      end
    end
  end

  describe "logged in" do
    before(:each) do
      controller.current_user = user
    end

    it "get_tokens" do
      get :get_tokens, {:format => 'json'}
      response.status.should == 200
    end

    it "start_pairing" do
      post :start_pairing, {:format => 'json'}
      response.status.should == 422
    end

    it "pair" do
      post :pair, {:format => 'json'}
      response.status.should == 404
    end
  end

  describe "not logged in" do
    before(:each) do
      controller.current_user = nil
    end

    it "get_tokens" do
      get :get_tokens, {:format => 'json'}
      response.status.should == 403
    end

    it "start_pairing" do
      post :start_pairing, {:format => 'json'}
      response.status.should == 403
    end

    it "pair" do
      post :pair, {:format => 'json'}
      response.status.should == 403
    end
  end
end
