catbase/plugins/admin/admin_test.go

80 lines
1.8 KiB
Go
Raw Normal View History

package admin
import (
2021-02-01 11:37:36 +00:00
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user"
)
var (
a *AdminPlugin
mb *bot.MockBot
)
func setup(t *testing.T) (*AdminPlugin, *bot.MockBot) {
mb = bot.NewMockBot()
a = New(mb)
mb.DB().MustExec(`delete from config`)
2020-10-09 16:53:50 +00:00
err := mb.Config().Set("admins", "tester")
if err != nil {
t.FailNow()
}
return a, mb
}
2021-02-01 11:37:36 +00:00
func makeMessage(payload string, r *regexp.Regexp) bot.Request {
isCmd := strings.HasPrefix(payload, "!")
if isCmd {
payload = payload[1:]
}
2021-02-01 11:37:36 +00:00
values := bot.ParseValues(r, payload)
return bot.Request{
Kind: bot.Message,
Values: values,
Msg: msg.Message{
User: &user.User{Name: "admin"},
Channel: "test",
Body: payload,
Command: isCmd,
},
}
}
func TestSet(t *testing.T) {
a, mb := setup(t)
expected := "test value"
2021-02-01 11:37:36 +00:00
a.setConfigCmd(makeMessage("!set test.key "+expected, setConfigRegex))
2019-01-22 00:16:57 +00:00
actual := mb.Config().Get("test.key", "ERR")
assert.Equal(t, expected, actual)
}
func TestGetValue(t *testing.T) {
a, mb := setup(t)
expected := "value"
mb.Config().Set("test.key", "value")
2021-02-01 11:37:36 +00:00
a.getConfigCmd(makeMessage("!get test.key", getConfigRegex))
assert.Len(t, mb.Messages, 1)
assert.Contains(t, mb.Messages[0], expected)
}
func TestGetEmpty(t *testing.T) {
a, mb := setup(t)
2019-01-22 00:16:57 +00:00
expected := "test.key: <unknown>"
2021-02-01 11:37:36 +00:00
a.getConfigCmd(makeMessage("!get test.key", getConfigRegex))
assert.Len(t, mb.Messages, 1)
assert.Equal(t, expected, mb.Messages[0])
}
func TestGetForbidden(t *testing.T) {
a, mb := setup(t)
expected := "cannot access"
2021-02-01 11:37:36 +00:00
a.getConfigCmd(makeMessage("!get slack.token", getConfigRegex))
assert.Len(t, mb.Messages, 1)
assert.Contains(t, mb.Messages[0], expected)
}