executor_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 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. }
  54. func TestArgumentNameCamelCase(t *testing.T) {
  55. a1 := config.Action{
  56. Title: "Do some tickles",
  57. Shell: "echo 'Tickling {{ personName }}'",
  58. Arguments: []config.ActionArgument{
  59. {
  60. Name: "personName",
  61. Type: "ascii",
  62. },
  63. },
  64. }
  65. values := map[string]string {
  66. "personName": "Fred",
  67. }
  68. out, err := parseActionArguments(a1.Shell, values, &a1);
  69. assert.Equal(t, "echo 'Tickling Fred'", out);
  70. assert.Nil(t, err)
  71. }
  72. func TestArgumentNameSnakeCase(t *testing.T) {
  73. a1 := config.Action{
  74. Title: "Do some tickles",
  75. Shell: "echo 'Tickling {{ person_name }}'",
  76. Arguments: []config.ActionArgument{
  77. {
  78. Name: "person_name",
  79. Type: "ascii",
  80. },
  81. },
  82. }
  83. values := map[string]string {
  84. "person_name": "Fred",
  85. }
  86. out, err := parseActionArguments(a1.Shell, values, &a1);
  87. assert.Equal(t, "echo 'Tickling Fred'", out);
  88. assert.Nil(t, err)
  89. }
  90. func TestArgumentNameNumbers(t *testing.T) {
  91. a1 := config.Action{
  92. Title: "Do some tickles",
  93. Shell: "echo 'Tickling {{ person1name }}'",
  94. Arguments: []config.ActionArgument{
  95. {
  96. Name: "person1name",
  97. Type: "ascii",
  98. },
  99. },
  100. }
  101. values := map[string]string {
  102. "person1name": "Fred",
  103. }
  104. out, err := parseActionArguments(a1.Shell, values, &a1);
  105. assert.Equal(t, "echo 'Tickling Fred'", out);
  106. assert.Nil(t, err)
  107. }
  108. func TestArgumentNotProvided(t *testing.T) {
  109. a1 := config.Action{
  110. Title: "Do some tickles",
  111. Shell: "echo 'Tickling {{ personName }}'",
  112. Arguments: []config.ActionArgument{
  113. {
  114. Name: "person",
  115. Type: "ascii",
  116. },
  117. },
  118. }
  119. values := map[string]string {}
  120. out, err := parseActionArguments(a1.Shell, values, &a1);
  121. assert.Equal(t, "", out);
  122. assert.Equal(t, err.Error(), "Required arg not provided: personName")
  123. }