require 'spec_helper'

describe Hostinator do
  after { Hostinator.reset }

  describe ".client" do
    it "should be a Hostinator::Client" do
      expect(Hostinator.client).to be_a Hostinator::Client
    end

    it "should not override each other" do
      client1 = Hostinator.client(endpoint: 'https://api1.example.com', private_token: '001')
      client2 = Hostinator.client(endpoint: 'https://api2.example.com', private_token: '002')
      expect(client1.endpoint).to eq('https://api1.example.com')
      expect(client2.endpoint).to eq('https://api2.example.com')
      expect(client1.private_token).to eq('001')
      expect(client2.private_token).to eq('002')
    end

    it "should set private_token to the auth_token when provided" do
      client = Hostinator.client(endpoint: 'https://api2.example.com', auth_token: '3225e2804d31fea13fc41fc83bffef00cfaedc463118646b154acc6f94747603')
      expect(client.private_token).to eq('3225e2804d31fea13fc41fc83bffef00cfaedc463118646b154acc6f94747603')
    end
  end

  describe ".actions" do
    it "should return an array of client methods" do
      actions = Hostinator.actions
      expect(actions).to be_an Array
      expect(actions.first).to be_a Symbol
      expect(actions.sort.first).to eq(:accept_merge_request)
    end
  end

  describe ".endpoint=" do
    it "should set endpoint" do
      Hostinator.endpoint = 'https://api.example.com'
      expect(Hostinator.endpoint).to eq('https://api.example.com')
    end
  end

  describe ".private_token=" do
    it "should set private_token" do
      Hostinator.private_token = 'secret'
      expect(Hostinator.private_token).to eq('secret')
    end
  end

  describe ".auth_token=" do
    it "should set auth_token", focus: true do
      Hostinator.auth_token = 'auth_secret'
      expect(Hostinator.private_token).to eq('auth_secret')
    end
  end

  describe ".sudo=" do
    it "should set sudo" do
      Hostinator.sudo = 'user'
      expect(Hostinator.sudo).to eq('user')
    end
  end

  describe ".user_agent" do
    it "should return default user_agent" do
      expect(Hostinator.user_agent).to eq(Hostinator::Configuration::DEFAULT_USER_AGENT)
    end
  end

  describe ".user_agent=" do
    it "should set user_agent" do
      Hostinator.user_agent = 'Custom User Agent'
      expect(Hostinator.user_agent).to eq('Custom User Agent')
    end
  end

  describe ".configure" do
    Hostinator::Configuration::VALID_OPTIONS_KEYS.each do |key|
      it "should set #{key}" do
        Hostinator.configure do |config|
          config.send("#{key}=", key)
          expect(Hostinator.send(key)).to eq(key)
        end
      end
    end
  end

  describe ".http_proxy" do
    it "delegates the method to Hostinator::Request" do
      Hostinator.endpoint = 'https://api.example.com'
      request = class_spy(Hostinator::Request).as_stubbed_const

      Hostinator.http_proxy('fazbearentertainment.com', 1987, 'ffazbear', 'itsme')
      expect(request).to have_received(:http_proxy).
        with('fazbearentertainment.com', 1987, 'ffazbear', 'itsme')
    end
  end
end
