Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 114 lines (96 sloc) 6.777 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
require "utils"
require 'capistrano/ssh'

class SSHTest < Test::Unit::TestCase
  def setup
    Capistrano::ServerDefinition.stubs(:default_user).returns("default-user")
    @options = { :password => nil,
                 :auth_methods => %w(publickey hostbased),
                 :config => false }
    @server = server("capistrano")
    Net::SSH.stubs(:configuration_for).returns({})
  end

  def test_connect_with_bare_server_without_options_or_config_with_public_key_succeeding_should_only_loop_once
    Net::SSH.expects(:start).with(@server.host, "default-user", @options).returns(success = Object.new)
    assert_equal success, Capistrano::SSH.connect(@server)
  end

  def test_connect_with_bare_server_without_options_with_public_key_failing_should_try_password
    Net::SSH.expects(:start).with(@server.host, "default-user", @options).raises(Net::SSH::AuthenticationFailed)
    Net::SSH.expects(:start).with(@server.host, "default-user", @options.merge(:password => "f4b13n", :auth_methods => %w(password keyboard-interactive))).returns(success = Object.new)
    assert_equal success, Capistrano::SSH.connect(@server, :password => "f4b13n")
  end

  def test_connect_with_bare_server_without_options_public_key_and_password_failing_should_raise_error
    Net::SSH.expects(:start).with(@server.host, "default-user", @options).raises(Net::SSH::AuthenticationFailed)
    Net::SSH.expects(:start).with(@server.host, "default-user", @options.merge(:password => "f4b13n", :auth_methods => %w(password keyboard-interactive))).raises(Net::SSH::AuthenticationFailed)
    assert_raises(Net::SSH::AuthenticationFailed) do
      Capistrano::SSH.connect(@server, :password => "f4b13n")
    end
  end

  def test_connect_with_bare_server_and_user_via_public_key_should_pass_user_to_net_ssh
    Net::SSH.expects(:start).with(@server.host, "jamis", @options).returns(success = Object.new)
    assert_equal success, Capistrano::SSH.connect(@server, :user => "jamis")
  end

  def test_connect_with_bare_server_and_user_via_password_should_pass_user_to_net_ssh
    Net::SSH.expects(:start).with(@server.host, "jamis", @options).raises(Net::SSH::AuthenticationFailed)
    Net::SSH.expects(:start).with(@server.host, "jamis", @options.merge(:password => "f4b13n", :auth_methods => %w(password keyboard-interactive))).returns(success = Object.new)
    assert_equal success, Capistrano::SSH.connect(@server, :user => "jamis", :password => "f4b13n")
  end

  def test_connect_with_bare_server_with_explicit_port_should_pass_port_to_net_ssh
    Net::SSH.expects(:start).with(@server.host, "default-user", @options.merge(:port => 1234)).returns(success = Object.new)
    assert_equal success, Capistrano::SSH.connect(@server, :port => 1234)
  end

  def test_connect_with_server_with_user_should_pass_user_to_net_ssh
    server = server("jamis@capistrano")
    Net::SSH.expects(:start).with(server.host, "jamis", @options).returns(success = Object.new)
    assert_equal success, Capistrano::SSH.connect(server)
  end

  def test_connect_with_server_with_port_should_pass_port_to_net_ssh
    server = server("capistrano:1235")
    Net::SSH.expects(:start).with(server.host, "default-user", @options.merge(:port => 1235)).returns(success = Object.new)
    assert_equal success, Capistrano::SSH.connect(server)
  end

  def test_connect_with_server_with_user_and_port_should_pass_user_and_port_to_net_ssh
    server = server("jamis@capistrano:1235")
    Net::SSH.expects(:start).with(server.host, "jamis", @options.merge(:port => 1235)).returns(success = Object.new)
    assert_equal success, Capistrano::SSH.connect(server)
  end

  def test_connect_with_server_with_other_ssh_options_should_pass_ssh_options_to_net_ssh
    server = server("jamis@capistrano:1235", :ssh_options => { :keys => %w(some_valid_key), :auth_methods => %w(a_method), :hmac => 'none' })
    Net::SSH.expects(:start).with(server.host, "jamis", @options.merge(:port => 1235, :keys => %w(some_valid_key), :auth_methods => %w(a_method), :hmac => 'none' )).returns(success = Object.new)
    assert_equal success, Capistrano::SSH.connect(server)
  end

  def test_connect_with_ssh_options_should_use_ssh_options
    ssh_options = { :username => "JamisMan", :port => 8125, :config => false }
    Net::SSH.expects(:start).with(@server.host, "JamisMan", @options.merge(:port => 8125, :config => false)).returns(success = Object.new)
    assert_equal success, Capistrano::SSH.connect(@server, {:ssh_options => ssh_options})
  end

  def test_connect_with_options_and_ssh_options_should_see_options_override_ssh_options
    ssh_options = { :username => "JamisMan", :port => 8125, :forward_agent => true }
    Net::SSH.expects(:start).with(@server.host, "jamis", @options.merge(:port => 1235, :forward_agent => true)).returns(success = Object.new)
    assert_equal success, Capistrano::SSH.connect(@server, :ssh_options => ssh_options, :user => "jamis", :port => 1235)
  end

  def test_connect_with_verbose_option_should_set_verbose_option_on_ssh
    Net::SSH.expects(:start).with(@server.host, "default-user", @options).returns(success = Object.new)
    assert_equal success, Capistrano::SSH.connect(@server, :verbose => 0)
    Net::SSH.expects(:start).with(@server.host, "default-user", @options.merge(:verbose => :debug)).returns(success = Object.new)
    assert_equal success, Capistrano::SSH.connect(@server, :verbose => 1)
    Net::SSH.expects(:start).with(@server.host, "default-user", @options.merge(:verbose => :debug)).returns(success = Object.new)
    assert_equal success, Capistrano::SSH.connect(@server, :verbose => 2)
  end

  def test_connect_with_ssh_options_should_see_server_options_override_ssh_options
    ssh_options = { :username => "JamisMan", :port => 8125, :forward_agent => true }
    server = server("jamis@capistrano:1235")
    Net::SSH.expects(:start).with(server.host, "jamis", @options.merge(:port => 1235, :forward_agent => true, :config => false)).returns(success = Object.new)
    assert_equal success, Capistrano::SSH.connect(server, {:ssh_options => ssh_options})
  end

  def test_connect_should_add_xserver_accessor_to_connection
    Net::SSH.expects(:start).with(@server.host, "default-user", @options).returns(success = Object.new)
    assert_equal success, Capistrano::SSH.connect(@server)
    assert success.respond_to?(:xserver)
    assert success.respond_to?(:xserver)
    assert_equal success.xserver, @server
  end

  def test_connect_should_not_retry_if_custom_auth_methods_are_given
    Net::SSH.expects(:start).with(@server.host, "default-user", @options.merge(:auth_methods => %w(publickey))).raises(Net::SSH::AuthenticationFailed)
    assert_raises(Net::SSH::AuthenticationFailed) { Capistrano::SSH.connect(@server, :ssh_options => { :auth_methods => %w(publickey) }) }
  end
end
Something went wrong with that request. Please try again.