@@ -11,7 +11,7 @@ import (
11
11
"errors"
12
12
"fmt"
13
13
"io"
14
- "log "
14
+ "io/ioutil "
15
15
"mime"
16
16
"mime/multipart"
17
17
"net/http"
@@ -24,8 +24,8 @@ import (
24
24
25
25
"github.com/jfcote87/ctxclient"
26
26
"github.com/jfcote87/esign"
27
- "github.com/jfcote87/esign/v2/folders"
28
- "github.com/jfcote87/esign/v2/templates"
27
+ "github.com/jfcote87/esign/v2.1 /folders"
28
+ "github.com/jfcote87/esign/v2.1 /templates"
29
29
"github.com/jfcote87/oauth2"
30
30
"github.com/jfcote87/testutils"
31
31
)
@@ -117,7 +117,7 @@ func TestOp_Do(t *testing.T) {
117
117
Path : strings .Join ([]string {"do" , "test0" , "testvar1" , "go" }, "/" ),
118
118
QueryOpts : make (url.Values ),
119
119
Method : "GET" ,
120
- Version : & esign.APIVersion { Prefix : "v2.1" } ,
120
+ Version : esign .VersionV21 ,
121
121
},
122
122
{
123
123
Credential : cx ,
@@ -519,75 +519,33 @@ type TokenCache struct {
519
519
User * esign.UserInfo `json:"user_info"`
520
520
}
521
521
522
- func TestGeneratedOpss ( t * testing. T ) {
523
- jwtConfigJSON , ok := os . LookupEnv ( "DOCUSIGN_JWTConfig" )
524
- if ! ok {
525
- t . Skip ()
526
- }
527
- f , err := os . Open ( jwtConfigJSON )
522
+ // TestGenerateOps creates an OAuth2Credential using environment
523
+ // variables DOCUSIGN_Token DOCUSIGN_AcccountID and or
524
+ // DOCUSIGN_JWTConfig and DOCUSIGN_JWTAPIUser. If neither of these
525
+ // variables are set, skip the test.
526
+ func TestGeneratedOps ( t * testing. T ) {
527
+ cred , err := getLocalCredential ( )
528
528
if err != nil {
529
- t .Fatalf ("%s open: %v" , jwtConfigJSON , err )
529
+ t .Errorf ("%v" , err )
530
+ return
530
531
}
531
- var cfg * esign.JWTConfig
532
- if err = json .NewDecoder (f ).Decode (& cfg ); err != nil {
533
- f .Close ()
534
- t .Fatalf ("%v" , err )
535
- }
536
- f .Close ()
537
- jwtAPIUserName , ok := os .LookupEnv ("DOCUSIGN_JWTAPIUser" )
538
- if ! ok {
532
+ if cred == nil {
539
533
t .Skip ()
540
534
}
541
- var cacheFunc func (context.Context , oauth2.Token , esign.UserInfo )
542
- var tokenCache * TokenCache
543
- if jwtConfigCache , ok := os .LookupEnv ("DOCUSIGN_CachedToken" ); ok {
544
- cacheFunc = func (ctx context.Context , tk oauth2.Token , ui esign.UserInfo ) {
545
- f , err := os .Create (jwtConfigCache )
546
- if err != nil {
547
- return
548
- }
549
- json .NewEncoder (f ).Encode (TokenCache {& tk , & ui })
550
- f .Close ()
551
- }
552
- fr , err := os .Open (jwtConfigCache )
553
- if err == nil {
554
- if err := json .NewDecoder (fr ).Decode (& tokenCache ); err != nil {
555
- t .Errorf ("decode token cache %s: %v" , jwtConfigCache , err )
556
- tokenCache = nil
557
- }
558
- fr .Close ()
559
- }
560
- }
561
-
562
- cfg .CacheFunc = cacheFunc
563
- var tk * oauth2.Token
564
- var uInfo * esign.UserInfo
565
- if tokenCache != nil {
566
- uInfo = tokenCache .User
567
- tk = tokenCache .Token
568
- }
569
- cred , err := cfg .Credential (jwtAPIUserName , tk , uInfo )
570
- if err != nil {
571
- log .Fatalf ("jwt credential: %v" , err )
572
- }
573
-
574
- u , err := cred .UserInfo (context .Background ())
575
- if err != nil {
576
- t .Fatalf ("Userinfo: %v" , err )
577
- }
578
- _ = u
579
-
535
+ ctx := context .Background ()
536
+ // Read throught all folders
580
537
sv := folders .New (cred )
581
- l , err := sv .List ().Do (context . Background () )
538
+ l , err := sv .List ().Do (ctx )
582
539
if err != nil {
583
540
t .Errorf ("List: %v" , err )
541
+ return
584
542
}
585
543
if len (l .Folders ) < 1 {
586
544
t .Errorf ("expecting multiple folders" )
587
545
}
588
546
547
+ // Read through all templates
589
548
svT := templates .New (cred )
590
-
591
549
tList , err := svT .List ().Do (context .Background ())
592
550
if err != nil {
593
551
t .Errorf ("Template List: %v" , err )
@@ -600,7 +558,31 @@ func TestGeneratedOpss(t *testing.T) {
600
558
t .Errorf ("unable to open template %s: %v" , tmpl .Name , err )
601
559
continue
602
560
}
603
- t .Logf ("Got: %s" , tx .EnvelopeTemplateDefinition .TemplateID )
561
+ t .Logf ("Got: %s" , tx .TemplateID )
562
+ }
563
+ }
604
564
565
+ func getLocalCredential () (* esign.OAuth2Credential , error ) {
566
+ if tk , ok := os .LookupEnv ("DOCUSIGN_Token" ); ok {
567
+ acctID , _ := os .LookupEnv ("DOCUSIGN_AccountID" )
568
+ return esign .TokenCredential (tk , true ).WithAccountID (acctID ), nil
569
+ }
570
+
571
+ if jwtConfigJSON , ok := os .LookupEnv ("DOCUSIGN_JWTConfig" ); ok {
572
+ jwtAPIUserName , ok := os .LookupEnv ("DOCUSIGN_JWTAPIUser" )
573
+ if ! ok {
574
+ return nil , fmt .Errorf ("expected DOCUSIGN_JWTAPIUser environment variable with DOCUSIGN_JWTConfig=%s" , jwtConfigJSON )
575
+ }
576
+
577
+ buffer , err := ioutil .ReadFile (jwtConfigJSON )
578
+ if err != nil {
579
+ return nil , fmt .Errorf ("%s open: %v" , jwtConfigJSON , err )
580
+ }
581
+ var cfg * esign.JWTConfig
582
+ if err = json .Unmarshal (buffer , & cfg ); err != nil {
583
+ return nil , fmt .Errorf ("%v" , err )
584
+ }
585
+ return cfg .Credential (jwtAPIUserName , nil , nil )
605
586
}
587
+ return nil , nil
606
588
}
0 commit comments