@@ -23,6 +23,8 @@ describe('VersionManagerService', () => {
2323
2424 const getVersion = jest . fn ( ) . mockReturnValue ( '4.3.0' ) ;
2525 const getStorageDir = jest . fn ( ) . mockReturnValue ( undefined ) ;
26+ const getUsername = jest . fn ( ) . mockReturnValue ( undefined ) ;
27+ const getPassword = jest . fn ( ) . mockReturnValue ( undefined ) ;
2628 const setVersion = jest . fn ( ) ;
2729
2830 let testBed : TestingModule ;
@@ -50,6 +52,14 @@ describe('VersionManagerService', () => {
5052 // return 'https://search.maven.custom/solrsearch/select?q=g:${repository.groupId}+AND+a:${repository.artifactId}&core=gav&start=0&rows=250';
5153 }
5254
55+ if ( k === 'generator-cli.repository.username' ) {
56+ return getUsername ( ) ;
57+ }
58+
59+ if ( k === 'generator-cli.repository.password' ) {
60+ return getPassword ( ) ;
61+ }
62+
5363 return getVersion ( k ) ;
5464 } ,
5565 set : setVersion ,
@@ -66,6 +76,8 @@ describe('VersionManagerService', () => {
6676 beforeEach ( async ( ) => {
6777 [ get ] . forEach ( ( fn ) => fn . mockClear ( ) ) ;
6878 getStorageDir . mockReturnValue ( undefined ) ;
79+ getUsername . mockReturnValue ( undefined ) ;
80+ getPassword . mockReturnValue ( undefined ) ;
6981 await compile ( ) ;
7082 fs . existsSync
7183 . mockReset ( )
@@ -143,6 +155,7 @@ describe('VersionManagerService', () => {
143155 expect ( get ) . toHaveBeenNthCalledWith (
144156 1 ,
145157 'https://central.sonatype.com/solrsearch/select?q=g:org.openapitools+AND+a:openapi-generator-cli&core=gav&start=0&rows=200' ,
158+ { } ,
146159 ) ;
147160 } ) ;
148161
@@ -185,6 +198,7 @@ describe('VersionManagerService', () => {
185198 expect ( get ) . toHaveBeenNthCalledWith (
186199 1 ,
187200 'https://central.sonatype.com/solrsearch/select?q=g:org.openapitools+AND+a:openapi-generator-cli&core=gav&start=0&rows=200' ,
201+ { } ,
188202 ) ;
189203 } ) ;
190204
@@ -220,6 +234,7 @@ describe('VersionManagerService', () => {
220234 expect ( get ) . toHaveBeenNthCalledWith (
221235 1 ,
222236 'https://central.sonatype.com/solrsearch/select?q=g:org.openapitools+AND+a:openapi-generator-cli&core=gav&start=0&rows=200' ,
237+ { } ,
223238 ) ;
224239 } ) ;
225240
@@ -372,7 +387,9 @@ describe('VersionManagerService', () => {
372387
373388 it ( 'logs the correct messages' , ( ) => {
374389 expect ( logMessages ) . toEqual ( {
375- before : [ chalk . yellow ( `Download 4.2.0 ...` ) ] ,
390+ before : [
391+ chalk . yellow ( `Download 4.2.0 ...` ) ,
392+ ] ,
376393 after : [
377394 chalk . red ( `Download failed, because of: "HTTP 404 Not Found"` ) ,
378395 ] ,
@@ -424,7 +441,9 @@ describe('VersionManagerService', () => {
424441 describe ( 'logging' , ( ) => {
425442 it ( 'logs the correct messages' , ( ) => {
426443 expect ( logMessages ) . toEqual ( {
427- before : [ chalk . yellow ( `Download 4.2.0 ...` ) ] ,
444+ before : [
445+ chalk . yellow ( `Download 4.2.0 ...` ) ,
446+ ] ,
428447 after : [ chalk . green ( `Downloaded 4.2.0` ) ] ,
429448 } ) ;
430449 } ) ;
@@ -443,7 +462,9 @@ describe('VersionManagerService', () => {
443462 await compile ( ) ;
444463 await fixture . download ( '4.2.0' ) ;
445464 expect ( logMessages ) . toEqual ( {
446- before : [ chalk . yellow ( `Download 4.2.0 ...` ) ] ,
465+ before : [
466+ chalk . yellow ( `Download 4.2.0 ...` ) ,
467+ ] ,
447468 after : [
448469 chalk . green (
449470 `Downloaded 4.2.0 to custom storage location ${ expected } ` ,
@@ -601,5 +622,85 @@ describe('VersionManagerService', () => {
601622 } ) ;
602623 } ) ;
603624 } ) ;
625+
626+ describe ( 'repository authentication' , ( ) => {
627+ const mavenDocs = {
628+ data : {
629+ response : {
630+ docs : [
631+ { v : '4.2.0' , timestamp : 1599197918000 } ,
632+ { v : '4.3.1' , timestamp : 1588758220000 } ,
633+ ] ,
634+ } ,
635+ } ,
636+ } ;
637+
638+ describe ( 'when username and password are configured' , ( ) => {
639+ beforeEach ( async ( ) => {
640+ getUsername . mockReturnValue ( 'myuser' ) ;
641+ getPassword . mockReturnValue ( 'mypass' ) ;
642+ await compile ( ) ;
643+ get . mockReturnValue ( of ( mavenDocs ) ) ;
644+ } ) ;
645+
646+ it ( 'passes auth to the query request' , async ( ) => {
647+ await fixture . getAll ( ) . toPromise ( ) ;
648+ expect ( get ) . toHaveBeenCalledWith (
649+ expect . any ( String ) ,
650+ { auth : { username : 'myuser' , password : 'mypass' } } ,
651+ ) ;
652+ } ) ;
653+
654+ it ( 'passes auth to the download request' , async ( ) => {
655+ const data = { pipe : jest . fn ( ) } ;
656+ const file = {
657+ on : jest . fn ( ) . mockImplementation ( ( listener , res ) => {
658+ if ( listener === 'finish' ) return res ( ) ;
659+ } ) ,
660+ } ;
661+
662+ fs . mkdtempSync . mockReturnValue ( '/tmp/generator-cli-abcDEF' ) ;
663+ fs . createWriteStream . mockReturnValue ( file ) ;
664+ get . mockReturnValue ( of ( { data } ) ) ;
665+
666+ await fixture . download ( '4.2.0' ) ;
667+ expect ( get ) . toHaveBeenCalledWith (
668+ expect . any ( String ) ,
669+ { responseType : 'stream' , auth : { username : 'myuser' , password : 'mypass' } } ,
670+ ) ;
671+ } ) ;
672+ } ) ;
673+
674+ describe ( 'when only username is configured' , ( ) => {
675+ beforeEach ( async ( ) => {
676+ getUsername . mockReturnValue ( 'myuser' ) ;
677+ getPassword . mockReturnValue ( undefined ) ;
678+ await compile ( ) ;
679+ get . mockReturnValue ( of ( mavenDocs ) ) ;
680+ } ) ;
681+
682+ it ( 'does not pass auth to the query request' , async ( ) => {
683+ await fixture . getAll ( ) ;
684+ expect ( get ) . toHaveBeenCalledWith (
685+ expect . any ( String ) ,
686+ { } ,
687+ ) ;
688+ } ) ;
689+ } ) ;
690+
691+ describe ( 'when neither username nor password is configured' , ( ) => {
692+ beforeEach ( async ( ) => {
693+ get . mockReturnValue ( of ( mavenDocs ) ) ;
694+ } ) ;
695+
696+ it ( 'does not pass auth to the query request' , async ( ) => {
697+ await fixture . getAll ( ) ;
698+ expect ( get ) . toHaveBeenCalledWith (
699+ expect . any ( String ) ,
700+ { } ,
701+ ) ;
702+ } ) ;
703+ } ) ;
704+ } ) ;
604705 } ) ;
605- } ) ;
706+ } ) ;
0 commit comments