summaryrefslogtreecommitdiff
path: root/test/authentication/methods/test_password.rb
blob: db520bdbd0d84de708532a03c77e09411f6105f6 (plain)
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
require 'common'
require 'net/ssh/authentication/methods/password'
require 'net/ssh/authentication/session'
require 'authentication/methods/common'

module Authentication
  module Methods
    class TestPassword < NetSSHTest
      include Common

      def test_authenticate_should_raise_if_password_disallowed
        transport.expect do |t,packet|
          assert_equal USERAUTH_REQUEST, packet.type
          assert_equal "jamis", packet.read_string
          assert_equal "ssh-connection", packet.read_string
          assert_equal "password", packet.read_string
          assert_equal false, packet.read_bool
          assert_equal "the-password", packet.read_string

          t.return(USERAUTH_FAILURE, :string, "publickey")
        end

        assert_raises Net::SSH::Authentication::DisallowedMethod do
          subject.authenticate("ssh-connection", "jamis", "the-password")
        end
      end

      def test_authenticate_ask_for_password_for_second_time_when_password_is_incorrect
        transport.expect do |t,packet|
          assert_equal USERAUTH_REQUEST, packet.type
          assert_equal "jamis", packet.read_string
          assert_equal "ssh-connection", packet.read_string
          assert_equal "password", packet.read_string
          assert_equal false, packet.read_bool
          assert_equal "the-password", packet.read_string
          t.return(USERAUTH_FAILURE, :string, "publickey,password")

          t.expect do |_t2, packet2|
            assert_equal USERAUTH_REQUEST, packet2.type
            assert_equal "jamis", packet2.read_string
            assert_equal "ssh-connection", packet2.read_string
            assert_equal "password", packet2.read_string
            assert_equal false, packet2.read_bool
            assert_equal "the-password-2", packet2.read_string
            t.return(USERAUTH_SUCCESS)
          end
        end

        prompt = MockPrompt.new
        prompt.expects(:_ask).with("jamis@'s password:", { type: 'password', user: 'jamis', host: nil }, false).returns("the-password-2")
        subject(password_prompt: prompt).authenticate("ssh-connection", "jamis", "the-password")
      end

      def test_authenticate_ask_for_password_if_not_given
        transport.expect do |t,packet|
          assert_equal USERAUTH_REQUEST, packet.type
          assert_equal "bill", packet.read_string
          assert_equal "ssh-connection", packet.read_string
          assert_equal "password", packet.read_string
          assert_equal false, packet.read_bool
          assert_equal "good-password", packet.read_string
          t.return(USERAUTH_SUCCESS)
        end

        transport.instance_eval { @host = 'testhost' }
        prompt = MockPrompt.new
        prompt.expects(:_ask).with("bill@testhost's password:", { type: 'password', user: 'bill', host: 'testhost' }, false).returns("good-password")
        subject(password_prompt: prompt).authenticate("ssh-connection", "bill", nil)
      end

      def test_authenticate_when_password_is_acceptible_should_return_true
        transport.expect do |t,packet|
          assert_equal USERAUTH_REQUEST, packet.type
          t.return(USERAUTH_SUCCESS)
        end

        assert subject.authenticate("ssh-connection", "jamis", "the-password")
      end

      def test_authenticate_should_return_false_if_password_change_request_is_received
        transport.expect do |t,packet|
          assert_equal USERAUTH_REQUEST, packet.type
          t.return(USERAUTH_PASSWD_CHANGEREQ, :string, "Change your password:", :string, "")
        end

        assert !subject.authenticate("ssh-connection", "jamis", "the-password")
      end

      private

      def subject(options={})
        @subject ||= Net::SSH::Authentication::Methods::Password.new(session(options), options)
      end
    end
  end
end