liquid_interpolatable.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. require 'spec_helper'
  2. shared_examples_for LiquidInterpolatable do
  3. before(:each) do
  4. @valid_params = {
  5. "normal" => "just some normal text",
  6. "variable" => "{{variable}}",
  7. "text" => "Some test with an embedded {{variable}}",
  8. "escape" => "This should be {{hello_world | uri_escape}}"
  9. }
  10. @checker = described_class.new(:name => "somename", :options => @valid_params)
  11. @checker.user = users(:jane)
  12. @event = Event.new
  13. @event.agent = agents(:bob_weather_agent)
  14. @event.payload = { :variable => 'hello', :hello_world => "Hello world"}
  15. @event.save!
  16. end
  17. describe "interpolating liquid templates" do
  18. it "should work" do
  19. @checker.interpolate_options(@checker.options, @event.payload).should == {
  20. "normal" => "just some normal text",
  21. "variable" => "hello",
  22. "text" => "Some test with an embedded hello",
  23. "escape" => "This should be Hello+world"
  24. }
  25. end
  26. it "should work with arrays", focus: true do
  27. @checker.options = {"value" => ["{{variable}}", "Much array", "Hey, {{hello_world}}"]}
  28. @checker.interpolate_options(@checker.options, @event.payload).should == {
  29. "value" => ["hello", "Much array", "Hey, Hello world"]
  30. }
  31. end
  32. it "should work recursively" do
  33. @checker.options['hash'] = {'recursive' => "{{variable}}"}
  34. @checker.options['indifferent_hash'] = ActiveSupport::HashWithIndifferentAccess.new({'recursive' => "{{variable}}"})
  35. @checker.interpolate_options(@checker.options, @event.payload).should == {
  36. "normal" => "just some normal text",
  37. "variable" => "hello",
  38. "text" => "Some test with an embedded hello",
  39. "escape" => "This should be Hello+world",
  40. "hash" => {'recursive' => 'hello'},
  41. "indifferent_hash" => {'recursive' => 'hello'},
  42. }
  43. end
  44. it "should work for strings" do
  45. @checker.interpolate_string("{{variable}}", @event.payload).should == "hello"
  46. @checker.interpolate_string("{{variable}} you", @event.payload).should == "hello you"
  47. end
  48. end
  49. describe "liquid tags" do
  50. it "should work with existing credentials" do
  51. @checker.interpolate_string("{% credential aws_key %}", {}).should == '2222222222-jane'
  52. end
  53. it "should raise an exception for undefined credentials" do
  54. expect {
  55. @checker.interpolate_string("{% credential unknown %}", {})
  56. }.to raise_error
  57. end
  58. end
  59. end