require 'test_helper'
require 'solusvm/cli'

class TestClientCli < Test::Unit::TestCase

  def setup
    # Prevents mocha from stubbing non existent methods so that we now if the CLI is failing because
    # something was moved around.
    Mocha::Configuration.prevent(:stubbing_non_existent_method)
  end

  def test_should_delegate_client_create_to_client
    Solusvm.expects(:config).with("thelogin", "thekey", { :url => "theurl" })
    api = mock do
      expects(:successful?).returns(true)
      expects(:create).with() do |options|
        expected = {
          :username => "theusername",
          :password => "thepassword",
          :email => "theemail",
          :firstname => "thefirstname",
          :lastname => "thelastname",
          :company => "thecompany"
        }

        expected.all? { |k,v| options[k] == v }
      end.returns("theresult")
    end
    Solusvm::Client.stubs(:new => api)

    $stdout.expects(:puts).with("theresult")
    Solusvm::Cli.start(cli_expand_base_arguments([
      "client", "create",
      "--username", "theusername",
      "--password", "thepassword",
      "--email", "theemail",
      "--firstname", "thefirstname",
      "--lastname", "thelastname",
      "--company", "thecompany"
    ]))
  end

  def test_should_delegate_client_change_password_to_client
    Solusvm.expects(:config).with("thelogin", "thekey", { :url => "theurl" })
    Solusvm::Client.stubs(:new => mock do
      expects(:successful?).returns(true)
      expects(:change_password).with("theusername", "thepassword").returns("theresult")
    end)

    $stdout.expects(:puts).with("theresult")
    Solusvm::Cli.start(cli_expand_base_arguments(["client", "change-password", "theusername", "thepassword"]))
  end

  def test_should_delegate_client_authenticate_to_client
    Solusvm.expects(:config).with("thelogin", "thekey", { :url => "theurl" })
    Solusvm::Client.stubs(:new => mock do
      expects(:successful?).returns(true)
      expects(:authenticate).with("theusername", "thepassword").returns("theresult")
    end)

    $stdout.expects(:puts).with("theresult")
    Solusvm::Cli.start(cli_expand_base_arguments(["client", "authenticate", "theusername", "thepassword"]))
  end

  def test_should_delegate_client_check_exists_to_client
    Solusvm.expects(:config).with("thelogin", "thekey", { :url => "theurl" })
    Solusvm::Client.stubs(:new => mock do
      expects(:successful?).returns(true)
      expects(:exists?).with("theusername").returns("theresult")
    end)

    $stdout.expects(:puts).with("theresult")
    Solusvm::Cli.start(cli_expand_base_arguments(["client", "check-exists", "theusername"]))
  end

  def test_should_delegate_client_delete_to_client
    Solusvm.expects(:config).with("thelogin", "thekey", { :url => "theurl" })
    Solusvm::Client.stubs(:new => mock do
      expects(:successful?).returns(true)
      expects(:delete).with("theusername").returns("theresult")
    end)

    $stdout.expects(:puts).with("theresult")
    Solusvm::Cli.start(cli_expand_base_arguments(["client", "delete", "theusername"]))
  end

  def test_should_delegate_client_list_to_client
    Solusvm.expects(:config).with("thelogin", "thekey", { :url => "theurl" })
    Solusvm::Client.stubs(:new => mock do
      expects(:successful?).returns(true)
      expects(:list).returns("theresult")
    end)

    $stdout.expects(:puts).with("theresult")
    Solusvm::Cli.start(cli_expand_base_arguments(["client", "list"]))
  end
end