<commit_before># Copyright (c) 2009-2012 VMware, Inc.

require 'spec_helper'

describe Bosh::Cli::Director do

  DUMMY_TARGET = 'https://target.example.com:8080'

  before do
    URI.should_receive(:parse).with(DUMMY_TARGET).and_call_original
    Resolv.should_receive(:getaddresses).with('target.example.com').and_return(['127.0.0.1'])
    @director = Bosh::Cli::Director.new(DUMMY_TARGET, 'user', 'pass')
    @director.stub(retry_wait_interval: 0)
  end

  describe 'checking availability' do
    it 'waits until director is ready' do
      @director.should_receive(:get_status).and_raise(Bosh::Cli::DirectorError)
      @director.should_receive(:get_status).and_return(cpi: 'aws')

      @director.wait_until_ready
    end
  end

  describe 'fetching status' do
    it 'tells if user is authenticated' do
      @director.should_receive(:get).with('/info', 'application/json').
          and_return([200, JSON.generate('user' => 'adam')])
      @director.authenticated?.should == true
    end

    it 'tells if user not authenticated' do
      @director.should_receive(:get).with('/info', 'application/json').
          and_return([403, 'Forbidden'])
      @director.authenticated?.should == false

      @director.should_receive(:get).with('/info', 'application/json').
          and_return([500, 'Error'])
      @director.authenticated?.should == false

      @director.should_receive(:get).with('/info', 'application/json').
          and_return([404, 'Not Found'])
      @director.authenticated?.should == false

      @director.should_receive(:get).with('/info', 'application/json').
          and_return([200, JSON.generate('user' => nil, 'version' => 1)])
      @director.authenticated?.should == false

      # Backward compatibility
      @director.should_receive(:get).with('/info', 'application/json').
          and_return([200, JSON.generate('status' => 'ZB')])
      @director.authenticated?.should == true
    end
  end

  describe 'interface REST API' do
    it 'has helper methods for HTTP verbs which delegate to generic request' do
      [:get, :put, :post, :delete].each do |verb|
        @director.should_receive(:request).with(verb, :arg1, :arg2)
        @director.send(verb, :arg1, :arg2)
      end
    end
  end

  describe 'API calls' do
    it 'creates user' do
      @director.should_receive(:post).
          with('/users', 'application/json',
               JSON.generate('username' => 'joe', 'password' => '<PASSWORD>')).
          and_return(true)
      @director.create_user('joe', 'pass')<commit_msg>backfill list_vms test for cli director client<commit_after># Copyright (c) 2009-2012 VMware, Inc.

require 'spec_helper'

describe Bosh::Cli::Director do

  DUMMY_TARGET = 'https://target.example.com:8080'

  before do
    URI.should_receive(:parse).with(DUMMY_TARGET).and_call_original
    Resolv.should_receive(:getaddresses).with('target.example.com').and_return(['127.0.0.1'])
    @director = Bosh::Cli::Director.new(DUMMY_TARGET, 'user', 'pass')
    @director.stub(retry_wait_interval: 0)
  end

  describe 'checking availability' do
    it 'waits until director is ready' do
      @director.should_receive(:get_status).and_raise(Bosh::Cli::DirectorError)
      @director.should_receive(:get_status).and_return(cpi: 'aws')

      @director.wait_until_ready
    end
  end

  describe 'fetching status' do
    it 'tells if user is authenticated' do
      @director.should_receive(:get).with('/info', 'application/json').
          and_return([200, JSON.generate('user' => 'adam')])
      @director.authenticated?.should == true
    end

    it 'tells if user not authenticated' do
      @director.should_receive(:get).with('/info', 'application/json').
          and_return([403, 'Forbidden'])
      @director.authenticated?.should == false

      @director.should_receive(:get).with('/info', 'application/json').
          and_return([500, 'Error'])
      @director.authenticated?.should == false

      @director.should_receive(:get).with('/info', 'application/json').
          and_return([404, 'Not Found'])
      @director.authenticated?.should == false

      @director.should_receive(:get).with('/info', 'application/json').
          and_return([200, JSON.generate('user' => nil, 'version' => 1)])
      @director.authenticated?.should == false

      # Backward compatibility
      @director.should_receive(:get).with('/info', 'application/json').
          and_return([200, JSON.generate('status' => 'ZB')])
      @director.authenticated?.should == true
    end
  end

  describe 'interface REST API' do
    it 'has helper methods for HTTP verbs which delegate to generic request' do
      [:get, :put, :post, :delete].each do |verb|
        @director.should_receive(:request).with(verb, :arg1, :arg2)
        @director.send(verb, :arg1, :arg2)
      end
    end
  end

  describe 'API calls' do
    describe '#list_vms' do
      let(:http_client) { double('HTTPClient').as_null_object }
      let(:response) { double('Response', body: response_body, code: 200, headers: {}) }
      let(:request_headers) { {'Content-Type' => 'application/json', 'Authorization' => 'Basic dXNlcjpwYXNz' } }

      before do
        HTTPClient.stub(new: http_client)
        http_client.stub(:request).with(:get, "https://127.0.0.1:8080/#{endpoint}", body: request_body, header: request_headers).and_return(response)
      end

      let(:vms) do
        [{
             'agent_id' => 'agent-id1',
             'cid' => 'vm-id1',
             'job' => 'dummy',
             'index' => 0},
         {
             'agent_id' => 'agent-id2',
             'cid' => 'vm-id2',
             'job' => 'dummy',
             'index' => 1
         },
         {
             'agent_id' => 'agent-id3',
             'cid' => 'vm-id3',
             'job' => 'dummy',
             'index' => 2
         }]
      end

      let(:response_body) do
        JSON.generate(vms)
      end

      let(:request_body) { nil }
      let(:endpoint) { 'deployments/foo/vms' }

      it 'lists vms for a given deployment' do
        expect(@director.list_vms('foo')).to eq vms
      end
    end

    it 'creates user' do
      @director.should_receive(:post).
          with('/users', 'application/json',
               JSON.generate('username' => 'joe', 'password' => '<PASSWORD>')).
          and_return(true)
      @director.create_user('joe', 'pass')
{'fraction_non_alphanumeric': 0.1566284006919327, 'fraction_numerical': 0.016669287623840227, 'mean_word_length': 2.9774859287054407, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '955232', 'n_tokens_mistral': 2130, 'n_tokens_neox': 1945, 'n_words': 396}