| 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 |
"encoding/json" |
| 11 |
"fmt" |
| 12 |
"net/http" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/google/go-cmp/cmp" |
| 16 |
) |
| 17 |
|
| 18 |
func TestActionsService_GetActionsPermissionsInEnterprise(t *testing.T) { |
| 19 |
client, mux, _, teardown := setup() |
| 20 |
defer teardown() |
| 21 |
|
| 22 |
mux.HandleFunc("/enterprises/e/actions/permissions", func(w http.ResponseWriter, r *http.Request) { |
| 23 |
testMethod(t, r, "GET") |
| 24 |
fmt.Fprint(w, `{"enabled_organizations": "all", "allowed_actions": "all"}`) |
| 25 |
}) |
| 26 |
|
| 27 |
ctx := context.Background() |
| 28 |
ent, _, err := client.Actions.GetActionsPermissionsInEnterprise(ctx, "e") |
| 29 |
if err != nil { |
| 30 |
t.Errorf("Actions.GetActionsPermissionsInEnterprise returned error: %v", err) |
| 31 |
} |
| 32 |
want := &ActionsPermissionsEnterprise{EnabledOrganizations: String("all"), AllowedActions: String("all")} |
| 33 |
if !cmp.Equal(ent, want) { |
| 34 |
t.Errorf("Actions.GetActionsPermissionsInEnterprise returned %+v, want %+v", ent, want) |
| 35 |
} |
| 36 |
|
| 37 |
const methodName = "GetActionsPermissionsInEnterprise" |
| 38 |
testBadOptions(t, methodName, func() (err error) { |
| 39 |
_, _, err = client.Actions.GetActionsPermissionsInEnterprise(ctx, "\n") |
| 40 |
return err |
| 41 |
}) |
| 42 |
|
| 43 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 44 |
got, resp, err := client.Actions.GetActionsPermissionsInEnterprise(ctx, "e") |
| 45 |
if got != nil { |
| 46 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 47 |
} |
| 48 |
return resp, err |
| 49 |
}) |
| 50 |
} |
| 51 |
|
| 52 |
func TestActionsService_EditActionsPermissionsInEnterprise(t *testing.T) { |
| 53 |
client, mux, _, teardown := setup() |
| 54 |
defer teardown() |
| 55 |
|
| 56 |
input := &ActionsPermissionsEnterprise{EnabledOrganizations: String("all"), AllowedActions: String("selected")} |
| 57 |
|
| 58 |
mux.HandleFunc("/enterprises/e/actions/permissions", func(w http.ResponseWriter, r *http.Request) { |
| 59 |
v := new(ActionsPermissionsEnterprise) |
| 60 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 61 |
|
| 62 |
testMethod(t, r, "PUT") |
| 63 |
if !cmp.Equal(v, input) { |
| 64 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 65 |
} |
| 66 |
|
| 67 |
fmt.Fprint(w, `{"enabled_organizations": "all", "allowed_actions": "selected"}`) |
| 68 |
}) |
| 69 |
|
| 70 |
ctx := context.Background() |
| 71 |
ent, _, err := client.Actions.EditActionsPermissionsInEnterprise(ctx, "e", *input) |
| 72 |
if err != nil { |
| 73 |
t.Errorf("Actions.EditActionsPermissionsInEnterprise returned error: %v", err) |
| 74 |
} |
| 75 |
|
| 76 |
want := &ActionsPermissionsEnterprise{EnabledOrganizations: String("all"), AllowedActions: String("selected")} |
| 77 |
if !cmp.Equal(ent, want) { |
| 78 |
t.Errorf("Actions.EditActionsPermissionsInEnterprise returned %+v, want %+v", ent, want) |
| 79 |
} |
| 80 |
|
| 81 |
const methodName = "EditActionsPermissionsInEnterprise" |
| 82 |
testBadOptions(t, methodName, func() (err error) { |
| 83 |
_, _, err = client.Actions.EditActionsPermissionsInEnterprise(ctx, "\n", *input) |
| 84 |
return err |
| 85 |
}) |
| 86 |
|
| 87 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 88 |
got, resp, err := client.Actions.EditActionsPermissionsInEnterprise(ctx, "e", *input) |
| 89 |
if got != nil { |
| 90 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 91 |
} |
| 92 |
return resp, err |
| 93 |
}) |
| 94 |
} |
| 95 |
|
| 96 |
func TestActionsService_ListEnabledOrgsInEnterprise(t *testing.T) { |
| 97 |
client, mux, _, teardown := setup() |
| 98 |
defer teardown() |
| 99 |
|
| 100 |
mux.HandleFunc("/enterprises/e/actions/permissions/organizations", func(w http.ResponseWriter, r *http.Request) { |
| 101 |
testMethod(t, r, "GET") |
| 102 |
testFormValues(t, r, values{ |
| 103 |
"page": "1", |
| 104 |
}) |
| 105 |
fmt.Fprint(w, `{"total_count":2,"organizations":[{"id":2}, {"id":3}]}`) |
| 106 |
}) |
| 107 |
|
| 108 |
ctx := context.Background() |
| 109 |
opt := &ListOptions{ |
| 110 |
Page: 1, |
| 111 |
} |
| 112 |
got, _, err := client.Actions.ListEnabledOrgsInEnterprise(ctx, "e", opt) |
| 113 |
if err != nil { |
| 114 |
t.Errorf("Actions.ListEnabledOrgsInEnterprise returned error: %v", err) |
| 115 |
} |
| 116 |
|
| 117 |
want := &ActionsEnabledOnEnterpriseRepos{TotalCount: int(2), Organizations: []*Organization{ |
| 118 |
{ID: Int64(2)}, |
| 119 |
{ID: Int64(3)}, |
| 120 |
}} |
| 121 |
if !cmp.Equal(got, want) { |
| 122 |
t.Errorf("Actions.ListEnabledOrgsInEnterprise returned %+v, want %+v", got, want) |
| 123 |
} |
| 124 |
|
| 125 |
const methodName = "ListEnabledOrgsInEnterprise" |
| 126 |
testBadOptions(t, methodName, func() (err error) { |
| 127 |
_, _, err = client.Actions.ListEnabledOrgsInEnterprise(ctx, "\n", opt) |
| 128 |
return err |
| 129 |
}) |
| 130 |
|
| 131 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 132 |
got, resp, err := client.Actions.ListEnabledOrgsInEnterprise(ctx, "e", opt) |
| 133 |
if got != nil { |
| 134 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 135 |
} |
| 136 |
return resp, err |
| 137 |
}) |
| 138 |
} |
| 139 |
|
| 140 |
func TestActionsService_SetEnabledOrgsInEnterprise(t *testing.T) { |
| 141 |
client, mux, _, teardown := setup() |
| 142 |
defer teardown() |
| 143 |
|
| 144 |
mux.HandleFunc("/enterprises/e/actions/permissions/organizations", func(w http.ResponseWriter, r *http.Request) { |
| 145 |
testMethod(t, r, "PUT") |
| 146 |
testHeader(t, r, "Content-Type", "application/json") |
| 147 |
testBody(t, r, `{"selected_organization_ids":[123,1234]}`+"\n") |
| 148 |
w.WriteHeader(http.StatusNoContent) |
| 149 |
}) |
| 150 |
|
| 151 |
ctx := context.Background() |
| 152 |
_, err := client.Actions.SetEnabledOrgsInEnterprise(ctx, "e", []int64{123, 1234}) |
| 153 |
if err != nil { |
| 154 |
t.Errorf("Actions.SetEnabledOrgsInEnterprise returned error: %v", err) |
| 155 |
} |
| 156 |
|
| 157 |
const methodName = "SetEnabledOrgsInEnterprise" |
| 158 |
|
| 159 |
testBadOptions(t, methodName, func() (err error) { |
| 160 |
_, err = client.Actions.SetEnabledOrgsInEnterprise(ctx, "\n", []int64{123, 1234}) |
| 161 |
return err |
| 162 |
}) |
| 163 |
|
| 164 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 165 |
return client.Actions.SetEnabledOrgsInEnterprise(ctx, "e", []int64{123, 1234}) |
| 166 |
}) |
| 167 |
} |
| 168 |
|
| 169 |
func TestActionsService_AddEnabledOrgInEnterprise(t *testing.T) { |
| 170 |
client, mux, _, teardown := setup() |
| 171 |
defer teardown() |
| 172 |
|
| 173 |
mux.HandleFunc("/enterprises/e/actions/permissions/organizations/123", func(w http.ResponseWriter, r *http.Request) { |
| 174 |
testMethod(t, r, "PUT") |
| 175 |
w.WriteHeader(http.StatusNoContent) |
| 176 |
}) |
| 177 |
|
| 178 |
ctx := context.Background() |
| 179 |
_, err := client.Actions.AddEnabledOrgInEnterprise(ctx, "e", 123) |
| 180 |
if err != nil { |
| 181 |
t.Errorf("Actions.AddEnabledOrgInEnterprise returned error: %v", err) |
| 182 |
} |
| 183 |
|
| 184 |
const methodName = "AddEnabledOrgInEnterprise" |
| 185 |
|
| 186 |
testBadOptions(t, methodName, func() (err error) { |
| 187 |
_, err = client.Actions.AddEnabledOrgInEnterprise(ctx, "\n", 123) |
| 188 |
return err |
| 189 |
}) |
| 190 |
|
| 191 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 192 |
return client.Actions.AddEnabledOrgInEnterprise(ctx, "e", 123) |
| 193 |
}) |
| 194 |
} |
| 195 |
|
| 196 |
func TestActionsService_RemoveEnabledOrgInEnterprise(t *testing.T) { |
| 197 |
client, mux, _, teardown := setup() |
| 198 |
defer teardown() |
| 199 |
|
| 200 |
mux.HandleFunc("/enterprises/e/actions/permissions/organizations/123", func(w http.ResponseWriter, r *http.Request) { |
| 201 |
testMethod(t, r, "DELETE") |
| 202 |
w.WriteHeader(http.StatusNoContent) |
| 203 |
}) |
| 204 |
|
| 205 |
ctx := context.Background() |
| 206 |
_, err := client.Actions.RemoveEnabledOrgInEnterprise(ctx, "e", 123) |
| 207 |
if err != nil { |
| 208 |
t.Errorf("Actions.RemoveEnabledOrgInEnterprise returned error: %v", err) |
| 209 |
} |
| 210 |
|
| 211 |
const methodName = "RemoveEnabledOrgInEnterprise" |
| 212 |
|
| 213 |
testBadOptions(t, methodName, func() (err error) { |
| 214 |
_, err = client.Actions.RemoveEnabledOrgInEnterprise(ctx, "\n", 123) |
| 215 |
return err |
| 216 |
}) |
| 217 |
|
| 218 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 219 |
return client.Actions.RemoveEnabledOrgInEnterprise(ctx, "e", 123) |
| 220 |
}) |
| 221 |
} |
| 222 |
|
| 223 |
func TestActionsService_GetActionsAllowedInEnterprise(t *testing.T) { |
| 224 |
client, mux, _, teardown := setup() |
| 225 |
defer teardown() |
| 226 |
|
| 227 |
mux.HandleFunc("/enterprises/e/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) { |
| 228 |
testMethod(t, r, "GET") |
| 229 |
fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`) |
| 230 |
}) |
| 231 |
|
| 232 |
ctx := context.Background() |
| 233 |
ent, _, err := client.Actions.GetActionsAllowedInEnterprise(ctx, "e") |
| 234 |
if err != nil { |
| 235 |
t.Errorf("Actions.GetActionsAllowedInEnterprise returned error: %v", err) |
| 236 |
} |
| 237 |
want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} |
| 238 |
if !cmp.Equal(ent, want) { |
| 239 |
t.Errorf("Actions.GetActionsAllowedInEnterprise returned %+v, want %+v", ent, want) |
| 240 |
} |
| 241 |
|
| 242 |
const methodName = "GetActionsAllowedInEnterprise" |
| 243 |
testBadOptions(t, methodName, func() (err error) { |
| 244 |
_, _, err = client.Actions.GetActionsAllowedInEnterprise(ctx, "\n") |
| 245 |
return err |
| 246 |
}) |
| 247 |
|
| 248 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 249 |
got, resp, err := client.Actions.GetActionsAllowedInEnterprise(ctx, "e") |
| 250 |
if got != nil { |
| 251 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 252 |
} |
| 253 |
return resp, err |
| 254 |
}) |
| 255 |
} |
| 256 |
|
| 257 |
func TestActionsService_EditActionsAllowedInEnterprise(t *testing.T) { |
| 258 |
client, mux, _, teardown := setup() |
| 259 |
defer teardown() |
| 260 |
input := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} |
| 261 |
|
| 262 |
mux.HandleFunc("/enterprises/e/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) { |
| 263 |
v := new(ActionsAllowed) |
| 264 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 265 |
|
| 266 |
testMethod(t, r, "PUT") |
| 267 |
if !cmp.Equal(v, input) { |
| 268 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 269 |
} |
| 270 |
|
| 271 |
fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`) |
| 272 |
}) |
| 273 |
|
| 274 |
ctx := context.Background() |
| 275 |
ent, _, err := client.Actions.EditActionsAllowedInEnterprise(ctx, "e", *input) |
| 276 |
if err != nil { |
| 277 |
t.Errorf("Actions.EditActionsAllowedInEnterprise returned error: %v", err) |
| 278 |
} |
| 279 |
|
| 280 |
want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} |
| 281 |
if !cmp.Equal(ent, want) { |
| 282 |
t.Errorf("Actions.EditActionsAllowedInEnterprise returned %+v, want %+v", ent, want) |
| 283 |
} |
| 284 |
|
| 285 |
const methodName = "EditActionsAllowedInEnterprise" |
| 286 |
testBadOptions(t, methodName, func() (err error) { |
| 287 |
_, _, err = client.Actions.EditActionsAllowedInEnterprise(ctx, "\n", *input) |
| 288 |
return err |
| 289 |
}) |
| 290 |
|
| 291 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 292 |
got, resp, err := client.Actions.EditActionsAllowedInEnterprise(ctx, "e", *input) |
| 293 |
if got != nil { |
| 294 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 295 |
} |
| 296 |
return resp, err |
| 297 |
}) |
| 298 |
} |
| 299 |
|
| 300 |
func TestActionsService_GetDefaultWorkflowPermissionsInEnterprise(t *testing.T) { |
| 301 |
client, mux, _, teardown := setup() |
| 302 |
defer teardown() |
| 303 |
|
| 304 |
mux.HandleFunc("/enterprises/e/actions/permissions/workflow", func(w http.ResponseWriter, r *http.Request) { |
| 305 |
testMethod(t, r, "GET") |
| 306 |
fmt.Fprint(w, `{ "default_workflow_permissions": "read", "can_approve_pull_request_reviews": true }`) |
| 307 |
}) |
| 308 |
|
| 309 |
ctx := context.Background() |
| 310 |
ent, _, err := client.Actions.GetDefaultWorkflowPermissionsInEnterprise(ctx, "e") |
| 311 |
if err != nil { |
| 312 |
t.Errorf("Actions.GetDefaultWorkflowPermissionsInEnterprise returned error: %v", err) |
| 313 |
} |
| 314 |
want := &DefaultWorkflowPermissionEnterprise{DefaultWorkflowPermissions: String("read"), CanApprovePullRequestReviews: Bool(true)} |
| 315 |
if !cmp.Equal(ent, want) { |
| 316 |
t.Errorf("Actions.GetDefaultWorkflowPermissionsInEnterprise returned %+v, want %+v", ent, want) |
| 317 |
} |
| 318 |
|
| 319 |
const methodName = "GetDefaultWorkflowPermissionsInEnterprise" |
| 320 |
testBadOptions(t, methodName, func() (err error) { |
| 321 |
_, _, err = client.Actions.GetDefaultWorkflowPermissionsInEnterprise(ctx, "\n") |
| 322 |
return err |
| 323 |
}) |
| 324 |
|
| 325 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 326 |
got, resp, err := client.Actions.GetDefaultWorkflowPermissionsInEnterprise(ctx, "e") |
| 327 |
if got != nil { |
| 328 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 329 |
} |
| 330 |
return resp, err |
| 331 |
}) |
| 332 |
} |
| 333 |
|
| 334 |
func TestActionsService_EditDefaultWorkflowPermissionsInEnterprise(t *testing.T) { |
| 335 |
client, mux, _, teardown := setup() |
| 336 |
defer teardown() |
| 337 |
input := &DefaultWorkflowPermissionEnterprise{DefaultWorkflowPermissions: String("read"), CanApprovePullRequestReviews: Bool(true)} |
| 338 |
|
| 339 |
mux.HandleFunc("/enterprises/e/actions/permissions/workflow", func(w http.ResponseWriter, r *http.Request) { |
| 340 |
v := new(DefaultWorkflowPermissionEnterprise) |
| 341 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 342 |
|
| 343 |
testMethod(t, r, "PUT") |
| 344 |
if !cmp.Equal(v, input) { |
| 345 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 346 |
} |
| 347 |
|
| 348 |
fmt.Fprint(w, `{ "default_workflow_permissions": "read", "can_approve_pull_request_reviews": true }`) |
| 349 |
}) |
| 350 |
|
| 351 |
ctx := context.Background() |
| 352 |
ent, _, err := client.Actions.EditDefaultWorkflowPermissionsInEnterprise(ctx, "e", *input) |
| 353 |
if err != nil { |
| 354 |
t.Errorf("Actions.EditDefaultWorkflowPermissionsInEnterprise returned error: %v", err) |
| 355 |
} |
| 356 |
|
| 357 |
want := &DefaultWorkflowPermissionEnterprise{DefaultWorkflowPermissions: String("read"), CanApprovePullRequestReviews: Bool(true)} |
| 358 |
if !cmp.Equal(ent, want) { |
| 359 |
t.Errorf("Actions.EditDefaultWorkflowPermissionsInEnterprise returned %+v, want %+v", ent, want) |
| 360 |
} |
| 361 |
|
| 362 |
const methodName = "EditDefaultWorkflowPermissionsInEnterprise" |
| 363 |
testBadOptions(t, methodName, func() (err error) { |
| 364 |
_, _, err = client.Actions.EditDefaultWorkflowPermissionsInEnterprise(ctx, "\n", *input) |
| 365 |
return err |
| 366 |
}) |
| 367 |
|
| 368 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 369 |
got, resp, err := client.Actions.EditDefaultWorkflowPermissionsInEnterprise(ctx, "e", *input) |
| 370 |
if got != nil { |
| 371 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 372 |
} |
| 373 |
return resp, err |
| 374 |
}) |
| 375 |
} |