executor_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 TestSanitizeUnimplemented(t *testing.T) {
  12. err := TypeSafetyCheck("", "I am a happy little argument", "greeting_type")
  13. assert.NotNil(t, err, "Test an argument type that does not exist")
  14. }
  15. func testingExecutor() (*Executor, *config.Config) {
  16. e := DefaultExecutor()
  17. cfg := config.DefaultConfig()
  18. a1 := config.Action{
  19. Title: "Do some tickles",
  20. Shell: "echo 'Tickling {{ person }}'",
  21. Arguments: []config.ActionArgument{
  22. {
  23. Name: "person",
  24. Type: "ascii",
  25. },
  26. },
  27. }
  28. cfg.Actions = append(cfg.Actions, a1)
  29. cfg.Sanitize()
  30. return e, cfg
  31. }
  32. func TestCreateExecutorAndExec(t *testing.T) {
  33. e, cfg := testingExecutor()
  34. req := ExecutionRequest{
  35. ActionName: "Do some tickles",
  36. AuthenticatedUser: &acl.AuthenticatedUser{Username: "Mr Tickle"},
  37. Cfg: cfg,
  38. Arguments: map[string]string{
  39. "person": "yourself",
  40. },
  41. }
  42. e.ExecRequest(&req)
  43. assert.NotNil(t, e, "Create an executor")
  44. assert.NotNil(t, e.ExecRequest(&req), "Execute a request")
  45. assert.Equal(t, int32(0), req.logEntry.ExitCode, "Exit code is zero")
  46. }
  47. func TestExecNonExistant(t *testing.T) {
  48. e, cfg := testingExecutor()
  49. req := ExecutionRequest{
  50. ActionName: "Waffles",
  51. logEntry: &InternalLogEntry{},
  52. Cfg: cfg,
  53. }
  54. e.ExecRequest(&req)
  55. assert.Equal(t, int32(-1337), req.logEntry.ExitCode, "Log entry is set to an internal error code")
  56. assert.Equal(t, "", req.logEntry.ActionIcon, "Log entry icon wasnt found")
  57. }
  58. func TestArgumentNameCamelCase(t *testing.T) {
  59. a1 := config.Action{
  60. Title: "Do some tickles",
  61. Shell: "echo 'Tickling {{ personName }}'",
  62. Arguments: []config.ActionArgument{
  63. {
  64. Name: "personName",
  65. Type: "ascii",
  66. },
  67. },
  68. }
  69. values := map[string]string{
  70. "personName": "Fred",
  71. }
  72. out, err := parseActionArguments(a1.Shell, values, &a1)
  73. assert.Equal(t, "echo 'Tickling Fred'", out)
  74. assert.Nil(t, err)
  75. }
  76. func TestArgumentNameSnakeCase(t *testing.T) {
  77. a1 := config.Action{
  78. Title: "Do some tickles",
  79. Shell: "echo 'Tickling {{ person_name }}'",
  80. Arguments: []config.ActionArgument{
  81. {
  82. Name: "person_name",
  83. Type: "ascii",
  84. },
  85. },
  86. }
  87. values := map[string]string{
  88. "person_name": "Fred",
  89. }
  90. out, err := parseActionArguments(a1.Shell, values, &a1)
  91. assert.Equal(t, "echo 'Tickling Fred'", out)
  92. assert.Nil(t, err)
  93. }
  94. func TestArgumentNameNumbers(t *testing.T) {
  95. a1 := config.Action{
  96. Title: "Do some tickles",
  97. Shell: "echo 'Tickling {{ person1name }}'",
  98. Arguments: []config.ActionArgument{
  99. {
  100. Name: "person1name",
  101. Type: "ascii",
  102. },
  103. },
  104. }
  105. values := map[string]string{
  106. "person1name": "Fred",
  107. }
  108. out, err := parseActionArguments(a1.Shell, values, &a1)
  109. assert.Equal(t, "echo 'Tickling Fred'", out)
  110. assert.Nil(t, err)
  111. }
  112. func TestArgumentNotProvided(t *testing.T) {
  113. a1 := config.Action{
  114. Title: "Do some tickles",
  115. Shell: "echo 'Tickling {{ personName }}'",
  116. Arguments: []config.ActionArgument{
  117. {
  118. Name: "person",
  119. Type: "ascii",
  120. },
  121. },
  122. }
  123. values := map[string]string{}
  124. out, err := parseActionArguments(a1.Shell, values, &a1)
  125. assert.Equal(t, "", out)
  126. assert.Equal(t, err.Error(), "Required arg not provided: personName")
  127. }
  128. func TestTypeSafetyCheckUrl(t *testing.T) {
  129. assert.Nil(t, TypeSafetyCheck("test1", "http://google.com", "url"), "Test URL: google.com")
  130. assert.Nil(t, TypeSafetyCheck("test2", "http://technowax.net:80?foo=bar", "url"), "Test URL: technowax.net with query arguments")
  131. assert.Nil(t, TypeSafetyCheck("test3", "http://localhost:80?foo=bar", "url"), "Test URL: localhost with query arguments")
  132. assert.NotNil(t, TypeSafetyCheck("test4", "http://lo host:80", "url"), "Test a badly formed URL")
  133. assert.NotNil(t, TypeSafetyCheck("test5", "12345", "url"), "Test a badly formed URL")
  134. assert.NotNil(t, TypeSafetyCheck("test6", "_!23;", "url"), "Test a badly formed URL")
  135. }