github.com/google/go-github/v66@v66.0.0/github/actions_permissions_orgs_test.go (about) 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_GetActionsPermissions(t *testing.T) { 19 t.Parallel() 20 client, mux, _ := setup(t) 21 22 mux.HandleFunc("/orgs/o/actions/permissions", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 fmt.Fprint(w, `{"enabled_repositories": "all", "allowed_actions": "all"}`) 25 }) 26 27 ctx := context.Background() 28 org, _, err := client.Actions.GetActionsPermissions(ctx, "o") 29 if err != nil { 30 t.Errorf("Actions.GetActionsPermissions returned error: %v", err) 31 } 32 want := &ActionsPermissions{EnabledRepositories: String("all"), AllowedActions: String("all")} 33 if !cmp.Equal(org, want) { 34 t.Errorf("Actions.GetActionsPermissions returned %+v, want %+v", org, want) 35 } 36 37 const methodName = "GetActionsPermissions" 38 testBadOptions(t, methodName, func() (err error) { 39 _, _, err = client.Actions.GetActionsPermissions(ctx, "\n") 40 return err 41 }) 42 43 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 44 got, resp, err := client.Actions.GetActionsPermissions(ctx, "o") 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_EditActionsPermissions(t *testing.T) { 53 t.Parallel() 54 client, mux, _ := setup(t) 55 56 input := &ActionsPermissions{EnabledRepositories: String("all"), AllowedActions: String("selected")} 57 58 mux.HandleFunc("/orgs/o/actions/permissions", func(w http.ResponseWriter, r *http.Request) { 59 v := new(ActionsPermissions) 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_repositories": "all", "allowed_actions": "selected"}`) 68 }) 69 70 ctx := context.Background() 71 org, _, err := client.Actions.EditActionsPermissions(ctx, "o", *input) 72 if err != nil { 73 t.Errorf("Actions.EditActionsPermissions returned error: %v", err) 74 } 75 76 want := &ActionsPermissions{EnabledRepositories: String("all"), AllowedActions: String("selected")} 77 if !cmp.Equal(org, want) { 78 t.Errorf("Actions.EditActionsPermissions returned %+v, want %+v", org, want) 79 } 80 81 const methodName = "EditActionsPermissions" 82 testBadOptions(t, methodName, func() (err error) { 83 _, _, err = client.Actions.EditActionsPermissions(ctx, "\n", *input) 84 return err 85 }) 86 87 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 88 got, resp, err := client.Actions.EditActionsPermissions(ctx, "o", *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_ListEnabledReposInOrg(t *testing.T) { 97 t.Parallel() 98 client, mux, _ := setup(t) 99 100 mux.HandleFunc("/orgs/o/actions/permissions/repositories", 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,"repositories":[{"id":2}, {"id": 3}]}`) 106 }) 107 108 ctx := context.Background() 109 opt := &ListOptions{ 110 Page: 1, 111 } 112 got, _, err := client.Actions.ListEnabledReposInOrg(ctx, "o", opt) 113 if err != nil { 114 t.Errorf("Actions.ListEnabledRepos returned error: %v", err) 115 } 116 117 want := &ActionsEnabledOnOrgRepos{TotalCount: int(2), Repositories: []*Repository{ 118 {ID: Int64(2)}, 119 {ID: Int64(3)}, 120 }} 121 if !cmp.Equal(got, want) { 122 t.Errorf("Actions.ListEnabledRepos returned %+v, want %+v", got, want) 123 } 124 125 const methodName = "ListEnabledRepos" 126 testBadOptions(t, methodName, func() (err error) { 127 _, _, err = client.Actions.ListEnabledReposInOrg(ctx, "\n", opt) 128 return err 129 }) 130 131 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 132 got, resp, err := client.Actions.ListEnabledReposInOrg(ctx, "o", 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_SetEnabledReposInOrg(t *testing.T) { 141 t.Parallel() 142 client, mux, _ := setup(t) 143 144 mux.HandleFunc("/orgs/o/actions/permissions/repositories", 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_repository_ids":[123,1234]}`+"\n") 148 w.WriteHeader(http.StatusNoContent) 149 }) 150 151 ctx := context.Background() 152 _, err := client.Actions.SetEnabledReposInOrg(ctx, "o", []int64{123, 1234}) 153 if err != nil { 154 t.Errorf("Actions.SetEnabledRepos returned error: %v", err) 155 } 156 157 const methodName = "SetEnabledRepos" 158 159 testBadOptions(t, methodName, func() (err error) { 160 _, err = client.Actions.SetEnabledReposInOrg(ctx, "\n", []int64{123, 1234}) 161 return err 162 }) 163 164 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 165 return client.Actions.SetEnabledReposInOrg(ctx, "o", []int64{123, 1234}) 166 }) 167 } 168 169 func TestActionsService_AddEnabledReposInOrg(t *testing.T) { 170 t.Parallel() 171 client, mux, _ := setup(t) 172 173 mux.HandleFunc("/orgs/o/actions/permissions/repositories/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.AddEnabledReposInOrg(ctx, "o", 123) 180 if err != nil { 181 t.Errorf("Actions.AddEnabledReposInOrg returned error: %v", err) 182 } 183 184 const methodName = "AddEnabledReposInOrg" 185 186 testBadOptions(t, methodName, func() (err error) { 187 _, err = client.Actions.AddEnabledReposInOrg(ctx, "\n", 123) 188 return err 189 }) 190 191 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 192 return client.Actions.AddEnabledReposInOrg(ctx, "o", 123) 193 }) 194 } 195 196 func TestActionsService_RemoveEnabledReposInOrg(t *testing.T) { 197 t.Parallel() 198 client, mux, _ := setup(t) 199 200 mux.HandleFunc("/orgs/o/actions/permissions/repositories/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.RemoveEnabledReposInOrg(ctx, "o", 123) 207 if err != nil { 208 t.Errorf("Actions.RemoveEnabledReposInOrg returned error: %v", err) 209 } 210 211 const methodName = "RemoveEnabledReposInOrg" 212 213 testBadOptions(t, methodName, func() (err error) { 214 _, err = client.Actions.RemoveEnabledReposInOrg(ctx, "\n", 123) 215 return err 216 }) 217 218 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 219 return client.Actions.RemoveEnabledReposInOrg(ctx, "o", 123) 220 }) 221 } 222 223 func TestActionsService_GetActionsAllowed(t *testing.T) { 224 t.Parallel() 225 client, mux, _ := setup(t) 226 227 mux.HandleFunc("/orgs/o/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 org, _, err := client.Actions.GetActionsAllowed(ctx, "o") 234 if err != nil { 235 t.Errorf("Actions.GetActionsAllowed returned error: %v", err) 236 } 237 want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} 238 if !cmp.Equal(org, want) { 239 t.Errorf("Actions.GetActionsAllowed returned %+v, want %+v", org, want) 240 } 241 242 const methodName = "GetActionsAllowed" 243 testBadOptions(t, methodName, func() (err error) { 244 _, _, err = client.Actions.GetActionsAllowed(ctx, "\n") 245 return err 246 }) 247 248 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 249 got, resp, err := client.Actions.GetActionsAllowed(ctx, "o") 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_EditActionsAllowed(t *testing.T) { 258 t.Parallel() 259 client, mux, _ := setup(t) 260 261 input := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} 262 263 mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) { 264 v := new(ActionsAllowed) 265 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 266 267 testMethod(t, r, "PUT") 268 if !cmp.Equal(v, input) { 269 t.Errorf("Request body = %+v, want %+v", v, input) 270 } 271 272 fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`) 273 }) 274 275 ctx := context.Background() 276 org, _, err := client.Actions.EditActionsAllowed(ctx, "o", *input) 277 if err != nil { 278 t.Errorf("Actions.EditActionsAllowed returned error: %v", err) 279 } 280 281 want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} 282 if !cmp.Equal(org, want) { 283 t.Errorf("Actions.EditActionsAllowed returned %+v, want %+v", org, want) 284 } 285 286 const methodName = "EditActionsAllowed" 287 testBadOptions(t, methodName, func() (err error) { 288 _, _, err = client.Actions.EditActionsAllowed(ctx, "\n", *input) 289 return err 290 }) 291 292 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 293 got, resp, err := client.Actions.EditActionsAllowed(ctx, "o", *input) 294 if got != nil { 295 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 296 } 297 return resp, err 298 }) 299 } 300 301 func TestActionsAllowed_Marshal(t *testing.T) { 302 t.Parallel() 303 testJSONMarshal(t, &ActionsAllowed{}, "{}") 304 305 u := &ActionsAllowed{ 306 GithubOwnedAllowed: Bool(false), 307 VerifiedAllowed: Bool(false), 308 PatternsAllowed: []string{"s"}, 309 } 310 311 want := `{ 312 "github_owned_allowed": false, 313 "verified_allowed": false, 314 "patterns_allowed": [ 315 "s" 316 ] 317 }` 318 319 testJSONMarshal(t, u, want) 320 } 321 322 func TestActionsPermissions_Marshal(t *testing.T) { 323 t.Parallel() 324 testJSONMarshal(t, &ActionsPermissions{}, "{}") 325 326 u := &ActionsPermissions{ 327 EnabledRepositories: String("e"), 328 AllowedActions: String("a"), 329 SelectedActionsURL: String("sau"), 330 } 331 332 want := `{ 333 "enabled_repositories": "e", 334 "allowed_actions": "a", 335 "selected_actions_url": "sau" 336 }` 337 338 testJSONMarshal(t, u, want) 339 } 340 341 func TestActionsService_GetDefaultWorkflowPermissionsInOrganization(t *testing.T) { 342 t.Parallel() 343 client, mux, _ := setup(t) 344 345 mux.HandleFunc("/orgs/o/actions/permissions/workflow", func(w http.ResponseWriter, r *http.Request) { 346 testMethod(t, r, "GET") 347 fmt.Fprint(w, `{ "default_workflow_permissions": "read", "can_approve_pull_request_reviews": true }`) 348 }) 349 350 ctx := context.Background() 351 org, _, err := client.Actions.GetDefaultWorkflowPermissionsInOrganization(ctx, "o") 352 if err != nil { 353 t.Errorf("Actions.GetDefaultWorkflowPermissionsInOrganization returned error: %v", err) 354 } 355 want := &DefaultWorkflowPermissionOrganization{DefaultWorkflowPermissions: String("read"), CanApprovePullRequestReviews: Bool(true)} 356 if !cmp.Equal(org, want) { 357 t.Errorf("Actions.GetDefaultWorkflowPermissionsInOrganization returned %+v, want %+v", org, want) 358 } 359 360 const methodName = "GetDefaultWorkflowPermissionsInOrganization" 361 testBadOptions(t, methodName, func() (err error) { 362 _, _, err = client.Actions.GetDefaultWorkflowPermissionsInOrganization(ctx, "\n") 363 return err 364 }) 365 366 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 367 got, resp, err := client.Actions.GetDefaultWorkflowPermissionsInOrganization(ctx, "o") 368 if got != nil { 369 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 370 } 371 return resp, err 372 }) 373 } 374 375 func TestActionsService_EditDefaultWorkflowPermissionsInOrganization(t *testing.T) { 376 t.Parallel() 377 client, mux, _ := setup(t) 378 379 input := &DefaultWorkflowPermissionOrganization{DefaultWorkflowPermissions: String("read"), CanApprovePullRequestReviews: Bool(true)} 380 381 mux.HandleFunc("/orgs/o/actions/permissions/workflow", func(w http.ResponseWriter, r *http.Request) { 382 v := new(DefaultWorkflowPermissionOrganization) 383 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 384 385 testMethod(t, r, "PUT") 386 if !cmp.Equal(v, input) { 387 t.Errorf("Request body = %+v, want %+v", v, input) 388 } 389 390 fmt.Fprint(w, `{ "default_workflow_permissions": "read", "can_approve_pull_request_reviews": true }`) 391 }) 392 393 ctx := context.Background() 394 org, _, err := client.Actions.EditDefaultWorkflowPermissionsInOrganization(ctx, "o", *input) 395 if err != nil { 396 t.Errorf("Actions.EditDefaultWorkflowPermissionsInOrganization returned error: %v", err) 397 } 398 399 want := &DefaultWorkflowPermissionOrganization{DefaultWorkflowPermissions: String("read"), CanApprovePullRequestReviews: Bool(true)} 400 if !cmp.Equal(org, want) { 401 t.Errorf("Actions.EditDefaultWorkflowPermissionsInOrganization returned %+v, want %+v", org, want) 402 } 403 404 const methodName = "EditDefaultWorkflowPermissionsInOrganization" 405 testBadOptions(t, methodName, func() (err error) { 406 _, _, err = client.Actions.EditDefaultWorkflowPermissionsInOrganization(ctx, "\n", *input) 407 return err 408 }) 409 410 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 411 got, resp, err := client.Actions.EditDefaultWorkflowPermissionsInOrganization(ctx, "o", *input) 412 if got != nil { 413 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 414 } 415 return resp, err 416 }) 417 }