|
| 1 | +// Copyright 2025 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 | + "fmt" |
| 10 | + "net/http" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/google/go-cmp/cmp" |
| 15 | +) |
| 16 | + |
| 17 | +func TestEnterpriseService_ListTeams(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + client, mux, _ := setup(t) |
| 20 | + |
| 21 | + mux.HandleFunc("/enterprises/e/teams", func(w http.ResponseWriter, r *http.Request) { |
| 22 | + testMethod(t, r, "GET") |
| 23 | + fmt.Fprint(w, `[{ |
| 24 | + "id": 1, |
| 25 | + "url": "https://example.com/team1", |
| 26 | + "member_url": "https://example.com/members", |
| 27 | + "name": "Team One", |
| 28 | + "html_url": "https://example.com/html", |
| 29 | + "slug": "team-one", |
| 30 | + "created_at": "2020-01-01T00:00:00Z", |
| 31 | + "updated_at": "2020-01-02T00:00:00Z", |
| 32 | + "group_id": "99" |
| 33 | + }]`) |
| 34 | + }) |
| 35 | + |
| 36 | + ctx := t.Context() |
| 37 | + opts := &ListOptions{Page: 1, PerPage: 10} |
| 38 | + got, _, err := client.Enterprise.ListTeams(ctx, "e", opts) |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("Enterprise.ListTeams returned error: %v", err) |
| 41 | + } |
| 42 | + |
| 43 | + want := []*EnterpriseTeam{ |
| 44 | + { |
| 45 | + ID: 1, |
| 46 | + URL: "https://example.com/team1", |
| 47 | + MemberURL: "https://example.com/members", |
| 48 | + Name: "Team One", |
| 49 | + HTMLURL: "https://example.com/html", |
| 50 | + Slug: "team-one", |
| 51 | + GroupID: "99", |
| 52 | + CreatedAt: Timestamp{Time: time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)}, |
| 53 | + UpdatedAt: Timestamp{Time: time.Date(2020, time.January, 2, 0, 0, 0, 0, time.UTC)}, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + if !cmp.Equal(got, want) { |
| 58 | + t.Errorf("Enterprise.ListTeams = %+v, want %+v", got, want) |
| 59 | + } |
| 60 | + |
| 61 | + const methodName = "ListTeams" |
| 62 | + testBadOptions(t, methodName, func() error { |
| 63 | + _, _, err := client.Enterprise.ListTeams(ctx, "\n", opts) |
| 64 | + return err |
| 65 | + }) |
| 66 | + |
| 67 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 68 | + got, resp, err := client.Enterprise.ListTeams(ctx, "e", opts) |
| 69 | + if got != nil { |
| 70 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 71 | + } |
| 72 | + return resp, err |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +func TestEnterpriseService_CreateTeam(t *testing.T) { |
| 77 | + t.Parallel() |
| 78 | + client, mux, _ := setup(t) |
| 79 | + |
| 80 | + input := EnterpriseTeamCreateOrUpdateRequest{ |
| 81 | + Name: "New Team", |
| 82 | + } |
| 83 | + |
| 84 | + mux.HandleFunc("/enterprises/e/teams", func(w http.ResponseWriter, r *http.Request) { |
| 85 | + testMethod(t, r, "POST") |
| 86 | + testBody(t, r, `{"name":"New Team"}`+"\n") |
| 87 | + fmt.Fprint(w, `{ |
| 88 | + "id": 10, |
| 89 | + "name": "New Team", |
| 90 | + "slug": "new-team", |
| 91 | + "url": "https://example.com/team" |
| 92 | + }`) |
| 93 | + }) |
| 94 | + |
| 95 | + ctx := t.Context() |
| 96 | + got, _, err := client.Enterprise.CreateTeam(ctx, "e", input) |
| 97 | + if err != nil { |
| 98 | + t.Fatalf("Enterprise.CreateTeam returned error: %v", err) |
| 99 | + } |
| 100 | + |
| 101 | + want := &EnterpriseTeam{ |
| 102 | + ID: 10, |
| 103 | + Name: "New Team", |
| 104 | + Slug: "new-team", |
| 105 | + URL: "https://example.com/team", |
| 106 | + } |
| 107 | + |
| 108 | + if !cmp.Equal(got, want) { |
| 109 | + t.Errorf("Enterprise.CreateTeam = %+v, want %+v", got, want) |
| 110 | + } |
| 111 | + |
| 112 | + const methodName = "CreateTeam" |
| 113 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 114 | + got, resp, err := client.Enterprise.CreateTeam(ctx, "e", input) |
| 115 | + if got != nil { |
| 116 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 117 | + } |
| 118 | + return resp, err |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +func TestEnterpriseService_GetTeam(t *testing.T) { |
| 123 | + t.Parallel() |
| 124 | + client, mux, _ := setup(t) |
| 125 | + |
| 126 | + mux.HandleFunc("/enterprises/e/teams/t1", func(w http.ResponseWriter, r *http.Request) { |
| 127 | + testMethod(t, r, "GET") |
| 128 | + fmt.Fprint(w, `{ |
| 129 | + "id": 2, |
| 130 | + "name": "Team One", |
| 131 | + "slug": "t1" |
| 132 | + }`) |
| 133 | + }) |
| 134 | + |
| 135 | + ctx := t.Context() |
| 136 | + got, _, err := client.Enterprise.GetTeam(ctx, "e", "t1") |
| 137 | + if err != nil { |
| 138 | + t.Fatalf("Enterprise.GetTeam returned error: %v", err) |
| 139 | + } |
| 140 | + |
| 141 | + want := &EnterpriseTeam{ |
| 142 | + ID: 2, |
| 143 | + Name: "Team One", |
| 144 | + Slug: "t1", |
| 145 | + } |
| 146 | + |
| 147 | + if !cmp.Equal(got, want) { |
| 148 | + t.Errorf("Enterprise.GetTeam = %+v, want %+v", got, want) |
| 149 | + } |
| 150 | + |
| 151 | + const methodName = "GetTeam" |
| 152 | + testBadOptions(t, methodName, func() error { |
| 153 | + _, _, err := client.Enterprise.GetTeam(ctx, "\n", "t1") |
| 154 | + return err |
| 155 | + }) |
| 156 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 157 | + got, resp, err := client.Enterprise.GetTeam(ctx, "e", "t1") |
| 158 | + if got != nil { |
| 159 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 160 | + } |
| 161 | + return resp, err |
| 162 | + }) |
| 163 | +} |
| 164 | + |
| 165 | +func TestEnterpriseService_UpdateTeam(t *testing.T) { |
| 166 | + t.Parallel() |
| 167 | + client, mux, _ := setup(t) |
| 168 | + |
| 169 | + input := EnterpriseTeamCreateOrUpdateRequest{ |
| 170 | + Name: "Updated Team", |
| 171 | + } |
| 172 | + |
| 173 | + mux.HandleFunc("/enterprises/e/teams/t1", func(w http.ResponseWriter, r *http.Request) { |
| 174 | + testMethod(t, r, "PATCH") |
| 175 | + testBody(t, r, `{"name":"Updated Team"}`+"\n") |
| 176 | + fmt.Fprint(w, `{ |
| 177 | + "id": 3, |
| 178 | + "name": "Updated Team", |
| 179 | + "slug": "t1" |
| 180 | + }`) |
| 181 | + }) |
| 182 | + |
| 183 | + ctx := t.Context() |
| 184 | + got, _, err := client.Enterprise.UpdateTeam(ctx, "e", "t1", input) |
| 185 | + if err != nil { |
| 186 | + t.Fatalf("Enterprise.UpdateTeam returned error: %v", err) |
| 187 | + } |
| 188 | + |
| 189 | + want := &EnterpriseTeam{ |
| 190 | + ID: 3, |
| 191 | + Name: "Updated Team", |
| 192 | + Slug: "t1", |
| 193 | + } |
| 194 | + |
| 195 | + if !cmp.Equal(got, want) { |
| 196 | + t.Errorf("Enterprise.UpdateTeam = %+v, want %+v", got, want) |
| 197 | + } |
| 198 | + |
| 199 | + const methodName = "UpdateTeam" |
| 200 | + testBadOptions(t, methodName, func() error { |
| 201 | + _, _, err := client.Enterprise.UpdateTeam(ctx, "\n", "t1", input) |
| 202 | + return err |
| 203 | + }) |
| 204 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 205 | + got, resp, err := client.Enterprise.UpdateTeam(ctx, "e", "t1", input) |
| 206 | + if got != nil { |
| 207 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 208 | + } |
| 209 | + return resp, err |
| 210 | + }) |
| 211 | +} |
| 212 | + |
| 213 | +func TestEnterpriseService_DeleteTeam(t *testing.T) { |
| 214 | + t.Parallel() |
| 215 | + client, mux, _ := setup(t) |
| 216 | + |
| 217 | + mux.HandleFunc("/enterprises/e/teams/t1", func(w http.ResponseWriter, r *http.Request) { |
| 218 | + testMethod(t, r, "DELETE") |
| 219 | + w.WriteHeader(http.StatusNoContent) |
| 220 | + }) |
| 221 | + |
| 222 | + ctx := t.Context() |
| 223 | + _, err := client.Enterprise.DeleteTeam(ctx, "e", "t1") |
| 224 | + if err != nil { |
| 225 | + t.Fatalf("Enterprise.DeleteTeam returned error: %v", err) |
| 226 | + } |
| 227 | + |
| 228 | + const methodName = "DeleteTeam" |
| 229 | + testBadOptions(t, methodName, func() error { |
| 230 | + _, err := client.Enterprise.DeleteTeam(ctx, "\n", "t1") |
| 231 | + return err |
| 232 | + }) |
| 233 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 234 | + return client.Enterprise.DeleteTeam(ctx, "e", "t1") |
| 235 | + }) |
| 236 | +} |
0 commit comments