Debian Codegraph @

bookmarks_test.go

raw symbols SCIP
1 package slack
2
3 import (
4 "encoding/json"
5 "fmt"
6 "net/http"
7 "reflect"
8 "testing"
9 )
10
11 func getTestBookmark(channelID, bookmarkID string) Bookmark {
12 return Bookmark{
13 ID: bookmarkID,
14 ChannelID: channelID,
15 Title: "bookmark",
16 Type: "link",
17 Link: "https://example.com",
18 IconURL: "https://example.com/icon.png",
19 }
20 }
21
22 func addBookmarkLinkHandler(rw http.ResponseWriter, r *http.Request) {
23 channelID := r.FormValue("channel_id")
24 title := r.FormValue("title")
25 bookmarkType := r.FormValue("type")
26 link := r.FormValue("link")
27
28 rw.Header().Set("Content-Type", "application/json")
29
30 if bookmarkType == "link" && link != "" && channelID != "" && title != "" {
31 bookmark := getTestBookmark(channelID, "Bk123RBZG8GZ")
32 bookmark.Title = title
33 bookmark.Type = bookmarkType
34 bookmark.Link = link
35
36 resp, _ := json.Marshal(&addBookmarkResponse{
37 SlackResponse: SlackResponse{Ok: true},
38 Bookmark: bookmark})
39 rw.Write(resp)
40 } else {
41 rw.Write([]byte(`{ "ok": false, "error": "errored" }`))
42 }
43 }
44
45 func TestAddBookmarkLink(t *testing.T) {
46 http.HandleFunc("/bookmarks.add", addBookmarkLinkHandler)
47 once.Do(startServer)
48 api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
49 params := AddBookmarkParameters{
50 Title: "test",
51 Type: "link",
52 Link: "https://example.com",
53 }
54 _, err := api.AddBookmark("CXXXXXXXX", params)
55 if err != nil {
56 t.Fatalf("Unexpected error: %s", err)
57 }
58 }
59
60 func listBookmarksHandler(rw http.ResponseWriter, r *http.Request) {
61 channelID := r.FormValue("channel_id")
62
63 rw.Header().Set("Content-Type", "application/json")
64
65 if channelID != "" {
66 bookmarks := []Bookmark{
67 getTestBookmark(channelID, "Bk001"),
68 getTestBookmark(channelID, "Bk002"),
69 getTestBookmark(channelID, "Bk003"),
70 getTestBookmark(channelID, "Bk004"),
71 }
72
73 resp, _ := json.Marshal(&listBookmarksResponse{
74 SlackResponse: SlackResponse{Ok: true},
75 Bookmarks: bookmarks})
76 rw.Write(resp)
77 } else {
78 rw.Write([]byte(`{ "ok": false, "error": "errored" }`))
79 }
80 }
81
82 func TestListBookmarks(t *testing.T) {
83 http.HandleFunc("/bookmarks.list", listBookmarksHandler)
84 once.Do(startServer)
85 api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
86 channel := "CXXXXXXXX"
87 bookmarks, err := api.ListBookmarks(channel)
88 if err != nil {
89 t.Fatalf("Unexpected error: %s", err)
90 }
91
92 if !reflect.DeepEqual([]Bookmark{
93 getTestBookmark(channel, "Bk001"),
94 getTestBookmark(channel, "Bk002"),
95 getTestBookmark(channel, "Bk003"),
96 getTestBookmark(channel, "Bk004"),
97 }, bookmarks) {
98 t.Fatal(ErrIncorrectResponse)
99 }
100 }
101
102 func removeBookmarkHandler(bookmark *Bookmark) func(rw http.ResponseWriter, r *http.Request) {
103 return func(rw http.ResponseWriter, r *http.Request) {
104 channelID := r.FormValue("channel_id")
105 bookmarkID := r.FormValue("bookmark_id")
106
107 rw.Header().Set("Content-Type", "application/json")
108
109 if channelID == bookmark.ChannelID && bookmarkID == bookmark.ID {
110 rw.Write([]byte(`{ "ok": true }`))
111 } else {
112 rw.Write([]byte(`{ "ok": false, "error": "errored" }`))
113 }
114 }
115 }
116
117 func TestRemoveBookmark(t *testing.T) {
118 channel := "CXXXXXXXX"
119 bookmark := getTestBookmark(channel, "BkXXXXX")
120 http.HandleFunc("/bookmarks.remove", removeBookmarkHandler(&bookmark))
121 once.Do(startServer)
122 api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
123
124 err := api.RemoveBookmark(channel, bookmark.ID)
125 if err != nil {
126 t.Fatalf("Unexpected error: %s", err)
127 }
128 }
129
130 func editBookmarkHandler(bookmarks []Bookmark) func(rw http.ResponseWriter, r *http.Request) {
131 return func(rw http.ResponseWriter, r *http.Request) {
132 channelID := r.FormValue("channel_id")
133 bookmarkID := r.FormValue("bookmark_id")
134
135 rw.Header().Set("Content-Type", "application/json")
136 if err := r.ParseForm(); err != nil {
137 httpTestErrReply(rw, true, fmt.Sprintf("err parsing form: %s", err.Error()))
138 return
139 }
140
141 for _, bookmark := range bookmarks {
142 if bookmark.ID == bookmarkID && bookmark.ChannelID == channelID {
143 if v := r.Form.Get("link"); v != "" {
144 bookmark.Link = v
145 }
146 // Emoji and title require special handling since empty string sets to null
147 if _, ok := r.Form["emoji"]; ok {
148 bookmark.Emoji = r.Form.Get("emoji")
149 }
150 if _, ok := r.Form["title"]; ok {
151 bookmark.Title = r.Form.Get("title")
152 }
153 resp, _ := json.Marshal(&editBookmarkResponse{
154 SlackResponse: SlackResponse{Ok: true},
155 Bookmark: bookmark})
156 rw.Write(resp)
157 return
158 }
159 }
160 // Fail if the bookmark doesn't exist
161 rw.Write([]byte(`{ "ok": false, "error": "not_found" }`))
162 }
163 }
164
165 func TestEditBookmark(t *testing.T) {
166 channel := "CXXXXXXXX"
167 bookmarks := []Bookmark{
168 getTestBookmark(channel, "Bk001"),
169 getTestBookmark(channel, "Bk002"),
170 getTestBookmark(channel, "Bk003"),
171 getTestBookmark(channel, "Bk004"),
172 }
173 http.HandleFunc("/bookmarks.edit", editBookmarkHandler(bookmarks))
174 once.Do(startServer)
175 api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
176
177 smileEmoji := ":smile:"
178 empty := ""
179 title := "hello, world!"
180 changes := []struct {
181 ID string
182 Params EditBookmarkParameters
183 }{
184 { // add emoji
185 ID: "Bk001",
186 Params: EditBookmarkParameters{Emoji: &smileEmoji},
187 },
188 { // delete emoji
189 ID: "Bk001",
190 Params: EditBookmarkParameters{Emoji: &empty},
191 },
192 { // add title
193 ID: "Bk002",
194 Params: EditBookmarkParameters{Title: &title},
195 },
196 { // delete title
197 ID: "Bk002",
198 Params: EditBookmarkParameters{Title: &empty},
199 },
200 { // Change multiple fields at once
201 ID: "Bk003",
202 Params: EditBookmarkParameters{
203 Title: &title,
204 Emoji: &empty,
205 Link: "https://example.com/changed",
206 },
207 },
208 { // noop
209 ID: "Bk004",
210 },
211 }
212
213 for _, change := range changes {
214 bookmark, err := api.EditBookmark(channel, change.ID, change.Params)
215 if err != nil {
216 t.Fatalf("Unexpected error: %s", err)
217 }
218 if change.ID != bookmark.ID {
219 t.Fatalf("expected to modify bookmark with ID = %s, got %s", change.ID, bookmark.ID)
220 }
221 if change.Params.Emoji != nil && bookmark.Emoji != *change.Params.Emoji {
222 t.Fatalf("expected bookmark.Emoji = %s, got %s", *change.Params.Emoji, bookmark.Emoji)
223 }
224 if change.Params.Title != nil && bookmark.Title != *change.Params.Title {
225 t.Fatalf("expected bookmark.Title = %s, got %s", *change.Params.Title, bookmark.Emoji)
226 }
227 if change.Params.Link != "" && change.Params.Link != bookmark.Link {
228 t.Fatalf("expected bookmark.Link = %s, got %s", change.Params.Link, bookmark.Link)
229 }
230 }
231
232 // Cover the final case of trying to edit a bookmark which doesn't exist
233 bookmark, err := api.EditBookmark(channel, "BkMissing", EditBookmarkParameters{})
234 if err == nil {
235 t.Fatalf("Expected not found error, but got bookmark %s", bookmark.ID)
236 }
237 }