@@ -3,38 +3,86 @@ import { render, screen, fireEvent } from '@testing-library/react';
3
3
import { expect } from 'chai' ;
4
4
import sinon from 'sinon' ;
5
5
import ConnectionStringUrl from 'mongodb-connection-string-url' ;
6
+ import { AuthMechanism } from 'mongodb' ;
6
7
7
8
import AuthenticationTab from './authentication-tab' ;
8
9
import type { ConnectionFormError } from '../../../utils/validation' ;
9
10
import type { UpdateConnectionFormField } from '../../../hooks/use-connect-form' ;
11
+ import { ConnectionFormPreferencesContext } from '../../../hooks/use-connect-form-preferences' ;
12
+ import type { ConnectionFormPreferences } from '../../../hooks/use-connect-form-preferences' ;
10
13
11
14
function renderComponent ( {
12
15
errors = [ ] ,
13
16
connectionStringUrl = new ConnectionStringUrl ( 'mongodb://localhost:27017' ) ,
17
+ connectionFormPreferences = {
18
+ enableOidc : true ,
19
+ } ,
14
20
updateConnectionFormField,
15
21
} : {
16
22
connectionStringUrl ?: ConnectionStringUrl ;
23
+ connectionFormPreferences ?: Partial < ConnectionFormPreferences > ;
17
24
errors ?: ConnectionFormError [ ] ;
18
25
updateConnectionFormField : UpdateConnectionFormField ;
19
26
} ) {
20
27
render (
21
- < AuthenticationTab
22
- errors = { errors }
23
- connectionStringUrl = { connectionStringUrl }
24
- updateConnectionFormField = { updateConnectionFormField }
25
- connectionOptions = { {
26
- connectionString : 'mongodb://localhost:27017' ,
27
- } }
28
- />
28
+ < ConnectionFormPreferencesContext . Provider
29
+ value = { connectionFormPreferences }
30
+ >
31
+ < AuthenticationTab
32
+ errors = { errors }
33
+ connectionStringUrl = { connectionStringUrl }
34
+ updateConnectionFormField = { updateConnectionFormField }
35
+ connectionOptions = { {
36
+ connectionString : 'mongodb://localhost:27017' ,
37
+ } }
38
+ />
39
+ </ ConnectionFormPreferencesContext . Provider >
29
40
) ;
30
41
}
31
42
43
+ const authMechanisms = [
44
+ {
45
+ title : 'Username/Password' ,
46
+ id : AuthMechanism . MONGODB_DEFAULT ,
47
+ } ,
48
+ {
49
+ title : 'OIDC (Preview)' ,
50
+ id : AuthMechanism . MONGODB_OIDC ,
51
+ } ,
52
+ {
53
+ title : 'X.509' ,
54
+ id : AuthMechanism . MONGODB_X509 ,
55
+ } ,
56
+ {
57
+ title : 'Kerberos' ,
58
+ id : AuthMechanism . MONGODB_GSSAPI ,
59
+ } ,
60
+ {
61
+ title : 'LDAP' ,
62
+ id : AuthMechanism . MONGODB_PLAIN ,
63
+ } ,
64
+ {
65
+ title : 'AWS IAM' ,
66
+ id : AuthMechanism . MONGODB_AWS ,
67
+ } ,
68
+ ] ;
69
+
32
70
describe ( 'AuthenticationTab Component' , function ( ) {
33
71
let updateConnectionFormFieldSpy : sinon . SinonSpy ;
34
72
beforeEach ( function ( ) {
35
73
updateConnectionFormFieldSpy = sinon . spy ( ) ;
36
74
} ) ;
37
75
76
+ it ( 'renders all of the auth mechanisms' , function ( ) {
77
+ renderComponent ( {
78
+ updateConnectionFormField : updateConnectionFormFieldSpy ,
79
+ } ) ;
80
+
81
+ authMechanisms . forEach ( ( mechanism ) => {
82
+ expect ( screen . getByText ( mechanism . title ) ) . to . be . visible ;
83
+ } ) ;
84
+ } ) ;
85
+
38
86
describe ( 'when a new auth mechanism is clicked' , function ( ) {
39
87
beforeEach ( function ( ) {
40
88
renderComponent ( {
@@ -66,7 +114,6 @@ describe('AuthenticationTab Component', function () {
66
114
67
115
it ( 'renders the username/password tab when auth is set' , function ( ) {
68
116
renderComponent ( {
69
- errors : [ ] ,
70
117
connectionStringUrl : new ConnectionStringUrl (
71
118
'mongodb://a123:b123@localhost'
72
119
) ,
@@ -79,7 +126,6 @@ describe('AuthenticationTab Component', function () {
79
126
80
127
it ( 'renders the username/password tab when only password is set' , function ( ) {
81
128
renderComponent ( {
82
- errors : [ ] ,
83
129
connectionStringUrl : new ConnectionStringUrl (
84
130
'mongodb://:b123@localhost' ,
85
131
{ looseValidation : true }
@@ -90,4 +136,30 @@ describe('AuthenticationTab Component', function () {
90
136
expect ( screen . getByLabelText ( 'Username' ) ) . to . be . visible ;
91
137
expect ( screen . getByLabelText ( 'Password' ) ) . to . be . visible ;
92
138
} ) ;
139
+
140
+ it ( 'should not render OIDC auth when its set to false in the preferences' , function ( ) {
141
+ renderComponent ( {
142
+ connectionFormPreferences : { showOIDCAuth : false } ,
143
+ updateConnectionFormField : updateConnectionFormFieldSpy ,
144
+ } ) ;
145
+
146
+ const oidcAuthName = authMechanisms . find (
147
+ ( tab ) => tab . id === 'MONGODB-OIDC'
148
+ ) ?. title ;
149
+ expect ( oidcAuthName ) . to . not . be . undefined ;
150
+ expect ( screen . queryByText ( oidcAuthName as string ) ) . to . not . exist ;
151
+ } ) ;
152
+
153
+ it ( 'should not render Kerberos auth when its set to false in the preferences' , function ( ) {
154
+ renderComponent ( {
155
+ connectionFormPreferences : { showKerberosAuth : false } ,
156
+ updateConnectionFormField : updateConnectionFormFieldSpy ,
157
+ } ) ;
158
+
159
+ const kerberosAuthName = authMechanisms . find (
160
+ ( tab ) => tab . id === 'GSSAPI'
161
+ ) ?. title ;
162
+ expect ( kerberosAuthName ) . to . not . be . undefined ;
163
+ expect ( screen . queryByText ( kerberosAuthName as string ) ) . to . not . exist ;
164
+ } ) ;
93
165
} ) ;
0 commit comments