| 1 |
// Copyright 2014 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 TestActivityService_ListNotification(t *testing.T) { |
| 20 |
client, mux, _, teardown := setup() |
| 21 |
defer teardown() |
| 22 |
|
| 23 |
mux.HandleFunc("/notifications", func(w http.ResponseWriter, r *http.Request) { |
| 24 |
testMethod(t, r, "GET") |
| 25 |
testFormValues(t, r, values{ |
| 26 |
"all": "true", |
| 27 |
"participating": "true", |
| 28 |
"since": "2006-01-02T15:04:05Z", |
| 29 |
"before": "2007-03-04T15:04:05Z", |
| 30 |
}) |
| 31 |
|
| 32 |
fmt.Fprint(w, `[{"id":"1", "subject":{"title":"t"}}]`) |
| 33 |
}) |
| 34 |
|
| 35 |
opt := &NotificationListOptions{ |
| 36 |
All: true, |
| 37 |
Participating: true, |
| 38 |
Since: time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC), |
| 39 |
Before: time.Date(2007, time.March, 04, 15, 04, 05, 0, time.UTC), |
| 40 |
} |
| 41 |
ctx := context.Background() |
| 42 |
notifications, _, err := client.Activity.ListNotifications(ctx, opt) |
| 43 |
if err != nil { |
| 44 |
t.Errorf("Activity.ListNotifications returned error: %v", err) |
| 45 |
} |
| 46 |
|
| 47 |
want := []*Notification{{ID: String("1"), Subject: &NotificationSubject{Title: String("t")}}} |
| 48 |
if !cmp.Equal(notifications, want) { |
| 49 |
t.Errorf("Activity.ListNotifications returned %+v, want %+v", notifications, want) |
| 50 |
} |
| 51 |
|
| 52 |
const methodName = "ListNotifications" |
| 53 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 54 |
got, resp, err := client.Activity.ListNotifications(ctx, opt) |
| 55 |
if got != nil { |
| 56 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 57 |
} |
| 58 |
return resp, err |
| 59 |
}) |
| 60 |
} |
| 61 |
|
| 62 |
func TestActivityService_ListRepositoryNotifications(t *testing.T) { |
| 63 |
client, mux, _, teardown := setup() |
| 64 |
defer teardown() |
| 65 |
|
| 66 |
mux.HandleFunc("/repos/o/r/notifications", func(w http.ResponseWriter, r *http.Request) { |
| 67 |
testMethod(t, r, "GET") |
| 68 |
fmt.Fprint(w, `[{"id":"1"}]`) |
| 69 |
}) |
| 70 |
|
| 71 |
ctx := context.Background() |
| 72 |
notifications, _, err := client.Activity.ListRepositoryNotifications(ctx, "o", "r", nil) |
| 73 |
if err != nil { |
| 74 |
t.Errorf("Activity.ListRepositoryNotifications returned error: %v", err) |
| 75 |
} |
| 76 |
|
| 77 |
want := []*Notification{{ID: String("1")}} |
| 78 |
if !cmp.Equal(notifications, want) { |
| 79 |
t.Errorf("Activity.ListRepositoryNotifications returned %+v, want %+v", notifications, want) |
| 80 |
} |
| 81 |
|
| 82 |
const methodName = "ListRepositoryNotifications" |
| 83 |
testBadOptions(t, methodName, func() (err error) { |
| 84 |
_, _, err = client.Activity.ListRepositoryNotifications(ctx, "\n", "\n", &NotificationListOptions{}) |
| 85 |
return err |
| 86 |
}) |
| 87 |
|
| 88 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 89 |
got, resp, err := client.Activity.ListRepositoryNotifications(ctx, "o", "r", nil) |
| 90 |
if got != nil { |
| 91 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 92 |
} |
| 93 |
return resp, err |
| 94 |
}) |
| 95 |
} |
| 96 |
|
| 97 |
func TestActivityService_MarkNotificationsRead(t *testing.T) { |
| 98 |
client, mux, _, teardown := setup() |
| 99 |
defer teardown() |
| 100 |
|
| 101 |
mux.HandleFunc("/notifications", func(w http.ResponseWriter, r *http.Request) { |
| 102 |
testMethod(t, r, "PUT") |
| 103 |
testHeader(t, r, "Content-Type", "application/json") |
| 104 |
testBody(t, r, `{"last_read_at":"2006-01-02T15:04:05Z"}`+"\n") |
| 105 |
|
| 106 |
w.WriteHeader(http.StatusResetContent) |
| 107 |
}) |
| 108 |
|
| 109 |
ctx := context.Background() |
| 110 |
_, err := client.Activity.MarkNotificationsRead(ctx, Timestamp{time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)}) |
| 111 |
if err != nil { |
| 112 |
t.Errorf("Activity.MarkNotificationsRead returned error: %v", err) |
| 113 |
} |
| 114 |
|
| 115 |
const methodName = "MarkNotificationsRead" |
| 116 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 117 |
return client.Activity.MarkNotificationsRead(ctx, Timestamp{time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)}) |
| 118 |
}) |
| 119 |
} |
| 120 |
|
| 121 |
func TestActivityService_MarkRepositoryNotificationsRead(t *testing.T) { |
| 122 |
client, mux, _, teardown := setup() |
| 123 |
defer teardown() |
| 124 |
|
| 125 |
mux.HandleFunc("/repos/o/r/notifications", func(w http.ResponseWriter, r *http.Request) { |
| 126 |
testMethod(t, r, "PUT") |
| 127 |
testHeader(t, r, "Content-Type", "application/json") |
| 128 |
testBody(t, r, `{"last_read_at":"2006-01-02T15:04:05Z"}`+"\n") |
| 129 |
|
| 130 |
w.WriteHeader(http.StatusResetContent) |
| 131 |
}) |
| 132 |
|
| 133 |
ctx := context.Background() |
| 134 |
_, err := client.Activity.MarkRepositoryNotificationsRead(ctx, "o", "r", Timestamp{time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)}) |
| 135 |
if err != nil { |
| 136 |
t.Errorf("Activity.MarkRepositoryNotificationsRead returned error: %v", err) |
| 137 |
} |
| 138 |
|
| 139 |
const methodName = "MarkRepositoryNotificationsRead" |
| 140 |
testBadOptions(t, methodName, func() (err error) { |
| 141 |
_, err = client.Activity.MarkRepositoryNotificationsRead(ctx, "\n", "\n", Timestamp{time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)}) |
| 142 |
return err |
| 143 |
}) |
| 144 |
|
| 145 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 146 |
return client.Activity.MarkRepositoryNotificationsRead(ctx, "o", "r", Timestamp{time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)}) |
| 147 |
}) |
| 148 |
} |
| 149 |
|
| 150 |
func TestActivityService_GetThread(t *testing.T) { |
| 151 |
client, mux, _, teardown := setup() |
| 152 |
defer teardown() |
| 153 |
|
| 154 |
mux.HandleFunc("/notifications/threads/1", func(w http.ResponseWriter, r *http.Request) { |
| 155 |
testMethod(t, r, "GET") |
| 156 |
fmt.Fprint(w, `{"id":"1"}`) |
| 157 |
}) |
| 158 |
|
| 159 |
ctx := context.Background() |
| 160 |
notification, _, err := client.Activity.GetThread(ctx, "1") |
| 161 |
if err != nil { |
| 162 |
t.Errorf("Activity.GetThread returned error: %v", err) |
| 163 |
} |
| 164 |
|
| 165 |
want := &Notification{ID: String("1")} |
| 166 |
if !cmp.Equal(notification, want) { |
| 167 |
t.Errorf("Activity.GetThread returned %+v, want %+v", notification, want) |
| 168 |
} |
| 169 |
|
| 170 |
const methodName = "GetThread" |
| 171 |
testBadOptions(t, methodName, func() (err error) { |
| 172 |
_, _, err = client.Activity.GetThread(ctx, "\n") |
| 173 |
return err |
| 174 |
}) |
| 175 |
|
| 176 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 177 |
got, resp, err := client.Activity.GetThread(ctx, "1") |
| 178 |
if got != nil { |
| 179 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 180 |
} |
| 181 |
return resp, err |
| 182 |
}) |
| 183 |
} |
| 184 |
|
| 185 |
func TestActivityService_MarkThreadRead(t *testing.T) { |
| 186 |
client, mux, _, teardown := setup() |
| 187 |
defer teardown() |
| 188 |
|
| 189 |
mux.HandleFunc("/notifications/threads/1", func(w http.ResponseWriter, r *http.Request) { |
| 190 |
testMethod(t, r, "PATCH") |
| 191 |
w.WriteHeader(http.StatusResetContent) |
| 192 |
}) |
| 193 |
|
| 194 |
ctx := context.Background() |
| 195 |
_, err := client.Activity.MarkThreadRead(ctx, "1") |
| 196 |
if err != nil { |
| 197 |
t.Errorf("Activity.MarkThreadRead returned error: %v", err) |
| 198 |
} |
| 199 |
|
| 200 |
const methodName = "MarkThreadRead" |
| 201 |
testBadOptions(t, methodName, func() (err error) { |
| 202 |
_, err = client.Activity.MarkThreadRead(ctx, "\n") |
| 203 |
return err |
| 204 |
}) |
| 205 |
|
| 206 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 207 |
return client.Activity.MarkThreadRead(ctx, "1") |
| 208 |
}) |
| 209 |
} |
| 210 |
|
| 211 |
func TestActivityService_GetThreadSubscription(t *testing.T) { |
| 212 |
client, mux, _, teardown := setup() |
| 213 |
defer teardown() |
| 214 |
|
| 215 |
mux.HandleFunc("/notifications/threads/1/subscription", func(w http.ResponseWriter, r *http.Request) { |
| 216 |
testMethod(t, r, "GET") |
| 217 |
fmt.Fprint(w, `{"subscribed":true}`) |
| 218 |
}) |
| 219 |
|
| 220 |
ctx := context.Background() |
| 221 |
sub, _, err := client.Activity.GetThreadSubscription(ctx, "1") |
| 222 |
if err != nil { |
| 223 |
t.Errorf("Activity.GetThreadSubscription returned error: %v", err) |
| 224 |
} |
| 225 |
|
| 226 |
want := &Subscription{Subscribed: Bool(true)} |
| 227 |
if !cmp.Equal(sub, want) { |
| 228 |
t.Errorf("Activity.GetThreadSubscription returned %+v, want %+v", sub, want) |
| 229 |
} |
| 230 |
|
| 231 |
const methodName = "GetThreadSubscription" |
| 232 |
testBadOptions(t, methodName, func() (err error) { |
| 233 |
_, _, err = client.Activity.GetThreadSubscription(ctx, "\n") |
| 234 |
return err |
| 235 |
}) |
| 236 |
|
| 237 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 238 |
got, resp, err := client.Activity.GetThreadSubscription(ctx, "1") |
| 239 |
if got != nil { |
| 240 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 241 |
} |
| 242 |
return resp, err |
| 243 |
}) |
| 244 |
} |
| 245 |
|
| 246 |
func TestActivityService_SetThreadSubscription(t *testing.T) { |
| 247 |
client, mux, _, teardown := setup() |
| 248 |
defer teardown() |
| 249 |
|
| 250 |
input := &Subscription{Subscribed: Bool(true)} |
| 251 |
|
| 252 |
mux.HandleFunc("/notifications/threads/1/subscription", func(w http.ResponseWriter, r *http.Request) { |
| 253 |
v := new(Subscription) |
| 254 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 255 |
|
| 256 |
testMethod(t, r, "PUT") |
| 257 |
if !cmp.Equal(v, input) { |
| 258 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 259 |
} |
| 260 |
|
| 261 |
fmt.Fprint(w, `{"ignored":true}`) |
| 262 |
}) |
| 263 |
|
| 264 |
ctx := context.Background() |
| 265 |
sub, _, err := client.Activity.SetThreadSubscription(ctx, "1", input) |
| 266 |
if err != nil { |
| 267 |
t.Errorf("Activity.SetThreadSubscription returned error: %v", err) |
| 268 |
} |
| 269 |
|
| 270 |
want := &Subscription{Ignored: Bool(true)} |
| 271 |
if !cmp.Equal(sub, want) { |
| 272 |
t.Errorf("Activity.SetThreadSubscription returned %+v, want %+v", sub, want) |
| 273 |
} |
| 274 |
|
| 275 |
const methodName = "SetThreadSubscription" |
| 276 |
testBadOptions(t, methodName, func() (err error) { |
| 277 |
_, _, err = client.Activity.SetThreadSubscription(ctx, "\n", input) |
| 278 |
return err |
| 279 |
}) |
| 280 |
|
| 281 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 282 |
got, resp, err := client.Activity.SetThreadSubscription(ctx, "1", input) |
| 283 |
if got != nil { |
| 284 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 285 |
} |
| 286 |
return resp, err |
| 287 |
}) |
| 288 |
} |
| 289 |
|
| 290 |
func TestActivityService_DeleteThreadSubscription(t *testing.T) { |
| 291 |
client, mux, _, teardown := setup() |
| 292 |
defer teardown() |
| 293 |
|
| 294 |
mux.HandleFunc("/notifications/threads/1/subscription", func(w http.ResponseWriter, r *http.Request) { |
| 295 |
testMethod(t, r, "DELETE") |
| 296 |
w.WriteHeader(http.StatusNoContent) |
| 297 |
}) |
| 298 |
|
| 299 |
ctx := context.Background() |
| 300 |
_, err := client.Activity.DeleteThreadSubscription(ctx, "1") |
| 301 |
if err != nil { |
| 302 |
t.Errorf("Activity.DeleteThreadSubscription returned error: %v", err) |
| 303 |
} |
| 304 |
|
| 305 |
const methodName = "DeleteThreadSubscription" |
| 306 |
testBadOptions(t, methodName, func() (err error) { |
| 307 |
_, err = client.Activity.DeleteThreadSubscription(ctx, "\n") |
| 308 |
return err |
| 309 |
}) |
| 310 |
|
| 311 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 312 |
return client.Activity.DeleteThreadSubscription(ctx, "1") |
| 313 |
}) |
| 314 |
} |
| 315 |
|
| 316 |
func TestNotification_Marshal(t *testing.T) { |
| 317 |
testJSONMarshal(t, &Notification{}, "{}") |
| 318 |
|
| 319 |
u := &Notification{ |
| 320 |
ID: String("id"), |
| 321 |
Repository: &Repository{ |
| 322 |
ID: Int64(1), |
| 323 |
URL: String("u"), |
| 324 |
Name: String("n"), |
| 325 |
}, |
| 326 |
Subject: &NotificationSubject{ |
| 327 |
Title: String("t"), |
| 328 |
URL: String("u"), |
| 329 |
LatestCommentURL: String("l"), |
| 330 |
Type: String("t"), |
| 331 |
}, |
| 332 |
Reason: String("r"), |
| 333 |
Unread: Bool(true), |
| 334 |
UpdatedAt: &Timestamp{referenceTime}, |
| 335 |
LastReadAt: &Timestamp{referenceTime}, |
| 336 |
URL: String("u"), |
| 337 |
} |
| 338 |
|
| 339 |
want := `{ |
| 340 |
"id": "id", |
| 341 |
"repository": { |
| 342 |
"id": 1, |
| 343 |
"url": "u", |
| 344 |
"name": "n" |
| 345 |
}, |
| 346 |
"subject": { |
| 347 |
"title": "t", |
| 348 |
"url": "u", |
| 349 |
"latest_comment_url": "l", |
| 350 |
"type": "t" |
| 351 |
}, |
| 352 |
"reason": "r", |
| 353 |
"unread": true, |
| 354 |
"updated_at": ` + referenceTimeStr + `, |
| 355 |
"last_read_at": ` + referenceTimeStr + `, |
| 356 |
"url": "u" |
| 357 |
}` |
| 358 |
|
| 359 |
testJSONMarshal(t, u, want) |
| 360 |
} |
| 361 |
|
| 362 |
func TestNotificationSubject_Marshal(t *testing.T) { |
| 363 |
testJSONMarshal(t, &NotificationSubject{}, "{}") |
| 364 |
|
| 365 |
u := &NotificationSubject{ |
| 366 |
Title: String("t"), |
| 367 |
URL: String("u"), |
| 368 |
LatestCommentURL: String("l"), |
| 369 |
Type: String("t"), |
| 370 |
} |
| 371 |
|
| 372 |
want := `{ |
| 373 |
"title": "t", |
| 374 |
"url": "u", |
| 375 |
"latest_comment_url": "l", |
| 376 |
"type": "t" |
| 377 |
}` |
| 378 |
|
| 379 |
testJSONMarshal(t, u, want) |
| 380 |
} |
| 381 |
|
| 382 |
func TestMarkReadOptions_Marshal(t *testing.T) { |
| 383 |
testJSONMarshal(t, &markReadOptions{}, "{}") |
| 384 |
|
| 385 |
u := &markReadOptions{ |
| 386 |
LastReadAt: Timestamp{referenceTime}, |
| 387 |
} |
| 388 |
|
| 389 |
want := `{ |
| 390 |
"last_read_at": ` + referenceTimeStr + ` |
| 391 |
}` |
| 392 |
|
| 393 |
testJSONMarshal(t, u, want) |
| 394 |
} |