executor_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package executor
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/OliveTin/OliveTin/internal/auth"
  6. authpublic "github.com/OliveTin/OliveTin/internal/auth/authpublic"
  7. config "github.com/OliveTin/OliveTin/internal/config"
  8. )
  9. func testingExecutor() (*Executor, *config.Config) {
  10. cfg := config.DefaultConfig()
  11. e := DefaultExecutor(cfg)
  12. a1 := &config.Action{
  13. Title: "Do some tickles",
  14. Shell: "echo 'Tickling {{ person }}'",
  15. Arguments: []config.ActionArgument{
  16. {
  17. Name: "person",
  18. Type: "ascii",
  19. },
  20. },
  21. }
  22. cfg.Actions = append(cfg.Actions, a1)
  23. cfg.Sanitize()
  24. return e, cfg
  25. }
  26. func TestCreateExecutorAndExec(t *testing.T) {
  27. e, cfg := testingExecutor()
  28. req := ExecutionRequest{
  29. AuthenticatedUser: &authpublic.AuthenticatedUser{Username: "Mr Tickle"},
  30. Cfg: cfg,
  31. Arguments: map[string]string{
  32. "person": "yourself",
  33. },
  34. }
  35. // Ensure bindings are available and set the binding to the only configured action
  36. e.RebuildActionMap()
  37. if len(cfg.Actions) > 0 {
  38. req.Binding = e.FindBindingWithNoEntity(cfg.Actions[0])
  39. }
  40. assert.NotNil(t, e, "Create an executor")
  41. wg, _ := e.ExecRequest(&req)
  42. wg.Wait()
  43. assert.Equal(t, int32(0), req.logEntry.ExitCode, "Exit code is zero")
  44. }
  45. func TestExecNonExistant(t *testing.T) {
  46. e, cfg := testingExecutor()
  47. req := ExecutionRequest{
  48. // Binding: e.FindBindingWithNoEntity("waffles"),
  49. logEntry: &InternalLogEntry{},
  50. Cfg: cfg,
  51. }
  52. wg, _ := e.ExecRequest(&req)
  53. wg.Wait()
  54. assert.Equal(t, int32(-1337), req.logEntry.ExitCode, "Log entry is set to an internal error code")
  55. assert.Equal(t, "💩", req.logEntry.ActionIcon, "Log entry icon is a poop (not found)")
  56. }
  57. func TestArgumentNameCamelCase(t *testing.T) {
  58. req := newExecRequest()
  59. req.Binding.Action = &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. req.Arguments = map[string]string{
  70. "personName": "Fred",
  71. }
  72. out, err := parseActionArguments(req)
  73. assert.Equal(t, "echo 'Tickling Fred'", out)
  74. assert.Nil(t, err)
  75. }
  76. func TestArgumentNameSnakeCase(t *testing.T) {
  77. req := newExecRequest()
  78. req.Binding.Action = &config.Action{
  79. Title: "Do some tickles",
  80. Shell: "echo 'Tickling {{ person_name }}'",
  81. Arguments: []config.ActionArgument{
  82. {
  83. Name: "person_name",
  84. Type: "ascii",
  85. },
  86. },
  87. }
  88. req.Arguments = map[string]string{
  89. "person_name": "Fred",
  90. }
  91. out, err := parseActionArguments(req)
  92. assert.Equal(t, "echo 'Tickling Fred'", out)
  93. assert.Nil(t, err)
  94. }
  95. func TestGetLogsEmpty(t *testing.T) {
  96. e, cfg := testingExecutor()
  97. assert.Equal(t, int64(10), cfg.LogHistoryPageSize, "Logs page size should be 10")
  98. logs, paging := e.GetLogTrackingIds(0, 10)
  99. assert.NotNil(t, logs, "Logs should not be nil")
  100. assert.Equal(t, 0, len(logs), "No logs yet")
  101. assert.Equal(t, int64(0), paging.CountRemaining, "There should be no remaining logs")
  102. }
  103. func TestGetLogsLessThanPageSize(t *testing.T) {
  104. e, cfg := testingExecutor()
  105. cfg.Actions = append(cfg.Actions, &config.Action{
  106. Title: "blat",
  107. Shell: "date",
  108. })
  109. cfg.Sanitize()
  110. // Rebuild action map to include newly added action
  111. e.RebuildActionMap()
  112. assert.Equal(t, int64(10), cfg.LogHistoryPageSize, "Logs page size should be 10")
  113. logEntries, paging := e.GetLogTrackingIds(0, 10)
  114. assert.Equal(t, 0, len(logEntries), "There should be 0 logs")
  115. assert.Zero(t, paging.CountRemaining, "There should be no remaining logs")
  116. execNewReqAndWait(e, "blat", cfg)
  117. execNewReqAndWait(e, "blat", cfg)
  118. execNewReqAndWait(e, "blat", cfg)
  119. execNewReqAndWait(e, "blat", cfg)
  120. execNewReqAndWait(e, "blat", cfg)
  121. execNewReqAndWait(e, "blat", cfg)
  122. execNewReqAndWait(e, "blat", cfg)
  123. logEntries, paging = e.GetLogTrackingIds(0, 10)
  124. assert.Equal(t, 7, len(logEntries), "There should be 7 logs")
  125. assert.Zero(t, paging.CountRemaining, "There should be no remaining logs")
  126. execNewReqAndWait(e, "blat", cfg)
  127. execNewReqAndWait(e, "blat", cfg)
  128. execNewReqAndWait(e, "blat", cfg)
  129. execNewReqAndWait(e, "blat", cfg)
  130. execNewReqAndWait(e, "blat", cfg)
  131. logEntries, paging = e.GetLogTrackingIds(0, 10)
  132. assert.Equal(t, 10, len(logEntries), "There should be 10 logs")
  133. assert.Equal(t, int64(2), paging.CountRemaining, "There should be 1 remaining logs")
  134. }
  135. func execNewReqAndWait(e *Executor, title string, cfg *config.Config) {
  136. req := &ExecutionRequest{
  137. // ActionTitle: title,
  138. Cfg: cfg,
  139. }
  140. // Ensure we have a binding for the requested title
  141. e.RebuildActionMap()
  142. var action *config.Action
  143. for _, a := range cfg.Actions {
  144. if a.Title == title {
  145. action = a
  146. break
  147. }
  148. }
  149. if action != nil {
  150. req.Binding = e.FindBindingWithNoEntity(action)
  151. }
  152. wg, _ := e.ExecRequest(req)
  153. wg.Wait()
  154. }
  155. func TestGetPagingIndexes(t *testing.T) {
  156. assert.Zero(t, getPagingStartIndex(5, 0), "Testing start index from empty list")
  157. assert.Equal(t, int64(4), getPagingStartIndex(5, 10), "Testing start index from mid point")
  158. assert.Equal(t, int64(9), getPagingStartIndex(-1, 10), "Testing start index with negative offset")
  159. assert.Equal(t, int64(0), getPagingStartIndex(15, 10), "Testing start index with large offset")
  160. assert.Equal(t, int64(9), getPagingStartIndex(0, 10), "Testing start index with zero count")
  161. }
  162. func TestUnsetRequiredArgument(t *testing.T) {
  163. req := newExecRequest()
  164. req.Binding.Action = &config.Action{
  165. Title: "Print your name",
  166. Shell: "echo 'Your name is: {{ name }}'",
  167. Arguments: []config.ActionArgument{
  168. {
  169. Name: "name",
  170. Type: "ascii",
  171. },
  172. },
  173. }
  174. req.Arguments = map[string]string{}
  175. out, err := parseActionArguments(req)
  176. assert.Equal(t, "", out)
  177. assert.NotNil(t, err)
  178. }
  179. func TestUnusedArgumentStillPassesTypeSafetyCheck(t *testing.T) {
  180. req := newExecRequest()
  181. req.Binding.Action = &config.Action{
  182. Title: "Print your name",
  183. Shell: "echo 'Your name is: {{ name }}'",
  184. Arguments: []config.ActionArgument{
  185. {
  186. Name: "name",
  187. Type: "ascii",
  188. },
  189. {
  190. Name: "age",
  191. Type: "int",
  192. },
  193. },
  194. }
  195. req.Arguments = map[string]string{
  196. "name": "Fred",
  197. "age": "Not an integer",
  198. }
  199. out, err := parseActionArguments(req)
  200. assert.Equal(t, "", out)
  201. assert.NotNil(t, err)
  202. }
  203. // https://github.com/OliveTin/OliveTin/issues/564
  204. func TestMangleInvalidArgumentValues(t *testing.T) {
  205. e, cfg := testingExecutor()
  206. a1 := &config.Action{
  207. Title: "Validate my date without seconds because I am from an Android phone",
  208. Shell: "echo 'The date is: {{ date }}'",
  209. Arguments: []config.ActionArgument{
  210. {
  211. Name: "date",
  212. Type: "datetime",
  213. },
  214. },
  215. }
  216. cfg.Actions = append(cfg.Actions, a1)
  217. cfg.Sanitize()
  218. // Build bindings for newly added action
  219. e.RebuildActionMap()
  220. req := ExecutionRequest{
  221. // Action: a1,
  222. AuthenticatedUser: auth.UserFromSystem(cfg, "testuser"),
  223. Cfg: cfg,
  224. Arguments: map[string]string{
  225. "date": "1990-01-10T12:00", // Invalid format, should be without seconds
  226. },
  227. }
  228. // Set binding to our appended action
  229. req.Binding = e.FindBindingWithNoEntity(a1)
  230. wg, _ := e.ExecRequest(&req)
  231. wg.Wait()
  232. assert.NotNil(t, req.logEntry, "Log entry should not be nil")
  233. assert.Equal(t, req.logEntry.Output, "The date is: 1990-01-10T12:00:00\n", "Date should be mangled to a valid format")
  234. }