forked from google/go-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity_watching_test.go
184 lines (148 loc) · 4.92 KB
/
activity_watching_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2014 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestActivityService_ListWatchers(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/repos/o/r/subscribers", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"page": "2",
})
fmt.Fprint(w, `[{"id":1}]`)
})
watchers, _, err := client.Activity.ListWatchers(context.Background(), "o", "r", &ListOptions{Page: 2})
if err != nil {
t.Errorf("Activity.ListWatchers returned error: %v", err)
}
want := []*User{{ID: Int(1)}}
if !reflect.DeepEqual(watchers, want) {
t.Errorf("Activity.ListWatchers returned %+v, want %+v", watchers, want)
}
}
func TestActivityService_ListWatched_authenticatedUser(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/user/subscriptions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"page": "2",
})
fmt.Fprint(w, `[{"id":1}]`)
})
watched, _, err := client.Activity.ListWatched(context.Background(), "", &ListOptions{Page: 2})
if err != nil {
t.Errorf("Activity.ListWatched returned error: %v", err)
}
want := []*Repository{{ID: Int(1)}}
if !reflect.DeepEqual(watched, want) {
t.Errorf("Activity.ListWatched returned %+v, want %+v", watched, want)
}
}
func TestActivityService_ListWatched_specifiedUser(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users/u/subscriptions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"page": "2",
})
fmt.Fprint(w, `[{"id":1}]`)
})
watched, _, err := client.Activity.ListWatched(context.Background(), "u", &ListOptions{Page: 2})
if err != nil {
t.Errorf("Activity.ListWatched returned error: %v", err)
}
want := []*Repository{{ID: Int(1)}}
if !reflect.DeepEqual(watched, want) {
t.Errorf("Activity.ListWatched returned %+v, want %+v", watched, want)
}
}
func TestActivityService_GetRepositorySubscription_true(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"subscribed":true}`)
})
sub, _, err := client.Activity.GetRepositorySubscription(context.Background(), "o", "r")
if err != nil {
t.Errorf("Activity.GetRepositorySubscription returned error: %v", err)
}
want := &Subscription{Subscribed: Bool(true)}
if !reflect.DeepEqual(sub, want) {
t.Errorf("Activity.GetRepositorySubscription returned %+v, want %+v", sub, want)
}
}
func TestActivityService_GetRepositorySubscription_false(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.WriteHeader(http.StatusNotFound)
})
sub, _, err := client.Activity.GetRepositorySubscription(context.Background(), "o", "r")
if err != nil {
t.Errorf("Activity.GetRepositorySubscription returned error: %v", err)
}
var want *Subscription
if !reflect.DeepEqual(sub, want) {
t.Errorf("Activity.GetRepositorySubscription returned %+v, want %+v", sub, want)
}
}
func TestActivityService_GetRepositorySubscription_error(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.WriteHeader(http.StatusBadRequest)
})
_, _, err := client.Activity.GetRepositorySubscription(context.Background(), "o", "r")
if err == nil {
t.Errorf("Expected HTTP 400 response")
}
}
func TestActivityService_SetRepositorySubscription(t *testing.T) {
setup()
defer teardown()
input := &Subscription{Subscribed: Bool(true)}
mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) {
v := new(Subscription)
json.NewDecoder(r.Body).Decode(v)
testMethod(t, r, "PUT")
if !reflect.DeepEqual(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}
fmt.Fprint(w, `{"ignored":true}`)
})
sub, _, err := client.Activity.SetRepositorySubscription(context.Background(), "o", "r", input)
if err != nil {
t.Errorf("Activity.SetRepositorySubscription returned error: %v", err)
}
want := &Subscription{Ignored: Bool(true)}
if !reflect.DeepEqual(sub, want) {
t.Errorf("Activity.SetRepositorySubscription returned %+v, want %+v", sub, want)
}
}
func TestActivityService_DeleteRepositorySubscription(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
w.WriteHeader(http.StatusNoContent)
})
_, err := client.Activity.DeleteRepositorySubscription(context.Background(), "o", "r")
if err != nil {
t.Errorf("Activity.DeleteRepositorySubscription returned error: %v", err)
}
}