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