-
Notifications
You must be signed in to change notification settings - Fork 2
/
routes_test.go
194 lines (147 loc) · 5.07 KB
/
routes_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
185
186
187
188
189
190
191
192
193
194
package main
import (
"context"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/geckoboard/cake-bot/github"
"github.com/geckoboard/cake-bot/log"
"github.com/slack-go/slack"
)
type notification struct {
action string
repo *github.Repository
pr *github.PullRequest
reviewer *github.User
}
type fakeNotifier struct {
notifications []notification
}
func (f *fakeNotifier) Approved(_ context.Context, repo *github.Repository, pr *github.PullRequest, review *github.Review) error {
f.notifications = append(f.notifications, notification{"approved", repo, pr, review.User})
return nil
}
func (f *fakeNotifier) ChangesRequested(_ context.Context, repo *github.Repository, pr *github.PullRequest, review *github.Review) error {
f.notifications = append(f.notifications, notification{"changes_requested", repo, pr, review.User})
return nil
}
func (f *fakeNotifier) ReviewRequested(_ context.Context, repo *github.Repository, pr *github.PullRequest, reviewer *github.User) error {
f.notifications = append(f.notifications, notification{"review_requested", repo, pr, reviewer})
return nil
}
func (f *fakeNotifier) RespondToSlackAction(_ context.Context, _ *slack.InteractionCallback, _ string) error {
return nil
}
type fakeWebhookValidator struct{}
func (f *fakeWebhookValidator) ValidateSignature(r *http.Request) error {
return nil
}
func TestHandlePullRequestEvent(t *testing.T) {
outcome := &fakeNotifier{}
validator := &fakeWebhookValidator{}
s := httptest.NewServer(NewServer(outcome, validator))
defer s.Close()
webhookURL := s.URL + "/github"
file, err := os.Open("./example-webhooks/pull_request_review_requested.json")
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("POST", webhookURL, file)
if err != nil {
t.Fatal(err)
}
req.Header.Add("X-Github-Event", "pull_request")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Errorf("expected status code to be 200, got %d", resp.StatusCode)
}
if len(outcome.notifications) != 1 {
t.Fatalf("expected 1 notification that PR has been approved, instead got: %#v", outcome.notifications)
}
if outcome.notifications[0].action != "review_requested" {
t.Fatalf("expected notification to be review_requested, got: %q", outcome.notifications[0].action)
}
if outcome.notifications[0].pr.Number != 14 {
t.Fatalf("expected PR number %d, got: %v", 14, outcome.notifications[0].pr.Number)
}
if outcome.notifications[0].reviewer.ID != 20394 {
t.Fatalf("unexpected review passed to notifier: %v", outcome.notifications[0].reviewer)
}
}
func TestHandleReviewRequiresChanges(t *testing.T) {
outcome := &fakeNotifier{}
validator := &fakeWebhookValidator{}
s := httptest.NewServer(NewServer(outcome, validator))
defer s.Close()
webhookURL := s.URL + "/github"
file, err := os.Open("./example-webhooks/pull_request_review_submitted.json")
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("POST", webhookURL, file)
if err != nil {
t.Fatal(err)
}
req.Header.Add("X-Github-Event", "pull_request_review")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Errorf("expected status code to be 200, got %d", resp.StatusCode)
}
if len(outcome.notifications) != 1 {
t.Fatalf("expected 1 notification that PR has been approved, instead got: %#v", outcome.notifications)
}
if outcome.notifications[0].action != "changes_requested" {
t.Fatalf("expected notification to be changes_requested, got: %q", outcome.notifications[0].action)
}
if outcome.notifications[0].pr.Number != 12 {
t.Fatalf("expected PR number %d, got: %q", 12, outcome.notifications[0].pr.Number)
}
if outcome.notifications[0].reviewer.ID != 12532520 {
t.Fatalf("unexpected review passed to notifier: %v", outcome.notifications[0].reviewer)
}
}
func TestHandleReviewApproved(t *testing.T) {
outcome := &fakeNotifier{}
validator := &fakeWebhookValidator{}
s := httptest.NewServer(NewServer(outcome, validator))
defer s.Close()
webhookURL := s.URL + "/github"
file, err := os.Open("./example-webhooks/pull_request_review_approved.json")
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("POST", webhookURL, file)
if err != nil {
t.Fatal(err)
}
req.Header.Add("X-Github-Event", "pull_request_review")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Errorf("expected status code to be 200, got %d", resp.StatusCode)
}
if len(outcome.notifications) != 1 {
t.Fatalf("expected 1 notification that PR has been approved, instead got: %#v", outcome.notifications)
}
if outcome.notifications[0].action != "approved" {
t.Fatalf("expected notification to be approved, got: %q", outcome.notifications[0].action)
}
if outcome.notifications[0].pr.Number != 12 {
t.Fatalf("expected PR number %d, got: %d", 12, outcome.notifications[0].pr.Number)
}
if outcome.notifications[0].reviewer.ID != 12532520 {
t.Fatalf("unexpected review passed to notifier: %v", outcome.notifications[0].reviewer)
}
}
func init() {
logger = log.New()
}