forked from mongodb/mongo-ruby-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuri_spec.rb
1443 lines (1063 loc) · 39.6 KB
/
uri_spec.rb
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
# rubocop:todo all
require 'lite_spec_helper'
describe Mongo::URI do
shared_examples "roundtrips string" do
it "returns the correct string for the uri" do
expect(uri.to_s).to eq(URI::DEFAULT_PARSER.unescape(string))
end
end
describe '.get' do
let(:uri) { described_class.get(string) }
describe 'invalid uris' do
context 'string is not uri' do
let(:string) { 'tyler' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'nil' do
let(:string) { nil }
it 'raises an error' do
expect do
uri
end.to raise_error(Mongo::Error::InvalidURI, /URI must be a string, not nil/)
end
end
context 'empty string' do
let(:string) { '' }
it 'raises an error' do
expect do
uri
end.to raise_error(Mongo::Error::InvalidURI, /Cannot parse an empty URI/)
end
end
end
context 'when the scheme is mongodb://' do
let(:string) do
'mongodb://localhost:27017'
end
it 'returns a Mongo::URI object' do
expect(uri).to be_a(Mongo::URI)
end
end
context 'when the scheme is mongodb+srv://' do
require_external_connectivity
let(:string) do
'mongodb+srv://test5.test.build.10gen.cc'
end
it 'returns a Mongo::URI::SRVProtocol object' do
expect(uri).to be_a(Mongo::URI::SRVProtocol)
end
include_examples "roundtrips string"
end
context 'when the scheme is invalid' do
let(:string) do
'mongo://localhost:27017'
end
it 'raises an exception' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
end
let(:scheme) { 'mongodb://' }
let(:uri) { described_class.new(string) }
describe 'invalid uris' do
context 'string is not uri' do
let(:string) { 'tyler' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'nil' do
let(:string) { nil }
it 'raises an error' do
expect do
uri
end.to raise_error(Mongo::Error::InvalidURI, /URI must be a string, not nil/)
end
end
context 'empty string' do
let(:string) { '' }
it 'raises an error' do
expect do
uri
end.to raise_error(Mongo::Error::InvalidURI, /Cannot parse an empty URI/)
end
end
context 'mongo://localhost:27017' do
let(:string) { 'mongo://localhost:27017' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://' do
let(:string) { 'mongodb://' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://localhost::27017' do
let(:string) { 'mongodb://localhost::27017' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://localhost::27017/' do
let(:string) { 'mongodb://localhost::27017/' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://::' do
let(:string) { 'mongodb://::' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://localhost,localhost::' do
let(:string) { 'mongodb://localhost,localhost::' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://localhost::27017,abc' do
let(:string) { 'mongodb://localhost::27017,abc' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://localhost:-1' do
let(:string) { 'mongodb://localhost:-1' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://localhost:0/' do
let(:string) { 'mongodb://localhost:0/' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://localhost:65536' do
let(:string) { 'mongodb://localhost:65536' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://localhost:foo' do
let(:string) { 'mongodb://localhost:foo' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://[::1]:-1' do
let(:string) { 'mongodb://[::1]:-1' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://[::1]:0/' do
let(:string) { 'mongodb://[::1]:0/' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://[::1]:65536' do
let(:string) { 'mongodb://[::1]:65536' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://[::1]:65536/' do
let(:string) { 'mongodb://[::1]:65536/' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://[::1]:foo' do
let(:string) { 'mongodb://[::1]:foo' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://example.com/?w' do
let(:string) { 'mongodb://example.com/?w' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI, /Option w has no value/)
end
end
context 'equal sign in option value' do
let(:string) { 'mongodb://example.com/?authmechanismproperties=foo:a=b&appname=test' }
it 'is allowed' do
expect(uri.uri_options[:auth_mech_properties]).to eq('foo' => 'a=b')
end
end
context 'slash in option value' do
let(:string) { 'mongodb://example.com/?tlsCAFile=a/b' }
it 'returns a Mongo::URI object' do
expect(uri).to be_a(Mongo::URI)
end
it 'parses correctly' do
expect(uri.servers).to eq(['example.com'])
expect(uri.uri_options[:ssl_ca_cert]).to eq('a/b')
end
end
context 'numeric value in a string option' do
let(:string) { 'mongodb://example.com/?appName=1' }
it 'returns a Mongo::URI object' do
expect(uri).to be_a(Mongo::URI)
end
it 'sets option to the string value' do
expect(uri.uri_options[:app_name]).to eq('1')
end
end
context 'options start with ampersand' do
let(:string) { 'mongodb://example.com/?&appName=foo' }
it 'returns a Mongo::URI object' do
expect(uri).to be_a(Mongo::URI)
end
it 'parses the options' do
expect(uri.uri_options[:app_name]).to eq('foo')
end
end
context 'mongodb://alice:foo:[email protected]' do
let(:string) { 'mongodb://alice:foo:[email protected]' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://alice@@127.0.0.1' do
let(:string) { 'mongodb://alice@@127.0.0.1' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
context 'mongodb://alice@foo:[email protected]' do
let(:string) { 'mongodb://alice@foo:[email protected]' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
end
describe '#initialize' do
context 'string is not uri' do
let(:string) { 'tyler' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
end
describe "#to_s" do
context "string is a uri" do
let(:string) { 'mongodb://localhost:27017' }
it "returns the original string" do
expect(uri.to_s).to eq(string)
end
end
end
describe '#servers' do
let(:string) { "#{scheme}#{servers}" }
context 'single server' do
let(:servers) { 'localhost' }
it 'returns an array with the parsed server' do
expect(uri.servers).to eq([servers])
end
include_examples "roundtrips string"
end
context 'single server with port' do
let(:servers) { 'localhost:27017' }
it 'returns an array with the parsed server' do
expect(uri.servers).to eq([servers])
end
include_examples "roundtrips string"
end
context 'numerical ipv4 server' do
let(:servers) { '127.0.0.1' }
it 'returns an array with the parsed server' do
expect(uri.servers).to eq([servers])
end
include_examples "roundtrips string"
end
context 'numerical ipv6 server' do
let(:servers) { '[::1]:27107' }
it 'returns an array with the parsed server' do
expect(uri.servers).to eq([servers])
end
include_examples "roundtrips string"
end
context 'unix socket server' do
let(:servers) { '%2Ftmp%2Fmongodb-27017.sock' }
it 'returns an array with the parsed server' do
expect(uri.servers).to eq([URI::DEFAULT_PARSER.unescape(servers)])
end
include_examples "roundtrips string"
end
context 'multiple servers' do
let(:servers) { 'localhost,127.0.0.1' }
it 'returns an array with the parsed servers' do
expect(uri.servers).to eq(servers.split(','))
end
include_examples "roundtrips string"
end
context 'multiple servers with ports' do
let(:servers) { '127.0.0.1:27107,localhost:27018' }
it 'returns an array with the parsed servers' do
expect(uri.servers).to eq(servers.split(','))
end
include_examples "roundtrips string"
end
end
describe '#client_options' do
let(:db) { 'dummy_db' }
let(:servers) { 'localhost' }
let(:string) { "#{scheme}#{credentials}@#{servers}/#{db}" }
let(:user) { 'tyler' }
let(:password) { 's3kr4t' }
let(:credentials) { "#{user}:#{password}" }
let(:options) do
uri.client_options
end
it 'includes the database in the options' do
expect(options[:database]).to eq('dummy_db')
end
it 'includes the user in the options' do
expect(options[:user]).to eq(user)
end
it 'includes the password in the options' do
expect(options[:password]).to eq(password)
end
include_examples "roundtrips string"
end
describe '#credentials' do
let(:servers) { 'localhost' }
let(:string) { "#{scheme}#{credentials}@#{servers}" }
let(:user) { 'tyler' }
context 'username provided' do
let(:credentials) { "#{user}:" }
it 'returns the username' do
expect(uri.credentials[:user]).to eq(user)
end
it "roundtrips string without the colon" do
expect(uri.to_s).to eq("mongodb://tyler@localhost")
end
end
context 'username and password provided' do
let(:password) { 's3kr4t' }
let(:credentials) { "#{user}:#{password}" }
it 'returns the username' do
expect(uri.credentials[:user]).to eq(user)
end
it 'returns the password' do
expect(uri.credentials[:password]).to eq(password)
end
include_examples "roundtrips string"
end
end
describe '#database' do
let(:servers) { 'localhost' }
let(:string) { "#{scheme}#{servers}/#{db}" }
let(:db) { 'auth-db' }
context 'database provided' do
it 'returns the database name' do
expect(uri.database).to eq(db)
end
include_examples "roundtrips string"
end
end
describe '#uri_options' do
let(:servers) { 'localhost' }
let(:string) { "#{scheme}#{servers}/?#{options}" }
context 'when no options were provided' do
let(:string) { "#{scheme}#{servers}" }
it 'returns an empty hash' do
expect(uri.uri_options).to be_empty
end
include_examples "roundtrips string"
end
context 'write concern options provided' do
context 'numerical w value' do
let(:options) { 'w=1' }
let(:concern) { Mongo::Options::Redacted.new(:w => 1)}
it 'sets the write concern options' do
expect(uri.uri_options[:write_concern]).to eq(concern)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:write_concern]).to eq(concern)
end
include_examples "roundtrips string"
end
context 'w=majority' do
let(:options) { 'w=majority' }
let(:concern) { Mongo::Options::Redacted.new(:w => :majority) }
it 'sets the write concern options' do
expect(uri.uri_options[:write_concern]).to eq(concern)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:write_concern]).to eq(concern)
end
include_examples "roundtrips string"
end
context 'journal' do
let(:options) { 'journal=true' }
let(:concern) { Mongo::Options::Redacted.new(:j => true) }
it 'sets the write concern options' do
expect(uri.uri_options[:write_concern]).to eq(concern)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:write_concern]).to eq(concern)
end
include_examples "roundtrips string"
end
context 'fsync' do
let(:options) { 'fsync=true' }
let(:concern) { Mongo::Options::Redacted.new(:fsync => true) }
it 'sets the write concern options' do
expect(uri.uri_options[:write_concern]).to eq(concern)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:write_concern]).to eq(concern)
end
include_examples "roundtrips string"
end
context 'wtimeoutMS' do
let(:timeout) { 1234 }
let(:options) { "w=2&wtimeoutMS=#{timeout}" }
let(:concern) { Mongo::Options::Redacted.new(:w => 2, :wtimeout => timeout) }
it 'sets the write concern options' do
expect(uri.uri_options[:write_concern]).to eq(concern)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:write_concern]).to eq(concern)
end
it "roundtrips the string with camelCase" do
expect(uri.to_s).to eq("mongodb://localhost/?w=2&wTimeoutMS=1234")
end
end
end
context 'read preference option provided' do
let(:options) { "readPreference=#{mode}" }
context 'primary' do
let(:mode) { 'primary' }
let(:read) { Mongo::Options::Redacted.new(:mode => :primary) }
it 'sets the read preference' do
expect(uri.uri_options[:read]).to eq(read)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:read]).to eq(read)
end
include_examples "roundtrips string"
end
context 'primaryPreferred' do
let(:mode) { 'primaryPreferred' }
let(:read) { Mongo::Options::Redacted.new(:mode => :primary_preferred) }
it 'sets the read preference' do
expect(uri.uri_options[:read]).to eq(read)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:read]).to eq(read)
end
include_examples "roundtrips string"
end
context 'secondary' do
let(:mode) { 'secondary' }
let(:read) { Mongo::Options::Redacted.new(:mode => :secondary) }
it 'sets the read preference' do
expect(uri.uri_options[:read]).to eq(read)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:read]).to eq(read)
end
include_examples "roundtrips string"
end
context 'secondaryPreferred' do
let(:mode) { 'secondaryPreferred' }
let(:read) { Mongo::Options::Redacted.new(:mode => :secondary_preferred) }
it 'sets the read preference' do
expect(uri.uri_options[:read]).to eq(read)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:read]).to eq(read)
end
include_examples "roundtrips string"
end
context 'nearest' do
let(:mode) { 'nearest' }
let(:read) { Mongo::Options::Redacted.new(:mode => :nearest) }
it 'sets the read preference' do
expect(uri.uri_options[:read]).to eq(read)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:read]).to eq(read)
end
include_examples "roundtrips string"
end
end
context 'read preference tags provided' do
context 'single read preference tag set' do
let(:options) do
'readPreferenceTags=dc:ny,rack:1'
end
let(:read) do
Mongo::Options::Redacted.new(:tag_sets => [{ 'dc' => 'ny', 'rack' => '1' }])
end
it 'sets the read preference tag set' do
expect(uri.uri_options[:read]).to eq(read)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:read]).to eq(read)
end
include_examples "roundtrips string"
end
context 'multiple read preference tag sets' do
let(:options) do
'readPreferenceTags=dc:ny&readPreferenceTags=dc:bos'
end
let(:read) do
Mongo::Options::Redacted.new(:tag_sets => [{ 'dc' => 'ny' }, { 'dc' => 'bos' }])
end
it 'sets the read preference tag sets' do
expect(uri.uri_options[:read]).to eq(read)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:read]).to eq(read)
end
include_examples "roundtrips string"
end
end
context 'read preference max staleness option provided' do
let(:options) do
'readPreference=Secondary&maxStalenessSeconds=120'
end
let(:read) do
Mongo::Options::Redacted.new(mode: :secondary, :max_staleness => 120)
end
it 'sets the read preference max staleness in seconds' do
expect(uri.uri_options[:read]).to eq(read)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:read]).to eq(read)
end
context 'when the read preference and max staleness combination is invalid' do
context 'when max staleness is combined with read preference mode primary' do
let(:options) do
'readPreference=primary&maxStalenessSeconds=120'
end
it 'raises an exception when read preference is accessed on the client' do
client = new_local_client_nmio(string)
expect {
client.server_selector
}.to raise_exception(Mongo::Error::InvalidServerPreference)
end
end
context 'when the max staleness value is too small' do
let(:options) do
'readPreference=secondary&maxStalenessSeconds=89'
end
it 'does not raise an exception and drops the option' do
client = new_local_client_nmio(string)
expect(client.read_preference).to eq(BSON::Document.new(mode: :secondary))
end
it "returns the string without the dropped option" do
expect(uri.to_s).to eq("mongodb://localhost/?readPreference=secondary")
end
end
end
end
context 'replica set option provided' do
let(:rs_name) { 'dummy_rs' }
let(:options) { "replicaSet=#{rs_name}" }
it 'sets the replica set option' do
expect(uri.uri_options[:replica_set]).to eq(rs_name)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:replica_set]).to eq(rs_name)
end
include_examples "roundtrips string"
end
context 'auth mechanism provided' do
let(:string) { "#{scheme}#{credentials}@#{servers}/?#{options}" }
let(:user) { 'tyler' }
let(:password) { 's3kr4t' }
let(:credentials) { "#{user}:#{password}" }
let(:options) { "authMechanism=#{mechanism}" }
context 'plain' do
let(:mechanism) { 'PLAIN' }
let(:expected) { :plain }
it 'sets the auth mechanism to :plain' do
expect(uri.uri_options[:auth_mech]).to eq(expected)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:auth_mech]).to eq(expected)
end
it 'is case-insensitive' do
client = new_local_client_nmio(string.downcase)
expect(client.options[:auth_mech]).to eq(expected)
end
include_examples "roundtrips string"
context 'when mechanism_properties are provided' do
let(:options) { "authMechanism=#{mechanism}&authMechanismProperties=CANONICALIZE_HOST_NAME:true" }
it 'does not allow a client to be created' do
expect {
new_local_client_nmio(string)
}.to raise_error(Mongo::Auth::InvalidConfiguration, /mechanism_properties are not supported/)
end
end
end
context 'mongodb-cr' do
let(:mechanism) { 'MONGODB-CR' }
let(:expected) { :mongodb_cr }
it 'sets the auth mechanism to :mongodb_cr' do
expect(uri.uri_options[:auth_mech]).to eq(expected)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:auth_mech]).to eq(expected)
end
it 'is case-insensitive' do
client = new_local_client_nmio(string.downcase)
expect(client.options[:auth_mech]).to eq(expected)
end
include_examples "roundtrips string"
context 'when mechanism_properties are provided' do
let(:options) { "authMechanism=#{mechanism}&authMechanismProperties=CANONICALIZE_HOST_NAME:true" }
it 'does not allow a client to be created' do
expect {
new_local_client_nmio(string)
}.to raise_error(Mongo::Auth::InvalidConfiguration, /mechanism_properties are not supported/)
end
end
end
context 'gssapi' do
require_mongo_kerberos
let(:mechanism) { 'GSSAPI' }
let(:expected) { :gssapi }
let(:client) { new_local_client_nmio(string) }
it 'sets the auth mechanism to :gssapi' do
expect(uri.uri_options[:auth_mech]).to eq(expected)
end
it 'sets the options on a client created with the uri' do
expect(client.options[:auth_mech]).to eq(expected)
end
it 'is case-insensitive' do
client = new_local_client_nmio(string.downcase)
expect(client.options[:auth_mech]).to eq(expected)
end
include_examples "roundtrips string"
context 'when auth source is invalid' do
let(:options) { "authMechanism=#{mechanism}&authSource=foo" }
it 'does not allow a client to be created' do
expect {
client
}.to raise_error(Mongo::Auth::InvalidConfiguration, /invalid auth source/)
end
end
context 'when mechanism_properties are provided' do
let(:options) { "authMechanism=#{mechanism}&authMechanismProperties=SERVICE_NAME:other,CANONICALIZE_HOST_NAME:true" }
it 'sets the options on a client created with the uri' do
expect(client.options[:auth_mech_properties]).to eq({ 'canonicalize_host_name' => true, 'service_name' => 'other' })
end
include_examples "roundtrips string"
context 'when a mapping value is missing' do
let(:options) { "authMechanism=#{mechanism}&authMechanismProperties=SERVICE_NAME:,CANONICALIZE_HOST_NAME:" }
it 'sets the options to defaults' do
expect(client.options[:auth_mech_properties]).to eq({ 'service_name' => 'mongodb' })
end
it "roundtrips the string" do
expect(uri.to_s).to eq("mongodb://tyler:s3kr4t@localhost/?authMechanism=GSSAPI")
end
end
context 'when a mapping value is missing but another is present' do
let(:options) { "authMechanism=#{mechanism}&authMechanismProperties=SERVICE_NAME:foo,CANONICALIZE_HOST_NAME:" }
it 'only sets the present value' do
expect(client.options[:auth_mech_properties]).to eq({ 'service_name' => 'foo' })
end
it "roundtrips the string" do
expect(uri.to_s).to eq("mongodb://tyler:s3kr4t@localhost/?authMechanism=GSSAPI&authMechanismProperties=SERVICE_NAME:foo")
end
end
end
end
context 'scram-sha-1' do
let(:mechanism) { 'SCRAM-SHA-1' }
let(:expected) { :scram }
it 'sets the auth mechanism to :scram' do
expect(uri.uri_options[:auth_mech]).to eq(expected)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:auth_mech]).to eq(expected)
end
it 'is case-insensitive' do
client = new_local_client_nmio(string.downcase)
expect(client.options[:auth_mech]).to eq(expected)
end
include_examples "roundtrips string"
context 'when mechanism_properties are provided' do
let(:options) { "authMechanism=#{mechanism}&authMechanismProperties=CANONICALIZE_HOST_NAME:true" }
it 'does not allow a client to be created' do
expect {
new_local_client_nmio(string)
}.to raise_error(Mongo::Auth::InvalidConfiguration, /mechanism_properties are not supported/)
end
end
end
context 'mongodb-x509' do
let(:mechanism) { 'MONGODB-X509' }
let(:expected) { :mongodb_x509 }
let(:credentials) { user }
it 'sets the auth mechanism to :mongodb_x509' do
expect(uri.uri_options[:auth_mech]).to eq(expected)
end
it 'sets the options on a client created with the uri' do
client = new_local_client_nmio(string)
expect(client.options[:auth_mech]).to eq(expected)
end