-
Notifications
You must be signed in to change notification settings - Fork 1
/
kik_test.go
212 lines (189 loc) · 4.98 KB
/
kik_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Package system runs tests against the real Kik API.
// Generally they will only fail if there is an error in the payloads being sent.
// This is useful to verify the Kik API hasn't introduced breaking request / response types.
package system
import (
"fmt"
"github.com/4kelly/go-kik/kik"
"github.com/google/go-cmp/cmp"
"io/ioutil"
"log"
"net/http"
"os"
"testing"
)
var (
kikClient *kik.Client
err error
)
func init() {
username := os.Getenv("KIKBOT_USERNAME")
key := os.Getenv("KIKBOT_API_KEY")
webhook := os.Getenv("KIKBOT_WEBHOOK")
if username == "" {
log.Fatal("!!! No KIKBOT_USERNAME set. Tests can't run !!!\n\n")
}
if key == "" {
log.Fatal("!!! No KIKBOT_API_KEY set. Tests can't run !!!\n\n")
}
if webhook == "" {
log.Fatal("!!! No KIKBOT_WEBHOOK set. Tests can't run !!!\n\n")
}
kikClient, err = kik.NewKikClient("https://api.kik.com/", username, key, nil)
if err != nil {
log.Fatalf("could not initiate client: %v ", err)
}
err = kikClient.SetConfiguration(&kik.Configuration{
Webhook: webhook,
Features: kik.Features{},
StaticKeyboard: nil,
})
if err != nil {
log.Fatalf("could not configure kik client: %v ", err)
}
}
func TestGetUser_HappyPath(t *testing.T) {
_, err := kikClient.GetUser(testUserName)
if err != nil {
t.Errorf("Could not find user: %v ", err)
}
}
func TestSendMessage_HappyPath(t *testing.T) {
err := kikClient.SendMessage(allMessageTypesTestData)
if err != nil {
t.Errorf("Error while trying to send a message. %v.", err)
}
}
func TestBroadcastMessage_HappyPath(t *testing.T) {
err := kikClient.BroadcastMessage(allMessageTypesTestData)
if err != nil {
t.Errorf("Error while trying to broadcast a message. %v.", err)
}
}
// TestConfig_HappyPath Sets then gets Kik bot configuration.
func TestConfig_HappyPath(t *testing.T) {
keyboard := &kik.SuggestedResponseKeyboard{
Type: "suggested",
Responses: []interface{}{
kik.KeyboardTextResponse{
Type: "text",
Body: "StaticKeyboardTest",
},
},
}
wantConfig := &kik.Configuration{
Webhook: "http://example.com",
Features: kik.Features{},
StaticKeyboard: keyboard,
}
err := kikClient.SetConfiguration(wantConfig)
if err != nil {
t.Errorf("Error while trying to set configuration. %v.", err)
}
gotConfig, err := kikClient.GetConfiguration()
if err != nil {
t.Errorf("Error while trying to get configuration. %v.", err)
}
if !cmp.Equal(gotConfig, wantConfig) {
t.Errorf("SetConfiguration() = %v; want %v", gotConfig, wantConfig)
}
}
// TestCreateCode_HappyPath Creates then gets image for a Kik Scan Code.
func TestCreateCode_HappyPath(t *testing.T) {
scanCodeData := &kik.ScanData{
Data: "Kik Scan Code Example Data!",
}
code, err := kikClient.CreateCode(scanCodeData)
if err != nil {
t.Errorf("Error while trying create a Kik scan code. %v.", err)
}
url := fmt.Sprintf("https://api.kik.com/v1/code/%s?c=1", code.Id)
codeID, err := http.Get(url)
if err != nil {
t.Errorf("Error while trying to get scan code image. %v.", err)
}
defer codeID.Body.Close()
body, _ := ioutil.ReadAll(codeID.Body)
contentType := http.DetectContentType(body)
if contentType != "image/png" {
t.Errorf("Returned data must be an image.")
}
}
/*
Test data
*/
// Who we send test messages to
var testUserName = "rmdkelly"
// Contains an example of all the keyboard response types.
var allKeyboardTypesTestData = []kik.SuggestedResponseKeyboard{
{Type: "suggested",
Responses: []interface{}{
kik.KeyboardPictureResponse{
Type: "picture",
PicUrl: "https://i.imgur.com/8rqLdgy.png",
Metadata: "picture1",
},
kik.KeyboardFriendPickerResponse{
Type: "friend-picker",
Body: "Test",
Min: 0,
Max: 2,
Preselected: []string{"cacolvil"},
},
kik.KeyboardTextResponse{
Type: "text",
Body: "KeyboardTextResponse",
},
},
},
}
// Contains an example of all the message types
var allMessageTypesTestData = []kik.Message{
kik.TextMessage{
SendMessage: kik.SendMessage{
To: testUserName,
Type: "text",
},
Body: "Test_TestMessage",
},
kik.PictureMessage{
SendMessage: kik.SendMessage{
To: testUserName,
Type: "picture",
},
PicUrl: "https://i.imgur.com/TsoLODG.png",
Attribution: &kik.Attribution{
Name: "Attribution Test",
},
},
kik.LinkMessage{
SendMessage: kik.SendMessage{
To: testUserName,
Type: "link",
},
Url: "https://duckduckgo.com/",
PicUrl: "https://i.imgur.com/hp5ix8B.jpg",
Title: "Link Test",
Text: "Such testing.",
NoForward: false,
Attribution: &kik.Attribution{
Name: "Attribution Test",
},
},
kik.VideoMessage{
SendMessage: kik.SendMessage{
To: testUserName,
Type: "video",
Delay: 500,
Keyboards: allKeyboardTypesTestData,
},
VideoUrl: "https://media.tenor.com/videos/a912c20be335cfd78610916c97198438/mp4",
Loop: true,
Muted: false,
Autoplay: true,
NoSave: false,
Attribution: &kik.Attribution{
Name: "Attribution Test",
},
},
}