github.com/google/go-github/v49@v49.1.0/github/repos_actions_permissions_test.go (about) 1 // Copyright 2022 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 TestRepositoriesService_GetActionsPermissions(t *testing.T) { 19 client, mux, _, teardown := setup() 20 defer teardown() 21 22 mux.HandleFunc("/repos/o/r/actions/permissions", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 fmt.Fprint(w, `{"enabled": true, "allowed_actions": "all"}`) 25 }) 26 27 ctx := context.Background() 28 org, _, err := client.Repositories.GetActionsPermissions(ctx, "o", "r") 29 if err != nil { 30 t.Errorf("Repositories.GetActionsPermissions returned error: %v", err) 31 } 32 want := &ActionsPermissionsRepository{Enabled: Bool(true), AllowedActions: String("all")} 33 if !cmp.Equal(org, want) { 34 t.Errorf("Repositories.GetActionsPermissions returned %+v, want %+v", org, want) 35 } 36 37 const methodName = "GetActionsPermissions" 38 testBadOptions(t, methodName, func() (err error) { 39 _, _, err = client.Repositories.GetActionsPermissions(ctx, "\n", "\n") 40 return err 41 }) 42 43 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 44 got, resp, err := client.Repositories.GetActionsPermissions(ctx, "o", "r") 45 if got != nil { 46 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 47 } 48 return resp, err 49 }) 50 } 51 52 func TestRepositoriesService_EditActionsPermissions(t *testing.T) { 53 client, mux, _, teardown := setup() 54 defer teardown() 55 56 input := &ActionsPermissionsRepository{Enabled: Bool(true), AllowedActions: String("selected")} 57 58 mux.HandleFunc("/repos/o/r/actions/permissions", func(w http.ResponseWriter, r *http.Request) { 59 v := new(ActionsPermissionsRepository) 60 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": true, "allowed_actions": "selected"}`) 68 }) 69 70 ctx := context.Background() 71 org, _, err := client.Repositories.EditActionsPermissions(ctx, "o", "r", *input) 72 if err != nil { 73 t.Errorf("Repositories.EditActionsPermissions returned error: %v", err) 74 } 75 76 want := &ActionsPermissionsRepository{Enabled: Bool(true), AllowedActions: String("selected")} 77 if !cmp.Equal(org, want) { 78 t.Errorf("Repositories.EditActionsPermissions returned %+v, want %+v", org, want) 79 } 80 81 const methodName = "EditActionsPermissions" 82 testBadOptions(t, methodName, func() (err error) { 83 _, _, err = client.Repositories.EditActionsPermissions(ctx, "\n", "\n", *input) 84 return err 85 }) 86 87 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 88 got, resp, err := client.Repositories.EditActionsPermissions(ctx, "o", "r", *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 TestActionsPermissionsRepository_Marshal(t *testing.T) { 97 testJSONMarshal(t, &ActionsPermissions{}, "{}") 98 99 u := &ActionsPermissionsRepository{ 100 Enabled: Bool(true), 101 AllowedActions: String("all"), 102 SelectedActionsURL: String("someURL"), 103 } 104 105 want := `{ 106 "enabled": true, 107 "allowed_actions": "all", 108 "selected_actions_url": "someURL" 109 }` 110 111 testJSONMarshal(t, u, want) 112 }