@@ -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,10 @@ 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+ chalk . yellow ( `Downloading from repo1.maven.org ...` ) ,
393+ ] ,
376394 after : [
377395 chalk . red ( `Download failed, because of: "HTTP 404 Not Found"` ) ,
378396 ] ,
@@ -424,7 +442,10 @@ describe('VersionManagerService', () => {
424442 describe ( 'logging' , ( ) => {
425443 it ( 'logs the correct messages' , ( ) => {
426444 expect ( logMessages ) . toEqual ( {
427- before : [ chalk . yellow ( `Download 4.2.0 ...` ) ] ,
445+ before : [
446+ chalk . yellow ( `Download 4.2.0 ...` ) ,
447+ chalk . yellow ( `Downloading from repo1.maven.org ...` ) ,
448+ ] ,
428449 after : [ chalk . green ( `Downloaded 4.2.0` ) ] ,
429450 } ) ;
430451 } ) ;
@@ -443,7 +464,10 @@ describe('VersionManagerService', () => {
443464 await compile ( ) ;
444465 await fixture . download ( '4.2.0' ) ;
445466 expect ( logMessages ) . toEqual ( {
446- before : [ chalk . yellow ( `Download 4.2.0 ...` ) ] ,
467+ before : [
468+ chalk . yellow ( `Download 4.2.0 ...` ) ,
469+ chalk . yellow ( `Downloading from repo1.maven.org ...` ) ,
470+ ] ,
447471 after : [
448472 chalk . green (
449473 `Downloaded 4.2.0 to custom storage location ${ expected } ` ,
@@ -601,5 +625,85 @@ describe('VersionManagerService', () => {
601625 } ) ;
602626 } ) ;
603627 } ) ;
628+
629+ describe ( 'repository authentication' , ( ) => {
630+ const mavenDocs = {
631+ data : {
632+ response : {
633+ docs : [
634+ { v : '4.2.0' , timestamp : 1599197918000 } ,
635+ { v : '4.3.1' , timestamp : 1588758220000 } ,
636+ ] ,
637+ } ,
638+ } ,
639+ } ;
640+
641+ describe ( 'when username and password are configured' , ( ) => {
642+ beforeEach ( async ( ) => {
643+ getUsername . mockReturnValue ( 'myuser' ) ;
644+ getPassword . mockReturnValue ( 'mypass' ) ;
645+ await compile ( ) ;
646+ get . mockReturnValue ( of ( mavenDocs ) ) ;
647+ } ) ;
648+
649+ it ( 'passes auth to the query request' , async ( ) => {
650+ await fixture . getAll ( ) . toPromise ( ) ;
651+ expect ( get ) . toHaveBeenCalledWith (
652+ expect . any ( String ) ,
653+ { auth : { username : 'myuser' , password : 'mypass' } } ,
654+ ) ;
655+ } ) ;
656+
657+ it ( 'passes auth to the download request' , async ( ) => {
658+ const data = { pipe : jest . fn ( ) } ;
659+ const file = {
660+ on : jest . fn ( ) . mockImplementation ( ( listener , res ) => {
661+ if ( listener === 'finish' ) return res ( ) ;
662+ } ) ,
663+ } ;
664+
665+ fs . mkdtempSync . mockReturnValue ( '/tmp/generator-cli-abcDEF' ) ;
666+ fs . createWriteStream . mockReturnValue ( file ) ;
667+ get . mockReturnValue ( of ( { data } ) ) ;
668+
669+ await fixture . download ( '4.2.0' ) ;
670+ expect ( get ) . toHaveBeenCalledWith (
671+ expect . any ( String ) ,
672+ { responseType : 'stream' , auth : { username : 'myuser' , password : 'mypass' } } ,
673+ ) ;
674+ } ) ;
675+ } ) ;
676+
677+ describe ( 'when only username is configured' , ( ) => {
678+ beforeEach ( async ( ) => {
679+ getUsername . mockReturnValue ( 'myuser' ) ;
680+ getPassword . mockReturnValue ( undefined ) ;
681+ await compile ( ) ;
682+ get . mockReturnValue ( of ( mavenDocs ) ) ;
683+ } ) ;
684+
685+ it ( 'does not pass auth to the query request' , async ( ) => {
686+ await fixture . getAll ( ) ;
687+ expect ( get ) . toHaveBeenCalledWith (
688+ expect . any ( String ) ,
689+ { } ,
690+ ) ;
691+ } ) ;
692+ } ) ;
693+
694+ describe ( 'when neither username nor password is configured' , ( ) => {
695+ beforeEach ( async ( ) => {
696+ get . mockReturnValue ( of ( mavenDocs ) ) ;
697+ } ) ;
698+
699+ it ( 'does not pass auth to the query request' , async ( ) => {
700+ await fixture . getAll ( ) ;
701+ expect ( get ) . toHaveBeenCalledWith (
702+ expect . any ( String ) ,
703+ { } ,
704+ ) ;
705+ } ) ;
706+ } ) ;
707+ } ) ;
604708 } ) ;
605- } ) ;
709+ } ) ;
0 commit comments