github.com/google/go-github/v33@v33.0.0/github/activity_notifications_test.go (about) 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 "reflect" 14 "testing" 15 "time" 16 ) 17 18 func TestActivityService_ListNotification(t *testing.T) { 19 client, mux, _, teardown := setup() 20 defer teardown() 21 22 mux.HandleFunc("/notifications", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 testFormValues(t, r, values{ 25 "all": "true", 26 "participating": "true", 27 "since": "2006-01-02T15:04:05Z", 28 "before": "2007-03-04T15:04:05Z", 29 }) 30 31 fmt.Fprint(w, `[{"id":"1", "subject":{"title":"t"}}]`) 32 }) 33 34 opt := &NotificationListOptions{ 35 All: true, 36 Participating: true, 37 Since: time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC), 38 Before: time.Date(2007, time.March, 04, 15, 04, 05, 0, time.UTC), 39 } 40 notifications, _, err := client.Activity.ListNotifications(context.Background(), opt) 41 if err != nil { 42 t.Errorf("Activity.ListNotifications returned error: %v", err) 43 } 44 45 want := []*Notification{{ID: String("1"), Subject: &NotificationSubject{Title: String("t")}}} 46 if !reflect.DeepEqual(notifications, want) { 47 t.Errorf("Activity.ListNotifications returned %+v, want %+v", notifications, want) 48 } 49 } 50 51 func TestActivityService_ListRepositoryNotification(t *testing.T) { 52 client, mux, _, teardown := setup() 53 defer teardown() 54 55 mux.HandleFunc("/repos/o/r/notifications", func(w http.ResponseWriter, r *http.Request) { 56 testMethod(t, r, "GET") 57 fmt.Fprint(w, `[{"id":"1"}]`) 58 }) 59 60 notifications, _, err := client.Activity.ListRepositoryNotifications(context.Background(), "o", "r", nil) 61 if err != nil { 62 t.Errorf("Activity.ListRepositoryNotifications returned error: %v", err) 63 } 64 65 want := []*Notification{{ID: String("1")}} 66 if !reflect.DeepEqual(notifications, want) { 67 t.Errorf("Activity.ListRepositoryNotifications returned %+v, want %+v", notifications, want) 68 } 69 } 70 71 func TestActivityService_MarkNotificationsRead(t *testing.T) { 72 client, mux, _, teardown := setup() 73 defer teardown() 74 75 mux.HandleFunc("/notifications", func(w http.ResponseWriter, r *http.Request) { 76 testMethod(t, r, "PUT") 77 testHeader(t, r, "Content-Type", "application/json") 78 testBody(t, r, `{"last_read_at":"2006-01-02T15:04:05Z"}`+"\n") 79 80 w.WriteHeader(http.StatusResetContent) 81 }) 82 83 _, err := client.Activity.MarkNotificationsRead(context.Background(), time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)) 84 if err != nil { 85 t.Errorf("Activity.MarkNotificationsRead returned error: %v", err) 86 } 87 } 88 89 func TestActivityService_MarkRepositoryNotificationsRead(t *testing.T) { 90 client, mux, _, teardown := setup() 91 defer teardown() 92 93 mux.HandleFunc("/repos/o/r/notifications", func(w http.ResponseWriter, r *http.Request) { 94 testMethod(t, r, "PUT") 95 testHeader(t, r, "Content-Type", "application/json") 96 testBody(t, r, `{"last_read_at":"2006-01-02T15:04:05Z"}`+"\n") 97 98 w.WriteHeader(http.StatusResetContent) 99 }) 100 101 _, err := client.Activity.MarkRepositoryNotificationsRead(context.Background(), "o", "r", time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)) 102 if err != nil { 103 t.Errorf("Activity.MarkRepositoryNotificationsRead returned error: %v", err) 104 } 105 } 106 107 func TestActivityService_GetThread(t *testing.T) { 108 client, mux, _, teardown := setup() 109 defer teardown() 110 111 mux.HandleFunc("/notifications/threads/1", func(w http.ResponseWriter, r *http.Request) { 112 testMethod(t, r, "GET") 113 fmt.Fprint(w, `{"id":"1"}`) 114 }) 115 116 notification, _, err := client.Activity.GetThread(context.Background(), "1") 117 if err != nil { 118 t.Errorf("Activity.GetThread returned error: %v", err) 119 } 120 121 want := &Notification{ID: String("1")} 122 if !reflect.DeepEqual(notification, want) { 123 t.Errorf("Activity.GetThread returned %+v, want %+v", notification, want) 124 } 125 } 126 127 func TestActivityService_MarkThreadRead(t *testing.T) { 128 client, mux, _, teardown := setup() 129 defer teardown() 130 131 mux.HandleFunc("/notifications/threads/1", func(w http.ResponseWriter, r *http.Request) { 132 testMethod(t, r, "PATCH") 133 w.WriteHeader(http.StatusResetContent) 134 }) 135 136 _, err := client.Activity.MarkThreadRead(context.Background(), "1") 137 if err != nil { 138 t.Errorf("Activity.MarkThreadRead returned error: %v", err) 139 } 140 } 141 142 func TestActivityService_GetThreadSubscription(t *testing.T) { 143 client, mux, _, teardown := setup() 144 defer teardown() 145 146 mux.HandleFunc("/notifications/threads/1/subscription", func(w http.ResponseWriter, r *http.Request) { 147 testMethod(t, r, "GET") 148 fmt.Fprint(w, `{"subscribed":true}`) 149 }) 150 151 sub, _, err := client.Activity.GetThreadSubscription(context.Background(), "1") 152 if err != nil { 153 t.Errorf("Activity.GetThreadSubscription returned error: %v", err) 154 } 155 156 want := &Subscription{Subscribed: Bool(true)} 157 if !reflect.DeepEqual(sub, want) { 158 t.Errorf("Activity.GetThreadSubscription returned %+v, want %+v", sub, want) 159 } 160 } 161 162 func TestActivityService_SetThreadSubscription(t *testing.T) { 163 client, mux, _, teardown := setup() 164 defer teardown() 165 166 input := &Subscription{Subscribed: Bool(true)} 167 168 mux.HandleFunc("/notifications/threads/1/subscription", func(w http.ResponseWriter, r *http.Request) { 169 v := new(Subscription) 170 json.NewDecoder(r.Body).Decode(v) 171 172 testMethod(t, r, "PUT") 173 if !reflect.DeepEqual(v, input) { 174 t.Errorf("Request body = %+v, want %+v", v, input) 175 } 176 177 fmt.Fprint(w, `{"ignored":true}`) 178 }) 179 180 sub, _, err := client.Activity.SetThreadSubscription(context.Background(), "1", input) 181 if err != nil { 182 t.Errorf("Activity.SetThreadSubscription returned error: %v", err) 183 } 184 185 want := &Subscription{Ignored: Bool(true)} 186 if !reflect.DeepEqual(sub, want) { 187 t.Errorf("Activity.SetThreadSubscription returned %+v, want %+v", sub, want) 188 } 189 } 190 191 func TestActivityService_DeleteThreadSubscription(t *testing.T) { 192 client, mux, _, teardown := setup() 193 defer teardown() 194 195 mux.HandleFunc("/notifications/threads/1/subscription", func(w http.ResponseWriter, r *http.Request) { 196 testMethod(t, r, "DELETE") 197 w.WriteHeader(http.StatusNoContent) 198 }) 199 200 _, err := client.Activity.DeleteThreadSubscription(context.Background(), "1") 201 if err != nil { 202 t.Errorf("Activity.DeleteThreadSubscription returned error: %v", err) 203 } 204 }