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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
package slack
import (
"net/http"
"reflect"
"testing"
)
type userGroupsHandler struct {
gotParams map[string]string
response string
}
func newUserGroupsHandler() *userGroupsHandler {
return &userGroupsHandler{
gotParams: make(map[string]string),
response: `{
"ok": true,
"usergroup": {
"id": "S0615G0KT",
"team_id": "T060RNRCH",
"is_usergroup": true,
"name": "Marketing Team",
"description": "Marketing gurus, PR experts and product advocates.",
"handle": "marketing-team",
"is_external": false,
"date_create": 1446746793,
"date_update": 1446746793,
"date_delete": 0,
"auto_type": null,
"created_by": "U060RNRCZ",
"updated_by": "U060RNRCZ",
"deleted_by": null,
"prefs": {
"channels": [
],
"groups": [
]
},
"user_count": 0
}
}`,
}
}
func (ugh *userGroupsHandler) accumulateFormValue(k string, r *http.Request) {
if v := r.FormValue(k); v != "" {
ugh.gotParams[k] = v
}
}
func (ugh *userGroupsHandler) handler(w http.ResponseWriter, r *http.Request) {
ugh.accumulateFormValue("name", r)
ugh.accumulateFormValue("description", r)
ugh.accumulateFormValue("handle", r)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(ugh.response))
}
func TestCreateUserGroup(t *testing.T) {
once.Do(startServer)
SLACK_API = "http://" + serverAddr + "/"
api := New("testing-token")
tests := []struct {
userGroup UserGroup
wantParams map[string]string
}{
{
UserGroup{
Name: "Marketing Team",
Description: "Marketing gurus, PR experts and product advocates.",
Handle: "marketing-team"},
map[string]string{
"name": "Marketing Team",
"description": "Marketing gurus, PR experts and product advocates.",
"handle": "marketing-team",
},
},
}
var rh *userGroupsHandler
http.HandleFunc("/usergroups.create", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
for i, test := range tests {
rh = newUserGroupsHandler()
_, err := api.CreateUserGroup(test.userGroup)
if err != nil {
t.Fatalf("%d: Unexpected error: %s", i, err)
}
if !reflect.DeepEqual(rh.gotParams, test.wantParams) {
t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams)
}
}
}
func getUserGroups(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
response := []byte(`{
"ok": true,
"usergroups": [
{
"id": "S0614TZR7",
"team_id": "T060RNRCH",
"is_usergroup": true,
"name": "Team Admins",
"description": "A group of all Administrators on your team.",
"handle": "admins",
"is_external": false,
"date_create": 1446598059,
"date_update": 1446670362,
"date_delete": 0,
"auto_type": "admin",
"created_by": "USLACKBOT",
"updated_by": "U060RNRCZ",
"deleted_by": null,
"prefs": {
"channels": [
"channel1",
"channel2"
],
"groups": [
"group1",
"group2",
"group3"
]
},
"user_count": 2
}
]
}`)
rw.Write(response)
}
func TestGetUserGroups(t *testing.T) {
http.HandleFunc("/usergroups.list", getUserGroups)
once.Do(startServer)
SLACK_API = "http://" + serverAddr + "/"
api := New("testing-token")
userGroups, err := api.GetUserGroups()
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
// t.Fatal refers to -> t.Errorf & return
if len(userGroups) != 1 {
t.Fatal(ErrIncorrectResponse)
}
S0614TZR7 := UserGroup{
ID: "S0614TZR7",
TeamID: "T060RNRCH",
IsUserGroup: true,
Name: "Team Admins",
Description: "A group of all Administrators on your team.",
Handle: "admins",
IsExternal: false,
DateCreate: 1446598059,
DateUpdate: 1446670362,
DateDelete: 0,
AutoType: "admin",
CreatedBy: "USLACKBOT",
UpdatedBy: "U060RNRCZ",
DeletedBy: "",
Prefs: UserGroupPrefs{
Channels: []string{"channel1", "channel2"},
Groups: []string{"group1", "group2", "group3"},
},
UserCount: 2,
}
if !reflect.DeepEqual(userGroups[0], S0614TZR7) {
t.Errorf("Got %#v, want %#v", userGroups[0], S0614TZR7)
}
}
|