github.com/google/go-github/v42@v42.0.0/github/users_test.go (about) 1 // Copyright 2013 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 TestUser_Marshal(t *testing.T) { 19 testJSONMarshal(t, &User{}, "{}") 20 21 u := &User{ 22 Login: String("l"), 23 ID: Int64(1), 24 URL: String("u"), 25 AvatarURL: String("a"), 26 GravatarID: String("g"), 27 Name: String("n"), 28 Company: String("c"), 29 Blog: String("b"), 30 Location: String("l"), 31 Email: String("e"), 32 Hireable: Bool(true), 33 Bio: String("b"), 34 TwitterUsername: String("t"), 35 PublicRepos: Int(1), 36 Followers: Int(1), 37 Following: Int(1), 38 CreatedAt: &Timestamp{referenceTime}, 39 SuspendedAt: &Timestamp{referenceTime}, 40 } 41 want := `{ 42 "login": "l", 43 "id": 1, 44 "avatar_url": "a", 45 "gravatar_id": "g", 46 "name": "n", 47 "company": "c", 48 "blog": "b", 49 "location": "l", 50 "email": "e", 51 "hireable": true, 52 "bio": "b", 53 "twitter_username": "t", 54 "public_repos": 1, 55 "followers": 1, 56 "following": 1, 57 "created_at": ` + referenceTimeStr + `, 58 "suspended_at": ` + referenceTimeStr + `, 59 "url": "u" 60 }` 61 testJSONMarshal(t, u, want) 62 } 63 64 func TestUsersService_Get_authenticatedUser(t *testing.T) { 65 client, mux, _, teardown := setup() 66 defer teardown() 67 68 mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) { 69 testMethod(t, r, "GET") 70 fmt.Fprint(w, `{"id":1}`) 71 }) 72 73 ctx := context.Background() 74 user, _, err := client.Users.Get(ctx, "") 75 if err != nil { 76 t.Errorf("Users.Get returned error: %v", err) 77 } 78 79 want := &User{ID: Int64(1)} 80 if !cmp.Equal(user, want) { 81 t.Errorf("Users.Get returned %+v, want %+v", user, want) 82 } 83 84 const methodName = "Get" 85 testBadOptions(t, methodName, func() (err error) { 86 _, _, err = client.Users.Get(ctx, "\n") 87 return err 88 }) 89 90 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 91 got, resp, err := client.Users.Get(ctx, "") 92 if got != nil { 93 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 94 } 95 return resp, err 96 }) 97 } 98 99 func TestUsersService_Get_specifiedUser(t *testing.T) { 100 client, mux, _, teardown := setup() 101 defer teardown() 102 103 mux.HandleFunc("/users/u", func(w http.ResponseWriter, r *http.Request) { 104 testMethod(t, r, "GET") 105 fmt.Fprint(w, `{"id":1}`) 106 }) 107 108 ctx := context.Background() 109 user, _, err := client.Users.Get(ctx, "u") 110 if err != nil { 111 t.Errorf("Users.Get returned error: %v", err) 112 } 113 114 want := &User{ID: Int64(1)} 115 if !cmp.Equal(user, want) { 116 t.Errorf("Users.Get returned %+v, want %+v", user, want) 117 } 118 } 119 120 func TestUsersService_Get_invalidUser(t *testing.T) { 121 client, _, _, teardown := setup() 122 defer teardown() 123 124 ctx := context.Background() 125 _, _, err := client.Users.Get(ctx, "%") 126 testURLParseError(t, err) 127 } 128 129 func TestUsersService_GetByID(t *testing.T) { 130 client, mux, _, teardown := setup() 131 defer teardown() 132 133 mux.HandleFunc("/user/1", func(w http.ResponseWriter, r *http.Request) { 134 testMethod(t, r, "GET") 135 fmt.Fprint(w, `{"id":1}`) 136 }) 137 138 ctx := context.Background() 139 user, _, err := client.Users.GetByID(ctx, 1) 140 if err != nil { 141 t.Fatalf("Users.GetByID returned error: %v", err) 142 } 143 144 want := &User{ID: Int64(1)} 145 if !cmp.Equal(user, want) { 146 t.Errorf("Users.GetByID returned %+v, want %+v", user, want) 147 } 148 149 const methodName = "GetByID" 150 testBadOptions(t, methodName, func() (err error) { 151 _, _, err = client.Users.GetByID(ctx, -1) 152 return err 153 }) 154 155 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 156 got, resp, err := client.Users.GetByID(ctx, 1) 157 if got != nil { 158 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 159 } 160 return resp, err 161 }) 162 } 163 164 func TestUsersService_Edit(t *testing.T) { 165 client, mux, _, teardown := setup() 166 defer teardown() 167 168 input := &User{Name: String("n")} 169 170 mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) { 171 v := new(User) 172 json.NewDecoder(r.Body).Decode(v) 173 174 testMethod(t, r, "PATCH") 175 if !cmp.Equal(v, input) { 176 t.Errorf("Request body = %+v, want %+v", v, input) 177 } 178 179 fmt.Fprint(w, `{"id":1}`) 180 }) 181 182 ctx := context.Background() 183 user, _, err := client.Users.Edit(ctx, input) 184 if err != nil { 185 t.Errorf("Users.Edit returned error: %v", err) 186 } 187 188 want := &User{ID: Int64(1)} 189 if !cmp.Equal(user, want) { 190 t.Errorf("Users.Edit returned %+v, want %+v", user, want) 191 } 192 193 const methodName = "Edit" 194 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 195 got, resp, err := client.Users.Edit(ctx, input) 196 if got != nil { 197 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 198 } 199 return resp, err 200 }) 201 } 202 203 func TestUsersService_GetHovercard(t *testing.T) { 204 client, mux, _, teardown := setup() 205 defer teardown() 206 207 mux.HandleFunc("/users/u/hovercard", func(w http.ResponseWriter, r *http.Request) { 208 testMethod(t, r, "GET") 209 testFormValues(t, r, values{"subject_type": "repository", "subject_id": "20180408"}) 210 fmt.Fprint(w, `{"contexts": [{"message":"Owns this repository", "octicon": "repo"}]}`) 211 }) 212 213 opt := &HovercardOptions{SubjectType: "repository", SubjectID: "20180408"} 214 ctx := context.Background() 215 hovercard, _, err := client.Users.GetHovercard(ctx, "u", opt) 216 if err != nil { 217 t.Errorf("Users.GetHovercard returned error: %v", err) 218 } 219 220 want := &Hovercard{Contexts: []*UserContext{{Message: String("Owns this repository"), Octicon: String("repo")}}} 221 if !cmp.Equal(hovercard, want) { 222 t.Errorf("Users.GetHovercard returned %+v, want %+v", hovercard, want) 223 } 224 225 const methodName = "GetHovercard" 226 testBadOptions(t, methodName, func() (err error) { 227 _, _, err = client.Users.GetHovercard(ctx, "\n", opt) 228 return err 229 }) 230 231 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 232 got, resp, err := client.Users.GetHovercard(ctx, "u", opt) 233 if got != nil { 234 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 235 } 236 return resp, err 237 }) 238 } 239 240 func TestUsersService_ListAll(t *testing.T) { 241 client, mux, _, teardown := setup() 242 defer teardown() 243 244 mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { 245 testMethod(t, r, "GET") 246 testFormValues(t, r, values{"since": "1", "page": "2"}) 247 fmt.Fprint(w, `[{"id":2}]`) 248 }) 249 250 opt := &UserListOptions{1, ListOptions{Page: 2}} 251 ctx := context.Background() 252 users, _, err := client.Users.ListAll(ctx, opt) 253 if err != nil { 254 t.Errorf("Users.Get returned error: %v", err) 255 } 256 257 want := []*User{{ID: Int64(2)}} 258 if !cmp.Equal(users, want) { 259 t.Errorf("Users.ListAll returned %+v, want %+v", users, want) 260 } 261 262 const methodName = "ListAll" 263 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 264 got, resp, err := client.Users.ListAll(ctx, opt) 265 if got != nil { 266 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 267 } 268 return resp, err 269 }) 270 } 271 272 func TestUsersService_ListInvitations(t *testing.T) { 273 client, mux, _, teardown := setup() 274 defer teardown() 275 276 mux.HandleFunc("/user/repository_invitations", func(w http.ResponseWriter, r *http.Request) { 277 testMethod(t, r, "GET") 278 fmt.Fprintf(w, `[{"id":1}, {"id":2}]`) 279 }) 280 281 ctx := context.Background() 282 got, _, err := client.Users.ListInvitations(ctx, nil) 283 if err != nil { 284 t.Errorf("Users.ListInvitations returned error: %v", err) 285 } 286 287 want := []*RepositoryInvitation{{ID: Int64(1)}, {ID: Int64(2)}} 288 if !cmp.Equal(got, want) { 289 t.Errorf("Users.ListInvitations = %+v, want %+v", got, want) 290 } 291 292 const methodName = "ListInvitations" 293 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 294 got, resp, err := client.Users.ListInvitations(ctx, nil) 295 if got != nil { 296 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 297 } 298 return resp, err 299 }) 300 } 301 302 func TestUsersService_ListInvitations_withOptions(t *testing.T) { 303 client, mux, _, teardown := setup() 304 defer teardown() 305 306 mux.HandleFunc("/user/repository_invitations", func(w http.ResponseWriter, r *http.Request) { 307 testMethod(t, r, "GET") 308 testFormValues(t, r, values{ 309 "page": "2", 310 }) 311 fmt.Fprintf(w, `[{"id":1}, {"id":2}]`) 312 }) 313 314 ctx := context.Background() 315 _, _, err := client.Users.ListInvitations(ctx, &ListOptions{Page: 2}) 316 if err != nil { 317 t.Errorf("Users.ListInvitations returned error: %v", err) 318 } 319 } 320 321 func TestUsersService_AcceptInvitation(t *testing.T) { 322 client, mux, _, teardown := setup() 323 defer teardown() 324 325 mux.HandleFunc("/user/repository_invitations/1", func(w http.ResponseWriter, r *http.Request) { 326 testMethod(t, r, "PATCH") 327 w.WriteHeader(http.StatusNoContent) 328 }) 329 330 ctx := context.Background() 331 if _, err := client.Users.AcceptInvitation(ctx, 1); err != nil { 332 t.Errorf("Users.AcceptInvitation returned error: %v", err) 333 } 334 335 const methodName = "AcceptInvitation" 336 testBadOptions(t, methodName, func() (err error) { 337 _, err = client.Users.AcceptInvitation(ctx, -1) 338 return err 339 }) 340 341 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 342 return client.Users.AcceptInvitation(ctx, 1) 343 }) 344 } 345 346 func TestUsersService_DeclineInvitation(t *testing.T) { 347 client, mux, _, teardown := setup() 348 defer teardown() 349 350 mux.HandleFunc("/user/repository_invitations/1", func(w http.ResponseWriter, r *http.Request) { 351 testMethod(t, r, "DELETE") 352 w.WriteHeader(http.StatusNoContent) 353 }) 354 355 ctx := context.Background() 356 if _, err := client.Users.DeclineInvitation(ctx, 1); err != nil { 357 t.Errorf("Users.DeclineInvitation returned error: %v", err) 358 } 359 360 const methodName = "DeclineInvitation" 361 testBadOptions(t, methodName, func() (err error) { 362 _, err = client.Users.DeclineInvitation(ctx, -1) 363 return err 364 }) 365 366 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 367 return client.Users.DeclineInvitation(ctx, 1) 368 }) 369 } 370 371 func TestUserContext_Marshal(t *testing.T) { 372 testJSONMarshal(t, &UserContext{}, "{}") 373 374 u := &UserContext{ 375 Message: String("message"), 376 Octicon: String("message"), 377 } 378 379 want := `{ 380 "message" : "message", 381 "octicon" : "message" 382 }` 383 384 testJSONMarshal(t, u, want) 385 } 386 387 func TestHovercard_Marshal(t *testing.T) { 388 testJSONMarshal(t, &Hovercard{}, "{}") 389 390 h := &Hovercard{ 391 Contexts: []*UserContext{ 392 { 393 Message: String("someMessage"), 394 Octicon: String("someOcticon"), 395 }, 396 }, 397 } 398 399 want := `{ 400 "contexts" : [ 401 { 402 "message" : "someMessage", 403 "octicon" : "someOcticon" 404 } 405 ] 406 }` 407 408 testJSONMarshal(t, h, want) 409 } 410 411 func TestUserListOptions_Marshal(t *testing.T) { 412 testJSONMarshal(t, &UserListOptions{}, "{}") 413 414 u := &UserListOptions{ 415 Since: int64(1900), 416 ListOptions: ListOptions{ 417 Page: int(1), 418 PerPage: int(10), 419 }, 420 } 421 422 want := `{ 423 "since" : 1900, 424 "page": 1, 425 "perPage": 10 426 }` 427 428 testJSONMarshal(t, u, want) 429 }