Debian Codegraph @

github/orgs_properties_test.go

raw
1 // Copyright 2023 The go-github AUTHORS. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 "net/http"
12 "testing"
13
14 "github.com/google/go-cmp/cmp"
15 )
16
17 func TestOrganizationsService_GetAllCustomProperties(t *testing.T) {
18 client, mux, _, teardown := setup()
19 defer teardown()
20
21 mux.HandleFunc("/orgs/o/properties/schema", func(w http.ResponseWriter, r *http.Request) {
22 testMethod(t, r, "GET")
23 fmt.Fprint(w, `[
24 {
25 "property_name": "name",
26 "value_type": "single_select",
27 "required": true,
28 "default_value": "production",
29 "description": "Prod or dev environment",
30 "allowed_values":[
31 "production",
32 "development"
33 ]
34 },
35 {
36 "property_name": "service",
37 "value_type": "string"
38 },
39 {
40 "property_name": "team",
41 "value_type": "string",
42 "description": "Team owning the repository"
43 }
44 ]`)
45 })
46
47 ctx := context.Background()
48 properties, _, err := client.Organizations.GetAllCustomProperties(ctx, "o")
49 if err != nil {
50 t.Errorf("Organizations.GetAllCustomProperties returned error: %v", err)
51 }
52
53 want := []*CustomProperty{
54 {
55 PropertyName: String("name"),
56 ValueType: "single_select",
57 Required: Bool(true),
58 DefaultValue: String("production"),
59 Description: String("Prod or dev environment"),
60 AllowedValues: []string{"production", "development"},
61 },
62 {
63 PropertyName: String("service"),
64 ValueType: "string",
65 },
66 {
67 PropertyName: String("team"),
68 ValueType: "string",
69 Description: String("Team owning the repository"),
70 },
71 }
72 if !cmp.Equal(properties, want) {
73 t.Errorf("Organizations.GetAllCustomProperties returned %+v, want %+v", properties, want)
74 }
75
76 const methodName = "GetAllCustomProperties"
77
78 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
79 got, resp, err := client.Organizations.GetAllCustomProperties(ctx, "o")
80 if got != nil {
81 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
82 }
83 return resp, err
84 })
85 }
86
87 func TestOrganizationsService_CreateOrUpdateCustomProperties(t *testing.T) {
88 client, mux, _, teardown := setup()
89 defer teardown()
90
91 mux.HandleFunc("/orgs/o/properties/schema", func(w http.ResponseWriter, r *http.Request) {
92 testMethod(t, r, "PATCH")
93 testBody(t, r, `{"properties":[{"property_name":"name","value_type":"single_select","required":true},{"property_name":"service","value_type":"string"}]}`+"\n")
94 fmt.Fprint(w, `[
95 {
96 "property_name": "name",
97 "value_type": "single_select",
98 "required": true
99 },
100 {
101 "property_name": "service",
102 "value_type": "string"
103 }
104 ]`)
105 })
106
107 ctx := context.Background()
108 properties, _, err := client.Organizations.CreateOrUpdateCustomProperties(ctx, "o", []*CustomProperty{
109 {
110 PropertyName: String("name"),
111 ValueType: "single_select",
112 Required: Bool(true),
113 },
114 {
115 PropertyName: String("service"),
116 ValueType: "string",
117 },
118 })
119 if err != nil {
120 t.Errorf("Organizations.CreateOrUpdateCustomProperties returned error: %v", err)
121 }
122
123 want := []*CustomProperty{
124 {
125 PropertyName: String("name"),
126 ValueType: "single_select",
127 Required: Bool(true),
128 },
129 {
130 PropertyName: String("service"),
131 ValueType: "string",
132 },
133 }
134
135 if !cmp.Equal(properties, want) {
136 t.Errorf("Organizations.CreateOrUpdateCustomProperties returned %+v, want %+v", properties, want)
137 }
138
139 const methodName = "CreateOrUpdateCustomProperties"
140
141 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
142 got, resp, err := client.Organizations.CreateOrUpdateCustomProperties(ctx, "o", nil)
143 if got != nil {
144 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
145 }
146 return resp, err
147 })
148 }
149
150 func TestOrganizationsService_GetCustomProperty(t *testing.T) {
151 client, mux, _, teardown := setup()
152 defer teardown()
153
154 mux.HandleFunc("/orgs/o/properties/schema/name", func(w http.ResponseWriter, r *http.Request) {
155 testMethod(t, r, "GET")
156 fmt.Fprint(w, `{
157 "property_name": "name",
158 "value_type": "single_select",
159 "required": true,
160 "default_value": "production",
161 "description": "Prod or dev environment",
162 "allowed_values":[
163 "production",
164 "development"
165 ]
166 }`)
167 })
168
169 ctx := context.Background()
170 property, _, err := client.Organizations.GetCustomProperty(ctx, "o", "name")
171 if err != nil {
172 t.Errorf("Organizations.GetCustomProperty returned error: %v", err)
173 }
174
175 want := &CustomProperty{
176 PropertyName: String("name"),
177 ValueType: "single_select",
178 Required: Bool(true),
179 DefaultValue: String("production"),
180 Description: String("Prod or dev environment"),
181 AllowedValues: []string{"production", "development"},
182 }
183 if !cmp.Equal(property, want) {
184 t.Errorf("Organizations.GetCustomProperty returned %+v, want %+v", property, want)
185 }
186
187 const methodName = "GetCustomProperty"
188
189 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
190 got, resp, err := client.Organizations.GetCustomProperty(ctx, "o", "name")
191 if got != nil {
192 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
193 }
194 return resp, err
195 })
196 }
197
198 func TestOrganizationsService_CreateOrUpdateCustomProperty(t *testing.T) {
199 client, mux, _, teardown := setup()
200 defer teardown()
201
202 mux.HandleFunc("/orgs/o/properties/schema/name", func(w http.ResponseWriter, r *http.Request) {
203 testMethod(t, r, "PUT")
204 fmt.Fprint(w, `{
205 "property_name": "name",
206 "value_type": "single_select",
207 "required": true,
208 "default_value": "production",
209 "description": "Prod or dev environment",
210 "allowed_values":[
211 "production",
212 "development"
213 ]
214 }`)
215 })
216
217 ctx := context.Background()
218 property, _, err := client.Organizations.CreateOrUpdateCustomProperty(ctx, "o", "name", &CustomProperty{
219 ValueType: "single_select",
220 Required: Bool(true),
221 DefaultValue: String("production"),
222 Description: String("Prod or dev environment"),
223 AllowedValues: []string{"production", "development"},
224 })
225 if err != nil {
226 t.Errorf("Organizations.CreateOrUpdateCustomProperty returned error: %v", err)
227 }
228
229 want := &CustomProperty{
230 PropertyName: String("name"),
231 ValueType: "single_select",
232 Required: Bool(true),
233 DefaultValue: String("production"),
234 Description: String("Prod or dev environment"),
235 AllowedValues: []string{"production", "development"},
236 }
237 if !cmp.Equal(property, want) {
238 t.Errorf("Organizations.CreateOrUpdateCustomProperty returned %+v, want %+v", property, want)
239 }
240
241 const methodName = "CreateOrUpdateCustomProperty"
242
243 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
244 got, resp, err := client.Organizations.CreateOrUpdateCustomProperty(ctx, "o", "name", nil)
245 if got != nil {
246 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
247 }
248 return resp, err
249 })
250 }
251
252 func TestOrganizationsService_RemoveCustomProperty(t *testing.T) {
253 client, mux, _, teardown := setup()
254 defer teardown()
255
256 mux.HandleFunc("/orgs/o/properties/schema/name", func(w http.ResponseWriter, r *http.Request) {
257 testMethod(t, r, "DELETE")
258 })
259
260 ctx := context.Background()
261 _, err := client.Organizations.RemoveCustomProperty(ctx, "o", "name")
262 if err != nil {
263 t.Errorf("Organizations.RemoveCustomProperty returned error: %v", err)
264 }
265
266 const methodName = "RemoveCustomProperty"
267
268 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
269 return client.Organizations.RemoveCustomProperty(ctx, "0", "name")
270 })
271 }
272
273 func TestOrganizationsService_ListCustomPropertyValues(t *testing.T) {
274 client, mux, _, teardown := setup()
275 defer teardown()
276
277 mux.HandleFunc("/orgs/o/properties/values", func(w http.ResponseWriter, r *http.Request) {
278 testMethod(t, r, "GET")
279 testFormValues(t, r, values{"page": "1", "per_page": "100"})
280 fmt.Fprint(w, `[{
281 "repository_id": 1296269,
282 "repository_name": "Hello-World",
283 "repository_full_name": "octocat/Hello-World",
284 "properties": [
285 {
286 "property_name": "environment",
287 "value": "production"
288 },
289 {
290 "property_name": "service",
291 "value": "web"
292 }
293 ]
294 }]`)
295 })
296
297 ctx := context.Background()
298 repoPropertyValues, _, err := client.Organizations.ListCustomPropertyValues(ctx, "o", &ListOptions{
299 Page: 1,
300 PerPage: 100,
301 })
302 if err != nil {
303 t.Errorf("Organizations.ListCustomPropertyValues returned error: %v", err)
304 }
305
306 want := []*RepoCustomPropertyValue{
307 {
308 RepositoryID: 1296269,
309 RepositoryName: "Hello-World",
310 RepositoryFullName: "octocat/Hello-World",
311 Properties: []*CustomPropertyValue{
312 {
313 PropertyName: "environment",
314 Value: String("production"),
315 },
316 {
317 PropertyName: "service",
318 Value: String("web"),
319 },
320 },
321 },
322 }
323
324 if !cmp.Equal(repoPropertyValues, want) {
325 t.Errorf("Organizations.ListCustomPropertyValues returned %+v, want %+v", repoPropertyValues, want)
326 }
327
328 const methodName = "ListCustomPropertyValues"
329
330 testBadOptions(t, methodName, func() (err error) {
331 _, _, err = client.Organizations.ListCustomPropertyValues(ctx, "\n", &ListOptions{})
332 return err
333 })
334
335 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
336 got, resp, err := client.Organizations.ListCustomPropertyValues(ctx, "o", nil)
337 if got != nil {
338 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
339 }
340 return resp, err
341 })
342 }
343
344 func TestOrganizationsService_CreateOrUpdateRepoCustomPropertyValues(t *testing.T) {
345 client, mux, _, teardown := setup()
346 defer teardown()
347
348 mux.HandleFunc("/orgs/o/properties/values", func(w http.ResponseWriter, r *http.Request) {
349 testMethod(t, r, "PATCH")
350 testBody(t, r, `{"repository_names":["repo"],"properties":[{"property_name":"service","value":"string"}]}`+"\n")
351 })
352
353 ctx := context.Background()
354 _, err := client.Organizations.CreateOrUpdateRepoCustomPropertyValues(ctx, "o", []string{"repo"}, []*CustomPropertyValue{
355 {
356 PropertyName: "service",
357 Value: String("string"),
358 },
359 })
360 if err != nil {
361 t.Errorf("Organizations.CreateOrUpdateCustomPropertyValuesForRepos returned error: %v", err)
362 }
363
364 const methodName = "CreateOrUpdateCustomPropertyValuesForRepos"
365
366 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
367 return client.Organizations.CreateOrUpdateRepoCustomPropertyValues(ctx, "o", nil, nil)
368 })
369 }