event_formatting_agent_spec.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 mode" do
  72. @checker.options[:mode] = "{{'merge'}}"
  73. @checker.receive([@event])
  74. expect(Event.last.payload[:content]).not_to eq(nil)
  75. end
  76. it "should handle Liquid templating in instructions" do
  77. @checker.receive([@event])
  78. expect(Event.last.payload[:message]).to eq("Received Some Lorem Ipsum from somevalue .")
  79. expect(Event.last.payload[:agent]).to eq("WeatherAgent")
  80. expect(Event.last.payload[:created_at]).to eq(@event.created_at.to_s)
  81. expect(Event.last.payload[:created_at_iso]).to eq(@event.created_at.iso8601)
  82. end
  83. it "should handle matchers and Liquid templating in instructions" do
  84. expect {
  85. @checker.receive([@event, @event2])
  86. }.to change { Event.count }.by(2)
  87. formatted_event1, formatted_event2 = Event.last(2)
  88. expect(formatted_event1.payload[:subject]).to eq("Weather looks like someothervalue according to the forecast at 10:00 PM EST")
  89. expect(formatted_event1.payload[:timezone]).to eq("EST")
  90. expect(formatted_event2.payload[:subject]).to eq("Weather looks like someothervalue2 according to the forecast at 08:00 AM EDT")
  91. expect(formatted_event2.payload[:timezone]).to eq("EDT")
  92. end
  93. it "should not fail if no matchers are defined" do
  94. @checker.options.delete(:matchers)
  95. expect {
  96. @checker.receive([@event, @event2])
  97. }.to change { Event.count }.by(2)
  98. formatted_event1, formatted_event2 = Event.last(2)
  99. expect(formatted_event1.payload[:subject]).to eq("Weather looks like someothervalue according to the forecast at ")
  100. expect(formatted_event1.payload[:timezone]).to eq("")
  101. expect(formatted_event2.payload[:subject]).to eq("Weather looks like someothervalue2 according to the forecast at ")
  102. expect(formatted_event2.payload[:timezone]).to eq("")
  103. end
  104. it "should allow escaping" do
  105. @event.payload[:content][:name] = "escape this!?"
  106. @event.save!
  107. @checker.options[:instructions][:message] = "Escaped: {{content.name | uri_escape}}\nNot escaped: {{content.name}}"
  108. @checker.save!
  109. @checker.receive([@event])
  110. expect(Event.last.payload[:message]).to eq("Escaped: escape+this%21%3F\nNot escaped: escape this!?")
  111. end
  112. it "should handle multiple events" do
  113. event1 = Event.new
  114. event1.agent = agents(:bob_weather_agent)
  115. event1.payload = {
  116. :content => {
  117. :text => "Some Lorem Ipsum",
  118. :name => "somevalue"
  119. },
  120. :conditions => "someothervalue"
  121. }
  122. event2 = Event.new
  123. event2.agent = agents(:bob_weather_agent)
  124. event2.payload = {
  125. :content => {
  126. :text => "Some Lorem Ipsum",
  127. :name => "somevalue"
  128. },
  129. :conditions => "someothervalue"
  130. }
  131. expect {
  132. @checker.receive([event2, event1])
  133. }.to change { Event.count }.by(2)
  134. end
  135. end
  136. describe "validation" do
  137. before do
  138. expect(@checker).to be_valid
  139. end
  140. it "should validate presence of instructions" do
  141. @checker.options[:instructions] = ""
  142. expect(@checker).not_to be_valid
  143. end
  144. it "should validate type of matchers" do
  145. @checker.options[:matchers] = ""
  146. expect(@checker).not_to be_valid
  147. @checker.options[:matchers] = {}
  148. expect(@checker).not_to be_valid
  149. end
  150. it "should validate the contents of matchers" do
  151. @checker.options[:matchers] = [
  152. {}
  153. ]
  154. expect(@checker).not_to be_valid
  155. @checker.options[:matchers] = [
  156. { :regexp => "(not closed", :path => "text" }
  157. ]
  158. expect(@checker).not_to be_valid
  159. @checker.options[:matchers] = [
  160. { :regexp => "(closed)", :path => "text", :to => "foo" }
  161. ]
  162. expect(@checker).to be_valid
  163. end
  164. it "should validate presence of mode" do
  165. @checker.options[:mode] = ""
  166. expect(@checker).not_to be_valid
  167. end
  168. it "requires mode to be 'clean' or 'merge'" do
  169. @checker.options['mode'] = 'what?'
  170. expect(@checker).not_to be_valid
  171. @checker.options['mode'] = 'clean'
  172. expect(@checker).to be_valid
  173. @checker.options['mode'] = 'merge'
  174. expect(@checker).to be_valid
  175. @checker.options['mode'] = :clean
  176. expect(@checker).to be_valid
  177. @checker.options['mode'] = :merge
  178. expect(@checker).to be_valid
  179. @checker.options['mode'] = '{{somekey}}'
  180. expect(@checker).to be_valid
  181. end
  182. end
  183. end