require 'fullcontact'
require 'helper'

describe FullContact do
  after do
    FullContact.reset
  end

  context "when delegating to a client" do

    before do
      FullContact.configure do |config|
        config.api_key = "api_key"
      end

      stub_get("person.json").
          with(:query => {:apiKey => "api_key", :email => "brawest@gmail.com"}).
          to_return(:body => fixture("person.json"), :headers => {:content_type => "application/json; charset=utf-8"})

      stub_get("person.json").
          with(:query => {:apiKey => "api_key", :twitter => "brawtest"}).
          to_return(:body => fixture("person.json"), :headers => {:content_type => "application/json; charset=utf-8"})

      stub_get("person.json").
          with(:query => {:email => "brawest@gmail.com"})
    end

    context "when using query auth type" do

      before do
        FullContact.configure do |config|
          config.auth_type = :query
        end
      end

      it "should get the correct resource" do
        FullContact.person(email: "brawest@gmail.com")
        a_get("person.json")
            .with(:query => {:apiKey => "api_key", :email => "brawest@gmail.com"})
            .should have_been_made
      end
    end

    context "when using header auth type" do

      before do
        FullContact.configure do |config|
          config.auth_type = :header
        end
      end

      it "should get the correct resource" do
        FullContact.person(email: "brawest@gmail.com")
        a_get("person.json")
            .with(:query => {:email => "brawest@gmail.com"}, :headers => {:'X-Fullcontact-Apikey'=>'api_key'})
            .should have_been_made
      end
    end

    it "should return the same results as a client by email" do
      FullContact.person(:email => "brawest@gmail.com").should == FullContact::Client.new.person(:email => "brawest@gmail.com")
    end

    it "should return the same results as a client by twitter" do
      FullContact.person(:twitter => "brawtest").should == FullContact::Client.new.person(:twitter => "brawtest")
    end
  end

  describe ".client" do
    it "should be a FullContact::Client" do
      FullContact.client.should be_a FullContact::Client
    end
  end

  describe ".adapter" do
    it "should return the default adapter" do
      FullContact.adapter.should == FullContact::Configuration::DEFAULT_ADAPTER
    end
  end

  describe ".adapter=" do
    it "should set the adapter" do
      FullContact.adapter = :typhoeus
      FullContact.adapter.should == :typhoeus
    end
  end

  describe ".endpoint" do
    it "should return the default endpoint" do
      FullContact.endpoint.should == FullContact::Configuration::DEFAULT_ENDPOINT
    end
  end

  describe ".endpoint=" do
    it "should set the endpoint" do
      FullContact.endpoint = 'http://tumblr.com/'
      FullContact.endpoint.should == 'http://tumblr.com/'
    end
  end

  describe ".format" do
    it "should return the default format" do
      FullContact.format.should == FullContact::Configuration::DEFAULT_FORMAT
    end
  end

  describe ".format=" do
    it "should set the format" do
      FullContact.format = 'xml'
      FullContact.format.should == 'xml'
    end
  end

  describe ".user_agent" do
    it "should return the default user agent" do
      FullContact.user_agent.should == FullContact::Configuration::DEFAULT_USER_AGENT
    end
  end

  describe ".user_agent" do
    it "should return the default user agent" do
      expect(FullContact.user_agent).to satisfy { |ua| ua.start_with? 'FullContact Ruby Client/' }
    end
  end

  describe ".user_agent=" do
    it "should set the user_agent" do
      FullContact.user_agent = 'Custom User Agent'
      FullContact.user_agent.should == 'Custom User Agent'
    end
  end

  describe ".configure" do

    FullContact::Configuration::VALID_OPTIONS_KEYS.each do |key|

      it "should set the #{key}" do
        FullContact.configure do |config|
          config.send("#{key}=", key)
          FullContact.send(key).should == key
        end
      end
    end
  end
end
