4
0

sanitize_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package config
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. )
  7. func TestSanitizeConfig(t *testing.T) {
  8. c := DefaultConfig()
  9. a := &Action{
  10. Title: "Mr Waffles",
  11. Arguments: []ActionArgument{
  12. {
  13. Name: "Carrots",
  14. Choices: []ActionArgumentChoice{
  15. {
  16. Value: "Waffle",
  17. },
  18. },
  19. },
  20. {
  21. Name: "foobar",
  22. },
  23. },
  24. }
  25. c.Actions = append(c.Actions, a)
  26. c.Sanitize()
  27. a2 := c.findAction("Mr Waffles")
  28. assert.NotNil(t, a2, "Found action after adding it")
  29. assert.Equal(t, 3, a2.Timeout, "Default timeout is set")
  30. assert.Equal(t, "hugeicons:CommandLineIcon", a2.Icon, "Default icon is the neutral CLI glyph")
  31. assert.Equal(t, "Carrots", a2.Arguments[0].Title, "Arg title is set to name")
  32. assert.Equal(t, "Waffle", a2.Arguments[0].Choices[0].Title, "Choice title is set to name")
  33. }
  34. func TestSanitizePopupOnStartHistory(t *testing.T) {
  35. c := DefaultConfig()
  36. c.DefaultPopupOnStart = "nothing"
  37. c.Actions = append(c.Actions, &Action{
  38. Title: "With history",
  39. PopupOnStart: "history",
  40. Shell: "true",
  41. })
  42. c.Sanitize()
  43. a := c.findAction("With history")
  44. if assert.NotNil(t, a) {
  45. assert.Equal(t, "history", a.OnClick, "history must be preserved on onclick")
  46. assert.Equal(t, "history", a.PopupOnStart, "legacy popupOnStart must mirror onclick")
  47. }
  48. }
  49. func TestSanitizeMigratesPopupOnStartToOnClick(t *testing.T) {
  50. c := DefaultConfig()
  51. c.Actions = append(c.Actions, &Action{
  52. Title: "Legacy popup",
  53. PopupOnStart: "execution-dialog",
  54. Shell: "true",
  55. })
  56. c.Sanitize()
  57. a := c.findAction("Legacy popup")
  58. require.NotNil(t, a)
  59. assert.Equal(t, "execution-dialog", a.OnClick)
  60. assert.Equal(t, "execution-dialog", a.PopupOnStart)
  61. }
  62. func TestSanitizeOnClickPreferredOverPopupOnStart(t *testing.T) {
  63. c := DefaultConfig()
  64. c.Actions = append(c.Actions, &Action{
  65. Title: "Preferred onclick",
  66. OnClick: "history",
  67. PopupOnStart: "execution-dialog",
  68. Shell: "true",
  69. })
  70. c.Sanitize()
  71. a := c.findAction("Preferred onclick")
  72. require.NotNil(t, a)
  73. assert.Equal(t, "history", a.OnClick)
  74. assert.Equal(t, "history", a.PopupOnStart)
  75. }
  76. func TestSanitizeMigratesDefaultPopupOnStartToDefaultOnClick(t *testing.T) {
  77. c := DefaultConfig()
  78. c.DefaultPopupOnStart = "execution-dialog"
  79. c.DefaultOnClick = ""
  80. c.Sanitize()
  81. assert.Equal(t, "execution-dialog", c.DefaultOnClick)
  82. assert.Equal(t, "execution-dialog", c.DefaultPopupOnStart)
  83. }
  84. func TestSanitizeMigratesDefaultPopupOnStartWhenDefaultOnClickUnchanged(t *testing.T) {
  85. c := DefaultConfig()
  86. c.DefaultPopupOnStart = "execution-dialog"
  87. c.Actions = append(c.Actions, &Action{
  88. Title: "Uses default onclick",
  89. Shell: "true",
  90. })
  91. c.Sanitize()
  92. assert.Equal(t, "execution-dialog", c.DefaultOnClick)
  93. assert.Equal(t, "execution-dialog", c.DefaultPopupOnStart)
  94. a := c.findAction("Uses default onclick")
  95. require.NotNil(t, a)
  96. assert.Equal(t, "execution-dialog", a.OnClick)
  97. assert.Equal(t, "execution-dialog", a.PopupOnStart)
  98. }
  99. func TestSanitizeConfigInlineDashboardActions(t *testing.T) {
  100. c := DefaultConfig()
  101. inline := &Action{
  102. Shell: "date",
  103. }
  104. dashboardActionTitle := "Inline Dashboard Action"
  105. c.Dashboards = []*DashboardComponent{
  106. {
  107. Title: "My Dashboard",
  108. Contents: []*DashboardComponent{
  109. {
  110. Title: dashboardActionTitle,
  111. InlineAction: inline,
  112. },
  113. },
  114. },
  115. }
  116. c.Sanitize()
  117. // Inline action should have been appended to the global Actions slice.
  118. assert.GreaterOrEqual(t, len(c.Actions), 1, "At least one action should exist after sanitization")
  119. // It should be discoverable by the dashboard component title when no explicit title was set.
  120. found := c.findAction(dashboardActionTitle)
  121. if assert.NotNil(t, found, "Inline dashboard action should be discoverable by title") {
  122. assert.Equal(t, dashboardActionTitle, found.Title, "Inline action title should default from dashboard component title")
  123. assert.Equal(t, 3, found.Timeout, "Inline action should have default timeout applied")
  124. assert.NotEmpty(t, found.Icon, "Inline action should have default icon applied")
  125. assert.NotEmpty(t, found.ID, "Inline action should have a generated ID")
  126. }
  127. }
  128. func TestValidateReservedActionArgumentNames(t *testing.T) {
  129. c := DefaultConfig()
  130. c.Actions = append(c.Actions, &Action{
  131. Title: "Reserved arg",
  132. Arguments: []ActionArgument{
  133. {Name: "ot_custom", Type: "ascii"},
  134. },
  135. })
  136. err := c.validateReservedActionArgumentNames()
  137. require.Error(t, err)
  138. assert.Contains(t, err.Error(), `action "Reserved arg" argument "ot_custom" uses reserved prefix "ot_"`)
  139. }
  140. func TestSanitizeActionGroupsDedupesGroupNames(t *testing.T) {
  141. c := DefaultConfig()
  142. c.ActionGroups = map[string]*ActionGroup{
  143. "unity": {MaxConcurrent: 1},
  144. }
  145. c.Actions = append(c.Actions, &Action{
  146. Title: "Build",
  147. Shell: "true",
  148. Groups: []string{"unity", "unity", ""},
  149. })
  150. c.Sanitize()
  151. action := c.findAction("Build")
  152. require.NotNil(t, action)
  153. assert.Equal(t, []string{"unity"}, action.Groups)
  154. }
  155. func TestSanitizeActionGroupsResolvesIcons(t *testing.T) {
  156. c := DefaultConfig()
  157. c.ActionGroups = map[string]*ActionGroup{
  158. "backup-jobs": {MaxConcurrent: 1, Icon: "backup"},
  159. }
  160. c.Sanitize()
  161. assert.Equal(t, "💾", c.ActionGroups["backup-jobs"].Icon)
  162. }
  163. func TestSanitizeActionGroupsDefaultsQueueSize(t *testing.T) {
  164. c := DefaultConfig()
  165. c.ActionGroups = map[string]*ActionGroup{
  166. "unity": {MaxConcurrent: 1},
  167. }
  168. c.Sanitize()
  169. assert.Equal(t, defaultActionGroupQueueSize, c.ActionGroups["unity"].QueueSize)
  170. }
  171. func TestSanitizeActionGroupsPreservesExplicitQueueSize(t *testing.T) {
  172. c := DefaultConfig()
  173. c.ActionGroups = map[string]*ActionGroup{
  174. "unity": {MaxConcurrent: 1, QueueSize: 2},
  175. }
  176. c.Sanitize()
  177. assert.Equal(t, 2, c.ActionGroups["unity"].QueueSize)
  178. }
  179. func TestValidateReservedActionArgumentNamesAllowsNonReserved(t *testing.T) {
  180. c := DefaultConfig()
  181. c.Actions = append(c.Actions, &Action{
  182. Title: "Allowed arg",
  183. Arguments: []ActionArgument{
  184. {Name: "target", Type: "ascii"},
  185. },
  186. })
  187. require.NoError(t, c.validateReservedActionArgumentNames())
  188. }
  189. func TestValidateReservedActionArgumentNamesChecksInlineActions(t *testing.T) {
  190. c := DefaultConfig()
  191. c.Dashboards = []*DashboardComponent{
  192. {
  193. Title: "Dashboard",
  194. Contents: []*DashboardComponent{
  195. {
  196. Title: "Inline reserved arg",
  197. InlineAction: &Action{
  198. Shell: "echo test",
  199. Arguments: []ActionArgument{
  200. {Name: "ot_custom", Type: "ascii"},
  201. },
  202. },
  203. },
  204. },
  205. },
  206. }
  207. c.sanitizeDashboardsForInlineActions()
  208. err := c.validateReservedActionArgumentNames()
  209. require.Error(t, err)
  210. assert.Contains(t, err.Error(), `action "Inline reserved arg" argument "ot_custom" uses reserved prefix "ot_"`)
  211. }
  212. func TestValidateUniqueLocalUserAPIKeys(t *testing.T) {
  213. t.Parallel()
  214. err := validateUniqueLocalUserAPIKeys([]*LocalUser{
  215. {Username: "a", ApiKey: "same"},
  216. {Username: "b", ApiKey: "same"},
  217. })
  218. require.Error(t, err)
  219. err = validateUniqueLocalUserAPIKeys([]*LocalUser{
  220. {Username: "a", ApiKey: "one"},
  221. {Username: "b", ApiKey: "two"},
  222. })
  223. require.NoError(t, err)
  224. }