-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot_test.go
108 lines (99 loc) · 2.91 KB
/
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
package tbb
import (
"github.com/NicoNex/echotron/v3"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestBot_Update(t *testing.T) {
t.Run("All users are allowed to use the bot if AllowedChatIDs is nil or empty", func(t *testing.T) {
cfg := LoadConfig("test/data/test.config.yml")
cfg.AllowedChatIDs = []int64{}
tbot := New(WithConfig(cfg))
bot := tbot.newBot(99999999, tbot.logger.WithGroup("bot"), func() UpdateHandler { return &DefaultUpdateHandler{} })
u := &echotron.Update{
Message: &echotron.Message{
Chat: echotron.Chat{
Type: "private",
Username: "test_user",
FirstName: "test",
LastName: "user",
ID: 99999999,
},
Text: "/test_command",
},
ID: 123456,
}
bot.EnableUser()
bot.user.UpdatedAt = time.Now()
bot.Update(u)
assert.Empty(t, cfg.AllowedChatIDs)
assert.Nil(t, bot.Command())
assert.True(t, bot.User().UserInfo.IsActive)
})
t.Run("Users which are not in AllowedChatIDs list cannot use the bot", func(t *testing.T) {
cfg := LoadConfig("test/data/test.config.yml")
cfg.AllowedChatIDs = []int64{12345678}
commands := []Command{
{
Name: "/test_command",
Description: "",
Handler: &DefaultCommandHandler{},
},
}
tbot := New(WithConfig(cfg), WithCommands(commands))
bot := tbot.newBot(99999999, tbot.logger.WithGroup("bot"), func() UpdateHandler { return &DefaultUpdateHandler{} })
u := &echotron.Update{
Message: &echotron.Message{
Chat: echotron.Chat{
Type: "private",
Username: "test_user",
FirstName: "test",
LastName: "user",
ID: 99999999,
},
Text: "/test_command",
},
ID: 123456,
}
bot.EnableUser()
bot.user.UpdatedAt = time.Now()
bot.Update(u)
assert.Equal(t, []int64{12345678}, cfg.AllowedChatIDs)
assert.Nil(t, bot.Command())
assert.False(t, bot.User().UserInfo.IsActive)
})
t.Run("Users which are not in AllowedChatIDs list cannot use the bot", func(t *testing.T) {
cfg := LoadConfig("test/data/test.config.yml")
cfg.AllowedChatIDs = []int64{12345678, 99999999}
commands := []Command{
{
Name: "/test_command",
Description: "",
Handler: &DefaultCommandHandler{},
},
}
tbot := New(WithConfig(cfg), WithCommands(commands))
bot := tbot.newBot(99999999, tbot.logger.WithGroup("bot"), func() UpdateHandler { return &DefaultUpdateHandler{} })
u := &echotron.Update{
Message: &echotron.Message{
Chat: echotron.Chat{
Type: "private",
Username: "test_user",
FirstName: "test",
LastName: "user",
ID: 99999999,
},
Text: "/test_command",
},
ID: 123456,
}
bot.EnableUser()
bot.user.UpdatedAt = time.Now()
bot.Update(u)
assert.Equal(t, []int64{12345678, 99999999}, cfg.AllowedChatIDs)
assert.NotNil(t, bot.Command())
assert.Equal(t, "/test_command", bot.Command().Name)
assert.True(t, bot.User().UserInfo.IsActive)
})
}