arguments_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package executor
  2. import (
  3. config "github.com/OliveTin/OliveTin/internal/config"
  4. "github.com/stretchr/testify/assert"
  5. "testing"
  6. )
  7. func TestSanitizeUnsafe(t *testing.T) {
  8. assert.Nil(t, TypeSafetyCheck("", "_zomg_ c:/ haxxor ' bobby tables && rm -rf ", "very_dangerous_raw_string"))
  9. }
  10. func TestSanitizeUnimplemented(t *testing.T) {
  11. err := TypeSafetyCheck("", "I am a happy little argument", "greeting_type")
  12. assert.NotNil(t, err, "Test an argument type that does not exist")
  13. }
  14. func TestArgumentValueNullable(t *testing.T) {
  15. a1 := config.Action{
  16. Title: "Release the hounds",
  17. Shell: "echo 'Releasing {{ count }} hounds'",
  18. Arguments: []config.ActionArgument{
  19. {
  20. Name: "count",
  21. Type: "int",
  22. },
  23. },
  24. }
  25. values := map[string]string{
  26. "count": "",
  27. }
  28. out, err := parseActionArguments(values, &a1, a1.Title, "")
  29. assert.Equal(t, "echo 'Releasing hounds'", out)
  30. assert.Nil(t, err)
  31. a1.Arguments[0].RejectNull = true
  32. _, err = parseActionArguments(values, &a1, a1.Title, "")
  33. assert.NotNil(t, err)
  34. }
  35. func TestArgumentNameNumbers(t *testing.T) {
  36. a1 := config.Action{
  37. Title: "Do some tickles",
  38. Shell: "echo 'Tickling {{ person1name }}'",
  39. Arguments: []config.ActionArgument{
  40. {
  41. Name: "person1name",
  42. Type: "ascii",
  43. },
  44. },
  45. }
  46. values := map[string]string{
  47. "person1name": "Fred",
  48. }
  49. out, err := parseActionArguments(values, &a1, a1.Title, "")
  50. assert.Equal(t, "echo 'Tickling Fred'", out)
  51. assert.Nil(t, err)
  52. }
  53. func TestArgumentNotProvided(t *testing.T) {
  54. a1 := config.Action{
  55. Title: "Do some tickles",
  56. Shell: "echo 'Tickling {{ personName }}'",
  57. Arguments: []config.ActionArgument{
  58. {
  59. Name: "person",
  60. Type: "ascii",
  61. },
  62. },
  63. }
  64. values := map[string]string{}
  65. out, err := parseActionArguments(values, &a1, a1.Title, "")
  66. assert.Equal(t, "", out)
  67. assert.Equal(t, err.Error(), "Required arg not provided: personName")
  68. }
  69. func TestTypeSafetyCheckUrl(t *testing.T) {
  70. assert.Nil(t, TypeSafetyCheck("test1", "http://google.com", "url"), "Test URL: google.com")
  71. assert.Nil(t, TypeSafetyCheck("test2", "http://technowax.net:80?foo=bar", "url"), "Test URL: technowax.net with query arguments")
  72. assert.Nil(t, TypeSafetyCheck("test3", "http://localhost:80?foo=bar", "url"), "Test URL: localhost with query arguments")
  73. assert.NotNil(t, TypeSafetyCheck("test4", "http://lo host:80", "url"), "Test a badly formed URL")
  74. assert.NotNil(t, TypeSafetyCheck("test5", "12345", "url"), "Test a badly formed URL")
  75. assert.NotNil(t, TypeSafetyCheck("test6", "_!23;", "url"), "Test a badly formed URL")
  76. }