forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 4
/
files_test.go
213 lines (188 loc) · 5.37 KB
/
files_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
213
package slack
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/url"
"reflect"
"strings"
"testing"
)
type fileCommentHandler struct {
gotParams map[string]string
}
func newFileCommentHandler() *fileCommentHandler {
return &fileCommentHandler{
gotParams: make(map[string]string),
}
}
func (h *fileCommentHandler) accumulateFormValue(k string, r *http.Request) {
if v := r.FormValue(k); v != "" {
h.gotParams[k] = v
}
}
func (h *fileCommentHandler) handler(w http.ResponseWriter, r *http.Request) {
h.accumulateFormValue("token", r)
h.accumulateFormValue("file", r)
h.accumulateFormValue("id", r)
w.Header().Set("Content-Type", "application/json")
if h.gotParams["id"] == "trigger-error" {
w.Write([]byte(`{ "ok": false, "error": "errored" }`))
} else {
w.Write([]byte(`{ "ok": true }`))
}
}
type mockHTTPClient struct{}
func (m *mockHTTPClient) Do(*http.Request) (*http.Response, error) {
return &http.Response{StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString(`OK`))}, nil
}
func TestSlack_GetFile(t *testing.T) {
api := &Client{
endpoint: "http://" + serverAddr + "/",
token: "testing-token",
httpclient: &mockHTTPClient{},
}
tests := []struct {
title string
downloadURL string
expectError bool
}{
{
title: "Testing with valid file",
downloadURL: "https://files.slack.com/files-pri/T99999999-FGGGGGGGG/download/test.csv",
expectError: false,
},
{
title: "Testing with invalid file (empty URL)",
downloadURL: "",
expectError: true,
},
}
for _, test := range tests {
err := api.GetFile(test.downloadURL, &bytes.Buffer{})
if !test.expectError && err != nil {
log.Fatalf("%s: Unexpected error: %s in test", test.title, err)
} else if test.expectError == true && err == nil {
log.Fatalf("Expected error but got none")
}
}
}
func TestSlack_DeleteFileComment(t *testing.T) {
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
tests := []struct {
title string
body url.Values
wantParams map[string]string
expectError bool
}{
{
title: "Testing with proper body",
body: url.Values{
"file": {"file12345"},
"id": {"id12345"},
},
wantParams: map[string]string{
"token": "testing-token",
"file": "file12345",
"id": "id12345",
},
expectError: false,
},
{
title: "Testing with false body",
body: url.Values{
"file": {""},
"id": {""},
},
wantParams: map[string]string{},
expectError: true,
},
{
title: "Testing with error",
body: url.Values{
"file": {"file12345"},
"id": {"trigger-error"},
},
wantParams: map[string]string{
"token": "testing-token",
"file": "file12345",
"id": "trigger-error",
},
expectError: true,
},
}
var fch *fileCommentHandler
http.HandleFunc("/files.comments.delete", func(w http.ResponseWriter, r *http.Request) {
fch.handler(w, r)
})
for _, test := range tests {
fch = newFileCommentHandler()
err := api.DeleteFileComment(test.body["id"][0], test.body["file"][0])
if !test.expectError && err != nil {
log.Fatalf("%s: Unexpected error: %s in test", test.title, err)
} else if test.expectError == true && err == nil {
log.Fatalf("Expected error but got none")
}
if !reflect.DeepEqual(fch.gotParams, test.wantParams) {
log.Fatalf("%s: Got params [%#v]\nBut received [%#v]\n", test.title, fch.gotParams, test.wantParams)
}
}
}
func authTestHandler(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(authTestResponseFull{
SlackResponse: SlackResponse{Ok: true}})
rw.Write(response)
}
func uploadFileHandler(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(fileResponseFull{
SlackResponse: SlackResponse{Ok: true}})
rw.Write(response)
}
func TestUploadFile(t *testing.T) {
http.HandleFunc("/auth.test", authTestHandler)
http.HandleFunc("/files.upload", uploadFileHandler)
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
params := FileUploadParameters{
Filename: "test.txt", Content: "test content",
Channels: []string{"CXXXXXXXX"}}
if _, err := api.UploadFile(params); err != nil {
t.Errorf("Unexpected error: %s", err)
}
reader := bytes.NewBufferString("test reader")
params = FileUploadParameters{
Filename: "test.txt",
Reader: reader,
Channels: []string{"CXXXXXXXX"}}
if _, err := api.UploadFile(params); err != nil {
t.Errorf("Unexpected error: %s", err)
}
largeByt := make([]byte, 107374200)
reader = bytes.NewBuffer(largeByt)
params = FileUploadParameters{
Filename: "test.txt", Reader: reader,
Channels: []string{"CXXXXXXXX"}}
if _, err := api.UploadFile(params); err != nil {
t.Errorf("Unexpected error: %s", err)
}
}
func TestUploadFileWithoutFilename(t *testing.T) {
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
reader := bytes.NewBufferString("test reader")
params := FileUploadParameters{
Reader: reader,
Channels: []string{"CXXXXXXXX"}}
_, err := api.UploadFile(params)
if err == nil {
t.Fatal("Expected error when omitting filename, instead got nil")
}
if !strings.Contains(err.Error(), ".Filename is mandatory") {
t.Errorf("Error message should mention empty FileUploadParameters.Filename")
}
}