-
Notifications
You must be signed in to change notification settings - Fork 27
/
esign_test.go
611 lines (564 loc) · 16.2 KB
/
esign_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
// Copyright 2019 James Cote
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package esign_test
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
"net/url"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/jfcote87/ctxclient"
"github.com/jfcote87/esign"
"github.com/jfcote87/esign/v2.1/folders"
"github.com/jfcote87/esign/v2.1/templates"
"github.com/jfcote87/oauth2"
"github.com/jfcote87/testutils"
)
type TestCred struct {
scheme string
host string
acctID string
ctxclient.Func
}
//func (t *TestCred) AuthDo(ctx context.Context, req *http.Request, v esign.APIVersion) (*http.Response, error) {
func (t *TestCred) AuthDo(ctx context.Context, op *esign.Op) (*http.Response, error) {
req, err := op.CreateRequest()
if err != nil {
return nil, err
}
req.URL = op.Version.ResolveDSURL(req.URL, t.host, t.acctID, false)
req.Header.Set("Authorization", "TESTAUTH")
res, err := t.Func.Do(ctx, req)
if nsErr, ok := err.(*ctxclient.NotSuccess); ok {
return nil, esign.NewResponseError(nsErr.Body, nsErr.StatusCode)
}
return res, err
}
func (t *TestCred) SetClient(cl *http.Client) {
t.Func = func(ctx context.Context) (*http.Client, error) {
return cl, nil
}
}
func TestOp_Do(t *testing.T) {
// Setup test credential and transport to check for path substitution
cx, testTransport := getTestCredentialClientTransport()
var jsonResponse = []byte(`{"a": "val", "b": 9, "c": "X"}`)
testTransport.Add(
&testutils.RequestTester{ // test 0
Path: "/restapi/v2.1/accounts/1234/do/test0/testvar1/go",
Auth: "TESTAUTH",
Method: "GET",
Response: testutils.MakeResponse(200, nil, nil),
},
&testutils.RequestTester{ // test 1
Path: "/restapi/v2/noaccount/test1/go",
Auth: "TESTAUTH",
Method: "GET",
Query: "a=B&c=D",
Response: testutils.MakeResponse(200, nil, nil),
},
&testutils.RequestTester{ // test 2
Path: "/restapi/v2/noaccount/test2/go",
Auth: "TESTAUTH",
Method: "POST",
Payload: []byte("a=B&c=D"),
Response: testutils.MakeResponse(200, nil, nil),
},
&testutils.RequestTester{ // test 3
Path: "/restapi/v2/noaccount/test3/go",
Auth: "TESTAUTH",
Method: "POST",
Payload: []byte("{\"a\":\"String\",\"b\":9}\n"),
Response: testutils.MakeResponse(200, nil, nil),
},
&testutils.RequestTester{ // test 4
Path: "/restapi/v2/noaccount/test4/go",
Auth: "TESTAUTH",
Method: "POST",
Payload: []byte("0123456789"),
Response: testutils.MakeResponse(200, nil, nil),
},
&testutils.RequestTester{ // test 5
Auth: "TESTAUTH",
Method: "GET",
Response: testutils.MakeResponse(400, []byte(`No JSON`), nil),
},
&testutils.RequestTester{ // test 6
Auth: "TESTAUTH",
Method: "GET",
Response: testutils.MakeResponse(400, []byte(`{"errorCode": "A", "message":"error desc"}`), nil),
},
&testutils.RequestTester{ // test 7
Path: "/restapi/v2/accounts/1234/do/test7/go",
Auth: "TESTAUTH",
Method: "POST",
Response: testutils.MakeResponse(200, jsonResponse, nil),
}) // Error Test
ops := []esign.Op{
{
Credential: cx,
Path: strings.Join([]string{"do", "test0", "testvar1", "go"}, "/"),
QueryOpts: make(url.Values),
Method: "GET",
Version: esign.APIv21,
},
{
Credential: cx,
Path: "/v2/noaccount/test1/go",
QueryOpts: url.Values{"a": {"B"}, "c": {"D"}},
Method: "GET",
Version: esign.APIv2,
},
{
Credential: cx,
Path: "/v2/noaccount/test2/go",
QueryOpts: make(url.Values),
Method: "POST",
Payload: url.Values{"a": {"B"}, "c": {"D"}},
Version: esign.APIv2,
},
{
Credential: cx,
Path: "/v2/noaccount/test3/go",
QueryOpts: make(url.Values),
Method: "POST",
Payload: map[string]interface{}{
"a": "String", "b": 9,
},
Version: esign.APIv2,
},
{
Credential: cx,
Path: "/v2/noaccount/test4/go",
QueryOpts: make(url.Values),
Method: "POST",
Payload: &esign.UploadFile{
Reader: bytes.NewReader([]byte("0123456789")),
ContentType: "text/plain",
},
Version: esign.APIv2,
},
}
for i, op := range ops {
if err := op.Do(context.Background(), nil); err != nil {
t.Errorf("Error %d: %v", i, err)
}
}
// check error handling for failed ResponseError
op := &esign.Op{
Credential: cx,
Path: "do/test5/go",
QueryOpts: make(url.Values),
Method: "GET",
Version: esign.APIv2,
}
err := op.Do(context.Background(), nil)
if ex, ok := err.(*esign.ResponseError); !ok {
t.Fatalf("test 5 expected *ResponseError; got %#v", err)
} else if ex.Status != 400 || string(ex.Raw) != "No JSON" {
t.Fatalf("test 5 expected status return of 400 and raw of \"No JSON\"; got %d, %q", ex.Status, string(ex.Raw))
}
// check error handling for properly formateed ResponseError
op.Path = "do/test6/go"
err = op.Do(context.Background(), nil)
if ex, ok := err.(*esign.ResponseError); !ok {
t.Fatalf("test 6 expected *ResponseError; got %#v", err)
} else if ex.Status != 400 || ex.ErrorCode != "A" || ex.Description != "error desc" {
t.Fatalf("test 6 expected status: 400, err: \"A\", description: \"error desc\"; got %#v", ex)
}
// check post and return value. expect success
op.Path = "do/test7/go"
op.Method = "POST"
var result struct {
A string
B int64
C string
}
if err := op.Do(context.Background(), &result); err != nil {
t.Fatalf("JSON response failed: %#v", err)
}
if result.A != "val" || result.B != 9 || result.C != "X" {
t.Fatalf("JSON response expected A=val, B=9, C=X; got %#v", result)
}
}
type testFile struct {
reader *bytes.Reader
closed bool
timesClosed int
readFunc func(*testFile, []byte) (int, error)
m sync.Mutex
}
func (t *testFile) Read(b []byte) (int, error) {
if t.readFunc != nil {
return t.readFunc(t, b)
}
return t.reader.Read(b)
}
func (t *testFile) IsClosed() bool {
defer t.m.Unlock()
t.m.Lock()
return t.closed
}
func (t *testFile) Close() error {
t.m.Lock()
t.closed = true
t.timesClosed++
t.m.Unlock()
return nil
}
func (t *testFile) ReOpen() {
t.m.Lock()
t.closed = false
t.timesClosed = 0
_, _ = t.reader.Seek(0, io.SeekStart)
t.m.Unlock()
}
type errReader struct{}
func (e errReader) Read(p []byte) (int, error) {
return 0, errors.New("Read Error")
}
// testMultipart validates formatting of a multipart op
func testMultipart(req *http.Request) (*http.Response, error) {
// Check multipart ops. ensure fileUploads closed Do() routine.
expectedContent := []string{"application/json", "text/plain", "application/octet-stream"}
expectedFileName := []string{"", "file1.txt", "file2.txt"}
defer req.Body.Close()
_, params, err := mime.ParseMediaType(req.Header.Get("Content-Type"))
if err != nil {
return nil, err
}
mpr := multipart.NewReader(req.Body, params["boundary"])
p, err := mpr.NextPart()
for i := 0; err == nil && i < len(expectedContent); i++ {
if expectedContent[i] != p.Header.Get("Content-Type") || expectedFileName[i] != p.FileName() {
err = fmt.Errorf("multipart[%d] expected content-type: %s and file name: %s; got %s %s", i, expectedContent[i], expectedFileName[i], p.Header.Get("Content-Type"), p.FileName())
}
p.Close()
if err == nil {
p, err = mpr.NextPart()
}
}
if err != io.EOF {
return testutils.MakeResponse(400, []byte(err.Error()), nil), nil
}
return testutils.MakeResponse(200, nil, nil), nil
}
func checkFilesClosed(t *testing.T, files ...*testFile) bool {
var ok = true
for i, f := range files {
if !f.IsClosed() {
t.Errorf("file%d not closed", i)
ok = false
}
f.ReOpen()
}
return ok
}
func TestOp_Do_FileUpload(t *testing.T) {
cx, testTransport := getTestCredentialClientTransport()
payload := struct {
A string
B int
}{"STRING", 9}
f1 := &testFile{
reader: bytes.NewReader([]byte("12345678")),
}
f2 := &testFile{
reader: bytes.NewReader([]byte("9876543210")),
}
// expect success
testTransport.Add(&testutils.RequestTester{
ResponseFunc: testMultipart,
})
op := &esign.Op{
Credential: cx,
Path: "multipart/go",
QueryOpts: make(url.Values),
Method: "POST",
Payload: payload,
Files: []*esign.UploadFile{
{
ContentType: "text/plain",
FileName: "file1.txt",
ID: "1",
Reader: f1,
},
{
ContentType: "application/octet-stream",
FileName: "file2.txt",
ID: "2",
Reader: f2,
},
},
Version: esign.APIv2,
}
if err := op.Do(context.Background(), nil); err != nil {
t.Fatalf("multipart test expected success; got %v", err)
}
if !checkFilesClosed(t, f1, f2) {
t.Fatalf("multipart test success expected closed files")
}
f1.ReOpen()
f2.ReOpen()
// ensure files close on transport/network error
cx.(*TestCred).SetClient(&http.Client{
Transport: &ctxclient.ErrorTransport{Err: errors.New("ERROR")},
})
ctx := context.Background()
err := op.Do(ctx, nil)
switch reterr := err.(type) {
case nil:
t.Fatalf("multipart test expected *url.Error; got success")
case *url.Error:
msg := reterr.Err.Error()
if msg != "ERROR" {
t.Fatalf("multipart test expected error msg = ERROR; got %s", msg)
}
default:
t.Fatalf("multipart test expected error msg = ERROR; got %s", err.Error())
}
time.Sleep(time.Second)
if !checkFilesClosed(t, f1, f2) {
t.Errorf("multipart network error expected closed files")
}
// ensure all files close if error reading previous file
f1.ReOpen()
f2.ReOpen()
op.Files[0].Reader = errReader{}
cx.(*TestCred).SetClient(&http.Client{
Transport: testTransport,
})
testTransport.Add(&testutils.RequestTester{
Payload: []byte("Expect Error"),
Response: testutils.MakeResponse(200, nil, nil),
})
switch err := op.Do(context.Background(), nil).(type) {
case *url.Error:
default:
t.Errorf("multipart read expected io error; got %v", err)
}
if !f2.IsClosed() {
t.Fatalf("multipart test post error expected closed files")
}
}
func TestOp_Do_FileDownload(t *testing.T) {
cx, testTransport := getTestCredentialClientTransport()
op := &esign.Op{
Credential: cx,
Path: "file",
QueryOpts: make(url.Values),
Method: "GET",
Accept: "text/plain",
Version: esign.APIv2,
}
f1 := &testFile{
reader: bytes.NewReader([]byte("0123456789")),
}
var file *esign.Download
testTransport.Add(&testutils.RequestTester{
Header: http.Header{"Accept": []string{"text/plain"}},
ResponseFunc: func(req *http.Request) (*http.Response, error) {
res := testutils.MakeResponse(200, []byte("0123456789"), http.Header{"Content-Type": []string{"text/plain"}})
res.Body = f1
return res, nil
},
})
if err := op.Do(context.Background(), &file); err != nil {
t.Fatalf("expecte esign.Download; got error %v", err)
}
if file == nil {
t.Errorf("expected *esign.Download; got nil")
} else if file.ContentType != "text/plain" || file.ContentLength != 10 {
t.Errorf("expected contentType of text/plain and ContentLength = 10; got %s, %d", file.ContentType, file.ContentLength)
}
if f1.IsClosed() {
t.Errorf("expected open res.Body. got closed.")
}
file.Close()
}
func TestOp_FilesClosed(t *testing.T) {
f1 := &testFile{
reader: bytes.NewReader([]byte("12345678")),
}
f2 := &testFile{
reader: bytes.NewReader([]byte("9876543210")),
}
op := &esign.Op{
Credential: nil,
Path: "multipart/go",
QueryOpts: make(url.Values),
Method: "POST",
Files: []*esign.UploadFile{
{
ContentType: "text/plain",
FileName: "file1.txt",
ID: "1",
Reader: f1,
},
{
ContentType: "application/octet-stream",
FileName: "file2.txt",
ID: "2",
Reader: f2,
},
},
}
// ensure files close on nil context/invalid client/invalid credential error
var ctx context.Context
if err := op.Do(ctx, nil); err != nil && err.Error() != "nil context" {
t.Errorf("expected nil context; got %v", err)
}
if !checkFilesClosed(t, f1, f2) {
t.Fatalf("multipart test nil context expected closed files")
}
ctx = context.Background()
if err := op.Do(ctx, nil); err != nil && err.Error() != "nil credential" {
t.Errorf("expected nil credential; got %v", err)
}
if !checkFilesClosed(t, f1, f2) {
t.Fatalf("multipart test nil credential expected closed files")
}
// create an error in Op.makeRequest
cx, _ := getTestCredentialClientTransport()
op.Credential = cx
op.Method = "PO ST" //invalid method
if err := op.Do(ctx, nil); err != nil && err.Error() != "net/http: invalid method \"PO ST\"" {
t.Errorf("expected net/http: invalid method \"PO ST\"; got %v", err)
}
time.Sleep(time.Second)
if !checkFilesClosed(t, f1, f2) {
t.Fatalf("invalid method expected closed files")
}
}
func TestOp_Do_ContextCancel(t *testing.T) {
cx, testTransport := getTestCredentialClientTransport()
ctx, cancelFunc := context.WithCancel(context.Background())
// First run without context cancel for baseline
op := &esign.Op{
Credential: cx,
Path: "multipart/go",
QueryOpts: make(url.Values),
Method: "POST",
Version: esign.APIv2,
}
testTransport.Add(&testutils.RequestTester{
Response: testutils.MakeResponse(200, []byte("0123456789"), nil),
})
var result *TokenCache
switch err := op.Do(ctx, &result).(type) {
case *json.UnmarshalTypeError:
default:
t.Fatalf("expected *json.UnmarshalTypeError; got %#v", err)
}
testTransport.Add(&testutils.RequestTester{
ResponseFunc: func(*http.Request) (*http.Response, error) {
cancelFunc()
return nil, errors.New("Should not see this error Error")
},
})
testTransport.Add(&testutils.RequestTester{
Response: testutils.MakeResponse(200, []byte("0123456789"), nil),
})
op = &esign.Op{
Credential: cx,
Path: "multipart/go",
QueryOpts: make(url.Values),
Method: "POST",
Version: esign.APIv2,
}
if err := op.Do(ctx, &result); err == nil || err.Error() != "context canceled" {
t.Errorf("expected context canceled; got %v", err)
}
}
func getTestCredentialClientTransport() (esign.Credential, *testutils.Transport) {
testTransport := &testutils.Transport{}
clx := &http.Client{Transport: testTransport}
var cx esign.Credential = &TestCred{
host: "www.example.com",
scheme: "https",
acctID: "1234",
Func: func(ctx context.Context) (*http.Client, error) {
return clx, nil
},
}
return cx, testTransport
}
type TokenCache struct {
Token *oauth2.Token `json:"token"`
User *esign.UserInfo `json:"user_info"`
}
// TestGenerateOps creates an OAuth2Credential using environment
// variables DOCUSIGN_Token DOCUSIGN_AcccountID and or
// DOCUSIGN_JWTConfig and DOCUSIGN_JWTAPIUser. If neither of these
// variables are set, skip the test.
func TestGeneratedOps(t *testing.T) {
cred, err := getLocalCredential()
if err != nil {
t.Errorf("%v", err)
return
}
if cred == nil {
t.Skip()
}
ctx := context.Background()
// Read throught all folders
sv := folders.New(cred)
l, err := sv.List().Do(ctx)
if err != nil {
t.Errorf("List: %v", err)
return
}
if len(l.Folders) < 1 {
t.Errorf("expecting multiple folders")
}
// Read through all templates
svT := templates.New(cred)
tList, err := svT.List().Do(context.Background())
if err != nil {
t.Errorf("Template List: %v", err)
}
for _, tmpl := range tList.EnvelopeTemplates {
t.Logf("Getting: %s", tmpl.TemplateID)
tx, err := svT.Get(tmpl.TemplateID).Include("recipients").Do(context.Background())
if err != nil {
t.Errorf("unable to open template %s: %v", tmpl.Name, err)
continue
}
t.Logf("Got: %s", tx.TemplateID)
}
}
func getLocalCredential() (*esign.OAuth2Credential, error) {
if tk, ok := os.LookupEnv("DOCUSIGN_Token"); ok {
acctID, _ := os.LookupEnv("DOCUSIGN_AccountID")
return esign.TokenCredential(tk, true).WithAccountID(acctID), nil
}
if jwtConfigJSON, ok := os.LookupEnv("DOCUSIGN_JWTConfig"); ok {
jwtAPIUserName, ok := os.LookupEnv("DOCUSIGN_JWTAPIUser")
if !ok {
return nil, fmt.Errorf("expected DOCUSIGN_JWTAPIUser environment variable with DOCUSIGN_JWTConfig=%s", jwtConfigJSON)
}
buffer, err := ioutil.ReadFile(jwtConfigJSON)
if err != nil {
return nil, fmt.Errorf("%s open: %v", jwtConfigJSON, err)
}
var cfg *esign.JWTConfig
if err = json.Unmarshal(buffer, &cfg); err != nil {
return nil, fmt.Errorf("%v", err)
}
return cfg.Credential(jwtAPIUserName, nil, nil)
}
return nil, nil
}