github.com/google/go-github/v66@v66.0.0/github/activity_star_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 "fmt" 11 "net/http" 12 "strings" 13 "testing" 14 "time" 15 16 "github.com/google/go-cmp/cmp" 17 ) 18 19 func TestActivityService_ListStargazers(t *testing.T) { 20 t.Parallel() 21 client, mux, _ := setup(t) 22 23 mux.HandleFunc("/repos/o/r/stargazers", func(w http.ResponseWriter, r *http.Request) { 24 testMethod(t, r, "GET") 25 testHeader(t, r, "Accept", mediaTypeStarringPreview) 26 testFormValues(t, r, values{ 27 "page": "2", 28 }) 29 30 fmt.Fprint(w, `[{"starred_at":"2002-02-10T15:30:00Z","user":{"id":1}}]`) 31 }) 32 33 ctx := context.Background() 34 stargazers, _, err := client.Activity.ListStargazers(ctx, "o", "r", &ListOptions{Page: 2}) 35 if err != nil { 36 t.Errorf("Activity.ListStargazers returned error: %v", err) 37 } 38 39 want := []*Stargazer{{StarredAt: &Timestamp{time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)}, User: &User{ID: Int64(1)}}} 40 if !cmp.Equal(stargazers, want) { 41 t.Errorf("Activity.ListStargazers returned %+v, want %+v", stargazers, want) 42 } 43 44 const methodName = "ListStargazers" 45 testBadOptions(t, methodName, func() (err error) { 46 _, _, err = client.Activity.ListStargazers(ctx, "\n", "\n", &ListOptions{Page: 2}) 47 return err 48 }) 49 50 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 51 got, resp, err := client.Activity.ListStargazers(ctx, "o", "r", &ListOptions{Page: 2}) 52 if got != nil { 53 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 54 } 55 return resp, err 56 }) 57 } 58 59 func TestActivityService_ListStarred_authenticatedUser(t *testing.T) { 60 t.Parallel() 61 client, mux, _ := setup(t) 62 63 mux.HandleFunc("/user/starred", func(w http.ResponseWriter, r *http.Request) { 64 testMethod(t, r, "GET") 65 testHeader(t, r, "Accept", strings.Join([]string{mediaTypeStarringPreview, mediaTypeTopicsPreview}, ", ")) 66 fmt.Fprint(w, `[{"starred_at":"2002-02-10T15:30:00Z","repo":{"id":1}}]`) 67 }) 68 69 ctx := context.Background() 70 repos, _, err := client.Activity.ListStarred(ctx, "", nil) 71 if err != nil { 72 t.Errorf("Activity.ListStarred returned error: %v", err) 73 } 74 75 want := []*StarredRepository{{StarredAt: &Timestamp{time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)}, Repository: &Repository{ID: Int64(1)}}} 76 if !cmp.Equal(repos, want) { 77 t.Errorf("Activity.ListStarred returned %+v, want %+v", repos, want) 78 } 79 80 const methodName = "ListStarred" 81 testBadOptions(t, methodName, func() (err error) { 82 _, _, err = client.Activity.ListStarred(ctx, "\n", nil) 83 return err 84 }) 85 86 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 87 got, resp, err := client.Activity.ListStarred(ctx, "", 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 TestActivityService_ListStarred_specifiedUser(t *testing.T) { 96 t.Parallel() 97 client, mux, _ := setup(t) 98 99 mux.HandleFunc("/users/u/starred", func(w http.ResponseWriter, r *http.Request) { 100 testMethod(t, r, "GET") 101 testHeader(t, r, "Accept", strings.Join([]string{mediaTypeStarringPreview, mediaTypeTopicsPreview}, ", ")) 102 testFormValues(t, r, values{ 103 "sort": "created", 104 "direction": "asc", 105 "page": "2", 106 }) 107 fmt.Fprint(w, `[{"starred_at":"2002-02-10T15:30:00Z","repo":{"id":2}}]`) 108 }) 109 110 opt := &ActivityListStarredOptions{"created", "asc", ListOptions{Page: 2}} 111 ctx := context.Background() 112 repos, _, err := client.Activity.ListStarred(ctx, "u", opt) 113 if err != nil { 114 t.Errorf("Activity.ListStarred returned error: %v", err) 115 } 116 117 want := []*StarredRepository{{StarredAt: &Timestamp{time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)}, Repository: &Repository{ID: Int64(2)}}} 118 if !cmp.Equal(repos, want) { 119 t.Errorf("Activity.ListStarred returned %+v, want %+v", repos, want) 120 } 121 122 const methodName = "ListStarred" 123 testBadOptions(t, methodName, func() (err error) { 124 _, _, err = client.Activity.ListStarred(ctx, "\n", opt) 125 return err 126 }) 127 128 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 129 got, resp, err := client.Activity.ListStarred(ctx, "u", opt) 130 if got != nil { 131 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 132 } 133 return resp, err 134 }) 135 } 136 137 func TestActivityService_ListStarred_invalidUser(t *testing.T) { 138 t.Parallel() 139 client, _, _ := setup(t) 140 141 ctx := context.Background() 142 _, _, err := client.Activity.ListStarred(ctx, "%", nil) 143 testURLParseError(t, err) 144 } 145 146 func TestActivityService_IsStarred_hasStar(t *testing.T) { 147 t.Parallel() 148 client, mux, _ := setup(t) 149 150 mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) { 151 testMethod(t, r, "GET") 152 w.WriteHeader(http.StatusNoContent) 153 }) 154 155 ctx := context.Background() 156 star, _, err := client.Activity.IsStarred(ctx, "o", "r") 157 if err != nil { 158 t.Errorf("Activity.IsStarred returned error: %v", err) 159 } 160 if want := true; star != want { 161 t.Errorf("Activity.IsStarred returned %+v, want %+v", star, want) 162 } 163 164 const methodName = "IsStarred" 165 testBadOptions(t, methodName, func() (err error) { 166 _, _, err = client.Activity.IsStarred(ctx, "\n", "\n") 167 return err 168 }) 169 170 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 171 got, resp, err := client.Activity.IsStarred(ctx, "o", "r") 172 if got { 173 t.Errorf("testNewRequestAndDoFailure %v = %#v, want false", methodName, got) 174 } 175 return resp, err 176 }) 177 } 178 179 func TestActivityService_IsStarred_noStar(t *testing.T) { 180 t.Parallel() 181 client, mux, _ := setup(t) 182 183 mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) { 184 testMethod(t, r, "GET") 185 w.WriteHeader(http.StatusNotFound) 186 }) 187 188 ctx := context.Background() 189 star, _, err := client.Activity.IsStarred(ctx, "o", "r") 190 if err != nil { 191 t.Errorf("Activity.IsStarred returned error: %v", err) 192 } 193 if want := false; star != want { 194 t.Errorf("Activity.IsStarred returned %+v, want %+v", star, want) 195 } 196 197 const methodName = "IsStarred" 198 testBadOptions(t, methodName, func() (err error) { 199 _, _, err = client.Activity.IsStarred(ctx, "\n", "\n") 200 return err 201 }) 202 203 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 204 got, resp, err := client.Activity.IsStarred(ctx, "o", "r") 205 if got { 206 t.Errorf("testNewRequestAndDoFailure %v = %#v, want false", methodName, got) 207 } 208 return resp, err 209 }) 210 } 211 212 func TestActivityService_IsStarred_invalidID(t *testing.T) { 213 t.Parallel() 214 client, _, _ := setup(t) 215 216 ctx := context.Background() 217 _, _, err := client.Activity.IsStarred(ctx, "%", "%") 218 testURLParseError(t, err) 219 } 220 221 func TestActivityService_Star(t *testing.T) { 222 t.Parallel() 223 client, mux, _ := setup(t) 224 225 mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) { 226 testMethod(t, r, "PUT") 227 }) 228 229 ctx := context.Background() 230 _, err := client.Activity.Star(ctx, "o", "r") 231 if err != nil { 232 t.Errorf("Activity.Star returned error: %v", err) 233 } 234 235 const methodName = "Star" 236 testBadOptions(t, methodName, func() (err error) { 237 _, err = client.Activity.Star(ctx, "\n", "\n") 238 return err 239 }) 240 241 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 242 return client.Activity.Star(ctx, "o", "r") 243 }) 244 } 245 246 func TestActivityService_Star_invalidID(t *testing.T) { 247 t.Parallel() 248 client, _, _ := setup(t) 249 250 ctx := context.Background() 251 _, err := client.Activity.Star(ctx, "%", "%") 252 testURLParseError(t, err) 253 } 254 255 func TestActivityService_Unstar(t *testing.T) { 256 t.Parallel() 257 client, mux, _ := setup(t) 258 259 mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) { 260 testMethod(t, r, "DELETE") 261 }) 262 263 ctx := context.Background() 264 _, err := client.Activity.Unstar(ctx, "o", "r") 265 if err != nil { 266 t.Errorf("Activity.Unstar returned error: %v", err) 267 } 268 269 const methodName = "Unstar" 270 testBadOptions(t, methodName, func() (err error) { 271 _, err = client.Activity.Unstar(ctx, "\n", "\n") 272 return err 273 }) 274 275 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 276 return client.Activity.Unstar(ctx, "o", "r") 277 }) 278 } 279 280 func TestActivityService_Unstar_invalidID(t *testing.T) { 281 t.Parallel() 282 client, _, _ := setup(t) 283 284 ctx := context.Background() 285 _, err := client.Activity.Unstar(ctx, "%", "%") 286 testURLParseError(t, err) 287 } 288 289 func TestStarredRepository_Marshal(t *testing.T) { 290 t.Parallel() 291 testJSONMarshal(t, &StarredRepository{}, "{}") 292 293 u := &StarredRepository{ 294 StarredAt: &Timestamp{referenceTime}, 295 Repository: &Repository{ 296 ID: Int64(1), 297 URL: String("u"), 298 Name: String("n"), 299 }, 300 } 301 302 want := `{ 303 "starred_at": ` + referenceTimeStr + `, 304 "repo": { 305 "id": 1, 306 "url": "u", 307 "name": "n" 308 } 309 }` 310 311 testJSONMarshal(t, u, want) 312 } 313 314 func TestStargazer_Marshal(t *testing.T) { 315 t.Parallel() 316 testJSONMarshal(t, &Stargazer{}, "{}") 317 318 u := &Stargazer{ 319 StarredAt: &Timestamp{referenceTime}, 320 User: &User{ 321 Login: String("l"), 322 ID: Int64(1), 323 URL: String("u"), 324 AvatarURL: String("a"), 325 GravatarID: String("g"), 326 Name: String("n"), 327 Company: String("c"), 328 Blog: String("b"), 329 Location: String("l"), 330 Email: String("e"), 331 Hireable: Bool(true), 332 Bio: String("b"), 333 TwitterUsername: String("t"), 334 PublicRepos: Int(1), 335 Followers: Int(1), 336 Following: Int(1), 337 CreatedAt: &Timestamp{referenceTime}, 338 SuspendedAt: &Timestamp{referenceTime}, 339 }, 340 } 341 342 want := `{ 343 "starred_at": ` + referenceTimeStr + `, 344 "user": { 345 "login": "l", 346 "id": 1, 347 "avatar_url": "a", 348 "gravatar_id": "g", 349 "name": "n", 350 "company": "c", 351 "blog": "b", 352 "location": "l", 353 "email": "e", 354 "hireable": true, 355 "bio": "b", 356 "twitter_username": "t", 357 "public_repos": 1, 358 "followers": 1, 359 "following": 1, 360 "created_at": ` + referenceTimeStr + `, 361 "suspended_at": ` + referenceTimeStr + `, 362 "url": "u" 363 } 364 }` 365 366 testJSONMarshal(t, u, want) 367 }