forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsers_test.go
145 lines (138 loc) · 2.83 KB
/
parsers_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
package slackevents
import (
"encoding/json"
"fmt"
"testing"
"github.com/nlopes/slack"
)
func TestParserOuterCallBackEvent(t *testing.T) {
eventsAPIRawCallbackEvent := `
{
"token": "XXYYZZ",
"team_id": "TXXXXXXXX",
"api_app_id": "AXXXXXXXXX",
"event": {
"type": "app_mention",
"event_ts": "1234567890.123456",
"user": "UXXXXXXX1"
},
"type": "event_callback",
"authed_users": [ "UXXXXXXX1" ],
"event_id": "Ev08MFMKH6",
"event_time": 1234567890
}
`
msg, e := ParseEvent(json.RawMessage(eventsAPIRawCallbackEvent), OptionVerifyToken(&TokenComparator{"XXYYZZ"}))
if e != nil {
fmt.Println(e)
t.Fail()
}
switch ev := msg.Data.(type) {
case *EventsAPICallbackEvent:
{
}
case *slack.UnmarshallingErrorEvent:
{
fmt.Println("Unmarshalling Error!")
fmt.Println(ev)
t.Fail()
}
default:
{
fmt.Println(ev)
t.Fail()
}
}
}
func TestParseURLVerificationEvent(t *testing.T) {
urlVerificationEvent := `
{
"token": "fake-token",
"challenge": "aljdsflaji3jj",
"type": "url_verification"
}
`
msg, e := ParseEvent(json.RawMessage(urlVerificationEvent), OptionVerifyToken(&TokenComparator{"fake-token"}))
if e != nil {
fmt.Println(e)
t.Fail()
}
switch ev := msg.Data.(type) {
case *EventsAPIURLVerificationEvent:
{
}
default:
{
fmt.Println(ev)
t.Fail()
}
}
}
func TestThatOuterCallbackEventHasInnerEvent(t *testing.T) {
eventsAPIRawCallbackEvent := `
{
"token": "XXYYZZ",
"team_id": "TXXXXXXXX",
"api_app_id": "AXXXXXXXXX",
"event": {
"type": "app_mention",
"event_ts": "1234567890.123456",
"user": "UXXXXXXX1"
},
"type": "event_callback",
"authed_users": [ "UXXXXXXX1" ],
"event_id": "Ev08MFMKH6",
"event_time": 1234567890
}
`
msg, e := ParseEvent(json.RawMessage(eventsAPIRawCallbackEvent), OptionVerifyToken(&TokenComparator{"XXYYZZ"}))
if e != nil {
fmt.Println(e)
t.Fail()
}
switch outterEvent := msg.Data.(type) {
case *EventsAPICallbackEvent:
{
switch innerEvent := msg.InnerEvent.Data.(type) {
case *AppMentionEvent:
{
}
default:
fmt.Println(innerEvent)
t.Fail()
}
}
default:
{
fmt.Println(outterEvent)
t.Fail()
}
}
}
func TestBadTokenVerification(t *testing.T) {
urlVerificationEvent := `
{
"token": "fake-token",
"challenge": "aljdsflaji3jj",
"type": "url_verification"
}
`
_, e := ParseEvent(json.RawMessage(urlVerificationEvent), OptionVerifyToken(TokenComparator{"real-token"}))
if e == nil {
t.Fail()
}
}
func TestNoTokenVerification(t *testing.T) {
urlVerificationEvent := `
{
"token": "fake-token",
"challenge": "aljdsflaji3jj",
"type": "url_verification"
}
`
_, e := ParseEvent(json.RawMessage(urlVerificationEvent), OptionNoVerifyToken())
if e != nil {
fmt.Println(e)
t.Fail()
}
}