| 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 |
"fmt" |
| 11 |
"net/http" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/google/go-cmp/cmp" |
| 15 |
) |
| 16 |
|
| 17 |
func TestOrganizationsService_ListCustomRepoRoles(t *testing.T) { |
| 18 |
client, mux, _, teardown := setup() |
| 19 |
defer teardown() |
| 20 |
|
| 21 |
mux.HandleFunc("/orgs/o/custom-repository-roles", func(w http.ResponseWriter, r *http.Request) { |
| 22 |
testMethod(t, r, "GET") |
| 23 |
fmt.Fprint(w, `{"total_count": 1, "custom_roles": [{ "id": 1, "name": "Developer", "base_role": "write", "permissions": ["delete_alerts_code_scanning"]}]}`) |
| 24 |
}) |
| 25 |
|
| 26 |
ctx := context.Background() |
| 27 |
apps, _, err := client.Organizations.ListCustomRepoRoles(ctx, "o") |
| 28 |
if err != nil { |
| 29 |
t.Errorf("Organizations.ListCustomRepoRoles returned error: %v", err) |
| 30 |
} |
| 31 |
|
| 32 |
want := &OrganizationCustomRepoRoles{TotalCount: Int(1), CustomRepoRoles: []*CustomRepoRoles{{ID: Int64(1), Name: String("Developer"), BaseRole: String("write"), Permissions: []string{"delete_alerts_code_scanning"}}}} |
| 33 |
if !cmp.Equal(apps, want) { |
| 34 |
t.Errorf("Organizations.ListCustomRepoRoles returned %+v, want %+v", apps, want) |
| 35 |
} |
| 36 |
|
| 37 |
const methodName = "ListCustomRepoRoles" |
| 38 |
testBadOptions(t, methodName, func() (err error) { |
| 39 |
_, _, err = client.Organizations.ListCustomRepoRoles(ctx, "\no") |
| 40 |
return err |
| 41 |
}) |
| 42 |
|
| 43 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 44 |
got, resp, err := client.Organizations.ListCustomRepoRoles(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_CreateCustomRepoRole(t *testing.T) { |
| 53 |
client, mux, _, teardown := setup() |
| 54 |
defer teardown() |
| 55 |
|
| 56 |
mux.HandleFunc("/orgs/o/custom-repository-roles", func(w http.ResponseWriter, r *http.Request) { |
| 57 |
testMethod(t, r, "POST") |
| 58 |
fmt.Fprint(w, `{"id":8030,"name":"Labeler","description":"A role for issue and PR labelers","base_role":"read","permissions":["add_label"]}`) |
| 59 |
}) |
| 60 |
|
| 61 |
ctx := context.Background() |
| 62 |
|
| 63 |
opts := &CreateOrUpdateCustomRoleOptions{ |
| 64 |
Name: String("Labeler"), |
| 65 |
Description: String("A role for issue and PR labelers"), |
| 66 |
BaseRole: String("read"), |
| 67 |
Permissions: []string{"add_label"}, |
| 68 |
} |
| 69 |
apps, _, err := client.Organizations.CreateCustomRepoRole(ctx, "o", opts) |
| 70 |
if err != nil { |
| 71 |
t.Errorf("Organizations.CreateCustomRepoRole returned error: %v", err) |
| 72 |
} |
| 73 |
|
| 74 |
want := &CustomRepoRoles{ID: Int64(8030), Name: String("Labeler"), BaseRole: String("read"), Permissions: []string{"add_label"}, Description: String("A role for issue and PR labelers")} |
| 75 |
|
| 76 |
if !cmp.Equal(apps, want) { |
| 77 |
t.Errorf("Organizations.CreateCustomRepoRole returned %+v, want %+v", apps, want) |
| 78 |
} |
| 79 |
|
| 80 |
const methodName = "CreateCustomRepoRole" |
| 81 |
testBadOptions(t, methodName, func() (err error) { |
| 82 |
_, _, err = client.Organizations.CreateCustomRepoRole(ctx, "\no", nil) |
| 83 |
return err |
| 84 |
}) |
| 85 |
|
| 86 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 87 |
got, resp, err := client.Organizations.CreateCustomRepoRole(ctx, "o", nil) |
| 88 |
if got != nil { |
| 89 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 90 |
} |
| 91 |
return resp, err |
| 92 |
}) |
| 93 |
} |
| 94 |
|
| 95 |
func TestOrganizationsService_UpdateCustomRepoRole(t *testing.T) { |
| 96 |
client, mux, _, teardown := setup() |
| 97 |
defer teardown() |
| 98 |
|
| 99 |
mux.HandleFunc("/orgs/o/custom-repository-roles/8030", func(w http.ResponseWriter, r *http.Request) { |
| 100 |
testMethod(t, r, "PATCH") |
| 101 |
fmt.Fprint(w, `{"id":8030,"name":"Updated Name","description":"Updated Description","base_role":"read","permissions":["add_label"]}`) |
| 102 |
}) |
| 103 |
|
| 104 |
ctx := context.Background() |
| 105 |
|
| 106 |
opts := &CreateOrUpdateCustomRoleOptions{ |
| 107 |
Name: String("Updated Name"), |
| 108 |
Description: String("Updated Description"), |
| 109 |
} |
| 110 |
apps, _, err := client.Organizations.UpdateCustomRepoRole(ctx, "o", "8030", opts) |
| 111 |
if err != nil { |
| 112 |
t.Errorf("Organizations.UpdateCustomRepoRole returned error: %v", err) |
| 113 |
} |
| 114 |
|
| 115 |
want := &CustomRepoRoles{ID: Int64(8030), Name: String("Updated Name"), BaseRole: String("read"), Permissions: []string{"add_label"}, Description: String("Updated Description")} |
| 116 |
|
| 117 |
if !cmp.Equal(apps, want) { |
| 118 |
t.Errorf("Organizations.UpdateCustomRepoRole returned %+v, want %+v", apps, want) |
| 119 |
} |
| 120 |
|
| 121 |
const methodName = "UpdateCustomRepoRole" |
| 122 |
testBadOptions(t, methodName, func() (err error) { |
| 123 |
_, _, err = client.Organizations.UpdateCustomRepoRole(ctx, "\no", "8030", nil) |
| 124 |
return err |
| 125 |
}) |
| 126 |
|
| 127 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 128 |
got, resp, err := client.Organizations.UpdateCustomRepoRole(ctx, "o", "8030", nil) |
| 129 |
if got != nil { |
| 130 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 131 |
} |
| 132 |
return resp, err |
| 133 |
}) |
| 134 |
} |
| 135 |
|
| 136 |
func TestOrganizationsService_DeleteCustomRepoRole(t *testing.T) { |
| 137 |
client, mux, _, teardown := setup() |
| 138 |
defer teardown() |
| 139 |
|
| 140 |
mux.HandleFunc("/orgs/o/custom-repository-roles/8030", func(w http.ResponseWriter, r *http.Request) { |
| 141 |
testMethod(t, r, "DELETE") |
| 142 |
w.WriteHeader(http.StatusNoContent) |
| 143 |
}) |
| 144 |
|
| 145 |
ctx := context.Background() |
| 146 |
|
| 147 |
resp, err := client.Organizations.DeleteCustomRepoRole(ctx, "o", "8030") |
| 148 |
if err != nil { |
| 149 |
t.Errorf("Organizations.DeleteCustomRepoRole returned error: %v", err) |
| 150 |
} |
| 151 |
|
| 152 |
if !cmp.Equal(resp.StatusCode, 204) { |
| 153 |
t.Errorf("Organizations.DeleteCustomRepoRole returned status code %+v, want %+v", resp.StatusCode, "204") |
| 154 |
} |
| 155 |
|
| 156 |
const methodName = "DeleteCustomRepoRole" |
| 157 |
testBadOptions(t, methodName, func() (err error) { |
| 158 |
_, err = client.Organizations.DeleteCustomRepoRole(ctx, "\no", "8030") |
| 159 |
return err |
| 160 |
}) |
| 161 |
} |