event_formatting_agent_spec.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. require 'rails_helper'
  2. describe Agents::EventFormattingAgent do
  3. before do
  4. @valid_params = {
  5. :name => "somename",
  6. :options => {
  7. :instructions => {
  8. :message => "Received {{content.text}} from {{content.name}} .",
  9. :subject => "Weather looks like {{conditions}} according to the forecast at {{pretty_date.time}}",
  10. :timezone => "{{timezone}}",
  11. :agent => "{{agent.type}}",
  12. :created_at => "{{created_at}}",
  13. :created_at_iso => "{{created_at | date:'%FT%T%:z'}}",
  14. },
  15. :mode => "clean",
  16. :matchers => [
  17. {
  18. :path => "{{date.pretty}}",
  19. :regexp => "\\A(?<time>\\d\\d:\\d\\d [AP]M [A-Z]+)",
  20. :to => "pretty_date",
  21. },
  22. {
  23. :path => "{{pretty_date.time}}",
  24. :regexp => "(?<timezone>[A-Z]+)\\z",
  25. },
  26. ],
  27. }
  28. }
  29. @checker = Agents::EventFormattingAgent.new(@valid_params)
  30. @checker.user = users(:jane)
  31. @checker.save!
  32. @event = Event.new
  33. @event.agent = agents(:jane_weather_agent)
  34. @event.created_at = Time.now
  35. @event.payload = {
  36. :content => {
  37. :text => "Some Lorem Ipsum",
  38. :name => "somevalue",
  39. },
  40. :date => {
  41. :epoch => "1357959600",
  42. :pretty => "10:00 PM EST on January 11, 2013"
  43. },
  44. :conditions => "someothervalue"
  45. }
  46. @event2 = Event.new
  47. @event2.agent = agents(:jane_weather_agent)
  48. @event2.created_at = Time.now
  49. @event2.payload = {
  50. :content => {
  51. :text => "Some Lorem Ipsum 2",
  52. :name => "somevalue2",
  53. },
  54. :date => {
  55. :epoch => "1366372800",
  56. :pretty => "08:00 AM EDT on April 19, 2013"
  57. },
  58. :conditions => "someothervalue2"
  59. }
  60. end
  61. describe "#receive" do
  62. it "should accept clean mode" do
  63. @checker.receive([@event])
  64. expect(Event.last.payload[:content]).to eq(nil)
  65. end
  66. it "should accept merge mode" do
  67. @checker.options[:mode] = "merge"
  68. @checker.receive([@event])
  69. expect(Event.last.payload[:content]).not_to eq(nil)
  70. end
  71. it "should handle Liquid templating in instructions" do
  72. @checker.receive([@event])
  73. expect(Event.last.payload[:message]).to eq("Received Some Lorem Ipsum from somevalue .")
  74. expect(Event.last.payload[:agent]).to eq("WeatherAgent")
  75. expect(Event.last.payload[:created_at]).to eq(@event.created_at.to_s)
  76. expect(Event.last.payload[:created_at_iso]).to eq(@event.created_at.iso8601)
  77. end
  78. it "should handle matchers and Liquid templating in instructions" do
  79. expect {
  80. @checker.receive([@event, @event2])
  81. }.to change { Event.count }.by(2)
  82. formatted_event1, formatted_event2 = Event.last(2)
  83. expect(formatted_event1.payload[:subject]).to eq("Weather looks like someothervalue according to the forecast at 10:00 PM EST")
  84. expect(formatted_event1.payload[:timezone]).to eq("EST")
  85. expect(formatted_event2.payload[:subject]).to eq("Weather looks like someothervalue2 according to the forecast at 08:00 AM EDT")
  86. expect(formatted_event2.payload[:timezone]).to eq("EDT")
  87. end
  88. it "should not fail if no matchers are defined" do
  89. @checker.options.delete(:matchers)
  90. expect {
  91. @checker.receive([@event, @event2])
  92. }.to change { Event.count }.by(2)
  93. formatted_event1, formatted_event2 = Event.last(2)
  94. expect(formatted_event1.payload[:subject]).to eq("Weather looks like someothervalue according to the forecast at ")
  95. expect(formatted_event1.payload[:timezone]).to eq("")
  96. expect(formatted_event2.payload[:subject]).to eq("Weather looks like someothervalue2 according to the forecast at ")
  97. expect(formatted_event2.payload[:timezone]).to eq("")
  98. end
  99. it "should allow escaping" do
  100. @event.payload[:content][:name] = "escape this!?"
  101. @event.save!
  102. @checker.options[:instructions][:message] = "Escaped: {{content.name | uri_escape}}\nNot escaped: {{content.name}}"
  103. @checker.save!
  104. @checker.receive([@event])
  105. expect(Event.last.payload[:message]).to eq("Escaped: escape+this%21%3F\nNot escaped: escape this!?")
  106. end
  107. it "should handle multiple events" do
  108. event1 = Event.new
  109. event1.agent = agents(:bob_weather_agent)
  110. event1.payload = {
  111. :content => {
  112. :text => "Some Lorem Ipsum",
  113. :name => "somevalue"
  114. },
  115. :conditions => "someothervalue"
  116. }
  117. event2 = Event.new
  118. event2.agent = agents(:bob_weather_agent)
  119. event2.payload = {
  120. :content => {
  121. :text => "Some Lorem Ipsum",
  122. :name => "somevalue"
  123. },
  124. :conditions => "someothervalue"
  125. }
  126. expect {
  127. @checker.receive([event2, event1])
  128. }.to change { Event.count }.by(2)
  129. end
  130. end
  131. describe "validation" do
  132. before do
  133. expect(@checker).to be_valid
  134. end
  135. it "should validate presence of instructions" do
  136. @checker.options[:instructions] = ""
  137. expect(@checker).not_to be_valid
  138. end
  139. it "should validate type of matchers" do
  140. @checker.options[:matchers] = ""
  141. expect(@checker).not_to be_valid
  142. @checker.options[:matchers] = {}
  143. expect(@checker).not_to be_valid
  144. end
  145. it "should validate the contents of matchers" do
  146. @checker.options[:matchers] = [
  147. {}
  148. ]
  149. expect(@checker).not_to be_valid
  150. @checker.options[:matchers] = [
  151. { :regexp => "(not closed", :path => "text" }
  152. ]
  153. expect(@checker).not_to be_valid
  154. @checker.options[:matchers] = [
  155. { :regexp => "(closed)", :path => "text", :to => "foo" }
  156. ]
  157. expect(@checker).to be_valid
  158. end
  159. it "should validate presence of mode" do
  160. @checker.options[:mode] = ""
  161. expect(@checker).not_to be_valid
  162. end
  163. end
  164. end