-
Notifications
You must be signed in to change notification settings - Fork 17
/
api_user_test.go
210 lines (198 loc) · 5.43 KB
/
api_user_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
// Treasure Data API client for Go
//
// Copyright (C) 2014 Treasure Data, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package td_client
import (
"testing"
)
func TestAuthenticate(t *testing.T) {
client, err := NewTDClient(Settings{
Transport: &DummyTransport{[]byte(authenticateResponse)},
})
if err != nil {
t.Fatalf("failed create client: %s", err.Error())
}
authenticate, err := client.Authenticate("[email protected]", "123456789")
if err != nil {
t.Fatalf("bad request: %s", err.Error())
}
t.Log(authenticate)
}
func TestListUsers(t *testing.T) {
client, err := NewTDClient(Settings{
Transport: &DummyTransport{[]byte(listUsersResponse)},
})
if err != nil {
t.Fatalf("failed create client: %s", err.Error())
}
userList, err := client.ListUsers()
if err != nil {
t.Fatalf("bad request: %s", err.Error())
}
if len(*userList) != 3 {
t.Fatal("not expected user count")
}
t.Log(userList)
}
func TestListAPIKeys(t *testing.T) {
client, err := NewTDClient(Settings{
Transport: &DummyTransport{[]byte(listAPIKeysResponse)},
})
if err != nil {
t.Fatalf("failed create client: %s", err.Error())
}
apiKeys, err := client.ListAPIKeys("[email protected]")
if err != nil {
t.Fatalf("bad request: %s", err.Error())
}
if len(apiKeys.APIKeys) != 2 {
t.Fatal("not expected apikeys count")
}
t.Log(apiKeys)
}
func TestAddUser(t *testing.T) {
client, err := NewTDClient(Settings{
Transport: &DummyTransport{[]byte(addUserResponse)},
})
if err != nil {
t.Fatalf("failed create client: %s", err.Error())
}
err = client.AddUser("Test User", "td", "[email protected]", "123456789")
if err != nil {
t.Fatalf("bad request: %s", err.Error())
}
}
func TestRemoveUser(t *testing.T) {
client, err := NewTDClient(Settings{
Transport: &DummyTransport{[]byte(removeUserResponse)},
})
if err != nil {
t.Fatalf("failed create client: %s", err.Error())
}
err = client.RemoveUser("[email protected]")
if err != nil {
t.Fatalf("bad request: %s", err.Error())
}
}
func TestAddAPIKey(t *testing.T) {
client, err := NewTDClient(Settings{
Transport: &DummyTransport{[]byte(addAPIKeyResponse)},
})
if err != nil {
t.Fatalf("failed create client: %s", err.Error())
}
apiKey, err := client.AddAPIKey("[email protected]")
if err != nil {
t.Fatalf("bad request: %s", err.Error())
}
t.Log(apiKey)
}
func TestRemoveAPIKey(t *testing.T) {
client, err := NewTDClient(Settings{
Transport: &DummyTransport{[]byte(removeAPIKeyResponse)},
})
if err != nil {
t.Fatalf("failed create client: %s", err.Error())
}
err = client.RemoveAPIKey("[email protected]", "0000/hogehogehogehogehogehogehogehogehogehoge")
if err != nil {
t.Fatalf("bad request: %s", err.Error())
}
}
const authenticateResponse = `
{
"name": "Test User",
"apikey": "0000/hbfr73pix9abuciofzg8gig55cndl8clpwaz2akb"
}
`
const listUsersResponse = `
{
"users": [
{
"id": 1,
"first_name": "HOGE",
"last_name": "FUGA",
"email": "[email protected]",
"phone": "00-00-0000-0000",
"gravatar_url": "https://secure.gravatar.com/avatar/1c56a29a2f0c3114822fa47c4308410f?size=80",
"administrator": true,
"created_at": "2016-05-17T09:26:38Z",
"updated_at": "2017-06-07T22:09:34Z",
"name": "HOGE FUGA",
"account_owner": true,
"organization": null,
"roles": []
},
{
"id": 2,
"first_name": "FIZZ",
"last_name": "BUZZ",
"email": "[email protected]",
"phone": null,
"gravatar_url": "https://secure.gravatar.com/avatar/11b59b7feed4ee2995328a666ae2eb3f?size=80",
"administrator": false,
"created_at": "2016-05-30T09:20:18Z",
"updated_at": "2017-09-06T02:50:23Z",
"name": "FIZZ BUZZ",
"account_owner": false,
"organization": null,
"roles": []
},
{
"id": 3,
"first_name": null,
"last_name": null,
"email": "[email protected]",
"phone": null,
"gravatar_url": "https://secure.gravatar.com/avatar/304d83e6eb7fca0b44e3a3a4edb1e063?size=80",
"administrator": true,
"created_at": "2016-05-30T09:20:35Z",
"updated_at": "2016-07-13T09:42:32Z",
"name": "[email protected]",
"account_owner": false,
"organization": null,
"roles": []
}
]
}
`
const listAPIKeysResponse = `
{
"apikeys": [
"0000/hogehogehogehogehogehogehogehogehogehoge",
"0000/fugafugafugafugafugafugafugafugafugafuga"
]
}
`
const addUserResponse = `
{
"name":"Test User"
}
`
const removeUserResponse = `
{
"user":"[email protected]"
}
`
const addAPIKeyResponse = `
{
"apikey":"0000/hogehogehogehogehogehogehogehogehogehoge"
}
`
const removeAPIKeyResponse = `
{
"user":"[email protected]"
}
`