executor_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package executor
  2. import (
  3. "github.com/stretchr/testify/assert"
  4. "testing"
  5. acl "github.com/jamesread/OliveTin/internal/acl"
  6. config "github.com/jamesread/OliveTin/internal/config"
  7. )
  8. func TestSanitizeUnsafe(t *testing.T) {
  9. assert.Nil(t, TypeSafetyCheck("", "_zomg_ c:/ haxxor ' bobby tables && rm -rf ", "very_dangerous_raw_string"))
  10. }
  11. func testingExecutor() (*Executor, *config.Config) {
  12. e := DefaultExecutor()
  13. cfg := config.DefaultConfig()
  14. a1 := config.Action{
  15. Title: "Do some tickles",
  16. Shell: "echo 'Tickling {{ person }}'",
  17. Arguments: []config.ActionArgument{
  18. {
  19. Name: "person",
  20. Type: "ascii",
  21. },
  22. },
  23. }
  24. cfg.Actions = append(cfg.Actions, a1)
  25. cfg.Sanitize()
  26. return e, cfg
  27. }
  28. func TestCreateExecutorAndExec(t *testing.T) {
  29. e, cfg := testingExecutor()
  30. req := ExecutionRequest{
  31. ActionName: "Do some tickles",
  32. User: &acl.User{Username: "Mr Tickle"},
  33. Cfg: cfg,
  34. Arguments: map[string]string{
  35. "person": "yourself",
  36. },
  37. }
  38. e.ExecRequest(&req)
  39. assert.NotNil(t, e, "Create an executor")
  40. assert.NotNil(t, e.ExecRequest(&req), "Execute a request")
  41. assert.Equal(t, int32(0), req.logEntry.ExitCode, "Exit code is zero")
  42. }
  43. func TestExecNonExistant(t *testing.T) {
  44. e, cfg := testingExecutor()
  45. req := ExecutionRequest{
  46. ActionName: "Waffles",
  47. logEntry: &InternalLogEntry{},
  48. Cfg: cfg,
  49. }
  50. e.ExecRequest(&req)
  51. assert.Equal(t, int32(-1337), req.logEntry.ExitCode, "Log entry is set to an internal error code")
  52. assert.Equal(t, "", req.logEntry.ActionIcon, "Log entry icon wasnt found")
  53. }