mongo 2.11.0.rc0 → 2.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/Rakefile +2 -0
  5. data/lib/mongo/auth.rb +11 -2
  6. data/lib/mongo/auth/cr/conversation.rb +1 -1
  7. data/lib/mongo/auth/ldap/conversation.rb +1 -1
  8. data/lib/mongo/auth/scram/conversation.rb +4 -1
  9. data/lib/mongo/auth/user.rb +15 -1
  10. data/lib/mongo/auth/user/view.rb +10 -4
  11. data/lib/mongo/auth/x509.rb +11 -1
  12. data/lib/mongo/auth/x509/conversation.rb +15 -6
  13. data/lib/mongo/background_thread.rb +28 -13
  14. data/lib/mongo/client.rb +23 -15
  15. data/lib/mongo/collection/view/change_stream.rb +5 -1
  16. data/lib/mongo/collection/view/readable.rb +5 -2
  17. data/lib/mongo/collection/view/writable.rb +3 -1
  18. data/lib/mongo/cursor/builder/get_more_command.rb +4 -1
  19. data/lib/mongo/cursor/builder/kill_cursors_command.rb +16 -5
  20. data/lib/mongo/cursor/builder/op_get_more.rb +2 -2
  21. data/lib/mongo/cursor/builder/op_kill_cursors.rb +17 -5
  22. data/lib/mongo/error/operation_failure.rb +3 -3
  23. data/lib/mongo/monitoring/command_log_subscriber.rb +5 -3
  24. data/lib/mongo/monitoring/event/command_started.rb +13 -3
  25. data/lib/mongo/monitoring/publishable.rb +4 -2
  26. data/lib/mongo/operation/create_user/command.rb +1 -0
  27. data/lib/mongo/operation/remove_user/command.rb +1 -0
  28. data/lib/mongo/operation/update_user/command.rb +1 -0
  29. data/lib/mongo/protocol/get_more.rb +2 -1
  30. data/lib/mongo/protocol/kill_cursors.rb +6 -13
  31. data/lib/mongo/protocol/serializers.rb +10 -4
  32. data/lib/mongo/retryable.rb +1 -1
  33. data/lib/mongo/server/connection.rb +6 -2
  34. data/lib/mongo/server/connection_base.rb +2 -1
  35. data/lib/mongo/server/monitor.rb +1 -1
  36. data/lib/mongo/server/pending_connection.rb +6 -0
  37. data/lib/mongo/socket/ssl.rb +1 -1
  38. data/lib/mongo/uri.rb +5 -41
  39. data/lib/mongo/version.rb +1 -1
  40. data/mongo.gemspec +11 -2
  41. data/spec/README.md +105 -9
  42. data/spec/USERS.md +72 -0
  43. data/spec/integration/auth_spec.rb +20 -6
  44. data/spec/integration/client_construction_spec.rb +3 -1
  45. data/spec/integration/client_options_spec.rb +437 -0
  46. data/spec/integration/command_monitoring_spec.rb +4 -1
  47. data/spec/integration/connection_pool_populator_spec.rb +4 -0
  48. data/spec/integration/connection_spec.rb +4 -2
  49. data/spec/integration/cursor_reaping_spec.rb +1 -1
  50. data/spec/integration/get_more_spec.rb +32 -0
  51. data/spec/integration/retryable_errors_spec.rb +99 -0
  52. data/spec/integration/retryable_writes_errors_spec.rb +11 -10
  53. data/spec/lite_spec_helper.rb +2 -1
  54. data/spec/mongo/auth/scram_spec.rb +1 -0
  55. data/spec/mongo/auth/user/view_spec.rb +102 -1
  56. data/spec/mongo/auth/user_spec.rb +56 -15
  57. data/spec/mongo/auth/x509_spec.rb +31 -1
  58. data/spec/mongo/bulk_write_spec.rb +2 -2
  59. data/spec/mongo/collection/view/change_stream_spec.rb +2 -2
  60. data/spec/mongo/collection/view/readable_spec.rb +8 -4
  61. data/spec/mongo/cursor/builder/get_more_command_spec.rb +4 -2
  62. data/spec/mongo/cursor/builder/op_get_more_spec.rb +4 -2
  63. data/spec/mongo/cursor_spec.rb +3 -3
  64. data/spec/mongo/retryable_spec.rb +31 -52
  65. data/spec/mongo/server/connection_auth_spec.rb +3 -0
  66. data/spec/mongo/server/connection_pool_spec.rb +4 -0
  67. data/spec/mongo/server/connection_spec.rb +12 -4
  68. data/spec/mongo/server/monitor_spec.rb +19 -1
  69. data/spec/mongo/socket/ssl_spec.rb +1 -1
  70. data/spec/mongo/uri/srv_protocol_spec.rb +0 -13
  71. data/spec/mongo/uri_option_parsing_spec.rb +0 -8
  72. data/spec/mongo/uri_spec.rb +6 -20
  73. data/spec/runners/connection_string.rb +116 -0
  74. data/spec/runners/read_write_concern_document.rb +67 -0
  75. data/spec/spec_tests/change_streams_spec.rb +17 -2
  76. data/spec/spec_tests/connection_string_spec.rb +2 -59
  77. data/spec/spec_tests/data/change_streams/change-streams-errors.yml +3 -3
  78. data/spec/spec_tests/data/change_streams/change-streams.yml +88 -20
  79. data/spec/spec_tests/data/cmap/connection-must-have-id.yml +6 -0
  80. data/spec/spec_tests/data/cmap/connection-must-order-ids.yml +6 -0
  81. data/spec/spec_tests/data/cmap/pool-checkin-destroy-closed.yml +3 -0
  82. data/spec/spec_tests/data/cmap/pool-checkin-destroy-stale.yml +3 -0
  83. data/spec/spec_tests/data/cmap/pool-checkin-make-available.yml +3 -0
  84. data/spec/spec_tests/data/cmap/pool-checkin.yml +1 -0
  85. data/spec/spec_tests/data/cmap/pool-checkout-connection.yml +2 -0
  86. data/spec/spec_tests/data/cmap/pool-checkout-error-closed.yml +5 -0
  87. data/spec/spec_tests/data/cmap/pool-checkout-multiple.yml +3 -0
  88. data/spec/spec_tests/data/cmap/pool-checkout-no-idle.yml +4 -0
  89. data/spec/spec_tests/data/cmap/pool-checkout-no-stale.yml +4 -0
  90. data/spec/spec_tests/data/cmap/pool-close-destroy-conns.yml +2 -0
  91. data/spec/spec_tests/data/cmap/pool-create-max-size.yml +15 -0
  92. data/spec/spec_tests/data/cmap/pool-create-min-size.yml +4 -0
  93. data/spec/spec_tests/data/cmap/wait-queue-fairness.yml +31 -1
  94. data/spec/spec_tests/data/cmap/wait-queue-timeout.yml +5 -0
  95. data/spec/spec_tests/data/read_write_concern/connection-string/read-concern.yml +32 -0
  96. data/spec/spec_tests/data/read_write_concern/connection-string/write-concern.yml +82 -0
  97. data/spec/spec_tests/data/read_write_concern/document/read-concern.yml +37 -0
  98. data/spec/spec_tests/data/read_write_concern/document/write-concern.yml +100 -0
  99. data/spec/spec_tests/data/retryable_reads/aggregate-merge.yml +39 -0
  100. data/spec/spec_tests/data/retryable_reads/aggregate-serverErrors.yml +1 -1
  101. data/spec/spec_tests/data/retryable_reads/changeStreams-client.watch-serverErrors.yml +2 -2
  102. data/spec/spec_tests/data/retryable_reads/changeStreams-client.watch.yml +1 -1
  103. data/spec/spec_tests/data/retryable_reads/changeStreams-db.coll.watch-serverErrors.yml +2 -2
  104. data/spec/spec_tests/data/retryable_reads/changeStreams-db.coll.watch.yml +1 -1
  105. data/spec/spec_tests/data/retryable_reads/changeStreams-db.watch-serverErrors.yml +2 -2
  106. data/spec/spec_tests/data/retryable_reads/changeStreams-db.watch.yml +1 -1
  107. data/spec/spec_tests/data/retryable_reads/count-serverErrors.yml +1 -1
  108. data/spec/spec_tests/data/retryable_reads/countDocuments-serverErrors.yml +1 -1
  109. data/spec/spec_tests/data/retryable_reads/distinct-serverErrors.yml +1 -1
  110. data/spec/spec_tests/data/retryable_reads/estimatedDocumentCount-serverErrors.yml +1 -1
  111. data/spec/spec_tests/data/retryable_reads/find-serverErrors.yml +1 -1
  112. data/spec/spec_tests/data/retryable_reads/findOne-serverErrors.yml +1 -1
  113. data/spec/spec_tests/data/retryable_reads/gridfs-download-serverErrors.yml +1 -1
  114. data/spec/spec_tests/data/retryable_reads/gridfs-downloadByName-serverErrors.yml +1 -1
  115. data/spec/spec_tests/data/retryable_reads/listCollectionNames-serverErrors.yml +1 -1
  116. data/spec/spec_tests/data/retryable_reads/listCollectionObjects-serverErrors.yml +1 -1
  117. data/spec/spec_tests/data/retryable_reads/listCollections-serverErrors.yml +1 -1
  118. data/spec/spec_tests/data/retryable_reads/listDatabaseNames-serverErrors.yml +1 -1
  119. data/spec/spec_tests/data/retryable_reads/listDatabaseObjects-serverErrors.yml +1 -1
  120. data/spec/spec_tests/data/retryable_reads/listDatabases-serverErrors.yml +1 -1
  121. data/spec/spec_tests/data/retryable_reads/listIndexNames-serverErrors.yml +1 -1
  122. data/spec/spec_tests/data/retryable_reads/listIndexes-serverErrors.yml +1 -1
  123. data/spec/spec_tests/data/transactions/read-concern.yml +6 -6
  124. data/spec/spec_tests/data/transactions/transaction-options-repl.yml +117 -0
  125. data/spec/spec_tests/data/transactions/transaction-options.yml +14 -121
  126. data/spec/spec_tests/data/transactions/write-concern.yml +3 -0
  127. data/spec/spec_tests/data/transactions_api/transaction-options.yml +11 -12
  128. data/spec/spec_tests/dns_seedlist_discovery_spec.rb +17 -7
  129. data/spec/spec_tests/read_write_concern_connection_string_spec.rb +8 -0
  130. data/spec/spec_tests/read_write_concern_document_spec.rb +74 -0
  131. data/spec/spec_tests/retryable_reads_spec.rb +32 -1
  132. data/spec/spec_tests/uri_options_spec.rb +4 -2
  133. data/spec/support/auth.rb +5 -14
  134. data/spec/support/certificates/client-x509.crt +78 -0
  135. data/spec/support/certificates/client-x509.key +27 -0
  136. data/spec/support/certificates/client-x509.pem +105 -0
  137. data/spec/support/change_streams.rb +8 -11
  138. data/spec/support/client_registry.rb +26 -12
  139. data/spec/support/cluster_tools.rb +2 -2
  140. data/spec/support/cmap.rb +11 -7
  141. data/spec/support/command_monitoring.rb +8 -8
  142. data/spec/support/connection_string.rb +56 -28
  143. data/spec/support/constraints.rb +8 -0
  144. data/spec/support/crud/spec.rb +5 -8
  145. data/spec/support/event_subscriber.rb +7 -0
  146. data/spec/support/gridfs.rb +4 -7
  147. data/spec/support/server_discovery_and_monitoring.rb +3 -8
  148. data/spec/support/server_selection.rb +4 -9
  149. data/spec/support/server_selection_rtt.rb +4 -7
  150. data/spec/support/spec_config.rb +47 -19
  151. data/spec/support/spec_setup.rb +5 -0
  152. data/spec/support/utils.rb +46 -8
  153. metadata +637 -597
  154. metadata.gz.sig +0 -0
@@ -66,10 +66,13 @@ describe 'Command monitoring' do
66
66
  end
67
67
 
68
68
  context 'client with no established connections' do
69
- # for simplicity use 3.6+ servers only, then we can assert
69
+ # For simplicity use 3.6+ servers only, then we can assert
70
70
  # scram auth commands
71
71
  min_server_fcv '3.6'
72
72
 
73
+ # X.509 auth uses authenticate instead of sasl* commands
74
+ require_no_x509_auth
75
+
73
76
  it 'does not nest auth and find' do
74
77
  expect(subscriber.started_events.length).to eq 0
75
78
  client['test-collection'].find(a: 1).first
@@ -21,6 +21,10 @@ describe 'Connection pool populator integration' do
21
21
 
22
22
  declare_topology_double
23
23
 
24
+ let(:app_metadata) do
25
+ Mongo::Server::AppMetadata.new(options)
26
+ end
27
+
24
28
  let(:cluster) do
25
29
  double('cluster').tap do |cl|
26
30
  allow(cl).to receive(:topology).and_return(topology)
@@ -158,8 +158,10 @@ describe 'Connections' do
158
158
  RSpec::Mocks.with_temporary_scope do
159
159
  # now pretend an ismaster returned a different range
160
160
  features = Mongo::Server::Description::Features.new(0..3)
161
- # the second Features instantiation is for SDAM event publication
162
- expect(Mongo::Server::Description::Features).to receive(:new).twice.and_return(features)
161
+ # One Features instantiation is for SDAM event publication, this
162
+ # one always happens. The second one happens on servers
163
+ # where we do not negotiate auth mechanism.
164
+ expect(Mongo::Server::Description::Features).to receive(:new).at_least(:once).and_return(features)
163
165
 
164
166
  connection = Mongo::Server::Connection.new(server, server.options)
165
167
  expect(connection.connect!).to be true
@@ -59,7 +59,7 @@ describe 'Cursor reaping' do
59
59
  client.cluster.instance_variable_get('@periodic_executor').execute
60
60
 
61
61
  started_event = EventSubscriber.started_events.detect do |event|
62
- event.command['killCursors'] && event.command['cursors'].include?(cursor_id)
62
+ event.command['killCursors'] && event.command['cursors'].map(&:value).include?(cursor_id)
63
63
  end
64
64
 
65
65
  expect(started_event).not_to be_nil
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'getMore operation' do
4
+ # https://jira.mongodb.org/browse/RUBY-1987
5
+ min_server_fcv '3.2'
6
+
7
+ let(:collection) do
8
+ subscribed_client['get_more_spec']
9
+ end
10
+
11
+ let(:scope) do
12
+ collection.find.batch_size(1).each
13
+ end
14
+
15
+ before do
16
+ collection.delete_many
17
+ collection.insert_one(a: 1)
18
+ #collection.insert_one(a: 2)
19
+ EventSubscriber.clear_events!
20
+ end
21
+
22
+ let(:get_more_command) do
23
+ event = EventSubscriber.single_command_started_event('getMore')
24
+ event.command['getMore']
25
+ end
26
+
27
+ it 'sends cursor id as int64' do
28
+ scope.to_a
29
+
30
+ expect(get_more_command).to be_a(BSON::Int64)
31
+ end
32
+ end
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Retryable writes tests' do
4
+ # Requirement for fail point
5
+ min_server_fcv '4.0'
6
+
7
+ let(:client) do
8
+ subscribed_client
9
+ end
10
+
11
+ let(:collection) do
12
+ client['retryable-writes-error-spec']
13
+ end
14
+
15
+ context 'when retry fails' do
16
+ require_topology :replica_set
17
+
18
+ let(:fail_point_command) do
19
+ {
20
+ configureFailPoint: 'failCommand',
21
+ mode: {times: 1},
22
+ data: {
23
+ failCommands: ['find'],
24
+ errorCode: 11600,
25
+ },
26
+ }
27
+ end
28
+
29
+ let(:clear_fail_point_command) do
30
+ {
31
+ configureFailPoint: 'failCommand',
32
+ mode: 'off',
33
+ }
34
+ end
35
+
36
+ after do
37
+ ClusterTools.instance.direct_client_for_each_server do |client|
38
+ client.use(:admin).database.command(clear_fail_point_command)
39
+ end
40
+ end
41
+
42
+ let(:collection) do
43
+ client['retryable-writes-error-spec', read: {mode: :secondary_preferred}]
44
+ end
45
+
46
+ let(:events) do
47
+ events = EventSubscriber.command_started_events('find')
48
+ end
49
+
50
+ let(:first_server) do
51
+ client.cluster.servers_list.detect do |server|
52
+ server.address.seed == events.first.address.seed
53
+ end
54
+ end
55
+
56
+ let(:second_server) do
57
+ client.cluster.servers_list.detect do |server|
58
+ server.address.seed == events.last.address.seed
59
+ end
60
+ end
61
+
62
+ let(:perform_read) do
63
+ client.cluster.servers_list.each do |server|
64
+ server.monitor.stop!
65
+ end
66
+
67
+ ClusterTools.instance.direct_client_for_each_server do |client|
68
+ client.use(:admin).database.command(fail_point_command)
69
+ end
70
+
71
+ begin
72
+ collection.find(a: 1).to_a
73
+ rescue Mongo::Error::OperationFailure => @exception
74
+ else
75
+ fail('Expected operation to fail')
76
+ end
77
+
78
+ puts @exception.message
79
+
80
+ expect(events.length).to eq(2)
81
+ expect(events.first.address.seed).not_to eq(events.last.address.seed)
82
+ end
83
+
84
+ it 'is reported on the server of the second attempt' do
85
+ perform_read
86
+
87
+ expect(@exception.message).not_to include(first_server.address.seed)
88
+ expect(@exception.message).to include(second_server.address.seed)
89
+ end
90
+
91
+ it 'marks servers used in both attempts unknown' do
92
+ perform_read
93
+
94
+ expect(first_server).to be_unknown
95
+
96
+ expect(second_server).to be_unknown
97
+ end
98
+ end
99
+ end
@@ -1,7 +1,16 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'Retryable writes errors tests' do
4
- describe 'when the storage engine does not support retryable writes but the server does' do
4
+
5
+ let(:client) do
6
+ authorized_client.with(retry_writes: true)
7
+ end
8
+
9
+ let(:collection) do
10
+ client['retryable-writes-error-spec']
11
+ end
12
+
13
+ context 'when the storage engine does not support retryable writes but the server does' do
5
14
  require_mmapv1
6
15
  min_server_fcv '3.6'
7
16
  require_topology :replica_set, :sharded
@@ -10,14 +19,6 @@ describe 'Retryable writes errors tests' do
10
19
  collection.delete_many
11
20
  end
12
21
 
13
- let(:collection) do
14
- client[authorized_collection.name]
15
- end
16
-
17
- let(:client) do
18
- authorized_client.with(retry_writes: true)
19
- end
20
-
21
22
  context 'when a retryable write is attempted' do
22
23
  it 'raises an actionable error message' do
23
24
  expect {
@@ -27,4 +28,4 @@ describe 'Retryable writes errors tests' do
27
28
  end
28
29
  end
29
30
  end
30
- end
31
+ end
@@ -47,6 +47,7 @@ else
47
47
  PossiblyConcurrentArray = Array
48
48
  end
49
49
 
50
+ require 'support/utils'
50
51
  require 'support/spec_config'
51
52
 
52
53
  Mongo::Logger.logger = Logger.new($stdout)
@@ -68,6 +69,7 @@ require 'support/sdam_monitoring'
68
69
  require 'support/crud'
69
70
  require 'support/command_monitoring'
70
71
  require 'support/cmap'
72
+ require 'runners/connection_string'
71
73
  require 'support/connection_string'
72
74
  require 'support/gridfs'
73
75
  require 'support/transactions'
@@ -77,7 +79,6 @@ require 'support/client_registry'
77
79
  require 'support/client_registry_macros'
78
80
  require 'support/json_ext_formatter'
79
81
  require 'support/sdam_formatter_integration'
80
- require 'support/utils'
81
82
  require 'support/background_thread_registry'
82
83
  require 'support/auth'
83
84
 
@@ -1,6 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Mongo::Auth::SCRAM do
4
+ require_no_x509_auth
4
5
 
5
6
  let(:server) do
6
7
  authorized_client.cluster.next_primary
@@ -7,7 +7,46 @@ describe Mongo::Auth::User::View do
7
7
  end
8
8
 
9
9
  before do
10
- begin; view.remove('durran'); rescue; end
10
+ # Separate view instance to not interfere with test assertions
11
+ view = described_class.new(root_authorized_client.database)
12
+ begin
13
+ view.remove('durran')
14
+ rescue Mongo::Error::OperationFailure
15
+ end
16
+ end
17
+
18
+ shared_context 'testing write concern' do
19
+
20
+ let(:subscriber) do
21
+ EventSubscriber.new
22
+ end
23
+
24
+ let(:client) do
25
+ root_authorized_client.tap do |client|
26
+ client.subscribe(Mongo::Monitoring::COMMAND, subscriber)
27
+ end
28
+ end
29
+
30
+ let(:view) do
31
+ described_class.new(client.database)
32
+ end
33
+
34
+ before do
35
+ allow_any_instance_of(Mongo::Monitoring::Event::CommandStarted).to receive(:redacted) do |instance, command_name, document|
36
+ document
37
+ end
38
+ end
39
+ end
40
+
41
+ shared_examples_for 'forwards write concern to server' do
42
+ # w:2 requires more than one node in the deployment
43
+ require_topology :replica_set
44
+
45
+ it 'forwards write concern to server' do
46
+ response
47
+
48
+ expect(event.command['writeConcern']).to eq('w' => 2)
49
+ end
11
50
  end
12
51
 
13
52
  describe '#create' do
@@ -73,6 +112,25 @@ describe Mongo::Auth::User::View do
73
112
 
74
113
  it_behaves_like 'an operation using a session'
75
114
  end
115
+
116
+ context 'when write concern is given' do
117
+ include_context 'testing write concern'
118
+
119
+ let(:response) do
120
+ view.create(
121
+ 'durran',
122
+ password: 'password',
123
+ roles: [Mongo::Auth::Roles::READ_WRITE],
124
+ write_concern: {w: 2},
125
+ )
126
+ end
127
+
128
+ let(:event) do
129
+ subscriber.single_command_started_event('createUser')
130
+ end
131
+
132
+ it_behaves_like 'forwards write concern to server'
133
+ end
76
134
  end
77
135
 
78
136
  describe '#update' do
@@ -183,6 +241,25 @@ describe Mongo::Auth::User::View do
183
241
  it_behaves_like 'an operation using a session'
184
242
  end
185
243
  end
244
+
245
+ context 'when write concern is given' do
246
+ include_context 'testing write concern'
247
+
248
+ let(:response) do
249
+ view.update(
250
+ 'durran',
251
+ password: 'password1',
252
+ roles: [Mongo::Auth::Roles::READ_WRITE],
253
+ write_concern: {w: 2},
254
+ )
255
+ end
256
+
257
+ let(:event) do
258
+ subscriber.single_command_started_event('updateUser')
259
+ end
260
+
261
+ it_behaves_like 'forwards write concern to server'
262
+ end
186
263
  end
187
264
 
188
265
  describe '#remove' do
@@ -260,6 +337,30 @@ describe Mongo::Auth::User::View do
260
337
  it_behaves_like 'a failed operation using a session'
261
338
  end
262
339
  end
340
+
341
+ context 'when write concern is given' do
342
+ include_context 'testing write concern'
343
+
344
+ before do
345
+ view.create(
346
+ 'durran',
347
+ password: 'password', roles: [ Mongo::Auth::Roles::READ_WRITE ]
348
+ )
349
+ end
350
+
351
+ let(:response) do
352
+ view.remove(
353
+ 'durran',
354
+ write_concern: {w: 2},
355
+ )
356
+ end
357
+
358
+ let(:event) do
359
+ subscriber.single_command_started_event('dropUser')
360
+ end
361
+
362
+ it_behaves_like 'forwards write concern to server'
363
+ end
263
364
  end
264
365
 
265
366
  describe '#info' do
@@ -10,6 +10,24 @@ describe Mongo::Auth::User do
10
10
  described_class.new(options)
11
11
  end
12
12
 
13
+ shared_examples_for 'sets database and auth source to admin' do
14
+
15
+ it 'sets database to admin' do
16
+ expect(user.database).to eq('admin')
17
+ end
18
+
19
+ it 'sets auth source to admin' do
20
+ expect(user.auth_source).to eq('admin')
21
+ end
22
+ end
23
+
24
+ shared_examples_for 'sets auth source to $external' do
25
+
26
+ it 'sets auth source to $external' do
27
+ expect(user.auth_source).to eq('$external')
28
+ end
29
+ end
30
+
13
31
  describe '#initialize' do
14
32
  let(:user) { Mongo::Auth::User.new(options) }
15
33
 
@@ -19,6 +37,8 @@ describe Mongo::Auth::User do
19
37
  it 'succeeds' do
20
38
  expect(user).to be_a(Mongo::Auth::User)
21
39
  end
40
+
41
+ it_behaves_like 'sets database and auth source to admin'
22
42
  end
23
43
 
24
44
  context 'invalid mechanism' do
@@ -45,6 +65,8 @@ describe Mongo::Auth::User do
45
65
  it 'converts mechanism to symbol' do
46
66
  expect(user.mechanism).to eq(:scram)
47
67
  end
68
+
69
+ it_behaves_like 'sets database and auth source to admin'
48
70
  end
49
71
 
50
72
  context 'linting' do
@@ -69,6 +91,40 @@ describe Mongo::Auth::User do
69
91
  it 'stores mechanism' do
70
92
  expect(user.mechanism).to eq(:scram)
71
93
  end
94
+
95
+ it_behaves_like 'sets database and auth source to admin'
96
+ end
97
+
98
+ context 'mechanism is x509' do
99
+ let(:options) { {auth_mech: :mongodb_x509} }
100
+
101
+ it 'sets database to admin' do
102
+ expect(user.database).to eq('admin')
103
+ end
104
+
105
+ it_behaves_like 'sets auth source to $external'
106
+
107
+ context 'database is explicitly given' do
108
+ let(:options) { {auth_mech: :mongodb_x509, database: 'foo'} }
109
+
110
+ it 'sets database to the specified one' do
111
+ expect(user.database).to eq('foo')
112
+ end
113
+
114
+ it_behaves_like 'sets auth source to $external'
115
+ end
116
+ end
117
+
118
+ it 'sets the database' do
119
+ expect(user.database).to eq('testing')
120
+ end
121
+
122
+ it 'sets the name' do
123
+ expect(user.name).to eq('user')
124
+ end
125
+
126
+ it 'sets the password' do
127
+ expect(user.password).to eq('pass')
72
128
  end
73
129
  end
74
130
 
@@ -131,21 +187,6 @@ describe Mongo::Auth::User do
131
187
  end
132
188
  end
133
189
 
134
- describe '#initialize' do
135
-
136
- it 'sets the database' do
137
- expect(user.database).to eq('testing')
138
- end
139
-
140
- it 'sets the name' do
141
- expect(user.name).to eq('user')
142
- end
143
-
144
- it 'sets the password' do
145
- expect(user.password).to eq('pass')
146
- end
147
- end
148
-
149
190
  describe '#hashed_password' do
150
191
 
151
192
  let(:expected) do