| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "reflect" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | type starsHandler struct { |
| 11 | gotParams map[string]string |
| 12 | response string |
| 13 | } |
| 14 | |
| 15 | func newStarsHandler() *starsHandler { |
| 16 | return &starsHandler{ |
| 17 | gotParams: make(map[string]string), |
| 18 | response: `{ "ok": true }`, |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | func (sh *starsHandler) accumulateFormValue(k string, r *http.Request) { |
| 23 | if v := r.FormValue(k); v != "" { |
| 24 | sh.gotParams[k] = v |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func (sh *starsHandler) handler(w http.ResponseWriter, r *http.Request) { |
| 29 | sh.accumulateFormValue("user", r) |
| 30 | sh.accumulateFormValue("count", r) |
| 31 | sh.accumulateFormValue("channel", r) |
| 32 | sh.accumulateFormValue("file", r) |
| 33 | sh.accumulateFormValue("file_comment", r) |
| 34 | sh.accumulateFormValue("page", r) |
| 35 | sh.accumulateFormValue("timestamp", r) |
| 36 | w.Header().Set("Content-Type", "application/json") |
| 37 | w.Write([]byte(sh.response)) |
| 38 | } |
| 39 | |
| 40 | func TestSlack_AddStar(t *testing.T) { |
| 41 | once.Do(startServer) |
| 42 | api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) |
| 43 | tests := []struct { |
| 44 | channel string |
| 45 | ref ItemRef |
| 46 | wantParams map[string]string |
| 47 | }{ |
| 48 | { |
| 49 | "ChannelID", |
| 50 | NewRefToMessage("ChannelID", "123"), |
| 51 | map[string]string{ |
| 52 | "channel": "ChannelID", |
| 53 | "timestamp": "123", |
| 54 | }, |
| 55 | }, |
| 56 | { |
| 57 | "ChannelID", |
| 58 | NewRefToFile("FileID"), |
| 59 | map[string]string{ |
| 60 | "channel": "ChannelID", |
| 61 | "file": "FileID", |
| 62 | }, |
| 63 | }, |
| 64 | { |
| 65 | "ChannelID", |
| 66 | NewRefToComment("FileCommentID"), |
| 67 | map[string]string{ |
| 68 | "channel": "ChannelID", |
| 69 | "file_comment": "FileCommentID", |
| 70 | }, |
| 71 | }, |
| 72 | } |
| 73 | var rh *starsHandler |
| 74 | http.HandleFunc("/stars.add", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) }) |
| 75 | for i, test := range tests { |
| 76 | rh = newStarsHandler() |
| 77 | err := api.AddStar(test.channel, test.ref) |
| 78 | if err != nil { |
| 79 | t.Fatalf("%d: Unexpected error: %s", i, err) |
| 80 | } |
| 81 | if !reflect.DeepEqual(rh.gotParams, test.wantParams) { |
| 82 | t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams) |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func TestSlack_RemoveStar(t *testing.T) { |
| 88 | once.Do(startServer) |
| 89 | api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) |
| 90 | tests := []struct { |
| 91 | channel string |
| 92 | ref ItemRef |
| 93 | wantParams map[string]string |
| 94 | }{ |
| 95 | { |
| 96 | "ChannelID", |
| 97 | NewRefToMessage("ChannelID", "123"), |
| 98 | map[string]string{ |
| 99 | "channel": "ChannelID", |
| 100 | "timestamp": "123", |
| 101 | }, |
| 102 | }, |
| 103 | { |
| 104 | "ChannelID", |
| 105 | NewRefToFile("FileID"), |
| 106 | map[string]string{ |
| 107 | "channel": "ChannelID", |
| 108 | "file": "FileID", |
| 109 | }, |
| 110 | }, |
| 111 | { |
| 112 | "ChannelID", |
| 113 | NewRefToComment("FileCommentID"), |
| 114 | map[string]string{ |
| 115 | "channel": "ChannelID", |
| 116 | "file_comment": "FileCommentID", |
| 117 | }, |
| 118 | }, |
| 119 | } |
| 120 | var rh *starsHandler |
| 121 | http.HandleFunc("/stars.remove", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) }) |
| 122 | for i, test := range tests { |
| 123 | rh = newStarsHandler() |
| 124 | err := api.RemoveStar(test.channel, test.ref) |
| 125 | if err != nil { |
| 126 | t.Fatalf("%d: Unexpected error: %s", i, err) |
| 127 | } |
| 128 | if !reflect.DeepEqual(rh.gotParams, test.wantParams) { |
| 129 | t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams) |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func TestSlack_ListStars(t *testing.T) { |
| 135 | once.Do(startServer) |
| 136 | api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) |
| 137 | rh := newStarsHandler() |
| 138 | http.HandleFunc("/stars.list", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) }) |
| 139 | rh.response = `{"ok": true, |
| 140 | "items": [ |
| 141 | { |
| 142 | "type": "message", |
| 143 | "channel": "C1", |
| 144 | "message": { |
| 145 | "text": "hello", |
| 146 | "reactions": [ |
| 147 | { |
| 148 | "name": "astonished", |
| 149 | "count": 3, |
| 150 | "users": [ "U1", "U2", "U3" ] |
| 151 | }, |
| 152 | { |
| 153 | "name": "clock1", |
| 154 | "count": 3, |
| 155 | "users": [ "U1", "U2" ] |
| 156 | } |
| 157 | ] |
| 158 | } |
| 159 | }, |
| 160 | { |
| 161 | "type": "file", |
| 162 | "file": { |
| 163 | "name": "toy", |
| 164 | "reactions": [ |
| 165 | { |
| 166 | "name": "clock1", |
| 167 | "count": 3, |
| 168 | "users": [ "U1", "U2" ] |
| 169 | } |
| 170 | ] |
| 171 | } |
| 172 | }, |
| 173 | { |
| 174 | "type": "file_comment", |
| 175 | "file": { |
| 176 | "name": "toy" |
| 177 | }, |
| 178 | "comment": { |
| 179 | "comment": "cool toy", |
| 180 | "reactions": [ |
| 181 | { |
| 182 | "name": "astonished", |
| 183 | "count": 3, |
| 184 | "users": [ "U1", "U2", "U3" ] |
| 185 | } |
| 186 | ] |
| 187 | } |
| 188 | } |
| 189 | ], |
| 190 | "paging": { |
| 191 | "count": 100, |
| 192 | "total": 4, |
| 193 | "page": 1, |
| 194 | "pages": 1 |
| 195 | }}` |
| 196 | want := []Item{ |
| 197 | NewMessageItem("C1", &Message{Msg: Msg{ |
| 198 | Text: "hello", |
| 199 | Reactions: []ItemReaction{ |
| 200 | {Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}}, |
| 201 | {Name: "clock1", Count: 3, Users: []string{"U1", "U2"}}, |
| 202 | }, |
| 203 | }}), |
| 204 | NewFileItem(&File{Name: "toy"}), |
| 205 | NewFileCommentItem(&File{Name: "toy"}, &Comment{Comment: "cool toy"}), |
| 206 | } |
| 207 | wantStarred := make([]StarredItem, len(want)) |
| 208 | for i, item := range want { |
| 209 | wantStarred[i] = StarredItem(item) |
| 210 | } |
| 211 | wantParams := map[string]string{ |
| 212 | "count": "200", |
| 213 | "page": "2", |
| 214 | } |
| 215 | params := NewStarsParameters() |
| 216 | params.Count = 200 |
| 217 | params.Page = 2 |
| 218 | got, paging, err := api.ListStars(params) |
| 219 | if err != nil { |
| 220 | t.Fatalf("Unexpected error: %s", err) |
| 221 | } |
| 222 | if !reflect.DeepEqual(got, want) { |
| 223 | t.Errorf("Got Stars %#v, want %#v", got, want) |
| 224 | for i, item := range got { |
| 225 | fmt.Printf("Item %d, Type: %s\n", i, item.Type) |
| 226 | fmt.Printf("Message %#v\n", item.Message) |
| 227 | fmt.Printf("File %#v\n", item.File) |
| 228 | fmt.Printf("Comment %#v\n", item.Comment) |
| 229 | } |
| 230 | } |
| 231 | if !reflect.DeepEqual(rh.gotParams, wantParams) { |
| 232 | t.Errorf("Got params %#v, want %#v", rh.gotParams, wantParams) |
| 233 | } |
| 234 | if reflect.DeepEqual(paging, Paging{}) { |
| 235 | t.Errorf("Want paging data, got empty struct") |
| 236 | } |
| 237 | // Test GetStarred |
| 238 | gotStarred, paging, err := api.GetStarred(params) |
| 239 | if err != nil { |
| 240 | t.Fatalf("Unexpected error: %s", err) |
| 241 | } |
| 242 | if !reflect.DeepEqual(gotStarred, wantStarred) { |
| 243 | t.Errorf("Got Stars %#v, want %#v", gotStarred, wantStarred) |
| 244 | for i, item := range got { |
| 245 | fmt.Printf("Item %d, Type: %s\n", i, item.Type) |
| 246 | fmt.Printf("Message %#v\n", item.Message) |
| 247 | fmt.Printf("File %#v\n", item.File) |
| 248 | fmt.Printf("Comment %#v\n", item.Comment) |
| 249 | } |
| 250 | } |
| 251 | if !reflect.DeepEqual(rh.gotParams, wantParams) { |
| 252 | t.Errorf("Got params %#v, want %#v", rh.gotParams, wantParams) |
| 253 | } |
| 254 | if reflect.DeepEqual(paging, Paging{}) { |
| 255 | t.Errorf("Want paging data, got empty struct") |
| 256 | } |
| 257 | } |