| 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 |
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": 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 |
} |
| 113 |
|
| 114 |
func TestRepositoriesService_GetDefaultWorkflowPermissions(t *testing.T) { |
| 115 |
client, mux, _, teardown := setup() |
| 116 |
defer teardown() |
| 117 |
|
| 118 |
mux.HandleFunc("/repos/o/r/actions/permissions/workflow", func(w http.ResponseWriter, r *http.Request) { |
| 119 |
testMethod(t, r, "GET") |
| 120 |
fmt.Fprint(w, `{ "default_workflow_permissions": "read", "can_approve_pull_request_reviews": true }`) |
| 121 |
}) |
| 122 |
|
| 123 |
ctx := context.Background() |
| 124 |
org, _, err := client.Repositories.GetDefaultWorkflowPermissions(ctx, "o", "r") |
| 125 |
if err != nil { |
| 126 |
t.Errorf("Repositories.GetDefaultWorkflowPermissions returned error: %v", err) |
| 127 |
} |
| 128 |
want := &DefaultWorkflowPermissionRepository{DefaultWorkflowPermissions: String("read"), CanApprovePullRequestReviews: Bool(true)} |
| 129 |
if !cmp.Equal(org, want) { |
| 130 |
t.Errorf("Repositories.GetDefaultWorkflowPermissions returned %+v, want %+v", org, want) |
| 131 |
} |
| 132 |
|
| 133 |
const methodName = "GetDefaultWorkflowPermissions" |
| 134 |
testBadOptions(t, methodName, func() (err error) { |
| 135 |
_, _, err = client.Repositories.GetDefaultWorkflowPermissions(ctx, "\n", "\n") |
| 136 |
return err |
| 137 |
}) |
| 138 |
|
| 139 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 140 |
got, resp, err := client.Repositories.GetDefaultWorkflowPermissions(ctx, "o", "r") |
| 141 |
if got != nil { |
| 142 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 143 |
} |
| 144 |
return resp, err |
| 145 |
}) |
| 146 |
} |
| 147 |
|
| 148 |
func TestRepositoriesService_EditDefaultWorkflowPermissions(t *testing.T) { |
| 149 |
client, mux, _, teardown := setup() |
| 150 |
defer teardown() |
| 151 |
|
| 152 |
input := &DefaultWorkflowPermissionRepository{DefaultWorkflowPermissions: String("read"), CanApprovePullRequestReviews: Bool(true)} |
| 153 |
|
| 154 |
mux.HandleFunc("/repos/o/r/actions/permissions/workflow", func(w http.ResponseWriter, r *http.Request) { |
| 155 |
v := new(DefaultWorkflowPermissionRepository) |
| 156 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 157 |
|
| 158 |
testMethod(t, r, "PUT") |
| 159 |
if !cmp.Equal(v, input) { |
| 160 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 161 |
} |
| 162 |
|
| 163 |
fmt.Fprint(w, `{ "default_workflow_permissions": "read", "can_approve_pull_request_reviews": true }`) |
| 164 |
}) |
| 165 |
|
| 166 |
ctx := context.Background() |
| 167 |
org, _, err := client.Repositories.EditDefaultWorkflowPermissions(ctx, "o", "r", *input) |
| 168 |
if err != nil { |
| 169 |
t.Errorf("Repositories.EditDefaultWorkflowPermissions returned error: %v", err) |
| 170 |
} |
| 171 |
|
| 172 |
want := &DefaultWorkflowPermissionRepository{DefaultWorkflowPermissions: String("read"), CanApprovePullRequestReviews: Bool(true)} |
| 173 |
if !cmp.Equal(org, want) { |
| 174 |
t.Errorf("Repositories.EditDefaultWorkflowPermissions returned %+v, want %+v", org, want) |
| 175 |
} |
| 176 |
|
| 177 |
const methodName = "EditDefaultWorkflowPermissions" |
| 178 |
testBadOptions(t, methodName, func() (err error) { |
| 179 |
_, _, err = client.Repositories.EditDefaultWorkflowPermissions(ctx, "\n", "\n", *input) |
| 180 |
return err |
| 181 |
}) |
| 182 |
|
| 183 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 184 |
got, resp, err := client.Repositories.EditDefaultWorkflowPermissions(ctx, "o", "r", *input) |
| 185 |
if got != nil { |
| 186 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 187 |
} |
| 188 |
return resp, err |
| 189 |
}) |
| 190 |
} |