actions_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package server
  2. import (
  3. "github.com/stretchr/testify/require"
  4. "testing"
  5. )
  6. func TestParseActions(t *testing.T) {
  7. actions, err := parseActions("[]")
  8. require.Nil(t, err)
  9. require.Empty(t, actions)
  10. // Basic test
  11. actions, err = parseActions("action=http, label=Open door, url=https://door.lan/open; view, Show portal, https://door.lan")
  12. require.Nil(t, err)
  13. require.Equal(t, 2, len(actions))
  14. require.Equal(t, "http", actions[0].Action)
  15. require.Equal(t, "Open door", actions[0].Label)
  16. require.Equal(t, "https://door.lan/open", actions[0].URL)
  17. require.Equal(t, "view", actions[1].Action)
  18. require.Equal(t, "Show portal", actions[1].Label)
  19. require.Equal(t, "https://door.lan", actions[1].URL)
  20. // JSON
  21. actions, err = parseActions(`[{"action":"http","label":"Open door","url":"https://door.lan/open"}, {"action":"view","label":"Show portal","url":"https://door.lan"}]`)
  22. require.Nil(t, err)
  23. require.Equal(t, 2, len(actions))
  24. require.Equal(t, "http", actions[0].Action)
  25. require.Equal(t, "Open door", actions[0].Label)
  26. require.Equal(t, "https://door.lan/open", actions[0].URL)
  27. require.Equal(t, "view", actions[1].Action)
  28. require.Equal(t, "Show portal", actions[1].Label)
  29. require.Equal(t, "https://door.lan", actions[1].URL)
  30. // Other params
  31. actions, err = parseActions("action=http, label=Open door, url=https://door.lan/open, body=this is a body, method=PUT")
  32. require.Nil(t, err)
  33. require.Equal(t, 1, len(actions))
  34. require.Equal(t, "http", actions[0].Action)
  35. require.Equal(t, "Open door", actions[0].Label)
  36. require.Equal(t, "https://door.lan/open", actions[0].URL)
  37. require.Equal(t, "PUT", actions[0].Method)
  38. require.Equal(t, "this is a body", actions[0].Body)
  39. // Extras with underscores
  40. actions, err = parseActions("action=broadcast, label=Do a thing, extras.command=some command, extras.some_param=a parameter")
  41. require.Nil(t, err)
  42. require.Equal(t, 1, len(actions))
  43. require.Equal(t, "broadcast", actions[0].Action)
  44. require.Equal(t, "Do a thing", actions[0].Label)
  45. require.Equal(t, 2, len(actions[0].Extras))
  46. require.Equal(t, "some command", actions[0].Extras["command"])
  47. require.Equal(t, "a parameter", actions[0].Extras["some_param"])
  48. // Headers with dashes
  49. actions, err = parseActions("action=http, label=Send request, url=http://example.com, method=GET, headers.Content-Type=application/json, headers.Authorization=Basic sdasffsf")
  50. require.Nil(t, err)
  51. require.Equal(t, 1, len(actions))
  52. require.Equal(t, "http", actions[0].Action)
  53. require.Equal(t, "Send request", actions[0].Label)
  54. require.Equal(t, 2, len(actions[0].Headers))
  55. require.Equal(t, "application/json", actions[0].Headers["Content-Type"])
  56. require.Equal(t, "Basic sdasffsf", actions[0].Headers["Authorization"])
  57. // Quotes
  58. actions, err = parseActions(`action=http, "Look ma, \"quotes\"; and semicolons", url=http://example.com`)
  59. require.Nil(t, err)
  60. require.Equal(t, 1, len(actions))
  61. require.Equal(t, "http", actions[0].Action)
  62. require.Equal(t, `Look ma, "quotes"; and semicolons`, actions[0].Label)
  63. require.Equal(t, `http://example.com`, actions[0].URL)
  64. // Single quotes
  65. actions, err = parseActions(`action=http, '"quotes" and \'single quotes\'', url=http://example.com`)
  66. require.Nil(t, err)
  67. require.Equal(t, 1, len(actions))
  68. require.Equal(t, "http", actions[0].Action)
  69. require.Equal(t, `"quotes" and 'single quotes'`, actions[0].Label)
  70. require.Equal(t, `http://example.com`, actions[0].URL)
  71. // Single quotes (JSON)
  72. actions, err = parseActions(`action=http, Post it, url=http://example.com, body='{"temperature": 65}'`)
  73. require.Nil(t, err)
  74. require.Equal(t, 1, len(actions))
  75. require.Equal(t, "http", actions[0].Action)
  76. require.Equal(t, "Post it", actions[0].Label)
  77. require.Equal(t, `http://example.com`, actions[0].URL)
  78. require.Equal(t, `{"temperature": 65}`, actions[0].Body)
  79. // Out of order
  80. actions, err = parseActions(`label="Out of order!" , action="http", url=http://example.com`)
  81. require.Nil(t, err)
  82. require.Equal(t, 1, len(actions))
  83. require.Equal(t, "http", actions[0].Action)
  84. require.Equal(t, `Out of order!`, actions[0].Label)
  85. require.Equal(t, `http://example.com`, actions[0].URL)
  86. // Spaces
  87. actions, err = parseActions(`action = http, label = 'this is a label', url = "http://google.com"`)
  88. require.Nil(t, err)
  89. require.Equal(t, 1, len(actions))
  90. require.Equal(t, "http", actions[0].Action)
  91. require.Equal(t, `this is a label`, actions[0].Label)
  92. require.Equal(t, `http://google.com`, actions[0].URL)
  93. // Non-ASCII
  94. actions, err = parseActions(`action = http, 'Кохайтеся а не воюйте, 💙🫤', url = "http://google.com"`)
  95. require.Nil(t, err)
  96. require.Equal(t, 1, len(actions))
  97. require.Equal(t, "http", actions[0].Action)
  98. require.Equal(t, `Кохайтеся а не воюйте, 💙🫤`, actions[0].Label)
  99. require.Equal(t, `http://google.com`, actions[0].URL)
  100. // Multiple actions, awkward spacing
  101. actions, err = parseActions(`http , 'Make love, not war 💙🫤' , https://ntfy.sh ; view, " yo ", https://x.org, clear=true`)
  102. require.Nil(t, err)
  103. require.Equal(t, 2, len(actions))
  104. require.Equal(t, "http", actions[0].Action)
  105. require.Equal(t, `Make love, not war 💙🫤`, actions[0].Label)
  106. require.Equal(t, `https://ntfy.sh`, actions[0].URL)
  107. require.Equal(t, false, actions[0].Clear)
  108. require.Equal(t, "view", actions[1].Action)
  109. require.Equal(t, " yo ", actions[1].Label)
  110. require.Equal(t, `https://x.org`, actions[1].URL)
  111. require.Equal(t, true, actions[1].Clear)
  112. // Invalid syntax
  113. _, err = parseActions(`label="Out of order!" x, action="http", url=http://example.com`)
  114. require.EqualError(t, err, "unexpected character 'x' at position 22")
  115. _, err = parseActions(`label="", action="http", url=http://example.com`)
  116. require.EqualError(t, err, "parameter 'label' is required")
  117. _, err = parseActions(`label=, action="http", url=http://example.com`)
  118. require.EqualError(t, err, "parameter 'label' is required")
  119. _, err = parseActions(`label="xx", action="http", url=http://example.com, what is this anyway`)
  120. require.EqualError(t, err, "term 'what is this anyway' unknown")
  121. _, err = parseActions(`fdsfdsf`)
  122. require.EqualError(t, err, "parameter 'action' cannot be 'fdsfdsf', valid values are 'view', 'broadcast' and 'http'")
  123. _, err = parseActions(`aaa=a, "bbb, 'ccc, ddd, eee "`)
  124. require.EqualError(t, err, "key 'aaa' unknown")
  125. _, err = parseActions(`action=http, label="omg the end quote is missing`)
  126. require.EqualError(t, err, "unexpected end of input, quote started at position 20")
  127. _, err = parseActions(`;;;;`)
  128. require.EqualError(t, err, "only 3 actions allowed")
  129. _, err = parseActions(`,,,,,,;;`)
  130. require.EqualError(t, err, "term '' unknown")
  131. _, err = parseActions(`''";,;"`)
  132. require.EqualError(t, err, "unexpected character '\"' at position 2")
  133. _, err = parseActions(`action=http, label=a label, body=somebody`)
  134. require.EqualError(t, err, "parameter 'url' is required for action 'http'")
  135. _, err = parseActions(`action=http, label=a label, url=http://ntfy.sh, method=HEAD, body=somebody`)
  136. require.EqualError(t, err, "parameter 'body' cannot be set if method is HEAD")
  137. _, err = parseActions(`[ invalid json ]`)
  138. require.EqualError(t, err, "JSON error: invalid character 'i' looking for beginning of value")
  139. _, err = parseActions(`[ { "some": "object" } ]`)
  140. require.EqualError(t, err, "parameter 'action' cannot be '', valid values are 'view', 'broadcast' and 'http'")
  141. _, err = parseActions("\x00\x01\xFFx\xFE")
  142. require.EqualError(t, err, "invalid utf-8 string")
  143. _, err = parseActions(`http, label, http://x.org, clear=x`)
  144. require.EqualError(t, err, "parameter 'clear' cannot be 'x', only boolean values are allowed (true/yes/1/false/no/0)")
  145. }