Nessuna descrizione http://j1x-huginn.herokuapp.com

event_formatting_agent_spec.rb 4.8KB

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