templates_test.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package tpl
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "testing"
  7. "github.com/OliveTin/OliveTin/internal/entities"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. type templateJsonCase struct {
  11. name string
  12. source string
  13. ent *entities.Entity
  14. args map[string]any
  15. expectedOutput string
  16. expectError bool
  17. checkJsonOnly bool
  18. }
  19. func (tt templateJsonCase) run(t *testing.T) {
  20. output, err := ParseTemplateWithActionContext(tt.source, tt.ent, tt.args)
  21. if tt.expectError {
  22. assert.Error(t, err)
  23. return
  24. }
  25. assert.NoError(t, err)
  26. if tt.checkJsonOnly {
  27. strArgs := make(map[string]string)
  28. for k, v := range tt.args {
  29. strArgs[k] = fmt.Sprintf("%v", v)
  30. }
  31. assertJsonOutput(t, output, tt.expectedOutput, strArgs)
  32. return
  33. }
  34. assert.Equal(t, tt.expectedOutput, output)
  35. }
  36. func TestParseTemplateWithActionContext_Json(t *testing.T) {
  37. tests := []templateJsonCase{
  38. {
  39. name: "Arguments piped to Json",
  40. source: `echo {{ .Arguments | Json }}`,
  41. ent: nil,
  42. args: map[string]any{"value": "true", "ot_username": "alice"},
  43. expectedOutput: `echo `,
  44. expectError: false,
  45. checkJsonOnly: true,
  46. },
  47. {
  48. name: "CurrentEntity field piped to Json",
  49. source: `curl -d {{ .CurrentEntity.foo.bar | Json }}`,
  50. ent: &entities.Entity{Data: map[string]any{"foo": map[string]any{"bar": "baz"}}},
  51. args: nil,
  52. expectedOutput: `curl -d "baz"`,
  53. expectError: false,
  54. },
  55. {
  56. name: "CurrentEntity nested object piped to Json",
  57. source: `curl --json -d {{ .CurrentEntity.payload | Json }}`,
  58. ent: &entities.Entity{Data: map[string]any{"payload": map[string]any{"on": true}}},
  59. args: nil,
  60. expectedOutput: `curl --json -d {"on":true}`,
  61. expectError: false,
  62. },
  63. {
  64. name: "Single argument value as Json",
  65. source: `echo {{ .Arguments.value | Json }}`,
  66. ent: nil,
  67. args: map[string]any{"value": "hello"},
  68. expectedOutput: `echo "hello"`,
  69. expectError: false,
  70. },
  71. }
  72. for _, tt := range tests {
  73. t.Run(tt.name, tt.run)
  74. }
  75. }
  76. func assertJsonOutput(t *testing.T, output, expectedPrefix string, args map[string]string) {
  77. t.Helper()
  78. prefix := strings.TrimSuffix(expectedPrefix, " ")
  79. assert.True(t, strings.HasPrefix(output, prefix), "output %q should start with %q", output, prefix)
  80. jsonPart := strings.TrimPrefix(output, prefix)
  81. jsonPart = strings.TrimSpace(jsonPart)
  82. var decoded map[string]string
  83. err := json.Unmarshal([]byte(jsonPart), &decoded)
  84. assert.NoError(t, err)
  85. for k, v := range args {
  86. assert.Equal(t, v, decoded[k], "decoded JSON should contain %s=%s", k, v)
  87. }
  88. assert.Len(t, decoded, len(args))
  89. }