11import React from 'react' ;
2- import { render , screen , fireEvent } from '@testing-library/react' ;
2+ import { render , screen , fireEvent , cleanup } from '@testing-library/react' ;
33import { expect } from 'chai' ;
44import sinon from 'sinon' ;
55import ConnectionStringUrl from 'mongodb-connection-string-url' ;
66
7- import AuthenticationGssapi , {
8- GSSAPI_CANONICALIZE_HOST_NAME_LABEL ,
9- GSSAPI_PRINCIPAL_NAME_LABEL ,
10- GSSAPI_SERVICE_NAME_LABEL ,
11- GSSAPI_SERVICE_REALM_LABEL ,
12- } from './authentication-gssapi' ;
7+ import AuthenticationGssapi from './authentication-gssapi' ;
138import type { ConnectionFormError } from '../../../utils/validation' ;
149import type { UpdateConnectionFormField } from '../../../hooks/use-connect-form' ;
1510
@@ -32,6 +27,8 @@ function renderComponent({
3227}
3328
3429describe ( 'AuthenticationGssapi Component' , function ( ) {
30+ afterEach ( cleanup ) ;
31+
3532 let updateConnectionFormFieldSpy : sinon . SinonSpy ;
3633 beforeEach ( function ( ) {
3734 updateConnectionFormFieldSpy = sinon . spy ( ) ;
@@ -44,7 +41,7 @@ describe('AuthenticationGssapi Component', function () {
4441 } ) ;
4542 expect ( updateConnectionFormFieldSpy . callCount ) . to . equal ( 0 ) ;
4643
47- fireEvent . change ( screen . getByLabelText ( GSSAPI_PRINCIPAL_NAME_LABEL ) , {
44+ fireEvent . change ( screen . getByTestId ( 'gssapi-principal-input' ) , {
4845 target : { value : 'good sandwich' } ,
4946 } ) ;
5047 } ) ;
@@ -65,7 +62,7 @@ describe('AuthenticationGssapi Component', function () {
6562 } ) ;
6663 expect ( updateConnectionFormFieldSpy . callCount ) . to . equal ( 0 ) ;
6764
68- fireEvent . change ( screen . getByLabelText ( GSSAPI_SERVICE_NAME_LABEL ) , {
65+ fireEvent . change ( screen . getByTestId ( 'gssapi-service-name-input' ) , {
6966 target : { value : 'good sandwich' } ,
7067 } ) ;
7168 } ) ;
@@ -87,7 +84,7 @@ describe('AuthenticationGssapi Component', function () {
8784 } ) ;
8885 expect ( updateConnectionFormFieldSpy . callCount ) . to . equal ( 0 ) ;
8986
90- fireEvent . change ( screen . getByLabelText ( GSSAPI_SERVICE_REALM_LABEL ) , {
87+ fireEvent . change ( screen . getByTestId ( 'gssapi-service-realm-input' ) , {
9188 target : { value : 'good sandwich' } ,
9289 } ) ;
9390 } ) ;
@@ -102,25 +99,136 @@ describe('AuthenticationGssapi Component', function () {
10299 } ) ;
103100 } ) ;
104101
105- describe ( 'when the canoncalize hostname is changed ' , function ( ) {
102+ describe ( 'when canoncalize hostname is empty ' , function ( ) {
106103 beforeEach ( function ( ) {
107104 renderComponent ( {
108105 updateConnectionFormField : updateConnectionFormFieldSpy ,
109106 } ) ;
110107
111108 expect ( updateConnectionFormFieldSpy . callCount ) . to . equal ( 0 ) ;
112- const checkbox = screen . getByLabelText (
113- GSSAPI_CANONICALIZE_HOST_NAME_LABEL
109+ } ) ;
110+
111+ it ( 'selects None' , function ( ) {
112+ const radio = screen
113+ . getByTestId ( 'gssapi-canonicalize-host-name-none' )
114+ . closest ( 'input' ) ;
115+
116+ expect ( radio . checked ) . to . be . true ;
117+ } ) ;
118+
119+ it ( 'updates the form field with CANONICALIZE_HOST_NAME forward' , function ( ) {
120+ const button = screen . getByTestId (
121+ 'gssapi-canonicalize-host-name-forward'
114122 ) ;
115- fireEvent . click ( checkbox ) ;
123+ fireEvent . click ( button ) ;
124+
125+ expect ( updateConnectionFormFieldSpy . callCount ) . to . equal ( 1 ) ;
126+ expect ( updateConnectionFormFieldSpy . firstCall . args [ 0 ] ) . to . deep . equal ( {
127+ key : 'CANONICALIZE_HOST_NAME' ,
128+ type : 'update-auth-mechanism-property' ,
129+ value : 'forward' ,
130+ } ) ;
116131 } ) ;
117132
118- it ( 'calls to update the form field' , function ( ) {
133+ it ( 'updates the form field with CANONICALIZE_HOST_NAME forwardAndReverse' , function ( ) {
134+ const button = screen . getByTestId (
135+ 'gssapi-canonicalize-host-name-forwardAndReverse'
136+ ) ;
137+ fireEvent . click ( button ) ;
138+
139+ expect ( updateConnectionFormFieldSpy . callCount ) . to . equal ( 1 ) ;
140+ expect ( updateConnectionFormFieldSpy . firstCall . args [ 0 ] ) . to . deep . equal ( {
141+ key : 'CANONICALIZE_HOST_NAME' ,
142+ type : 'update-auth-mechanism-property' ,
143+ value : 'forwardAndReverse' ,
144+ } ) ;
145+ } ) ;
146+ } ) ;
147+
148+ describe ( 'when canoncalize hostname is set' , function ( ) {
149+ beforeEach ( function ( ) {
150+ renderComponent ( {
151+ updateConnectionFormField : updateConnectionFormFieldSpy ,
152+ connectionStringUrl : new ConnectionStringUrl (
153+ 'mongodb://localhost:27017/?authMechanism=GSSAPI&authSource=%24external&authMechanismProperties=CANONICALIZE_HOST_NAME%3Aforward'
154+ ) ,
155+ } ) ;
156+
157+ expect ( updateConnectionFormFieldSpy . callCount ) . to . equal ( 0 ) ;
158+ } ) ;
159+
160+ it ( 'resets CANONICALIZE_HOST_NAME when None is selected' , function ( ) {
161+ const button = screen . getByTestId ( 'gssapi-canonicalize-host-name-none' ) ;
162+ fireEvent . click ( button ) ;
163+
119164 expect ( updateConnectionFormFieldSpy . callCount ) . to . equal ( 1 ) ;
120165 expect ( updateConnectionFormFieldSpy . firstCall . args [ 0 ] ) . to . deep . equal ( {
121166 key : 'CANONICALIZE_HOST_NAME' ,
122167 type : 'update-auth-mechanism-property' ,
123- value : 'true' ,
168+ value : '' ,
169+ } ) ;
170+ } ) ;
171+ } ) ;
172+
173+ describe ( 'when password is not in the connection string' , function ( ) {
174+ beforeEach ( function ( ) {
175+ renderComponent ( {
176+ updateConnectionFormField : updateConnectionFormFieldSpy ,
177+ } ) ;
178+
179+ expect ( updateConnectionFormFieldSpy . callCount ) . to . equal ( 0 ) ;
180+ } ) ;
181+
182+ it ( 'allows to edit the password when enter password directly is enabled' , function ( ) {
183+ expect ( screen . queryByTestId ( 'gssapi-password-input' ) ) . to . not . exist ;
184+ const checkbox = screen . getByTestId ( 'gssapi-password-checkbox' ) ;
185+ expect ( checkbox . closest ( 'input' ) . checked ) . to . be . false ;
186+
187+ fireEvent . click ( checkbox ) ;
188+
189+ const passwordInput = screen . getByTestId ( 'gssapi-password-input' ) ;
190+
191+ fireEvent . change ( passwordInput , {
192+ target : { value : 'some-password' } ,
193+ } ) ;
194+
195+ expect ( updateConnectionFormFieldSpy . callCount ) . to . equal ( 1 ) ;
196+ expect ( updateConnectionFormFieldSpy . firstCall . args [ 0 ] ) . to . deep . equal ( {
197+ type : 'update-password' ,
198+ password : 'some-password' ,
199+ } ) ;
200+ } ) ;
201+ } ) ;
202+
203+ describe ( 'when password is in the connection string' , function ( ) {
204+ beforeEach ( function ( ) {
205+ renderComponent ( {
206+ connectionStringUrl : new ConnectionStringUrl (
207+ 'mongodb://user:password@localhost:27017'
208+ ) ,
209+ updateConnectionFormField : updateConnectionFormFieldSpy ,
210+ } ) ;
211+
212+ expect ( updateConnectionFormFieldSpy . callCount ) . to . equal ( 0 ) ;
213+ } ) ;
214+
215+ it ( 'enables the checkbox and shows the password input' , function ( ) {
216+ const checkbox = screen . getByTestId ( 'gssapi-password-checkbox' ) ;
217+ expect ( checkbox . closest ( 'input' ) . checked ) . to . be . true ;
218+ const passwordInput = screen . queryByTestId ( 'gssapi-password-input' ) ;
219+ expect ( passwordInput ) . to . exist ;
220+ expect ( passwordInput . closest ( 'input' ) . value ) . to . equal ( 'password' ) ;
221+ } ) ;
222+
223+ it ( 'resets the password when the checkbox is unchecked' , function ( ) {
224+ const checkbox = screen . getByTestId ( 'gssapi-password-checkbox' ) ;
225+ expect ( checkbox . closest ( 'input' ) . checked ) . to . be . true ;
226+ fireEvent . click ( checkbox ) ;
227+
228+ expect ( updateConnectionFormFieldSpy . callCount ) . to . equal ( 1 ) ;
229+ expect ( updateConnectionFormFieldSpy . firstCall . args [ 0 ] ) . to . deep . equal ( {
230+ type : 'update-password' ,
231+ password : '' ,
124232 } ) ;
125233 } ) ;
126234 } ) ;
@@ -129,6 +237,7 @@ describe('AuthenticationGssapi Component', function () {
129237 renderComponent ( {
130238 errors : [
131239 {
240+ fieldTab : 'authentication' ,
132241 fieldName : 'kerberosPrincipal' ,
133242 message : 'kerberosPrincipal error' ,
134243 } ,
0 commit comments