@@ -974,6 +974,91 @@ describe('git-auth-helper tests', () => {
974974 ) . toBe ( false )
975975 expect ( ( authHelper as any ) . testCredentialsConfigPath ( '' ) ) . toBe ( false )
976976 } )
977+
978+ const configureAuth_uses_default_git_user_when_sshUser_empty =
979+ 'configureAuth uses default git user when sshUser is empty'
980+ it ( configureAuth_uses_default_git_user_when_sshUser_empty , async ( ) => {
981+ // Arrange
982+ await setup ( configureAuth_uses_default_git_user_when_sshUser_empty )
983+ settings . sshUser = ''
984+ settings . sshKey = ''
985+ const authHelper = gitAuthHelper . createAuthHelper ( git , settings )
986+
987+ // Act
988+ await authHelper . configureAuth ( )
989+ await authHelper . configureGlobalAuth ( )
990+
991+ // Assert - verify that git@github.com is in the insteadOf config
992+ const home = git . env [ 'HOME' ] || tempHomedir
993+ const configContent = (
994+ await fs . promises . readFile ( path . join ( home , '.gitconfig' ) )
995+ ) . toString ( )
996+ expect ( configContent . indexOf ( 'url.https://github.com/.insteadOf git@github.com' ) ) . toBeGreaterThanOrEqual ( 0 )
997+ } )
998+
999+ const configureAuth_uses_custom_ssh_user_when_sshUser_provided =
1000+ 'configureAuth uses custom SSH user when sshUser is provided'
1001+ it ( configureAuth_uses_custom_ssh_user_when_sshUser_provided , async ( ) => {
1002+ // Arrange
1003+ await setup ( configureAuth_uses_custom_ssh_user_when_sshUser_provided )
1004+ settings . sshUser = 'customuser'
1005+ settings . sshKey = ''
1006+ const authHelper = gitAuthHelper . createAuthHelper ( git , settings )
1007+
1008+ // Act
1009+ await authHelper . configureAuth ( )
1010+ await authHelper . configureGlobalAuth ( )
1011+
1012+ // Assert - verify that customuser@github.com is in the insteadOf config
1013+ const home = git . env [ 'HOME' ] || tempHomedir
1014+ const configContent = (
1015+ await fs . promises . readFile ( path . join ( home , '.gitconfig' ) )
1016+ ) . toString ( )
1017+ expect ( configContent . indexOf ( 'url.https://github.com/.insteadOf customuser@github.com' ) ) . toBeGreaterThanOrEqual ( 0 )
1018+ } )
1019+
1020+ const configureGlobalAuth_with_custom_sshUser =
1021+ 'configureGlobalAuth with custom sshUser'
1022+ it ( configureGlobalAuth_with_custom_sshUser , async ( ) => {
1023+ // Arrange
1024+ await setup ( configureGlobalAuth_with_custom_sshUser )
1025+ settings . sshUser = 'admin'
1026+ settings . sshKey = ''
1027+ const authHelper = gitAuthHelper . createAuthHelper ( git , settings )
1028+
1029+ // Act
1030+ await authHelper . configureAuth ( )
1031+ await authHelper . configureGlobalAuth ( )
1032+
1033+ // Assert - verify the .insteadOf config contains the custom user
1034+ const home = git . env [ 'HOME' ] || tempHomedir
1035+ const configContent = (
1036+ await fs . promises . readFile ( path . join ( home , '.gitconfig' ) )
1037+ ) . toString ( )
1038+ expect ( configContent . indexOf ( 'url.https://github.com/.insteadOf admin@github.com' ) ) . toBeGreaterThanOrEqual ( 0 )
1039+ } )
1040+
1041+ const configureSubmoduleAuth_with_custom_sshUser =
1042+ 'configureSubmoduleAuth with custom sshUser'
1043+ it ( configureSubmoduleAuth_with_custom_sshUser , async ( ) => {
1044+ // Arrange
1045+ await setup ( configureSubmoduleAuth_with_custom_sshUser )
1046+ settings . sshUser = 'deploy'
1047+ settings . sshKey = ''
1048+ const authHelper = gitAuthHelper . createAuthHelper ( git , settings )
1049+ await authHelper . configureAuth ( )
1050+ const mockSubmoduleForeach = git . submoduleForeach as jest . Mock < any , any >
1051+ mockSubmoduleForeach . mockClear ( )
1052+
1053+ // Act
1054+ await authHelper . configureSubmoduleAuth ( )
1055+
1056+ // Assert - verify the insteadOf config uses the custom user
1057+ expect ( mockSubmoduleForeach ) . toHaveBeenCalledTimes ( 3 )
1058+ expect ( mockSubmoduleForeach . mock . calls [ 1 ] [ 0 ] ) . toMatch (
1059+ / u r l .* i n s t e a d O f .* d e p l o y @ g i t h u b .c o m : /
1060+ )
1061+ } )
9771062} )
9781063
9791064async function setup ( testName : string ) : Promise < void > {
0 commit comments