executor_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package executor
  2. import (
  3. "github.com/stretchr/testify/assert"
  4. "testing"
  5. acl "github.com/OliveTin/OliveTin/internal/acl"
  6. config "github.com/OliveTin/OliveTin/internal/config"
  7. )
  8. func testingExecutor() (*Executor, *config.Config) {
  9. e := DefaultExecutor()
  10. cfg := config.DefaultConfig()
  11. a1 := config.Action{
  12. Title: "Do some tickles",
  13. Shell: "echo 'Tickling {{ person }}'",
  14. Arguments: []config.ActionArgument{
  15. {
  16. Name: "person",
  17. Type: "ascii",
  18. },
  19. },
  20. }
  21. cfg.Actions = append(cfg.Actions, a1)
  22. cfg.Sanitize()
  23. return e, cfg
  24. }
  25. func TestCreateExecutorAndExec(t *testing.T) {
  26. e, cfg := testingExecutor()
  27. req := ExecutionRequest{
  28. ActionName: "Do some tickles",
  29. AuthenticatedUser: &acl.AuthenticatedUser{Username: "Mr Tickle"},
  30. Cfg: cfg,
  31. Arguments: map[string]string{
  32. "person": "yourself",
  33. },
  34. }
  35. e.ExecRequest(&req)
  36. assert.NotNil(t, e, "Create an executor")
  37. assert.NotNil(t, e.ExecRequest(&req), "Execute a request")
  38. assert.Equal(t, int32(0), req.logEntry.ExitCode, "Exit code is zero")
  39. }
  40. func TestExecNonExistant(t *testing.T) {
  41. e, cfg := testingExecutor()
  42. req := ExecutionRequest{
  43. ActionName: "Waffles",
  44. logEntry: &InternalLogEntry{},
  45. Cfg: cfg,
  46. }
  47. e.ExecRequest(&req)
  48. assert.Equal(t, int32(-1337), req.logEntry.ExitCode, "Log entry is set to an internal error code")
  49. assert.Equal(t, "", req.logEntry.ActionIcon, "Log entry icon wasnt found")
  50. }
  51. func TestArgumentNameCamelCase(t *testing.T) {
  52. a1 := config.Action{
  53. Title: "Do some tickles",
  54. Shell: "echo 'Tickling {{ personName }}'",
  55. Arguments: []config.ActionArgument{
  56. {
  57. Name: "personName",
  58. Type: "ascii",
  59. },
  60. },
  61. }
  62. values := map[string]string{
  63. "personName": "Fred",
  64. }
  65. out, err := parseActionArguments(a1.Shell, values, &a1)
  66. assert.Equal(t, "echo 'Tickling Fred'", out)
  67. assert.Nil(t, err)
  68. }
  69. func TestArgumentNameSnakeCase(t *testing.T) {
  70. a1 := config.Action{
  71. Title: "Do some tickles",
  72. Shell: "echo 'Tickling {{ person_name }}'",
  73. Arguments: []config.ActionArgument{
  74. {
  75. Name: "person_name",
  76. Type: "ascii",
  77. },
  78. },
  79. }
  80. values := map[string]string{
  81. "person_name": "Fred",
  82. }
  83. out, err := parseActionArguments(a1.Shell, values, &a1)
  84. assert.Equal(t, "echo 'Tickling Fred'", out)
  85. assert.Nil(t, err)
  86. }