@@ -553,6 +553,119 @@ describe('test sandbox client module', function () {
553553 } ) ;
554554 } ) ;
555555
556+ it ( 'maps sandbox template filters and runtime injection APIs' , function ( ) {
557+ return startServer ( ( req , res ) => {
558+ res . setHeader ( 'Content-Type' , 'application/json' ) ;
559+ if ( req . method === 'GET' && req . url === '/v2/sandboxes?limit=5&template=tpl_1%2Ctpl_2%2Ctpl_3%2Ctpl_4' ) {
560+ res . statusCode = 200 ;
561+ res . end ( JSON . stringify ( [ ] ) ) ;
562+ return ;
563+ }
564+ if ( req . method === 'GET' && req . url === '/sandboxes/sbx_runtime/injections' ) {
565+ res . statusCode = 200 ;
566+ res . end ( JSON . stringify ( {
567+ injections : [ { type : 'github' , token : 'ghp_****' } ]
568+ } ) ) ;
569+ return ;
570+ }
571+ if ( req . method === 'PUT' && req . url === '/sandboxes/sbx_runtime/injections' ) {
572+ res . statusCode = 204 ;
573+ res . end ( ) ;
574+ return ;
575+ }
576+ if ( req . method === 'PUT' && req . url === '/sandboxes/sbx_runtime/github-token' ) {
577+ res . statusCode = 204 ;
578+ res . end ( ) ;
579+ return ;
580+ }
581+ res . statusCode = 404 ;
582+ res . end ( JSON . stringify ( { message : req . method + ' ' + req . url } ) ) ;
583+ } ) . then ( fixture => {
584+ const client = new qiniu . sandbox . SandboxClient ( {
585+ endpoint : fixture . endpoint ,
586+ apiKey : 'sandbox-key'
587+ } ) ;
588+
589+ return client . listSandboxesV2 ( {
590+ limit : 5 ,
591+ query : {
592+ template : [
593+ 'tpl_1' ,
594+ { templateID : 'tpl_2' } ,
595+ { template_id : 'tpl_3' } ,
596+ { id : 'tpl_4' } ,
597+ null
598+ ]
599+ }
600+ } ) . then ( ( ) => client . getSandboxInjections ( 'sbx_runtime' ) )
601+ . then ( result => {
602+ result . injections [ 0 ] . token . should . eql ( 'ghp_****' ) ;
603+ return client . updateSandboxInjections ( 'sbx_runtime' , [ {
604+ type : 'openai' ,
605+ apiKey : 'secret' ,
606+ baseUrl : 'https://api.openai.com/v1/*'
607+ } ] ) ;
608+ } )
609+ . then ( ( ) => client . updateSandboxGithubToken ( 'sbx_runtime' , 'github-token' ) )
610+ . then ( ( ) => {
611+ JSON . parse ( fixture . requests [ 2 ] . body ) . should . eql ( {
612+ injections : [ {
613+ type : 'openai' ,
614+ api_key : 'secret' ,
615+ base_url : 'https://api.openai.com/v1/*'
616+ } ]
617+ } ) ;
618+ JSON . parse ( fixture . requests [ 3 ] . body ) . should . eql ( {
619+ authorization_token : 'github-token'
620+ } ) ;
621+ } ) . then ( ( ) => closeServer ( fixture . server ) , err => {
622+ return closeServer ( fixture . server ) . then ( ( ) => {
623+ throw err ;
624+ } ) ;
625+ } ) ;
626+ } ) ;
627+ } ) ;
628+
629+ it ( 'validates runtime injection API arguments before sending requests' , function ( ) {
630+ return startServer ( ( req , res ) => {
631+ res . statusCode = 404 ;
632+ res . end ( ) ;
633+ } ) . then ( fixture => {
634+ const client = new qiniu . sandbox . SandboxClient ( {
635+ endpoint : fixture . endpoint ,
636+ apiKey : 'sandbox-key'
637+ } ) ;
638+ const calls = [
639+ ( ) => client . getSandboxInjections ( ) ,
640+ ( ) => client . updateSandboxInjections ( undefined , [ ] ) ,
641+ ( ) => client . updateSandboxGithubToken ( undefined , 'github-token' ) ,
642+ ( ) => client . updateSandboxInjections ( 'sbx_runtime' , { type : 'id' , id : 'rule_1' } ) ,
643+ ( ) => client . updateSandboxGithubToken ( 'sbx_runtime' ) ,
644+ ( ) => client . updateSandboxInjections ( 'sbx_runtime' , [ null ] )
645+ ] ;
646+
647+ return calls . reduce ( ( promise , call , index ) => {
648+ return promise . then ( ( ) => call ( ) . then ( ( ) => {
649+ throw new Error ( 'expected runtime injection argument validation to fail' ) ;
650+ } , err => {
651+ err . name . should . eql ( 'SandboxError' ) ;
652+ const expected = index < 3
653+ ? 'sandboxID is required'
654+ : ( index === 3
655+ ? 'injections must be an array'
656+ : ( index === 4 ? 'authorizationToken is required' : 'injections must contain objects' ) ) ;
657+ err . message . should . eql ( expected ) ;
658+ } ) ) ;
659+ } , Promise . resolve ( ) ) . then ( ( ) => {
660+ fixture . requests . should . have . length ( 0 ) ;
661+ } ) . then ( ( ) => closeServer ( fixture . server ) , err => {
662+ return closeServer ( fixture . server ) . then ( ( ) => {
663+ throw err ;
664+ } ) ;
665+ } ) ;
666+ } ) ;
667+ } ) ;
668+
556669 it ( 'surfaces sandbox API string errors and default connect timeout' , function ( ) {
557670 return startServer ( ( req , res ) => {
558671 if ( req . method === 'POST' && req . url === '/sandboxes/sbx_default/connect' ) {
0 commit comments