| 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_GetActionsPermissions(t *testing.T) { |
| 19 |
client, mux, _, teardown := setup() |
| 20 |
defer teardown() |
| 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.Organizations.GetActionsPermissions(ctx, "o") |
| 29 |
if err != nil { |
| 30 |
t.Errorf("Organizations.GetActionsPermissions returned error: %v", err) |
| 31 |
} |
| 32 |
want := &ActionsPermissions{EnabledRepositories: String("all"), AllowedActions: String("all")} |
| 33 |
if !cmp.Equal(org, want) { |
| 34 |
t.Errorf("Organizations.GetActionsPermissions returned %+v, want %+v", org, want) |
| 35 |
} |
| 36 |
|
| 37 |
const methodName = "GetActionsPermissions" |
| 38 |
testBadOptions(t, methodName, func() (err error) { |
| 39 |
_, _, err = client.Organizations.GetActionsPermissions(ctx, "\n") |
| 40 |
return err |
| 41 |
}) |
| 42 |
|
| 43 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 44 |
got, resp, err := client.Organizations.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 TestOrganizationsService_EditActionsPermissions(t *testing.T) { |
| 53 |
client, mux, _, teardown := setup() |
| 54 |
defer teardown() |
| 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.Organizations.EditActionsPermissions(ctx, "o", *input) |
| 72 |
if err != nil { |
| 73 |
t.Errorf("Organizations.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("Organizations.EditActionsPermissions returned %+v, want %+v", org, want) |
| 79 |
} |
| 80 |
|
| 81 |
const methodName = "EditActionsPermissions" |
| 82 |
testBadOptions(t, methodName, func() (err error) { |
| 83 |
_, _, err = client.Organizations.EditActionsPermissions(ctx, "\n", *input) |
| 84 |
return err |
| 85 |
}) |
| 86 |
|
| 87 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 88 |
got, resp, err := client.Organizations.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 |
} |