github.com/google/go-github/v33@v33.0.0/github/teams_discussions_test.go (about) 1 // Copyright 2018 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 "reflect" 14 "testing" 15 "time" 16 ) 17 18 func TestTeamsService_ListDiscussionsByID(t *testing.T) { 19 client, mux, _, teardown := setup() 20 defer teardown() 21 22 mux.HandleFunc("/organizations/1/team/2/discussions", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 testFormValues(t, r, values{ 25 "direction": "desc", 26 "page": "2", 27 }) 28 fmt.Fprintf(w, 29 `[ 30 { 31 "author": { 32 "login": "author", 33 "id": 0, 34 "avatar_url": "https://avatars1.githubusercontent.com/u/0?v=4", 35 "gravatar_id": "", 36 "url": "https://api.github.com/users/author", 37 "html_url": "https://github.com/author", 38 "followers_url": "https://api.github.com/users/author/followers", 39 "following_url": "https://api.github.com/users/author/following{/other_user}", 40 "gists_url": "https://api.github.com/users/author/gists{/gist_id}", 41 "starred_url": "https://api.github.com/users/author/starred{/owner}{/repo}", 42 "subscriptions_url": "https://api.github.com/users/author/subscriptions", 43 "organizations_url": "https://api.github.com/users/author/orgs", 44 "repos_url": "https://api.github.com/users/author/repos", 45 "events_url": "https://api.github.com/users/author/events{/privacy}", 46 "received_events_url": "https://api.github.com/users/author/received_events", 47 "type": "User", 48 "site_admin": false 49 }, 50 "body": "test", 51 "body_html": "<p>test</p>", 52 "body_version": "version", 53 "comments_count": 1, 54 "comments_url": "https://api.github.com/teams/2/discussions/3/comments", 55 "created_at": "2018-01-01T00:00:00Z", 56 "last_edited_at": null, 57 "html_url": "https://github.com/orgs/1/teams/2/discussions/3", 58 "node_id": "node", 59 "number": 3, 60 "pinned": false, 61 "private": false, 62 "team_url": "https://api.github.com/teams/2", 63 "title": "test", 64 "updated_at": "2018-01-01T00:00:00Z", 65 "url": "https://api.github.com/teams/2/discussions/3" 66 } 67 ]`) 68 }) 69 discussions, _, err := client.Teams.ListDiscussionsByID(context.Background(), 1, 2, &DiscussionListOptions{"desc", ListOptions{Page: 2}}) 70 if err != nil { 71 t.Errorf("Teams.ListDiscussionsByID returned error: %v", err) 72 } 73 74 want := []*TeamDiscussion{ 75 { 76 Author: &User{ 77 Login: String("author"), 78 ID: Int64(0), 79 AvatarURL: String("https://avatars1.githubusercontent.com/u/0?v=4"), 80 GravatarID: String(""), 81 URL: String("https://api.github.com/users/author"), 82 HTMLURL: String("https://github.com/author"), 83 FollowersURL: String("https://api.github.com/users/author/followers"), 84 FollowingURL: String("https://api.github.com/users/author/following{/other_user}"), 85 GistsURL: String("https://api.github.com/users/author/gists{/gist_id}"), 86 StarredURL: String("https://api.github.com/users/author/starred{/owner}{/repo}"), 87 SubscriptionsURL: String("https://api.github.com/users/author/subscriptions"), 88 OrganizationsURL: String("https://api.github.com/users/author/orgs"), 89 ReposURL: String("https://api.github.com/users/author/repos"), 90 EventsURL: String("https://api.github.com/users/author/events{/privacy}"), 91 ReceivedEventsURL: String("https://api.github.com/users/author/received_events"), 92 Type: String("User"), 93 SiteAdmin: Bool(false), 94 }, 95 Body: String("test"), 96 BodyHTML: String("<p>test</p>"), 97 BodyVersion: String("version"), 98 CommentsCount: Int(1), 99 CommentsURL: String("https://api.github.com/teams/2/discussions/3/comments"), 100 CreatedAt: &Timestamp{time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)}, 101 LastEditedAt: nil, 102 HTMLURL: String("https://github.com/orgs/1/teams/2/discussions/3"), 103 NodeID: String("node"), 104 Number: Int(3), 105 Pinned: Bool(false), 106 Private: Bool(false), 107 TeamURL: String("https://api.github.com/teams/2"), 108 Title: String("test"), 109 UpdatedAt: &Timestamp{time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)}, 110 URL: String("https://api.github.com/teams/2/discussions/3"), 111 }, 112 } 113 if !reflect.DeepEqual(discussions, want) { 114 t.Errorf("Teams.ListDiscussionsByID returned %+v, want %+v", discussions, want) 115 } 116 } 117 118 func TestTeamsService_ListDiscussionsBySlug(t *testing.T) { 119 client, mux, _, teardown := setup() 120 defer teardown() 121 122 mux.HandleFunc("/orgs/o/teams/s/discussions", func(w http.ResponseWriter, r *http.Request) { 123 testMethod(t, r, "GET") 124 testFormValues(t, r, values{ 125 "direction": "desc", 126 "page": "2", 127 }) 128 fmt.Fprintf(w, 129 `[ 130 { 131 "author": { 132 "login": "author", 133 "id": 0, 134 "avatar_url": "https://avatars1.githubusercontent.com/u/0?v=4", 135 "gravatar_id": "", 136 "url": "https://api.github.com/users/author", 137 "html_url": "https://github.com/author", 138 "followers_url": "https://api.github.com/users/author/followers", 139 "following_url": "https://api.github.com/users/author/following{/other_user}", 140 "gists_url": "https://api.github.com/users/author/gists{/gist_id}", 141 "starred_url": "https://api.github.com/users/author/starred{/owner}{/repo}", 142 "subscriptions_url": "https://api.github.com/users/author/subscriptions", 143 "organizations_url": "https://api.github.com/users/author/orgs", 144 "repos_url": "https://api.github.com/users/author/repos", 145 "events_url": "https://api.github.com/users/author/events{/privacy}", 146 "received_events_url": "https://api.github.com/users/author/received_events", 147 "type": "User", 148 "site_admin": false 149 }, 150 "body": "test", 151 "body_html": "<p>test</p>", 152 "body_version": "version", 153 "comments_count": 1, 154 "comments_url": "https://api.github.com/teams/2/discussions/3/comments", 155 "created_at": "2018-01-01T00:00:00Z", 156 "last_edited_at": null, 157 "html_url": "https://github.com/orgs/1/teams/2/discussions/3", 158 "node_id": "node", 159 "number": 3, 160 "pinned": false, 161 "private": false, 162 "team_url": "https://api.github.com/teams/2", 163 "title": "test", 164 "updated_at": "2018-01-01T00:00:00Z", 165 "url": "https://api.github.com/teams/2/discussions/3" 166 } 167 ]`) 168 }) 169 discussions, _, err := client.Teams.ListDiscussionsBySlug(context.Background(), "o", "s", &DiscussionListOptions{"desc", ListOptions{Page: 2}}) 170 if err != nil { 171 t.Errorf("Teams.ListDiscussionsBySlug returned error: %v", err) 172 } 173 174 want := []*TeamDiscussion{ 175 { 176 Author: &User{ 177 Login: String("author"), 178 ID: Int64(0), 179 AvatarURL: String("https://avatars1.githubusercontent.com/u/0?v=4"), 180 GravatarID: String(""), 181 URL: String("https://api.github.com/users/author"), 182 HTMLURL: String("https://github.com/author"), 183 FollowersURL: String("https://api.github.com/users/author/followers"), 184 FollowingURL: String("https://api.github.com/users/author/following{/other_user}"), 185 GistsURL: String("https://api.github.com/users/author/gists{/gist_id}"), 186 StarredURL: String("https://api.github.com/users/author/starred{/owner}{/repo}"), 187 SubscriptionsURL: String("https://api.github.com/users/author/subscriptions"), 188 OrganizationsURL: String("https://api.github.com/users/author/orgs"), 189 ReposURL: String("https://api.github.com/users/author/repos"), 190 EventsURL: String("https://api.github.com/users/author/events{/privacy}"), 191 ReceivedEventsURL: String("https://api.github.com/users/author/received_events"), 192 Type: String("User"), 193 SiteAdmin: Bool(false), 194 }, 195 Body: String("test"), 196 BodyHTML: String("<p>test</p>"), 197 BodyVersion: String("version"), 198 CommentsCount: Int(1), 199 CommentsURL: String("https://api.github.com/teams/2/discussions/3/comments"), 200 CreatedAt: &Timestamp{time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)}, 201 LastEditedAt: nil, 202 HTMLURL: String("https://github.com/orgs/1/teams/2/discussions/3"), 203 NodeID: String("node"), 204 Number: Int(3), 205 Pinned: Bool(false), 206 Private: Bool(false), 207 TeamURL: String("https://api.github.com/teams/2"), 208 Title: String("test"), 209 UpdatedAt: &Timestamp{time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)}, 210 URL: String("https://api.github.com/teams/2/discussions/3"), 211 }, 212 } 213 if !reflect.DeepEqual(discussions, want) { 214 t.Errorf("Teams.ListDiscussionsBySlug returned %+v, want %+v", discussions, want) 215 } 216 } 217 218 func TestTeamsService_GetDiscussionByID(t *testing.T) { 219 client, mux, _, teardown := setup() 220 defer teardown() 221 222 mux.HandleFunc("/organizations/1/team/2/discussions/3", func(w http.ResponseWriter, r *http.Request) { 223 testMethod(t, r, "GET") 224 fmt.Fprint(w, `{"number":3}`) 225 }) 226 227 discussion, _, err := client.Teams.GetDiscussionByID(context.Background(), 1, 2, 3) 228 if err != nil { 229 t.Errorf("Teams.GetDiscussionByID returned error: %v", err) 230 } 231 232 want := &TeamDiscussion{Number: Int(3)} 233 if !reflect.DeepEqual(discussion, want) { 234 t.Errorf("Teams.GetDiscussionByID returned %+v, want %+v", discussion, want) 235 } 236 } 237 238 func TestTeamsService_GetDiscussionBySlug(t *testing.T) { 239 client, mux, _, teardown := setup() 240 defer teardown() 241 242 mux.HandleFunc("/orgs/o/teams/s/discussions/3", func(w http.ResponseWriter, r *http.Request) { 243 testMethod(t, r, "GET") 244 fmt.Fprint(w, `{"number":3}`) 245 }) 246 247 discussion, _, err := client.Teams.GetDiscussionBySlug(context.Background(), "o", "s", 3) 248 if err != nil { 249 t.Errorf("Teams.GetDiscussionBySlug returned error: %v", err) 250 } 251 252 want := &TeamDiscussion{Number: Int(3)} 253 if !reflect.DeepEqual(discussion, want) { 254 t.Errorf("Teams.GetDiscussionBySlug returned %+v, want %+v", discussion, want) 255 } 256 } 257 258 func TestTeamsService_CreateDiscussionByID(t *testing.T) { 259 client, mux, _, teardown := setup() 260 defer teardown() 261 262 input := TeamDiscussion{Title: String("c_t"), Body: String("c_b")} 263 264 mux.HandleFunc("/organizations/1/team/2/discussions", func(w http.ResponseWriter, r *http.Request) { 265 v := new(TeamDiscussion) 266 json.NewDecoder(r.Body).Decode(v) 267 268 testMethod(t, r, "POST") 269 if !reflect.DeepEqual(v, &input) { 270 t.Errorf("Request body = %+v, want %+v", v, input) 271 } 272 273 fmt.Fprint(w, `{"number":3}`) 274 }) 275 276 comment, _, err := client.Teams.CreateDiscussionByID(context.Background(), 1, 2, input) 277 if err != nil { 278 t.Errorf("Teams.CreateDiscussionByID returned error: %v", err) 279 } 280 281 want := &TeamDiscussion{Number: Int(3)} 282 if !reflect.DeepEqual(comment, want) { 283 t.Errorf("Teams.CreateDiscussionByID returned %+v, want %+v", comment, want) 284 } 285 } 286 287 func TestTeamsService_CreateDiscussionBySlug(t *testing.T) { 288 client, mux, _, teardown := setup() 289 defer teardown() 290 291 input := TeamDiscussion{Title: String("c_t"), Body: String("c_b")} 292 293 mux.HandleFunc("/orgs/o/teams/s/discussions", func(w http.ResponseWriter, r *http.Request) { 294 v := new(TeamDiscussion) 295 json.NewDecoder(r.Body).Decode(v) 296 297 testMethod(t, r, "POST") 298 if !reflect.DeepEqual(v, &input) { 299 t.Errorf("Request body = %+v, want %+v", v, input) 300 } 301 302 fmt.Fprint(w, `{"number":3}`) 303 }) 304 305 comment, _, err := client.Teams.CreateDiscussionBySlug(context.Background(), "o", "s", input) 306 if err != nil { 307 t.Errorf("Teams.CreateDiscussionBySlug returned error: %v", err) 308 } 309 310 want := &TeamDiscussion{Number: Int(3)} 311 if !reflect.DeepEqual(comment, want) { 312 t.Errorf("Teams.CreateDiscussionBySlug returned %+v, want %+v", comment, want) 313 } 314 } 315 316 func TestTeamsService_EditDiscussionByID(t *testing.T) { 317 client, mux, _, teardown := setup() 318 defer teardown() 319 320 input := TeamDiscussion{Title: String("e_t"), Body: String("e_b")} 321 322 mux.HandleFunc("/organizations/1/team/2/discussions/3", func(w http.ResponseWriter, r *http.Request) { 323 v := new(TeamDiscussion) 324 json.NewDecoder(r.Body).Decode(v) 325 326 testMethod(t, r, "PATCH") 327 if !reflect.DeepEqual(v, &input) { 328 t.Errorf("Request body = %+v, want %+v", v, input) 329 } 330 331 fmt.Fprint(w, `{"number":3}`) 332 }) 333 334 comment, _, err := client.Teams.EditDiscussionByID(context.Background(), 1, 2, 3, input) 335 if err != nil { 336 t.Errorf("Teams.EditDiscussionByID returned error: %v", err) 337 } 338 339 want := &TeamDiscussion{Number: Int(3)} 340 if !reflect.DeepEqual(comment, want) { 341 t.Errorf("Teams.EditDiscussionByID returned %+v, want %+v", comment, want) 342 } 343 } 344 345 func TestTeamsService_EditDiscussionBySlug(t *testing.T) { 346 client, mux, _, teardown := setup() 347 defer teardown() 348 349 input := TeamDiscussion{Title: String("e_t"), Body: String("e_b")} 350 351 mux.HandleFunc("/orgs/o/teams/s/discussions/3", func(w http.ResponseWriter, r *http.Request) { 352 v := new(TeamDiscussion) 353 json.NewDecoder(r.Body).Decode(v) 354 355 testMethod(t, r, "PATCH") 356 if !reflect.DeepEqual(v, &input) { 357 t.Errorf("Request body = %+v, want %+v", v, input) 358 } 359 360 fmt.Fprint(w, `{"number":3}`) 361 }) 362 363 comment, _, err := client.Teams.EditDiscussionBySlug(context.Background(), "o", "s", 3, input) 364 if err != nil { 365 t.Errorf("Teams.EditDiscussionBySlug returned error: %v", err) 366 } 367 368 want := &TeamDiscussion{Number: Int(3)} 369 if !reflect.DeepEqual(comment, want) { 370 t.Errorf("Teams.EditDiscussionBySlug returned %+v, want %+v", comment, want) 371 } 372 } 373 374 func TestTeamsService_DeleteDiscussionByID(t *testing.T) { 375 client, mux, _, teardown := setup() 376 defer teardown() 377 378 mux.HandleFunc("/organizations/1/team/2/discussions/3", func(w http.ResponseWriter, r *http.Request) { 379 testMethod(t, r, "DELETE") 380 }) 381 382 _, err := client.Teams.DeleteDiscussionByID(context.Background(), 1, 2, 3) 383 if err != nil { 384 t.Errorf("Teams.DeleteDiscussionByID returned error: %v", err) 385 } 386 } 387 388 func TestTeamsService_DeleteDiscussionBySlug(t *testing.T) { 389 client, mux, _, teardown := setup() 390 defer teardown() 391 392 mux.HandleFunc("/orgs/o/teams/s/discussions/3", func(w http.ResponseWriter, r *http.Request) { 393 testMethod(t, r, "DELETE") 394 }) 395 396 _, err := client.Teams.DeleteDiscussionBySlug(context.Background(), "o", "s", 3) 397 if err != nil { 398 t.Errorf("Teams.DeleteDiscussionBySlug returned error: %v", err) 399 } 400 }