Yay more unit tests

This commit is contained in:
jamesread
2021-11-04 00:30:36 +00:00
parent 3aa7c97bfb
commit 78efc5c94e
7 changed files with 158 additions and 17 deletions

View File

@@ -9,3 +9,13 @@ func (cfg *Config) FindAction(actionTitle string) *Action {
return nil
}
func (action *Action) FindArg(name string) *ActionArgument {
for _, arg := range action.Arguments {
if arg.Name == name {
return &arg
}
}
return nil
}

View File

@@ -0,0 +1,33 @@
package config
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestFindAction(t *testing.T) {
c := DefaultConfig()
a1 := Action{}
a1.Title = "a1"
c.Actions = append(c.Actions, a1)
a2 := Action{
Title: "a2",
Arguments: []ActionArgument{
{
Name: "Blat",
},
},
}
c.Actions = append(c.Actions, a2)
assert.NotNil(t, c.FindAction("a1"), "Find action a1")
assert.NotNil(t, c.FindAction("a2"), "Find action a2")
assert.NotNil(t, c.FindAction("a2").FindArg("Blat"), "Find action argument")
assert.Nil(t, c.FindAction("a2").FindArg("Blatey Cake"), "Find non-existant action argument")
assert.Nil(t, c.FindAction("waffles"), "Find non-existant action")
}

View File

@@ -1,6 +1,7 @@
package config
var emojis = map[string]string{
"": "😀", // default icon
"poop": "💩",
"smile": "😀",
"ping": "📡",

View File

@@ -0,0 +1,39 @@
package config
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestSanitizeConfig(t *testing.T) {
c := DefaultConfig()
a := Action{
Title: "Mr Waffles",
Arguments: []ActionArgument{
ActionArgument{
Name: "Carrots",
Choices: []ActionArgumentChoice{
{
Value: "Waffle",
},
},
},
{
Name: "foobar",
},
},
}
c.Actions = append(c.Actions, a)
Sanitize(c)
a2 := c.FindAction("Mr Waffles")
assert.NotNil(t, a2, "Found action after adding it")
assert.Equal(t, 3, a2.Timeout, "Default timeout is set")
assert.Equal(t, "😀", a2.Icon, "Default icon is a smiley")
assert.Equal(t, "Carrots", a2.Arguments[0].Title, "Arg title is set to name")
assert.Equal(t, "Waffle", a2.Arguments[0].Choices[0].Title, "Choice title is set to name")
}

View File

@@ -85,7 +85,6 @@ func (s StepFindAction) Exec(req *ExecutionRequest) bool {
}).Warnf("Action not found")
req.logEntry.Stderr = "Action not found"
req.logEntry.ExitCode = -1337
return false
}
@@ -107,6 +106,9 @@ func (e *Executor) ExecRequest(req *ExecutionRequest) *pb.StartActionResponse {
req.logEntry = &InternalLogEntry{
Datetime: time.Now().Format("2006-01-02 15:04:05"),
ActionTitle: req.ActionName,
Stdout: "",
Stderr: "",
ExitCode: -1337, // If an Action is not actually executed, this is the default exit code.
}
for _, step := range e.chainOfCommand {
@@ -163,8 +165,6 @@ func (e StepParseArgs) Exec(req *ExecutionRequest) bool {
req.finalParsedCommand, err = parseActionArguments(req.action.Shell, req.Arguments, req.action)
if err != nil {
req.logEntry.ExitCode = -1337
req.logEntry.Stderr = ""
req.logEntry.Stdout = err.Error()
log.Warnf(err.Error())
@@ -236,7 +236,7 @@ func parseActionArguments(rawShellCommand string, values map[string]string, acti
}
func typecheckActionArgument(name string, value string, action *config.Action) error {
arg := findArg(name, action)
arg := action.FindArg(name)
if arg == nil {
return errors.New("Action arg not defined: " + name)
@@ -262,8 +262,6 @@ func typecheckChoice(value string, arg *config.ActionArgument) error {
func TypeSafetyCheck(name string, value string, typ string) error {
pattern, found := typecheckRegex[typ]
log.Infof("%v %v", pattern, typ)
if !found {
return errors.New("Arg type not implemented " + typ)
}
@@ -282,13 +280,3 @@ func TypeSafetyCheck(name string, value string, typ string) error {
return nil
}
func findArg(name string, action *config.Action) *config.ActionArgument {
for _, arg := range action.Arguments {
if arg.Name == name {
return &arg
}
}
return nil
}

View File

@@ -0,0 +1,70 @@
package executor
import (
"github.com/stretchr/testify/assert"
"testing"
acl "github.com/jamesread/OliveTin/internal/acl"
config "github.com/jamesread/OliveTin/internal/config"
)
func TestSanitizeUnsafe(t *testing.T) {
assert.Nil(t, TypeSafetyCheck("", "_zomg_ c:/ haxxor ' bobby tables && rm -rf ", "very_dangerous_raw_string"))
}
func testingExecutor() (*Executor, *config.Config) {
e := DefaultExecutor()
cfg := config.DefaultConfig()
a1 := config.Action{
Title: "Do some tickles",
Shell: "echo 'Tickling {{ person }}'",
Arguments: []config.ActionArgument{
config.ActionArgument{
Name: "person",
Type: "ascii",
},
},
}
cfg.Actions = append(cfg.Actions, a1)
config.Sanitize(cfg)
return e, cfg
}
func TestCreateExecutorAndExec(t *testing.T) {
e, cfg := testingExecutor()
req := ExecutionRequest{
ActionName: "Do some tickles",
User: &acl.User{Username: "Mr Tickle"},
Cfg: cfg,
Arguments: map[string]string{
"person": "yourself",
},
}
e.ExecRequest(&req)
assert.NotNil(t, e, "Create an executor")
assert.NotNil(t, e.ExecRequest(&req), "Execute a request")
assert.Equal(t, int32(0), req.logEntry.ExitCode, "Exit code is zero")
}
func TestExecNonExistant(t *testing.T) {
e, cfg := testingExecutor()
req := ExecutionRequest{
ActionName: "Waffles",
logEntry: &InternalLogEntry{},
Cfg: cfg,
}
e.ExecRequest(&req)
assert.Equal(t, int32(-1337), req.logEntry.ExitCode, "Log entry is set to an internal error code")
assert.Equal(t, "", req.logEntry.ActionIcon, "Log entry icon wasnt found")
}

View File

@@ -2,8 +2,8 @@ package httpservers
import (
"github.com/stretchr/testify/assert"
"testing"
"os"
"testing"
)
func TestGetWebuiDir(t *testing.T) {