-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanagement_test.go
162 lines (141 loc) · 4.99 KB
/
management_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"fmt"
"math/rand"
"os"
"regexp"
"strings"
"testing"
"time"
"github.com/bwmarrin/discordgo"
)
var response string
var timeBetweenCommands time.Duration = 2 * time.Second
var testingChannel string = "739852388264968243"
/**
Test anything that uses calculation outside of the discordgo commands.
Those commands don't need to be tested since they are verified to work
at github.com/bwmarrin/discordgo
**/
func TestMessageResponse(t *testing.T) {
// main bot
go runBot(os.Getenv("BOT_TOKEN"))
time.Sleep(2 * time.Second) // give bot time to start
// test bot
dg, err := discordgo.New("Bot " + os.Getenv("TEST_BOT_TOKEN"))
if err != nil {
fmt.Println("Error creating test bot's discord session")
return
}
dg.AddHandler(updateResponse)
err = dg.Open()
if err != nil {
fmt.Println("Error opening test bot's connection,", err)
return
}
// -- tests
t.Run("Responds correctly to ~uptime", func(t *testing.T) {
dg.ChannelMessageSend(testingChannel, "~uptime")
time.Sleep(timeBetweenCommands) // allow response to populate
regex := regexp.MustCompile(`^:robot: Uptime: \d\.\ds$`)
if !regex.MatchString(response) {
t.Logf("Failed to respond correctly to ~uptime; Response was `" + response + "`")
t.Fail()
}
})
t.Run("Generates invitation", func(t *testing.T) {
dg.ChannelMessageSend(testingChannel, "~invite")
time.Sleep(timeBetweenCommands) // allow response to populate
if strings.HasPrefix(response, ":mailbox_with_mail: Here's your invitation! https://discord.gg/") == false {
t.Logf("Failed to generate invitation")
t.Fail()
}
})
t.Run("Permissions work appropriately", func(t *testing.T) {
randNum := rand.Intn(10000)
dg.ChannelMessageSend(testingChannel, fmt.Sprintf("~nick <@!700962207785156668> test nickname %d", randNum))
time.Sleep(timeBetweenCommands) // allow response to populate
if response != "Done!" {
t.Logf("Failed to change valid nickname")
t.Fail()
}
})
t.Run("Incorrect usages are reported", func(t *testing.T) {
dg.ChannelMessageSend(testingChannel, "~nick <@!700962207785156668>")
time.Sleep(timeBetweenCommands) // allow response to populate
if response != "Usage: `~nick @<user> <new name>`" {
t.Logf("Should have reported incorrect usage, but didn't")
t.Fail()
}
dg.ChannelMessageSend(testingChannel, "~kick user")
time.Sleep(timeBetweenCommands) // allow response to populate
if response != "Usage: `~kick @<user> (reason: optional)`" {
t.Logf("Should have reported incorrect usage, but didn't")
t.Fail()
}
dg.ChannelMessageSend(testingChannel, "~kick")
time.Sleep(timeBetweenCommands) // allow response to populate
if response != "Usage: `~kick @<user> (reason: optional)`" {
t.Logf("Should have reported incorrect usage, but didn't")
t.Fail()
}
dg.ChannelMessageSend(testingChannel, "~ban user")
time.Sleep(timeBetweenCommands) // allow response to populate
if response != "Usage: `~ban @<user> (reason: optional)`" {
t.Logf("Should have reported incorrect usage, but didn't")
t.Fail()
}
dg.ChannelMessageSend(testingChannel, "~ban")
time.Sleep(timeBetweenCommands) // allow response to populate
if response != "Usage: `~ban @<user> (reason: optional)`" {
t.Logf("Should have reported incorrect usage, but didn't")
t.Fail()
}
dg.ChannelMessageSend(testingChannel, "~profile")
time.Sleep(timeBetweenCommands) // allow response to populate
if response != "Usage: `~profile @user`" {
t.Logf("Should have reported incorrect usage, but didn't")
t.Fail()
}
dg.ChannelMessageSend(testingChannel, "~profile user")
time.Sleep(timeBetweenCommands) // allow response to populate
if response != "Usage: `~profile @user`" {
t.Logf("Should have reported incorrect usage, but didn't")
t.Fail()
}
dg.ChannelMessageSend(testingChannel, "~cp 3")
time.Sleep(timeBetweenCommands) // allow response to populate
if response != "Usage: `~cp <number <= 100> <#channel>`" {
t.Logf("Should have reported incorrect usage, but didn't")
t.Fail()
}
dg.ChannelMessageSend(testingChannel, "~cp 3 fakechannel")
time.Sleep(timeBetweenCommands) // allow response to populate
if response != "Usage: `~cp <number <= 100> <#channel>`" {
t.Logf("Should have reported incorrect usage, but didn't")
t.Fail()
}
dg.ChannelMessageSend(testingChannel, "~mv 3")
time.Sleep(timeBetweenCommands) // allow response to populate
if response != "Usage: `~mv <number <= 100> <#channel>`" {
t.Logf("Should have reported incorrect usage, but didn't")
t.Fail()
}
dg.ChannelMessageSend(testingChannel, "~mv 3 fakechannel")
time.Sleep(timeBetweenCommands) // allow response to populate
if response != "Usage: `~mv <number <= 100> <#channel>`" {
t.Logf("Should have reported incorrect usage, but didn't")
t.Fail()
}
})
// -- teardown
time.Sleep(2 * time.Second)
dg.ChannelMessageSend(testingChannel, "~shutdown")
dg.Close()
}
func updateResponse(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
response = m.Content
}