-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_bot_test.go
147 lines (116 loc) · 3.16 KB
/
client_bot_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
package tg
import (
"context"
"fmt"
"os"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
var (
testToken = os.Getenv("GO_TG_BOT_TOKEN")
testChannelID ChatID
testClient = NewClient(testToken)
testUserID UserID
)
func init() {
testChannelIDString := os.Getenv("GO_TG_TEST_CHANNEL_ID")
if testChannelIDString != "" {
parsed, err := strconv.ParseInt(testChannelIDString, 10, 64)
if err != nil {
panic(fmt.Sprintf("test channel id is provided but not number (%s)", testChannelIDString))
}
testChannelID = ChatID(parsed)
}
testUserIDString := os.Getenv("GO_TG_TEST_USER_ID")
if testUserIDString != "" {
parsed, err := strconv.ParseInt(testUserIDString, 10, 64)
if err != nil {
panic(fmt.Sprintf("test user id is provided but not number (%s)", testUserIDString))
}
testUserID = UserID(parsed)
}
}
func skipIfNeed(t *testing.T) {
noToken := testToken == ""
isShort := testing.Short()
if isShort || noToken {
t.Skipf("skip test because isShort:%v, noToken:%v", isShort, noToken)
}
}
func TestClient_GetMe(t *testing.T) {
skipIfNeed(t)
user, err := testClient.GetMe(context.Background())
if assert.NoError(t, err) {
assert.NotZero(t, user.ID)
assert.True(t, user.IsBot)
assert.NotZero(t, user.FirstName)
assert.NotZero(t, user.Username)
assert.True(t, user.SupportsInlineQueries)
assert.True(t, user.CanReadAllGroupMessages)
assert.True(t, user.CanJoinGroups)
}
}
func TestClient_SetWebhook(t *testing.T) {
skipIfNeed(t)
ctx := context.Background()
opts := &SetWebhookOptions{
MaxConnections: 40,
URL: "https://bots.house",
}
err := testClient.SetWebhook(ctx, opts)
if assert.NoError(t, err) {
info, err := testClient.GetWebhookInfo(ctx)
if assert.NoError(t, err) {
assert.Equal(t, info.URL, opts.URL)
assert.Equal(t, info.MaxConnections, opts.MaxConnections)
assert.Equal(t, opts.AllowedUpdates, info.AllowedUpdates)
}
}
}
func TestClient_GetChat(t *testing.T) {
skipIfNeed(t)
ctx := context.Background()
chat, err := testClient.GetChat(ctx, testChannelID)
if assert.NoError(t, err) {
assert.Equal(t, testChannelID, chat.ID)
}
}
func TestClient_SetAndGetMyCommands(t *testing.T) {
skipIfNeed(t)
ctx := context.Background()
commands := []BotCommand{
{"start", "just start bot"},
{"stop", "just stop bot"},
{"restart", "just restart bot"},
{"help", "show helps"},
}
err := testClient.SetMyCommands(ctx, commands)
if assert.NoError(t, err) {
actual, err := testClient.GetMyCommands(ctx)
if assert.NoError(t, err) {
assert.Equal(t, commands, actual)
}
}
}
func TestClient_GetUserProfilePhotos(t *testing.T) {
skipIfNeed(t)
ctx := context.Background()
opts := &GetUserProfilePhotosOptions{
UserID: testUserID,
}
_, err := testClient.GetUserProfilePhotos(ctx, opts)
assert.NoError(t, err)
}
func TestClient_GetChatMembersCount(t *testing.T) {
skipIfNeed(t)
ctx := context.Background()
_, err := testClient.GetChatMembersCount(ctx, testChannelID)
assert.NoError(t, err)
}
func TestClient_GetChatMember(t *testing.T) {
skipIfNeed(t)
ctx := context.Background()
_, err := testClient.GetChatMember(ctx, testChannelID, testUserID)
assert.NoError(t, err)
}