admin: refactor tests

This commit is contained in:
Chris Sexton 2021-02-01 06:37:36 -05:00 committed by Chris Sexton
parent 8e4129ecb3
commit 951c345666
1 changed files with 17 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package admin package admin
import ( import (
"regexp"
"strings" "strings"
"testing" "testing"
@ -28,24 +29,30 @@ func setup(t *testing.T) (*AdminPlugin, *bot.MockBot) {
return a, mb return a, mb
} }
func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) { func makeMessage(payload string, r *regexp.Regexp) bot.Request {
isCmd := strings.HasPrefix(payload, "!") isCmd := strings.HasPrefix(payload, "!")
if isCmd { if isCmd {
payload = payload[1:] payload = payload[1:]
} }
c := cli.CliPlugin{} c := cli.CliPlugin{}
return &c, bot.Message, msg.Message{ values := bot.ParseValues(r, payload)
User: &user.User{Name: "admin"}, return bot.Request{
Channel: "test", Conn: &c,
Body: payload, Kind: bot.Message,
Command: isCmd, Values: values,
Msg: msg.Message{
User: &user.User{Name: "admin"},
Channel: "test",
Body: payload,
Command: isCmd,
},
} }
} }
func TestSet(t *testing.T) { func TestSet(t *testing.T) {
a, mb := setup(t) a, mb := setup(t)
expected := "test value" expected := "test value"
a.message(makeMessage("!set test.key " + expected)) a.setConfigCmd(makeMessage("!set test.key "+expected, setConfigRegex))
actual := mb.Config().Get("test.key", "ERR") actual := mb.Config().Get("test.key", "ERR")
assert.Equal(t, expected, actual) assert.Equal(t, expected, actual)
} }
@ -54,7 +61,7 @@ func TestGetValue(t *testing.T) {
a, mb := setup(t) a, mb := setup(t)
expected := "value" expected := "value"
mb.Config().Set("test.key", "value") mb.Config().Set("test.key", "value")
a.message(makeMessage("!get test.key")) a.getConfigCmd(makeMessage("!get test.key", getConfigRegex))
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
assert.Contains(t, mb.Messages[0], expected) assert.Contains(t, mb.Messages[0], expected)
} }
@ -62,7 +69,7 @@ func TestGetValue(t *testing.T) {
func TestGetEmpty(t *testing.T) { func TestGetEmpty(t *testing.T) {
a, mb := setup(t) a, mb := setup(t)
expected := "test.key: <unknown>" expected := "test.key: <unknown>"
a.message(makeMessage("!get test.key")) a.getConfigCmd(makeMessage("!get test.key", getConfigRegex))
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
assert.Equal(t, expected, mb.Messages[0]) assert.Equal(t, expected, mb.Messages[0])
} }
@ -70,7 +77,7 @@ func TestGetEmpty(t *testing.T) {
func TestGetForbidden(t *testing.T) { func TestGetForbidden(t *testing.T) {
a, mb := setup(t) a, mb := setup(t)
expected := "cannot access" expected := "cannot access"
a.message(makeMessage("!get slack.token")) a.getConfigCmd(makeMessage("!get slack.token", getConfigRegex))
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
assert.Contains(t, mb.Messages[0], expected) assert.Contains(t, mb.Messages[0], expected)
} }