2019-01-21 21:26:54 +00:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
2021-02-01 11:37:36 +00:00
|
|
|
"regexp"
|
2019-01-21 21:26:54 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-10-09 16:53:50 +00:00
|
|
|
"github.com/velour/catbase/plugins/cli"
|
|
|
|
|
2019-01-21 21:26:54 +00:00
|
|
|
"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
|
|
|
|
)
|
|
|
|
|
2021-12-21 19:08:20 +00:00
|
|
|
func setup(t *testing.T) (*AdminPlugin, *bot.MockBot, func()) {
|
2019-01-21 21:26:54 +00:00
|
|
|
mb = bot.NewMockBot()
|
|
|
|
a = New(mb)
|
2020-10-09 16:53:50 +00:00
|
|
|
err := mb.Config().Set("admins", "tester")
|
|
|
|
if err != nil {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2021-12-21 19:08:20 +00:00
|
|
|
return a, mb, func() {
|
|
|
|
mb.TearDown()
|
|
|
|
}
|
2019-01-21 21:26:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 11:37:36 +00:00
|
|
|
func makeMessage(payload string, r *regexp.Regexp) bot.Request {
|
2019-01-21 21:26:54 +00:00
|
|
|
isCmd := strings.HasPrefix(payload, "!")
|
|
|
|
if isCmd {
|
|
|
|
payload = payload[1:]
|
|
|
|
}
|
2019-05-27 23:21:53 +00:00
|
|
|
c := cli.CliPlugin{}
|
2021-02-01 11:37:36 +00:00
|
|
|
values := bot.ParseValues(r, payload)
|
|
|
|
return bot.Request{
|
|
|
|
Conn: &c,
|
|
|
|
Kind: bot.Message,
|
|
|
|
Values: values,
|
|
|
|
Msg: msg.Message{
|
|
|
|
User: &user.User{Name: "admin"},
|
|
|
|
Channel: "test",
|
|
|
|
Body: payload,
|
|
|
|
Command: isCmd,
|
|
|
|
},
|
2019-01-21 21:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSet(t *testing.T) {
|
2021-12-21 19:08:20 +00:00
|
|
|
a, mb, td := setup(t)
|
|
|
|
defer td()
|
2019-01-21 21:26:54 +00:00
|
|
|
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")
|
2019-01-21 21:26:54 +00:00
|
|
|
assert.Equal(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetValue(t *testing.T) {
|
2021-12-21 19:08:20 +00:00
|
|
|
a, mb, td := setup(t)
|
|
|
|
defer td()
|
2019-01-21 21:26:54 +00:00
|
|
|
expected := "value"
|
|
|
|
mb.Config().Set("test.key", "value")
|
2021-02-01 11:37:36 +00:00
|
|
|
a.getConfigCmd(makeMessage("!get test.key", getConfigRegex))
|
2019-01-21 21:26:54 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
|
|
|
assert.Contains(t, mb.Messages[0], expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetEmpty(t *testing.T) {
|
2021-12-21 19:08:20 +00:00
|
|
|
a, mb, td := setup(t)
|
|
|
|
defer td()
|
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))
|
2019-01-21 21:26:54 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
|
|
|
assert.Equal(t, expected, mb.Messages[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetForbidden(t *testing.T) {
|
2021-12-21 19:08:20 +00:00
|
|
|
a, mb, td := setup(t)
|
|
|
|
defer td()
|
2019-01-21 21:26:54 +00:00
|
|
|
expected := "cannot access"
|
2021-02-01 11:37:36 +00:00
|
|
|
a.getConfigCmd(makeMessage("!get slack.token", getConfigRegex))
|
2019-01-21 21:26:54 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
|
|
|
assert.Contains(t, mb.Messages[0], expected)
|
|
|
|
}
|