github.com/google/go-github/v49@v49.1.0/github/orgs_actions_allowed_test.go (about) 1 // Copyright 2021 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 TestOrganizationsService_GetActionsAllowed(t *testing.T) { 19 client, mux, _, teardown := setup() 20 defer teardown() 21 22 mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`) 25 }) 26 27 ctx := context.Background() 28 org, _, err := client.Organizations.GetActionsAllowed(ctx, "o") 29 if err != nil { 30 t.Errorf("Organizations.GetActionsAllowed returned error: %v", err) 31 } 32 want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} 33 if !cmp.Equal(org, want) { 34 t.Errorf("Organizations.GetActionsAllowed returned %+v, want %+v", org, want) 35 } 36 37 const methodName = "GetActionsAllowed" 38 testBadOptions(t, methodName, func() (err error) { 39 _, _, err = client.Organizations.GetActionsAllowed(ctx, "\n") 40 return err 41 }) 42 43 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 44 got, resp, err := client.Organizations.GetActionsAllowed(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 TestOrganizationsService_EditActionsAllowed(t *testing.T) { 53 client, mux, _, teardown := setup() 54 defer teardown() 55 input := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} 56 57 mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) { 58 v := new(ActionsAllowed) 59 json.NewDecoder(r.Body).Decode(v) 60 61 testMethod(t, r, "PUT") 62 if !cmp.Equal(v, input) { 63 t.Errorf("Request body = %+v, want %+v", v, input) 64 } 65 66 fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`) 67 }) 68 69 ctx := context.Background() 70 org, _, err := client.Organizations.EditActionsAllowed(ctx, "o", *input) 71 if err != nil { 72 t.Errorf("Organizations.EditActionsAllowed returned error: %v", err) 73 } 74 75 want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}} 76 if !cmp.Equal(org, want) { 77 t.Errorf("Organizations.EditActionsAllowed returned %+v, want %+v", org, want) 78 } 79 80 const methodName = "EditActionsAllowed" 81 testBadOptions(t, methodName, func() (err error) { 82 _, _, err = client.Organizations.EditActionsAllowed(ctx, "\n", *input) 83 return err 84 }) 85 86 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 87 got, resp, err := client.Organizations.EditActionsAllowed(ctx, "o", *input) 88 if got != nil { 89 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 90 } 91 return resp, err 92 }) 93 } 94 95 func TestActionsAllowed_Marshal(t *testing.T) { 96 testJSONMarshal(t, &ActionsAllowed{}, "{}") 97 98 u := &ActionsAllowed{ 99 GithubOwnedAllowed: Bool(false), 100 VerifiedAllowed: Bool(false), 101 PatternsAllowed: []string{"s"}, 102 } 103 104 want := `{ 105 "github_owned_allowed": false, 106 "verified_allowed": false, 107 "patterns_allowed": [ 108 "s" 109 ] 110 }` 111 112 testJSONMarshal(t, u, want) 113 } 114 115 func TestActionsPermissions_Marshal(t *testing.T) { 116 testJSONMarshal(t, &ActionsPermissions{}, "{}") 117 118 u := &ActionsPermissions{ 119 EnabledRepositories: String("e"), 120 AllowedActions: String("a"), 121 SelectedActionsURL: String("sau"), 122 } 123 124 want := `{ 125 "enabled_repositories": "e", 126 "allowed_actions": "a", 127 "selected_actions_url": "sau" 128 }` 129 130 testJSONMarshal(t, u, want) 131 }