From c298ef8263d9e03bdd8761c175a865212e7c6769 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Mon, 1 Feb 2021 06:37:36 -0500 Subject: [PATCH] admin: refactor tests --- plugins/admin/admin_test.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/plugins/admin/admin_test.go b/plugins/admin/admin_test.go index 92a337e..ba0d859 100644 --- a/plugins/admin/admin_test.go +++ b/plugins/admin/admin_test.go @@ -1,6 +1,7 @@ package admin import ( + "regexp" "strings" "testing" @@ -28,24 +29,30 @@ func setup(t *testing.T) (*AdminPlugin, *bot.MockBot) { 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, "!") if isCmd { payload = payload[1:] } c := cli.CliPlugin{} - return &c, bot.Message, msg.Message{ - User: &user.User{Name: "admin"}, - Channel: "test", - Body: payload, - Command: isCmd, + 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, + }, } } func TestSet(t *testing.T) { a, mb := setup(t) 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") assert.Equal(t, expected, actual) } @@ -54,7 +61,7 @@ func TestGetValue(t *testing.T) { a, mb := setup(t) expected := "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.Contains(t, mb.Messages[0], expected) } @@ -62,7 +69,7 @@ func TestGetValue(t *testing.T) { func TestGetEmpty(t *testing.T) { a, mb := setup(t) expected := "test.key: " - a.message(makeMessage("!get test.key")) + a.getConfigCmd(makeMessage("!get test.key", getConfigRegex)) assert.Len(t, mb.Messages, 1) assert.Equal(t, expected, mb.Messages[0]) } @@ -70,7 +77,7 @@ func TestGetEmpty(t *testing.T) { func TestGetForbidden(t *testing.T) { a, mb := setup(t) expected := "cannot access" - a.message(makeMessage("!get slack.token")) + a.getConfigCmd(makeMessage("!get slack.token", getConfigRegex)) assert.Len(t, mb.Messages, 1) assert.Contains(t, mb.Messages[0], expected) }