forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
files_test.go
144 lines (129 loc) · 3.53 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
package slack
import (
"bytes"
"encoding/json"
"log"
"net/http"
"net/url"
"reflect"
"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 }`))
}
}
func TestSlack_DeleteFileComment(t *testing.T) {
once.Do(startServer)
SLACK_API = "http://" + serverAddr + "/"
api := New("testing-token")
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)
SLACK_API = "http://" + serverAddr + "/"
api := New("testing-token")
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)
}
}