| 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 TestRepositoriesService_ListComments(t *testing.T) { |
| 19 |
client, mux, _, teardown := setup() |
| 20 |
defer teardown() |
| 21 |
|
| 22 |
mux.HandleFunc("/repos/o/r/comments", func(w http.ResponseWriter, r *http.Request) { |
| 23 |
testMethod(t, r, "GET") |
| 24 |
testHeader(t, r, "Accept", mediaTypeReactionsPreview) |
| 25 |
testFormValues(t, r, values{"page": "2"}) |
| 26 |
fmt.Fprint(w, `[{"id":1}, {"id":2}]`) |
| 27 |
}) |
| 28 |
|
| 29 |
opt := &ListOptions{Page: 2} |
| 30 |
ctx := context.Background() |
| 31 |
comments, _, err := client.Repositories.ListComments(ctx, "o", "r", opt) |
| 32 |
if err != nil { |
| 33 |
t.Errorf("Repositories.ListComments returned error: %v", err) |
| 34 |
} |
| 35 |
|
| 36 |
want := []*RepositoryComment{{ID: Int64(1)}, {ID: Int64(2)}} |
| 37 |
if !cmp.Equal(comments, want) { |
| 38 |
t.Errorf("Repositories.ListComments returned %+v, want %+v", comments, want) |
| 39 |
} |
| 40 |
|
| 41 |
const methodName = "ListComments" |
| 42 |
testBadOptions(t, methodName, func() (err error) { |
| 43 |
_, _, err = client.Repositories.ListComments(ctx, "\n", "\n", opt) |
| 44 |
return err |
| 45 |
}) |
| 46 |
|
| 47 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 48 |
got, resp, err := client.Repositories.ListComments(ctx, "o", "r", opt) |
| 49 |
if got != nil { |
| 50 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 51 |
} |
| 52 |
return resp, err |
| 53 |
}) |
| 54 |
} |
| 55 |
|
| 56 |
func TestRepositoriesService_ListComments_invalidOwner(t *testing.T) { |
| 57 |
client, _, _, teardown := setup() |
| 58 |
defer teardown() |
| 59 |
|
| 60 |
ctx := context.Background() |
| 61 |
_, _, err := client.Repositories.ListComments(ctx, "%", "%", nil) |
| 62 |
testURLParseError(t, err) |
| 63 |
} |
| 64 |
|
| 65 |
func TestRepositoriesService_ListCommitComments(t *testing.T) { |
| 66 |
client, mux, _, teardown := setup() |
| 67 |
defer teardown() |
| 68 |
|
| 69 |
mux.HandleFunc("/repos/o/r/commits/s/comments", func(w http.ResponseWriter, r *http.Request) { |
| 70 |
testMethod(t, r, "GET") |
| 71 |
testHeader(t, r, "Accept", mediaTypeReactionsPreview) |
| 72 |
testFormValues(t, r, values{"page": "2"}) |
| 73 |
fmt.Fprint(w, `[{"id":1}, {"id":2}]`) |
| 74 |
}) |
| 75 |
|
| 76 |
opt := &ListOptions{Page: 2} |
| 77 |
ctx := context.Background() |
| 78 |
comments, _, err := client.Repositories.ListCommitComments(ctx, "o", "r", "s", opt) |
| 79 |
if err != nil { |
| 80 |
t.Errorf("Repositories.ListCommitComments returned error: %v", err) |
| 81 |
} |
| 82 |
|
| 83 |
want := []*RepositoryComment{{ID: Int64(1)}, {ID: Int64(2)}} |
| 84 |
if !cmp.Equal(comments, want) { |
| 85 |
t.Errorf("Repositories.ListCommitComments returned %+v, want %+v", comments, want) |
| 86 |
} |
| 87 |
|
| 88 |
const methodName = "ListCommitComments" |
| 89 |
testBadOptions(t, methodName, func() (err error) { |
| 90 |
_, _, err = client.Repositories.ListCommitComments(ctx, "\n", "\n", "\n", opt) |
| 91 |
return err |
| 92 |
}) |
| 93 |
|
| 94 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 95 |
got, resp, err := client.Repositories.ListCommitComments(ctx, "o", "r", "s", opt) |
| 96 |
if got != nil { |
| 97 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 98 |
} |
| 99 |
return resp, err |
| 100 |
}) |
| 101 |
} |
| 102 |
|
| 103 |
func TestRepositoriesService_ListCommitComments_invalidOwner(t *testing.T) { |
| 104 |
client, _, _, teardown := setup() |
| 105 |
defer teardown() |
| 106 |
|
| 107 |
ctx := context.Background() |
| 108 |
_, _, err := client.Repositories.ListCommitComments(ctx, "%", "%", "%", nil) |
| 109 |
testURLParseError(t, err) |
| 110 |
} |
| 111 |
|
| 112 |
func TestRepositoriesService_CreateComment(t *testing.T) { |
| 113 |
client, mux, _, teardown := setup() |
| 114 |
defer teardown() |
| 115 |
|
| 116 |
input := &RepositoryComment{Body: String("b")} |
| 117 |
|
| 118 |
mux.HandleFunc("/repos/o/r/commits/s/comments", func(w http.ResponseWriter, r *http.Request) { |
| 119 |
v := new(RepositoryComment) |
| 120 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 121 |
|
| 122 |
testMethod(t, r, "POST") |
| 123 |
if !cmp.Equal(v, input) { |
| 124 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 125 |
} |
| 126 |
|
| 127 |
fmt.Fprint(w, `{"id":1}`) |
| 128 |
}) |
| 129 |
|
| 130 |
ctx := context.Background() |
| 131 |
comment, _, err := client.Repositories.CreateComment(ctx, "o", "r", "s", input) |
| 132 |
if err != nil { |
| 133 |
t.Errorf("Repositories.CreateComment returned error: %v", err) |
| 134 |
} |
| 135 |
|
| 136 |
want := &RepositoryComment{ID: Int64(1)} |
| 137 |
if !cmp.Equal(comment, want) { |
| 138 |
t.Errorf("Repositories.CreateComment returned %+v, want %+v", comment, want) |
| 139 |
} |
| 140 |
|
| 141 |
const methodName = "CreateComment" |
| 142 |
testBadOptions(t, methodName, func() (err error) { |
| 143 |
_, _, err = client.Repositories.CreateComment(ctx, "\n", "\n", "\n", input) |
| 144 |
return err |
| 145 |
}) |
| 146 |
|
| 147 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 148 |
got, resp, err := client.Repositories.CreateComment(ctx, "o", "r", "s", input) |
| 149 |
if got != nil { |
| 150 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 151 |
} |
| 152 |
return resp, err |
| 153 |
}) |
| 154 |
} |
| 155 |
|
| 156 |
func TestRepositoriesService_CreateComment_invalidOwner(t *testing.T) { |
| 157 |
client, _, _, teardown := setup() |
| 158 |
defer teardown() |
| 159 |
|
| 160 |
ctx := context.Background() |
| 161 |
_, _, err := client.Repositories.CreateComment(ctx, "%", "%", "%", nil) |
| 162 |
testURLParseError(t, err) |
| 163 |
} |
| 164 |
|
| 165 |
func TestRepositoriesService_GetComment(t *testing.T) { |
| 166 |
client, mux, _, teardown := setup() |
| 167 |
defer teardown() |
| 168 |
|
| 169 |
mux.HandleFunc("/repos/o/r/comments/1", func(w http.ResponseWriter, r *http.Request) { |
| 170 |
testMethod(t, r, "GET") |
| 171 |
testHeader(t, r, "Accept", mediaTypeReactionsPreview) |
| 172 |
fmt.Fprint(w, `{"id":1}`) |
| 173 |
}) |
| 174 |
|
| 175 |
ctx := context.Background() |
| 176 |
comment, _, err := client.Repositories.GetComment(ctx, "o", "r", 1) |
| 177 |
if err != nil { |
| 178 |
t.Errorf("Repositories.GetComment returned error: %v", err) |
| 179 |
} |
| 180 |
|
| 181 |
want := &RepositoryComment{ID: Int64(1)} |
| 182 |
if !cmp.Equal(comment, want) { |
| 183 |
t.Errorf("Repositories.GetComment returned %+v, want %+v", comment, want) |
| 184 |
} |
| 185 |
|
| 186 |
const methodName = "GetComment" |
| 187 |
testBadOptions(t, methodName, func() (err error) { |
| 188 |
_, _, err = client.Repositories.GetComment(ctx, "\n", "\n", -1) |
| 189 |
return err |
| 190 |
}) |
| 191 |
|
| 192 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 193 |
got, resp, err := client.Repositories.GetComment(ctx, "o", "r", 1) |
| 194 |
if got != nil { |
| 195 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 196 |
} |
| 197 |
return resp, err |
| 198 |
}) |
| 199 |
} |
| 200 |
|
| 201 |
func TestRepositoriesService_GetComment_invalidOwner(t *testing.T) { |
| 202 |
client, _, _, teardown := setup() |
| 203 |
defer teardown() |
| 204 |
|
| 205 |
ctx := context.Background() |
| 206 |
_, _, err := client.Repositories.GetComment(ctx, "%", "%", 1) |
| 207 |
testURLParseError(t, err) |
| 208 |
} |
| 209 |
|
| 210 |
func TestRepositoriesService_UpdateComment(t *testing.T) { |
| 211 |
client, mux, _, teardown := setup() |
| 212 |
defer teardown() |
| 213 |
|
| 214 |
input := &RepositoryComment{Body: String("b")} |
| 215 |
|
| 216 |
mux.HandleFunc("/repos/o/r/comments/1", func(w http.ResponseWriter, r *http.Request) { |
| 217 |
v := new(RepositoryComment) |
| 218 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 219 |
|
| 220 |
testMethod(t, r, "PATCH") |
| 221 |
if !cmp.Equal(v, input) { |
| 222 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 223 |
} |
| 224 |
|
| 225 |
fmt.Fprint(w, `{"id":1}`) |
| 226 |
}) |
| 227 |
|
| 228 |
ctx := context.Background() |
| 229 |
comment, _, err := client.Repositories.UpdateComment(ctx, "o", "r", 1, input) |
| 230 |
if err != nil { |
| 231 |
t.Errorf("Repositories.UpdateComment returned error: %v", err) |
| 232 |
} |
| 233 |
|
| 234 |
want := &RepositoryComment{ID: Int64(1)} |
| 235 |
if !cmp.Equal(comment, want) { |
| 236 |
t.Errorf("Repositories.UpdateComment returned %+v, want %+v", comment, want) |
| 237 |
} |
| 238 |
|
| 239 |
const methodName = "UpdateComment" |
| 240 |
testBadOptions(t, methodName, func() (err error) { |
| 241 |
_, _, err = client.Repositories.UpdateComment(ctx, "\n", "\n", -1, input) |
| 242 |
return err |
| 243 |
}) |
| 244 |
|
| 245 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 246 |
got, resp, err := client.Repositories.UpdateComment(ctx, "o", "r", 1, input) |
| 247 |
if got != nil { |
| 248 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 249 |
} |
| 250 |
return resp, err |
| 251 |
}) |
| 252 |
} |
| 253 |
|
| 254 |
func TestRepositoriesService_UpdateComment_invalidOwner(t *testing.T) { |
| 255 |
client, _, _, teardown := setup() |
| 256 |
defer teardown() |
| 257 |
|
| 258 |
ctx := context.Background() |
| 259 |
_, _, err := client.Repositories.UpdateComment(ctx, "%", "%", 1, nil) |
| 260 |
testURLParseError(t, err) |
| 261 |
} |
| 262 |
|
| 263 |
func TestRepositoriesService_DeleteComment(t *testing.T) { |
| 264 |
client, mux, _, teardown := setup() |
| 265 |
defer teardown() |
| 266 |
|
| 267 |
mux.HandleFunc("/repos/o/r/comments/1", func(w http.ResponseWriter, r *http.Request) { |
| 268 |
testMethod(t, r, "DELETE") |
| 269 |
}) |
| 270 |
|
| 271 |
ctx := context.Background() |
| 272 |
_, err := client.Repositories.DeleteComment(ctx, "o", "r", 1) |
| 273 |
if err != nil { |
| 274 |
t.Errorf("Repositories.DeleteComment returned error: %v", err) |
| 275 |
} |
| 276 |
|
| 277 |
const methodName = "DeleteComment" |
| 278 |
testBadOptions(t, methodName, func() (err error) { |
| 279 |
_, err = client.Repositories.DeleteComment(ctx, "\n", "\n", 1) |
| 280 |
return err |
| 281 |
}) |
| 282 |
|
| 283 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 284 |
return client.Repositories.DeleteComment(ctx, "o", "r", 1) |
| 285 |
}) |
| 286 |
} |
| 287 |
|
| 288 |
func TestRepositoriesService_DeleteComment_invalidOwner(t *testing.T) { |
| 289 |
client, _, _, teardown := setup() |
| 290 |
defer teardown() |
| 291 |
|
| 292 |
ctx := context.Background() |
| 293 |
_, err := client.Repositories.DeleteComment(ctx, "%", "%", 1) |
| 294 |
testURLParseError(t, err) |
| 295 |
} |
| 296 |
|
| 297 |
func TestRepositoryComment_Marshal(t *testing.T) { |
| 298 |
testJSONMarshal(t, &RepositoryComment{}, "{}") |
| 299 |
|
| 300 |
r := &RepositoryComment{ |
| 301 |
HTMLURL: String("hurl"), |
| 302 |
URL: String("url"), |
| 303 |
ID: Int64(1), |
| 304 |
NodeID: String("nid"), |
| 305 |
CommitID: String("cid"), |
| 306 |
User: &User{ |
| 307 |
Login: String("l"), |
| 308 |
ID: Int64(1), |
| 309 |
URL: String("u"), |
| 310 |
AvatarURL: String("a"), |
| 311 |
GravatarID: String("g"), |
| 312 |
Name: String("n"), |
| 313 |
Company: String("c"), |
| 314 |
Blog: String("b"), |
| 315 |
Location: String("l"), |
| 316 |
Email: String("e"), |
| 317 |
Hireable: Bool(true), |
| 318 |
Bio: String("b"), |
| 319 |
TwitterUsername: String("t"), |
| 320 |
PublicRepos: Int(1), |
| 321 |
Followers: Int(1), |
| 322 |
Following: Int(1), |
| 323 |
CreatedAt: &Timestamp{referenceTime}, |
| 324 |
SuspendedAt: &Timestamp{referenceTime}, |
| 325 |
}, |
| 326 |
Reactions: &Reactions{ |
| 327 |
TotalCount: Int(1), |
| 328 |
PlusOne: Int(1), |
| 329 |
MinusOne: Int(1), |
| 330 |
Laugh: Int(1), |
| 331 |
Confused: Int(1), |
| 332 |
Heart: Int(1), |
| 333 |
Hooray: Int(1), |
| 334 |
Rocket: Int(1), |
| 335 |
Eyes: Int(1), |
| 336 |
URL: String("u"), |
| 337 |
}, |
| 338 |
CreatedAt: &Timestamp{referenceTime}, |
| 339 |
UpdatedAt: &Timestamp{referenceTime}, |
| 340 |
Body: String("body"), |
| 341 |
Path: String("path"), |
| 342 |
Position: Int(1), |
| 343 |
} |
| 344 |
|
| 345 |
want := `{ |
| 346 |
"html_url": "hurl", |
| 347 |
"url": "url", |
| 348 |
"id": 1, |
| 349 |
"node_id": "nid", |
| 350 |
"commit_id": "cid", |
| 351 |
"user": { |
| 352 |
"login": "l", |
| 353 |
"id": 1, |
| 354 |
"avatar_url": "a", |
| 355 |
"gravatar_id": "g", |
| 356 |
"name": "n", |
| 357 |
"company": "c", |
| 358 |
"blog": "b", |
| 359 |
"location": "l", |
| 360 |
"email": "e", |
| 361 |
"hireable": true, |
| 362 |
"bio": "b", |
| 363 |
"twitter_username": "t", |
| 364 |
"public_repos": 1, |
| 365 |
"followers": 1, |
| 366 |
"following": 1, |
| 367 |
"created_at": ` + referenceTimeStr + `, |
| 368 |
"suspended_at": ` + referenceTimeStr + `, |
| 369 |
"url": "u" |
| 370 |
}, |
| 371 |
"reactions": { |
| 372 |
"total_count": 1, |
| 373 |
"+1": 1, |
| 374 |
"-1": 1, |
| 375 |
"laugh": 1, |
| 376 |
"confused": 1, |
| 377 |
"heart": 1, |
| 378 |
"hooray": 1, |
| 379 |
"rocket": 1, |
| 380 |
"eyes": 1, |
| 381 |
"url": "u" |
| 382 |
}, |
| 383 |
"created_at": ` + referenceTimeStr + `, |
| 384 |
"updated_at": ` + referenceTimeStr + `, |
| 385 |
"body": "body", |
| 386 |
"path": "path", |
| 387 |
"position": 1 |
| 388 |
}` |
| 389 |
|
| 390 |
testJSONMarshal(t, r, want) |
| 391 |
} |