-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathginiapi_test.go
134 lines (106 loc) · 3.15 KB
/
giniapi_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
package giniapi
import (
"bytes"
"context"
"fmt"
"reflect"
"testing"
)
func Test_ConfigVerify(t *testing.T) {
c := Config{}
// Empty config fails
assertNotEqual(t, c.Verify(), nil, "")
// Minimal Oauth2 config
c.ClientID = "client"
c.ClientSecret = "secret"
c.Authentication = UseOauth2
// Fail without auth_code || username & password
assertNotEqual(t, c.Verify(), nil, "")
c.Username = "user1"
assertNotEqual(t, c.Verify(), nil, "")
c.AuthCode = "12345"
assertEqual(t, c.Verify(), nil, "")
c.Password = "secret"
assertEqual(t, c.Verify(), nil, "")
// Verify defaults
c.Verify()
assertEqual(t, c.APIVersion, "v1", "")
assertEqual(t, c.Endpoints.API, "https://api.gini.net", "")
assertEqual(t, c.Endpoints.UserCenter, "https://user.gini.net", "")
}
func Test_NewClient(t *testing.T) {
// BasicAuth case
config := Config{
ClientID: "c",
ClientSecret: "s",
Authentication: UseBasicAuth,
Endpoints: Endpoints{
API: testHTTPServer.URL,
UserCenter: testHTTPServer.URL,
},
}
client, err := NewClient(&config)
assertEqual(t, err, nil, "")
assertEqual(t, reflect.TypeOf(*client).Name(), "APIClient", "")
assertEqual(t, reflect.TypeOf(client.HTTPClient.Transport).Name(), "BasicAuthTransport", "")
// OAuth2
config.Authentication = UseOauth2
config.Username = "user1"
config.Password = "secret"
client, err = NewClient(&config)
assertEqual(t, err, nil, "")
assertEqual(t, reflect.TypeOf(*client).Name(), "APIClient", "")
}
func Test_DocumentUpload(t *testing.T) {
config := Config{
ClientID: "c",
ClientSecret: "s",
Authentication: UseBasicAuth,
Endpoints: Endpoints{
API: testHTTPServer.URL,
UserCenter: testHTTPServer.URL,
},
}
ctx := context.Background()
client, err := NewClient(&config)
document, resp := client.Upload(ctx, bytes.NewReader([]byte("test")), UploadOptions{UserIdentifier: "user1"})
assertEqual(t, err, nil, "")
assertEqual(t, resp.Error, nil, "")
assertEqual(t, document.ID, "626626a0-749f-11e2-bfd6-000000000000", "")
}
func Test_DocumentGet(t *testing.T) {
config := Config{
ClientID: "c",
ClientSecret: "s",
Authentication: UseBasicAuth,
Endpoints: Endpoints{
API: testHTTPServer.URL,
UserCenter: testHTTPServer.URL,
},
}
ctx := context.Background()
client, err := NewClient(&config)
document, resp := client.Get(ctx, fmt.Sprintf("%s/test/document/get", testHTTPServer.URL), "user1")
assertEqual(t, err, nil, "")
assertEqual(t, resp.Error, nil, "")
assertEqual(t, document.Owner, "user1", "")
assertEqual(t, document.Progress, "COMPLETED", "")
}
func Test_DocumentList(t *testing.T) {
config := Config{
ClientID: "c",
ClientSecret: "s",
Authentication: UseBasicAuth,
Endpoints: Endpoints{
API: testHTTPServer.URL,
UserCenter: testHTTPServer.URL,
},
}
ctx := context.Background()
client, err := NewClient(&config)
documents, resp := client.List(ctx, ListOptions{UserIdentifier: "user1"})
assertEqual(t, err, nil, "")
assertEqual(t, resp.Error, nil, "")
assertEqual(t, documents.TotalCount, 2, "")
assertEqual(t, documents.Documents[1].String(), "626626a0-749f-11e2-abc2-000000000000", "")
}