-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifier_test.go
131 lines (110 loc) · 2.99 KB
/
notifier_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
package bqloader_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"testing"
"go.nownabe.dev/bqloader"
)
type roundTripperFunc func(req *http.Request) *http.Response
func (f roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}
func newTestClient(f roundTripperFunc) *http.Client {
return &http.Client{Transport: f}
}
type slackMessage struct {
Channel string `json:"channel"`
IconEmoji string `json:"icon_emoji,omitempty"`
Text string `json:"text"`
Username string `json:"username,omitempty"`
}
const validSlackToken = "validToken"
func newSlackClient() *http.Client {
return newTestClient(func(req *http.Request) *http.Response {
resBody := func() string {
if req.Header.Get("Authorization") != "Bearer "+validSlackToken {
return `{"ok":false,"error":"not_authed"}`
}
reqBody, err := ioutil.ReadAll(req.Body)
if err != nil {
return ""
}
var msg slackMessage
if err := json.Unmarshal(reqBody, &msg); err != nil {
return `{"ok":false,"error":"invalid_form_data"}`
}
if msg.Channel == "" {
return `{"ok":false,"error":"channel_not_found"}`
}
return `{"ok":true}`
}()
if resBody == "" {
return &http.Response{StatusCode: http.StatusInternalServerError}
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBufferString(resBody)),
Header: http.Header{},
}
})
}
func TestSlackNotifier(t *testing.T) {
t.Parallel()
result := &bqloader.Result{
Event: bqloader.Event{Name: "testfile"},
Handler: &bqloader.Handler{Name: "myhandler"},
}
cases := map[string]struct {
notifier *bqloader.SlackNotifier
result *bqloader.Result
expectedHasError bool
}{
"succeeded": {
notifier: &bqloader.SlackNotifier{Channel: "#channel", Token: validSlackToken},
result: result,
expectedHasError: false,
},
"failed": {
notifier: &bqloader.SlackNotifier{Channel: "#channel", Token: validSlackToken},
result: &bqloader.Result{
Event: bqloader.Event{Name: "testfile"},
Handler: &bqloader.Handler{Name: "myhandler"},
Error: fmt.Errorf("some error"),
},
expectedHasError: false,
},
"no token": {
notifier: &bqloader.SlackNotifier{Channel: "#channel"},
result: result,
expectedHasError: true,
},
"with options": {
notifier: &bqloader.SlackNotifier{
Channel: "#channel",
Token: validSlackToken,
IconEmoji: ":poop:",
Username: "username",
},
result: result,
expectedHasError: false,
},
}
slackClient := newSlackClient()
for name, c := range cases {
c.notifier.HTTPClient = slackClient
t.Run(name, func(t *testing.T) {
t.Parallel()
err := c.notifier.Notify(context.Background(), c.result)
if c.expectedHasError && err == nil {
t.Errorf("expected error but no error occurred")
}
if !c.expectedHasError && err != nil {
t.Errorf("unexpected error: %s", err)
}
})
}
}