@@ -14,9 +14,9 @@ import * as startProxyExports from "./start-proxy";
1414import { parseLanguage } from "./start-proxy" ;
1515import * as statusReport from "./status-report" ;
1616import {
17+ assertNotLogged ,
1718 checkExpectedLogMessages ,
1819 createFeatures ,
19- getRecordingLogger ,
2020 makeTestToken ,
2121 RecordingLogger ,
2222 setupTests ,
@@ -439,41 +439,155 @@ test("getCredentials accepts OIDC configurations", (t) => {
439439 t . assert ( credentials . some ( ( c ) => startProxyExports . isJFrogConfig ( c ) ) ) ;
440440} ) ;
441441
442- test ( "getCredentials logs a warning when a PAT is used without a username" , async ( t ) => {
443- const loggedMessages = [ ] ;
444- const logger = getRecordingLogger ( loggedMessages ) ;
445- const likelyWrongCredentials = toEncodedJSON ( [
442+ const getCredentialsMacro = test . macro ( {
443+ exec : async (
444+ t : ExecutionContext < unknown > ,
445+ credentials : startProxyExports . RawCredential [ ] ,
446+ checkAccepted : (
447+ t : ExecutionContext < unknown > ,
448+ logger : RecordingLogger ,
449+ results : startProxyExports . Credential [ ] ,
450+ ) => void ,
451+ ) => {
452+ const logger = new RecordingLogger ( ) ;
453+ const credentialsString = toEncodedJSON ( credentials ) ;
454+
455+ const results = startProxyExports . getCredentials (
456+ logger ,
457+ undefined ,
458+ credentialsString ,
459+ undefined ,
460+ ) ;
461+
462+ checkAccepted ( t , logger , results ) ;
463+ } ,
464+
465+ title : ( providedTitle = "" ) => `getCredentials - ${ providedTitle } ` ,
466+ } ) ;
467+
468+ test (
469+ "warns for PAT-like password without a username" ,
470+ getCredentialsMacro ,
471+ [
446472 {
447473 type : "git_server" ,
448474 host : "https://github.com/" ,
449475 password : `ghp_${ makeTestToken ( ) } ` ,
450476 } ,
451- ] ) ;
452-
453- const results = startProxyExports . getCredentials (
454- logger ,
455- undefined ,
456- likelyWrongCredentials ,
457- undefined ,
458- ) ;
477+ ] ,
478+ ( t , logger , results ) => {
479+ // The configurations should be accepted, despite the likely problem.
480+ t . assert ( results ) ;
481+ t . is ( results . length , 1 ) ;
482+ t . is ( results [ 0 ] . type , "git_server" ) ;
483+ t . is ( results [ 0 ] . host , "https://github.com/" ) ;
484+
485+ if ( startProxyExports . isUsernamePassword ( results [ 0 ] ) ) {
486+ t . assert ( results [ 0 ] . password ?. startsWith ( "ghp_" ) ) ;
487+ } else {
488+ t . fail ( "Expected a `UsernamePassword`-based credential." ) ;
489+ }
490+
491+ // A warning should have been logged.
492+ checkExpectedLogMessages ( t , logger . messages , [
493+ "using a GitHub Personal Access Token (PAT), but no username was provided" ,
494+ ] ) ;
495+ } ,
496+ ) ;
459497
460- // The configuration should be accepted, despite the likely problem.
461- t . assert ( results ) ;
462- t . is ( results . length , 1 ) ;
463- t . is ( results [ 0 ] . type , "git_server" ) ;
464- t . is ( results [ 0 ] . host , "https://github.com/" ) ;
498+ test (
499+ "no warning for PAT-like password with a username" ,
500+ getCredentialsMacro ,
501+ [
502+ {
503+ type : "git_server" ,
504+ host : "https://github.com/" ,
505+ username : "someone" ,
506+ password : `ghp_${ makeTestToken ( ) } ` ,
507+ } ,
508+ ] ,
509+ ( t , logger , results ) => {
510+ // The configurations should be accepted, despite the likely problem.
511+ t . assert ( results ) ;
512+ t . is ( results . length , 1 ) ;
513+ t . is ( results [ 0 ] . type , "git_server" ) ;
514+ t . is ( results [ 0 ] . host , "https://github.com/" ) ;
515+
516+ if ( startProxyExports . isUsernamePassword ( results [ 0 ] ) ) {
517+ t . assert ( results [ 0 ] . password ?. startsWith ( "ghp_" ) ) ;
518+ } else {
519+ t . fail ( "Expected a `UsernamePassword`-based credential." ) ;
520+ }
521+
522+ assertNotLogged (
523+ t ,
524+ logger ,
525+ "using a GitHub Personal Access Token (PAT), but no username was provided" ,
526+ ) ;
527+ } ,
528+ ) ;
465529
466- if ( startProxyExports . isUsernamePassword ( results [ 0 ] ) ) {
467- t . assert ( results [ 0 ] . password ?. startsWith ( "ghp_" ) ) ;
468- } else {
469- t . fail ( "Expected a `UsernamePassword`-based credential." ) ;
470- }
530+ test (
531+ "warns for PAT-like token without a username" ,
532+ getCredentialsMacro ,
533+ [
534+ {
535+ type : "git_server" ,
536+ host : "https://github.com/" ,
537+ token : `ghp_${ makeTestToken ( ) } ` ,
538+ } ,
539+ ] ,
540+ ( t , logger , results ) => {
541+ // The configurations should be accepted, despite the likely problem.
542+ t . assert ( results ) ;
543+ t . is ( results . length , 1 ) ;
544+ t . is ( results [ 0 ] . type , "git_server" ) ;
545+ t . is ( results [ 0 ] . host , "https://github.com/" ) ;
546+
547+ if ( startProxyExports . isToken ( results [ 0 ] ) ) {
548+ t . assert ( results [ 0 ] . token ?. startsWith ( "ghp_" ) ) ;
549+ } else {
550+ t . fail ( "Expected a `Token`-based credential." ) ;
551+ }
552+
553+ // A warning should have been logged.
554+ checkExpectedLogMessages ( t , logger . messages , [
555+ "using a GitHub Personal Access Token (PAT), but no username was provided" ,
556+ ] ) ;
557+ } ,
558+ ) ;
471559
472- // A warning should have been logged.
473- checkExpectedLogMessages ( t , loggedMessages , [
474- "using a GitHub Personal Access Token (PAT), but no username was provided" ,
475- ] ) ;
476- } ) ;
560+ test (
561+ "no warning for PAT-like token with a username" ,
562+ getCredentialsMacro ,
563+ [
564+ {
565+ type : "git_server" ,
566+ host : "https://github.com/" ,
567+ username : "someone" ,
568+ token : `ghp_${ makeTestToken ( ) } ` ,
569+ } ,
570+ ] ,
571+ ( t , logger , results ) => {
572+ // The configurations should be accepted, despite the likely problem.
573+ t . assert ( results ) ;
574+ t . is ( results . length , 1 ) ;
575+ t . is ( results [ 0 ] . type , "git_server" ) ;
576+ t . is ( results [ 0 ] . host , "https://github.com/" ) ;
577+
578+ if ( startProxyExports . isToken ( results [ 0 ] ) ) {
579+ t . assert ( results [ 0 ] . token ?. startsWith ( "ghp_" ) ) ;
580+ } else {
581+ t . fail ( "Expected a `Token`-based credential." ) ;
582+ }
583+
584+ assertNotLogged (
585+ t ,
586+ logger ,
587+ "using a GitHub Personal Access Token (PAT), but no username was provided" ,
588+ ) ;
589+ } ,
590+ ) ;
477591
478592test ( "getCredentials returns all credentials for Actions when using LANGUAGE_TO_REGISTRY_TYPE" , async ( t ) => {
479593 const credentialsInput = toEncodedJSON ( mixedCredentials ) ;
0 commit comments