-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathadmin_conversations_restrictAccess_test.go
More file actions
119 lines (100 loc) · 3.19 KB
/
admin_conversations_restrictAccess_test.go
File metadata and controls
119 lines (100 loc) · 3.19 KB
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
package slack
import (
"context"
"encoding/json"
"net/http"
"testing"
)
func TestAdminConversationsRestrictAccessAddGroup(t *testing.T) {
http.DefaultServeMux = new(http.ServeMux)
http.HandleFunc("/admin.conversations.restrictAccess.addGroup", mockRestrictAccessHandler(t, "group_id"))
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
err := api.AdminConversationsRestrictAccessAddGroup(context.Background(),
"C1234567890",
"G123",
AdminConversationsRestrictAccessAddGroupOptionTeamID("T789"),
)
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}
}
func TestAdminConversationsRestrictAccessListGroups(t *testing.T) {
http.DefaultServeMux = new(http.ServeMux)
http.HandleFunc("/admin.conversations.restrictAccess.listGroups", mockRestrictAccessListGroupsHandler(t))
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
groupIDs, err := api.AdminConversationsRestrictAccessListGroups(context.Background(),
"C1234567890",
AdminConversationsRestrictAccessListGroupsOptionTeamID("T789"),
)
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}
if len(groupIDs) != 2 {
t.Errorf("unexpected group count: %d", len(groupIDs))
return
}
}
func TestAdminConversationsRestrictAccessRemoveGroup(t *testing.T) {
http.DefaultServeMux = new(http.ServeMux)
http.HandleFunc("/admin.conversations.restrictAccess.removeGroup", mockRestrictAccessHandler(t, "group_id"))
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
err := api.AdminConversationsRestrictAccessRemoveGroup(context.Background(),
"C1234567890",
"G123",
AdminConversationsRestrictAccessRemoveGroupOptionTeamID("T789"),
)
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}
}
func mockRestrictAccessHandler(t *testing.T, requiredField string) func(rw http.ResponseWriter, r *http.Request) {
return func(rw http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST request, got %s", r.Method)
}
if err := r.ParseForm(); err != nil {
t.Errorf("unexpected error: %s", err)
return
}
if len(r.Form["channel_id"]) == 0 {
t.Error("missing channel_id in request")
return
}
if len(r.Form[requiredField]) == 0 {
t.Errorf("missing %s in request", requiredField)
return
}
rw.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(SlackResponse{
Ok: true,
})
_, _ = rw.Write(response)
}
}
func mockRestrictAccessListGroupsHandler(t *testing.T) func(rw http.ResponseWriter, r *http.Request) {
return func(rw http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST request, got %s", r.Method)
}
if err := r.ParseForm(); err != nil {
t.Errorf("unexpected error: %s", err)
return
}
if len(r.Form["channel_id"]) == 0 {
t.Error("missing channel_id in request")
return
}
rw.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(AdminConversationsRestrictAccessListGroupsResponse{
SlackResponse: SlackResponse{Ok: true},
GroupIDs: []string{"G123", "G456"},
})
_, _ = rw.Write(response)
}
}