-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient_test.go
121 lines (104 loc) · 2.6 KB
/
client_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
package teams_api_test
import (
"bytes"
"fmt"
teams_api "github.com/fossteams/teams-api"
"github.com/fossteams/teams-api/pkg/csa"
"github.com/logrusorgru/aurora"
"github.com/stretchr/testify/assert"
"golang.org/x/net/html"
"sort"
"strings"
"testing"
)
func TestTeamsClient_GetConversations(t *testing.T) {
c := mustGetClient(t)
c.Debug(true)
convs, err := c.GetConversations()
if err != nil {
t.Fatalf("unable to get conversations: %v", err)
}
if convs == nil {
t.Fatal("convs should never be nil!")
}
// Pretty print conversations
fmt.Printf("%s\n", aurora.Bold("Teams"))
sort.Sort(csa.TeamsByName(convs.Teams))
for _, t := range convs.Teams {
fmt.Printf("%s (%d users)\n",
aurora.Magenta(t.DisplayName),
aurora.Green(t.MembershipSummary.UserRoleCount),
)
sort.Sort(csa.ChannelsByName(t.Channels))
for _, channel := range t.Channels {
fmt.Printf("\t%s\n",
aurora.Red(channel.DisplayName))
}
fmt.Printf("\n")
}
}
func TestTeamsClient_GetMessages(t *testing.T) {
c := mustGetClient(t)
c.Debug(true)
convs, err := c.GetConversations()
if err != nil {
t.Fatalf("unable to get conversations: %v", err)
}
if convs == nil {
t.Fatal("convs should never be nil!")
}
// Get first team, first channel
team := convs.Teams[0]
channel := team.Channels[0]
assert.NotNil(t, channel)
fmt.Printf("%s\n", aurora.Red(team.DisplayName))
fmt.Printf("%s\n", aurora.Yellow(channel.DisplayName))
messages, err := c.GetMessages(&channel)
if err != nil {
t.Fatalf("unable to get channel messages: %v", err)
}
assert.Greater(t, len(messages), 0)
for _, m := range messages {
fmt.Printf("%s\n", aurora.Bold(aurora.Green(m.ImDisplayName)))
z := html.NewTokenizer(bytes.NewBuffer([]byte(m.Content)))
for {
tt := z.Next()
if tt == html.ErrorToken {
break
}
switch tt {
case html.TextToken:
text := string(z.Text())
if strings.TrimSpace(text) == "" {
continue
}
fmt.Printf("\t%v\n", aurora.Blue(text))
}
if tt == html.ErrorToken {
break
}
}
fmt.Printf("\n")
}
}
func mustGetClient(t *testing.T) *teams_api.TeamsClient {
c, err := teams_api.New()
if err != nil {
t.Fatalf("unable to create teams client: %v", err)
}
return c
}
func TestTeamsClient_GetMe(t *testing.T) {
c := mustGetClient(t)
user, err := c.GetMe()
assert.Nil(t, err)
assert.NotNil(t, user)
fmt.Printf("user = %#v\n", user)
}
func TestTeamsClient_GetPinnedChannels(t *testing.T) {
c := mustGetClient(t)
pinnedChannels, err := c.GetPinnedChannels()
assert.Nil(t, err)
assert.NotNil(t, pinnedChannels)
fmt.Printf("pinnedChannels = %#v\n", pinnedChannels)
}