github.com/google/go-github/v49@v49.1.0/github/activity_watching_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 "testing" 14 15 "github.com/google/go-cmp/cmp" 16 ) 17 18 func TestActivityService_ListWatchers(t *testing.T) { 19 client, mux, _, teardown := setup() 20 defer teardown() 21 22 mux.HandleFunc("/repos/o/r/subscribers", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 testFormValues(t, r, values{ 25 "page": "2", 26 }) 27 28 fmt.Fprint(w, `[{"id":1}]`) 29 }) 30 31 ctx := context.Background() 32 watchers, _, err := client.Activity.ListWatchers(ctx, "o", "r", &ListOptions{Page: 2}) 33 if err != nil { 34 t.Errorf("Activity.ListWatchers returned error: %v", err) 35 } 36 37 want := []*User{{ID: Int64(1)}} 38 if !cmp.Equal(watchers, want) { 39 t.Errorf("Activity.ListWatchers returned %+v, want %+v", watchers, want) 40 } 41 42 const methodName = "ListWatchers" 43 testBadOptions(t, methodName, func() (err error) { 44 _, _, err = client.Activity.ListWatchers(ctx, "\n", "\n", &ListOptions{Page: 2}) 45 return err 46 }) 47 48 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 49 got, resp, err := client.Activity.ListWatchers(ctx, "o", "r", &ListOptions{Page: 2}) 50 if got != nil { 51 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 52 } 53 return resp, err 54 }) 55 } 56 57 func TestActivityService_ListWatched_authenticatedUser(t *testing.T) { 58 client, mux, _, teardown := setup() 59 defer teardown() 60 61 mux.HandleFunc("/user/subscriptions", func(w http.ResponseWriter, r *http.Request) { 62 testMethod(t, r, "GET") 63 testFormValues(t, r, values{ 64 "page": "2", 65 }) 66 fmt.Fprint(w, `[{"id":1}]`) 67 }) 68 69 ctx := context.Background() 70 watched, _, err := client.Activity.ListWatched(ctx, "", &ListOptions{Page: 2}) 71 if err != nil { 72 t.Errorf("Activity.ListWatched returned error: %v", err) 73 } 74 75 want := []*Repository{{ID: Int64(1)}} 76 if !cmp.Equal(watched, want) { 77 t.Errorf("Activity.ListWatched returned %+v, want %+v", watched, want) 78 } 79 80 const methodName = "ListWatched" 81 testBadOptions(t, methodName, func() (err error) { 82 _, _, err = client.Activity.ListWatched(ctx, "\n", &ListOptions{Page: 2}) 83 return err 84 }) 85 86 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 87 got, resp, err := client.Activity.ListWatched(ctx, "", &ListOptions{Page: 2}) 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_ListWatched_specifiedUser(t *testing.T) { 96 client, mux, _, teardown := setup() 97 defer teardown() 98 99 mux.HandleFunc("/users/u/subscriptions", func(w http.ResponseWriter, r *http.Request) { 100 testMethod(t, r, "GET") 101 testFormValues(t, r, values{ 102 "page": "2", 103 }) 104 fmt.Fprint(w, `[{"id":1}]`) 105 }) 106 107 ctx := context.Background() 108 watched, _, err := client.Activity.ListWatched(ctx, "u", &ListOptions{Page: 2}) 109 if err != nil { 110 t.Errorf("Activity.ListWatched returned error: %v", err) 111 } 112 113 want := []*Repository{{ID: Int64(1)}} 114 if !cmp.Equal(watched, want) { 115 t.Errorf("Activity.ListWatched returned %+v, want %+v", watched, want) 116 } 117 } 118 119 func TestActivityService_GetRepositorySubscription_true(t *testing.T) { 120 client, mux, _, teardown := setup() 121 defer teardown() 122 123 mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) { 124 testMethod(t, r, "GET") 125 fmt.Fprint(w, `{"subscribed":true}`) 126 }) 127 128 ctx := context.Background() 129 sub, _, err := client.Activity.GetRepositorySubscription(ctx, "o", "r") 130 if err != nil { 131 t.Errorf("Activity.GetRepositorySubscription returned error: %v", err) 132 } 133 134 want := &Subscription{Subscribed: Bool(true)} 135 if !cmp.Equal(sub, want) { 136 t.Errorf("Activity.GetRepositorySubscription returned %+v, want %+v", sub, want) 137 } 138 139 const methodName = "GetRepositorySubscription" 140 testBadOptions(t, methodName, func() (err error) { 141 _, _, err = client.Activity.GetRepositorySubscription(ctx, "\n", "\n") 142 return err 143 }) 144 145 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 146 got, resp, err := client.Activity.GetRepositorySubscription(ctx, "o", "r") 147 if got != nil { 148 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 149 } 150 return resp, err 151 }) 152 } 153 154 func TestActivityService_GetRepositorySubscription_false(t *testing.T) { 155 client, mux, _, teardown := setup() 156 defer teardown() 157 158 mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) { 159 testMethod(t, r, "GET") 160 w.WriteHeader(http.StatusNotFound) 161 }) 162 163 ctx := context.Background() 164 sub, _, err := client.Activity.GetRepositorySubscription(ctx, "o", "r") 165 if err != nil { 166 t.Errorf("Activity.GetRepositorySubscription returned error: %v", err) 167 } 168 169 var want *Subscription 170 if !cmp.Equal(sub, want) { 171 t.Errorf("Activity.GetRepositorySubscription returned %+v, want %+v", sub, want) 172 } 173 } 174 175 func TestActivityService_GetRepositorySubscription_error(t *testing.T) { 176 client, mux, _, teardown := setup() 177 defer teardown() 178 179 mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) { 180 testMethod(t, r, "GET") 181 w.WriteHeader(http.StatusBadRequest) 182 }) 183 184 ctx := context.Background() 185 _, _, err := client.Activity.GetRepositorySubscription(ctx, "o", "r") 186 if err == nil { 187 t.Errorf("Expected HTTP 400 response") 188 } 189 } 190 191 func TestActivityService_SetRepositorySubscription(t *testing.T) { 192 client, mux, _, teardown := setup() 193 defer teardown() 194 195 input := &Subscription{Subscribed: Bool(true)} 196 197 mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) { 198 v := new(Subscription) 199 json.NewDecoder(r.Body).Decode(v) 200 201 testMethod(t, r, "PUT") 202 if !cmp.Equal(v, input) { 203 t.Errorf("Request body = %+v, want %+v", v, input) 204 } 205 206 fmt.Fprint(w, `{"ignored":true}`) 207 }) 208 209 ctx := context.Background() 210 sub, _, err := client.Activity.SetRepositorySubscription(ctx, "o", "r", input) 211 if err != nil { 212 t.Errorf("Activity.SetRepositorySubscription returned error: %v", err) 213 } 214 215 want := &Subscription{Ignored: Bool(true)} 216 if !cmp.Equal(sub, want) { 217 t.Errorf("Activity.SetRepositorySubscription returned %+v, want %+v", sub, want) 218 } 219 220 const methodName = "SetRepositorySubscription" 221 testBadOptions(t, methodName, func() (err error) { 222 _, _, err = client.Activity.SetRepositorySubscription(ctx, "\n", "\n", input) 223 return err 224 }) 225 226 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 227 got, resp, err := client.Activity.SetRepositorySubscription(ctx, "o", "r", input) 228 if got != nil { 229 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 230 } 231 return resp, err 232 }) 233 } 234 235 func TestActivityService_DeleteRepositorySubscription(t *testing.T) { 236 client, mux, _, teardown := setup() 237 defer teardown() 238 239 mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) { 240 testMethod(t, r, "DELETE") 241 w.WriteHeader(http.StatusNoContent) 242 }) 243 244 ctx := context.Background() 245 _, err := client.Activity.DeleteRepositorySubscription(ctx, "o", "r") 246 if err != nil { 247 t.Errorf("Activity.DeleteRepositorySubscription returned error: %v", err) 248 } 249 250 const methodName = "DeleteRepositorySubscription" 251 testBadOptions(t, methodName, func() (err error) { 252 _, err = client.Activity.DeleteRepositorySubscription(ctx, "\n", "\n") 253 return err 254 }) 255 256 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 257 return client.Activity.DeleteRepositorySubscription(ctx, "o", "r") 258 }) 259 } 260 261 func TestSubscription_Marshal(t *testing.T) { 262 testJSONMarshal(t, &Subscription{}, "{}") 263 264 u := &Subscription{ 265 Subscribed: Bool(true), 266 Ignored: Bool(false), 267 Reason: String("r"), 268 CreatedAt: &Timestamp{referenceTime}, 269 URL: String("u"), 270 RepositoryURL: String("ru"), 271 ThreadURL: String("tu"), 272 } 273 274 want := `{ 275 "subscribed": true, 276 "ignored": false, 277 "reason": "r", 278 "created_at": ` + referenceTimeStr + `, 279 "url": "u", 280 "repository_url": "ru", 281 "thread_url": "tu" 282 }` 283 284 testJSONMarshal(t, u, want) 285 }