forked from google/go-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorgs_test.go
More file actions
271 lines (222 loc) · 8.56 KB
/
Copy pathorgs_test.go
File metadata and controls
271 lines (222 loc) · 8.56 KB
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
// Copyright 2013 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestOrganization_marshal(t *testing.T) {
testJSONMarshal(t, &Organization{}, "{}")
o := &Organization{
Blog: String("https://github.com/blog"),
Company: String("GitHub"),
Location: String("San Francisco"),
Name: String("github"),
Description: String("GitHub, the company."),
DefaultRepoPermission: String("read"),
MembersCanCreateRepos: Bool(true),
MembersCanCreateInternalRepos: Bool(true),
MembersCanCreatePrivateRepos: Bool(true),
MembersCanCreatePublicRepos: Bool(false),
MembersAllowedRepositoryCreationType: String("all"),
}
want := `
{
"billing_email": "[email protected]",
"blog": "https://github.com/blog",
"company": "GitHub",
"email": "[email protected]",
"location": "San Francisco",
"name": "github",
"description": "GitHub, the company.",
"default_repository_permission": "read",
"members_can_create_repositories": true,
"members_can_create_public_repositories": false,
"members_can_create_private_repositories": true,
"members_can_create_internal_repositories": true,
"members_allowed_repository_creation_type": "all"
}
`
testJSONMarshal(t, o, want)
}
func TestOrganizationsService_ListAll(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
since := int64(1342004)
mux.HandleFunc("/organizations", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{"since": "1342004"})
fmt.Fprint(w, `[{"id":4314092}]`)
})
opt := &OrganizationsListOptions{Since: since}
orgs, _, err := client.Organizations.ListAll(context.Background(), opt)
if err != nil {
t.Errorf("Organizations.ListAll returned error: %v", err)
}
want := []*Organization{{ID: Int64(4314092)}}
if !reflect.DeepEqual(orgs, want) {
t.Errorf("Organizations.ListAll returned %+v, want %+v", orgs, want)
}
}
func TestOrganizationsService_List_authenticatedUser(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/user/orgs", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
orgs, _, err := client.Organizations.List(context.Background(), "", nil)
if err != nil {
t.Errorf("Organizations.List returned error: %v", err)
}
want := []*Organization{{ID: Int64(1)}, {ID: Int64(2)}}
if !reflect.DeepEqual(orgs, want) {
t.Errorf("Organizations.List returned %+v, want %+v", orgs, want)
}
}
func TestOrganizationsService_List_specifiedUser(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/users/u/orgs", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{"page": "2"})
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
opt := &ListOptions{Page: 2}
orgs, _, err := client.Organizations.List(context.Background(), "u", opt)
if err != nil {
t.Errorf("Organizations.List returned error: %v", err)
}
want := []*Organization{{ID: Int64(1)}, {ID: Int64(2)}}
if !reflect.DeepEqual(orgs, want) {
t.Errorf("Organizations.List returned %+v, want %+v", orgs, want)
}
}
func TestOrganizationsService_List_invalidUser(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()
_, _, err := client.Organizations.List(context.Background(), "%", nil)
testURLParseError(t, err)
}
func TestOrganizationsService_Get(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/orgs/o", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeMemberAllowedRepoCreationTypePreview)
fmt.Fprint(w, `{"id":1, "login":"l", "url":"u", "avatar_url": "a", "location":"l"}`)
})
org, _, err := client.Organizations.Get(context.Background(), "o")
if err != nil {
t.Errorf("Organizations.Get returned error: %v", err)
}
want := &Organization{ID: Int64(1), Login: String("l"), URL: String("u"), AvatarURL: String("a"), Location: String("l")}
if !reflect.DeepEqual(org, want) {
t.Errorf("Organizations.Get returned %+v, want %+v", org, want)
}
}
func TestOrganizationsService_Get_invalidOrg(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()
_, _, err := client.Organizations.Get(context.Background(), "%")
testURLParseError(t, err)
}
func TestOrganizationsService_GetByID(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/organizations/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":1, "login":"l", "url":"u", "avatar_url": "a", "location":"l"}`)
})
org, _, err := client.Organizations.GetByID(context.Background(), 1)
if err != nil {
t.Fatalf("Organizations.GetByID returned error: %v", err)
}
want := &Organization{ID: Int64(1), Login: String("l"), URL: String("u"), AvatarURL: String("a"), Location: String("l")}
if !reflect.DeepEqual(org, want) {
t.Errorf("Organizations.GetByID returned %+v, want %+v", org, want)
}
}
func TestOrganizationsService_Edit(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
input := &Organization{Login: String("l")}
mux.HandleFunc("/orgs/o", func(w http.ResponseWriter, r *http.Request) {
v := new(Organization)
json.NewDecoder(r.Body).Decode(v)
testHeader(t, r, "Accept", mediaTypeMemberAllowedRepoCreationTypePreview)
testMethod(t, r, "PATCH")
if !reflect.DeepEqual(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}
fmt.Fprint(w, `{"id":1}`)
})
org, _, err := client.Organizations.Edit(context.Background(), "o", input)
if err != nil {
t.Errorf("Organizations.Edit returned error: %v", err)
}
want := &Organization{ID: Int64(1)}
if !reflect.DeepEqual(org, want) {
t.Errorf("Organizations.Edit returned %+v, want %+v", org, want)
}
}
func TestOrganizationsService_Edit_invalidOrg(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()
_, _, err := client.Organizations.Edit(context.Background(), "%", nil)
testURLParseError(t, err)
}
func TestOrganizationsService_ListInstallations(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/orgs/o/installations", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeIntegrationPreview)
fmt.Fprint(w, `{"total_count": 1, "installations": [{ "id": 1, "app_id": 5}]}`)
})
apps, _, err := client.Organizations.ListInstallations(context.Background(), "o", nil)
if err != nil {
t.Errorf("Organizations.ListInstallations returned error: %v", err)
}
want := &OrganizationInstallations{TotalCount: Int(1), Installations: []*Installation{{ID: Int64(1), AppID: Int64(5)}}}
if !reflect.DeepEqual(apps, want) {
t.Errorf("Organizations.ListInstallations returned %+v, want %+v", apps, want)
}
}
func TestOrganizationsService_ListInstallations_invalidOrg(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()
_, _, err := client.Organizations.ListInstallations(context.Background(), "%", nil)
testURLParseError(t, err)
}
func TestOrganizationsService_ListInstallations_withListOptions(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/orgs/o/installations", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeIntegrationPreview)
testFormValues(t, r, values{"page": "2"})
fmt.Fprint(w, `{"total_count": 2, "installations": [{ "id": 2, "app_id": 10}]}`)
})
apps, _, err := client.Organizations.ListInstallations(context.Background(), "o", &ListOptions{Page: 2})
if err != nil {
t.Errorf("Organizations.ListInstallations returned error: %v", err)
}
want := &OrganizationInstallations{TotalCount: Int(2), Installations: []*Installation{{ID: Int64(2), AppID: Int64(10)}}}
if !reflect.DeepEqual(apps, want) {
t.Errorf("Organizations.ListInstallations returned %+v, want %+v", apps, want)
}
// Test ListOptions failure
_, _, err = client.Organizations.ListInstallations(context.Background(), "%", &ListOptions{})
if err == nil {
t.Error("Organizations.ListInstallations returned error: nil")
}
}