forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookmarks_test.go
153 lines (129 loc) · 3.37 KB
/
bookmarks_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
package slack
import (
"net/http"
"testing"
)
var listBookmarksResp = `{
"ok": true,
"bookmarks": [
{
"id": "Bk12345",
"channel_id": "C12345",
"title": "Homepage",
"link": "https://app.incident.io/incidents/12",
"emoji": ":globe_with_meridians:",
"icon_url": null,
"type": "link",
"entity_id": null,
"date_created": 1644767331,
"date_updated": 0,
"rank": "U",
"last_updated_by_user_id": "U12345",
"last_updated_by_team_id": "T12345",
"shortcut_id": null,
"app_id": null
}
]
}`
func listBookmarks(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
rw.Write([]byte(listBookmarksResp))
}
func TestBookmarkList(t *testing.T) {
http.HandleFunc("/bookmarks.list", listBookmarks)
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
bookmarks, err := api.ListBookmarks("C12345")
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
if len(bookmarks) != 1 {
t.Fatalf("expected 1 bookmark, got %d", len(bookmarks))
return
}
bookmark := bookmarks[0]
if bookmark.ID != "Bk12345" {
t.Fatalf("expected bookmark ID Bk12345, got %s", bookmark.ID)
}
}
var singleBookmarkResp = `{
"ok": true,
"bookmark": {
"id": "Bk12345",
"channel_id": "C12345",
"title": "Homepage",
"link": "https://app.incident.io/incidents/12",
"emoji": ":globe_with_meridians:",
"icon_url": null,
"type": "link",
"entity_id": null,
"date_created": 1644767331,
"date_updated": 0,
"rank": "U",
"last_updated_by_user_id": "U12345",
"last_updated_by_team_id": "T12345",
"shortcut_id": null,
"app_id": null
}
}`
func getBookmark(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
rw.Write([]byte(singleBookmarkResp))
}
func TestAddBookmark(t *testing.T) {
http.HandleFunc("/bookmarks.add", getBookmark)
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
bookmark, err := api.AddBookmark(AddBookmarkParameters{
ChannelID: "C12345",
Title: "Homepage",
Type: "link",
})
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
if bookmark == nil {
t.Fatal("bookmark returned was nil")
return
}
if bookmark.ID != "Bk12345" {
t.Errorf("bookmark ID should be Bk12345, got %s", bookmark.ID)
}
}
func TestEditBookmark(t *testing.T) {
http.HandleFunc("/bookmarks.edit", getBookmark)
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
bookmark, err := api.EditBookmark(EditBookmarkParameters{
ChannelID: "C12345",
BookmarkID: "Bk12345",
Emoji: ":siren:",
Title: "hello2",
})
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
if bookmark == nil {
t.Fatal("bookmark returned was nil")
return
}
if bookmark.ID != "Bk12345" {
t.Errorf("bookmark ID should be Bk12345, got %s", bookmark.ID)
}
}
func okResponse(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte(`{"ok": true}`))
}
func TestRemoveBookmark(t *testing.T) {
http.HandleFunc("/bookmarks.remove", getBookmark)
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
err := api.RemoveBookmark("C12345", "Bk12345")
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
}