| 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 TestRepositoryService_GetActionsAllowed(t *testing.T) { |
| 19 |
client, mux, _, teardown := setup() |
| 20 |
defer teardown() |
| 21 |
|
| 22 |
mux.HandleFunc("/repos/o/r/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.Repositories.GetActionsAllowed(ctx, "o", "r") |
| 29 |
if err != nil { |
| 30 |
t.Errorf("Repositories.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("Repositories.GetActionsAllowed returned %+v, want %+v", org, want) |
| 35 |
} |
| 36 |
|
| 37 |
const methodName = "GetActionsAllowed" |
| 38 |
testBadOptions(t, methodName, func() (err error) { |
| 39 |
_, _, err = client.Repositories.GetActionsAllowed(ctx, "\n", "\n") |
| 40 |
return err |
| 41 |
}) |
| 42 |
|
| 43 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 44 |
got, resp, err := client.Repositories.GetActionsAllowed(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_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("/repos/o/r/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) { |
| 58 |
v := new(ActionsAllowed) |
| 59 |
assertNilError(t, 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.Repositories.EditActionsAllowed(ctx, "o", "r", *input) |
| 71 |
if err != nil { |
| 72 |
t.Errorf("Repositories.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("Repositories.EditActionsAllowed returned %+v, want %+v", org, want) |
| 78 |
} |
| 79 |
|
| 80 |
const methodName = "EditActionsAllowed" |
| 81 |
testBadOptions(t, methodName, func() (err error) { |
| 82 |
_, _, err = client.Repositories.EditActionsAllowed(ctx, "\n", "\n", *input) |
| 83 |
return err |
| 84 |
}) |
| 85 |
|
| 86 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 87 |
got, resp, err := client.Repositories.EditActionsAllowed(ctx, "o", "r", *input) |
| 88 |
if got != nil { |
| 89 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 90 |
} |
| 91 |
return resp, err |
| 92 |
}) |
| 93 |
} |