|
| 1 | +// Copyright 2024 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 | + "time" |
| 14 | + |
| 15 | + "github.com/google/go-cmp/cmp" |
| 16 | +) |
| 17 | + |
| 18 | +func TestOrganizationsService_ListCustomRepoRoles(t *testing.T) { |
| 19 | + client, mux, _, teardown := setup() |
| 20 | + defer teardown() |
| 21 | + |
| 22 | + mux.HandleFunc("/orgs/o/custom-repository-roles", func(w http.ResponseWriter, r *http.Request) { |
| 23 | + testMethod(t, r, "GET") |
| 24 | + fmt.Fprint(w, `{"total_count": 1, "custom_roles": [ |
| 25 | + { |
| 26 | + "id": 1, |
| 27 | + "name": "Developer", |
| 28 | + "base_role": "write", |
| 29 | + "permissions": ["delete_alerts_code_scanning"], |
| 30 | + "organization": { |
| 31 | + "login": "l", |
| 32 | + "id": 1, |
| 33 | + "node_id": "n", |
| 34 | + "avatar_url": "a", |
| 35 | + "html_url": "h", |
| 36 | + "name": "n", |
| 37 | + "company": "c", |
| 38 | + "blog": "b", |
| 39 | + "location": "l", |
| 40 | + "email": "e" |
| 41 | + }, |
| 42 | + "created_at": "2024-07-21T19:33:08Z", |
| 43 | + "updated_at": "2024-07-21T19:33:08Z" |
| 44 | + } |
| 45 | + ] |
| 46 | + }`) |
| 47 | + }) |
| 48 | + |
| 49 | + ctx := context.Background() |
| 50 | + apps, _, err := client.Organizations.ListCustomRepoRoles(ctx, "o") |
| 51 | + if err != nil { |
| 52 | + t.Errorf("Organizations.ListCustomRepoRoles returned error: %v", err) |
| 53 | + } |
| 54 | + |
| 55 | + want := &OrganizationCustomRepoRoles{ |
| 56 | + TotalCount: Int(1), |
| 57 | + CustomRepoRoles: []*CustomRepoRoles{ |
| 58 | + { |
| 59 | + ID: Int64(1), |
| 60 | + Name: String("Developer"), |
| 61 | + BaseRole: String("write"), |
| 62 | + Permissions: []string{"delete_alerts_code_scanning"}, |
| 63 | + Org: &Organization{ |
| 64 | + Login: String("l"), |
| 65 | + ID: Int64(1), |
| 66 | + NodeID: String("n"), |
| 67 | + AvatarURL: String("a"), |
| 68 | + HTMLURL: String("h"), |
| 69 | + Name: String("n"), |
| 70 | + Company: String("c"), |
| 71 | + Blog: String("b"), |
| 72 | + Location: String("l"), |
| 73 | + Email: String("e"), |
| 74 | + }, |
| 75 | + CreatedAt: &Timestamp{time.Date(2024, time.July, 21, 19, 33, 8, 0, time.UTC)}, |
| 76 | + UpdatedAt: &Timestamp{time.Date(2024, time.July, 21, 19, 33, 8, 0, time.UTC)}, |
| 77 | + }, |
| 78 | + }, |
| 79 | + } |
| 80 | + if !cmp.Equal(apps, want) { |
| 81 | + t.Errorf("Organizations.ListCustomRepoRoles returned %+v, want %+v", apps, want) |
| 82 | + } |
| 83 | + |
| 84 | + const methodName = "ListCustomRepoRoles" |
| 85 | + testBadOptions(t, methodName, func() (err error) { |
| 86 | + _, _, err = client.Organizations.ListCustomRepoRoles(ctx, "\no") |
| 87 | + return err |
| 88 | + }) |
| 89 | + |
| 90 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 91 | + got, resp, err := client.Organizations.ListCustomRepoRoles(ctx, "o") |
| 92 | + if got != nil { |
| 93 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 94 | + } |
| 95 | + return resp, err |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +func TestOrganizationsService_CreateCustomRepoRole(t *testing.T) { |
| 100 | + client, mux, _, teardown := setup() |
| 101 | + defer teardown() |
| 102 | + |
| 103 | + mux.HandleFunc("/orgs/o/custom-repository-roles", func(w http.ResponseWriter, r *http.Request) { |
| 104 | + testMethod(t, r, "POST") |
| 105 | + fmt.Fprint(w, `{"id":8030,"name":"Labeler","description":"A role for issue and PR labelers","base_role":"read","permissions":["add_label"]}`) |
| 106 | + }) |
| 107 | + |
| 108 | + ctx := context.Background() |
| 109 | + |
| 110 | + opts := &CreateOrUpdateCustomRepoRoleOptions{ |
| 111 | + Name: String("Labeler"), |
| 112 | + Description: String("A role for issue and PR labelers"), |
| 113 | + BaseRole: String("read"), |
| 114 | + Permissions: []string{"add_label"}, |
| 115 | + } |
| 116 | + apps, _, err := client.Organizations.CreateCustomRepoRole(ctx, "o", opts) |
| 117 | + if err != nil { |
| 118 | + t.Errorf("Organizations.CreateCustomRepoRole returned error: %v", err) |
| 119 | + } |
| 120 | + |
| 121 | + want := &CustomRepoRoles{ID: Int64(8030), Name: String("Labeler"), BaseRole: String("read"), Permissions: []string{"add_label"}, Description: String("A role for issue and PR labelers")} |
| 122 | + |
| 123 | + if !cmp.Equal(apps, want) { |
| 124 | + t.Errorf("Organizations.CreateCustomRepoRole returned %+v, want %+v", apps, want) |
| 125 | + } |
| 126 | + |
| 127 | + const methodName = "CreateCustomRepoRole" |
| 128 | + testBadOptions(t, methodName, func() (err error) { |
| 129 | + _, _, err = client.Organizations.CreateCustomRepoRole(ctx, "\no", nil) |
| 130 | + return err |
| 131 | + }) |
| 132 | + |
| 133 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 134 | + got, resp, err := client.Organizations.CreateCustomRepoRole(ctx, "o", nil) |
| 135 | + if got != nil { |
| 136 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 137 | + } |
| 138 | + return resp, err |
| 139 | + }) |
| 140 | +} |
| 141 | + |
| 142 | +func TestOrganizationsService_UpdateCustomRepoRole(t *testing.T) { |
| 143 | + client, mux, _, teardown := setup() |
| 144 | + defer teardown() |
| 145 | + |
| 146 | + mux.HandleFunc("/orgs/o/custom-repository-roles/8030", func(w http.ResponseWriter, r *http.Request) { |
| 147 | + testMethod(t, r, "PATCH") |
| 148 | + fmt.Fprint(w, `{"id":8030,"name":"Updated Name","description":"Updated Description","base_role":"read","permissions":["add_label"]}`) |
| 149 | + }) |
| 150 | + |
| 151 | + ctx := context.Background() |
| 152 | + |
| 153 | + opts := &CreateOrUpdateCustomRepoRoleOptions{ |
| 154 | + Name: String("Updated Name"), |
| 155 | + Description: String("Updated Description"), |
| 156 | + } |
| 157 | + apps, _, err := client.Organizations.UpdateCustomRepoRole(ctx, "o", 8030, opts) |
| 158 | + if err != nil { |
| 159 | + t.Errorf("Organizations.UpdateCustomRepoRole returned error: %v", err) |
| 160 | + } |
| 161 | + |
| 162 | + want := &CustomRepoRoles{ID: Int64(8030), Name: String("Updated Name"), BaseRole: String("read"), Permissions: []string{"add_label"}, Description: String("Updated Description")} |
| 163 | + |
| 164 | + if !cmp.Equal(apps, want) { |
| 165 | + t.Errorf("Organizations.UpdateCustomRepoRole returned %+v, want %+v", apps, want) |
| 166 | + } |
| 167 | + |
| 168 | + const methodName = "UpdateCustomRepoRole" |
| 169 | + testBadOptions(t, methodName, func() (err error) { |
| 170 | + _, _, err = client.Organizations.UpdateCustomRepoRole(ctx, "\no", 8030, nil) |
| 171 | + return err |
| 172 | + }) |
| 173 | + |
| 174 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 175 | + got, resp, err := client.Organizations.UpdateCustomRepoRole(ctx, "o", 8030, nil) |
| 176 | + if got != nil { |
| 177 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 178 | + } |
| 179 | + return resp, err |
| 180 | + }) |
| 181 | +} |
| 182 | + |
| 183 | +func TestOrganizationsService_DeleteCustomRepoRole(t *testing.T) { |
| 184 | + client, mux, _, teardown := setup() |
| 185 | + defer teardown() |
| 186 | + |
| 187 | + mux.HandleFunc("/orgs/o/custom-repository-roles/8030", func(w http.ResponseWriter, r *http.Request) { |
| 188 | + testMethod(t, r, "DELETE") |
| 189 | + w.WriteHeader(http.StatusNoContent) |
| 190 | + }) |
| 191 | + |
| 192 | + ctx := context.Background() |
| 193 | + |
| 194 | + resp, err := client.Organizations.DeleteCustomRepoRole(ctx, "o", 8030) |
| 195 | + if err != nil { |
| 196 | + t.Errorf("Organizations.DeleteCustomRepoRole returned error: %v", err) |
| 197 | + } |
| 198 | + |
| 199 | + if !cmp.Equal(resp.StatusCode, 204) { |
| 200 | + t.Errorf("Organizations.DeleteCustomRepoRole returned status code %+v, want %+v", resp.StatusCode, "204") |
| 201 | + } |
| 202 | + |
| 203 | + const methodName = "DeleteCustomRepoRole" |
| 204 | + testBadOptions(t, methodName, func() (err error) { |
| 205 | + _, err = client.Organizations.DeleteCustomRepoRole(ctx, "\no", 8030) |
| 206 | + return err |
| 207 | + }) |
| 208 | +} |
0 commit comments