File: connection_auth_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (138 lines) | stat: -rw-r--r-- 3,972 bytes parent folder | download
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

# these tests fail intermittently in evergreen
describe Mongo::Server::Connection do
  retry_test

  let(:address) do
    Mongo::Address.new(SpecConfig.instance.addresses.first)
  end

  let(:monitoring) do
    Mongo::Monitoring.new(monitoring: false)
  end

  let(:listeners) do
    Mongo::Event::Listeners.new
  end

  let(:app_metadata) do
    Mongo::Server::AppMetadata.new(SpecConfig.instance.test_options)
  end

  let(:cluster) do
    double('cluster').tap do |cl|
      allow(cl).to receive(:topology).and_return(topology)
      allow(cl).to receive(:app_metadata).and_return(app_metadata)
      allow(cl).to receive(:options).and_return({})
      allow(cl).to receive(:cluster_time).and_return(nil)
      allow(cl).to receive(:update_cluster_time)
      allow(cl).to receive(:run_sdam_flow)
      pool = double('pool')
      allow(pool).to receive(:disconnect!)
      allow(cl).to receive(:pool).and_return(pool)
    end
  end

  declare_topology_double

  let(:server) do
    register_server(
      Mongo::Server.new(address, cluster, monitoring, listeners,
        SpecConfig.instance.test_options.merge(monitoring_io: false))
    )
  end

  before(:all) do
    ClientRegistry.instance.close_all_clients
  end

  describe '#auth_mechanism' do
    require_no_external_user

    let(:connection) do
      described_class.new(server, server.options)
    end

    context 'when the hello response includes saslSupportedMechs' do
      min_server_fcv '4.0'

      let(:server_options) do
        SpecConfig.instance.test_options.merge(
          user: SpecConfig.instance.test_user.name,
          password: SpecConfig.instance.test_user.password,
          auth_source: 'admin',
        )
      end

      let(:app_metadata) do
        Mongo::Server::AppMetadata.new(server_options)
      end

      before do
        client = authorized_client.with(database: 'admin')
        info = client.database.users.info(SpecConfig.instance.test_user.name)
        expect(info.length).to eq(1)
        # this before block may have made 2 or 3 clients
        ClientRegistry.instance.close_all_clients
      end

      it 'uses scram256' do
        connection
        RSpec::Mocks.with_temporary_scope do
          pending_conn = nil
          Mongo::Server::PendingConnection.should receive(:new).and_wrap_original do |m, *args|
            pending_conn = m.call(*args)
          end
          connection.connect!
          expect(pending_conn.send(:default_mechanism)).to eq(:scram256)
        end
      end
    end

    context 'when the hello response indicates the auth mechanism is :scram' do
      require_no_external_user

      let(:features) do
        Mongo::Server::Description::Features.new(0..7)
      end

      it 'uses scram' do
        connection
        RSpec::Mocks.with_temporary_scope do
          expect(Mongo::Server::Description::Features).to receive(:new).and_return(features)

          pending_conn = nil
          Mongo::Server::PendingConnection.should receive(:new).and_wrap_original do |m, *args|
            pending_conn = m.call(*args)
          end
          connection.connect!
          expect(pending_conn.send(:default_mechanism)).to eq(:scram)
        end
      end
    end

    context 'when the hello response indicates the auth mechanism is :mongodb_cr' do
      let(:features) do
        Mongo::Server::Description::Features.new(0..2)
      end

      it 'uses mongodb_cr' do
        connection
        RSpec::Mocks.with_temporary_scope do
          expect(Mongo::Server::Description::Features).to receive(:new).and_return(features)

          pending_conn = nil
          Mongo::Server::PendingConnection.should receive(:new).and_wrap_original do |m, *args|
            pending_conn = m.call(*args)
          end
          connection.connect!
          expect(pending_conn.send(:default_mechanism)).to eq(:mongodb_cr)
        end
      end
    end
  end
end