data_output_agent_spec.rb 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # encoding: utf-8
  2. require 'spec_helper'
  3. require 'models/concerns/liquid_interpolatable'
  4. describe Agents::DataOutputAgent do
  5. it_behaves_like LiquidInterpolatable
  6. let(:agent) do
  7. _agent = Agents::DataOutputAgent.new(:name => 'My Data Output Agent')
  8. _agent.options = _agent.default_options.merge('secrets' => ['secret1', 'secret2'], 'events_to_show' => 2)
  9. _agent.user = users(:bob)
  10. _agent.sources << agents(:bob_website_agent)
  11. _agent.save!
  12. _agent
  13. end
  14. describe "#working?" do
  15. it "checks if events have been received within expected receive period" do
  16. agent.should_not be_working
  17. Agents::DataOutputAgent.async_receive agent.id, [events(:bob_website_agent_event).id]
  18. agent.reload.should be_working
  19. two_days_from_now = 2.days.from_now
  20. stub(Time).now { two_days_from_now }
  21. agent.reload.should_not be_working
  22. end
  23. end
  24. describe "validation" do
  25. before do
  26. agent.should be_valid
  27. end
  28. it "should validate presence and length of secrets" do
  29. agent.options[:secrets] = ""
  30. agent.should_not be_valid
  31. agent.options[:secrets] = "foo"
  32. agent.should_not be_valid
  33. agent.options[:secrets] = []
  34. agent.should_not be_valid
  35. agent.options[:secrets] = ["hello"]
  36. agent.should be_valid
  37. agent.options[:secrets] = ["hello", "world"]
  38. agent.should be_valid
  39. end
  40. it "should validate presence of expected_receive_period_in_days" do
  41. agent.options[:expected_receive_period_in_days] = ""
  42. agent.should_not be_valid
  43. agent.options[:expected_receive_period_in_days] = 0
  44. agent.should_not be_valid
  45. agent.options[:expected_receive_period_in_days] = -1
  46. agent.should_not be_valid
  47. end
  48. it "should validate presence of template and template.item" do
  49. agent.options[:template] = ""
  50. agent.should_not be_valid
  51. agent.options[:template] = {}
  52. agent.should_not be_valid
  53. agent.options[:template] = { 'item' => 'foo' }
  54. agent.should_not be_valid
  55. agent.options[:template] = { 'item' => { 'title' => 'hi' } }
  56. agent.should be_valid
  57. end
  58. end
  59. describe "#receive_web_request" do
  60. before do
  61. current_time = Time.now
  62. stub(Time).now { current_time }
  63. agents(:bob_website_agent).events.destroy_all
  64. end
  65. it "requires a valid secret" do
  66. content, status, content_type = agent.receive_web_request({ 'secret' => 'fake' }, 'get', 'text/xml')
  67. status.should == 401
  68. content.should == "Not Authorized"
  69. content, status, content_type = agent.receive_web_request({ 'secret' => 'fake' }, 'get', 'application/json')
  70. status.should == 401
  71. content.should == { :error => "Not Authorized" }
  72. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'application/json')
  73. status.should == 200
  74. end
  75. describe "returning events as RSS and JSON" do
  76. let!(:event1) do
  77. agents(:bob_website_agent).create_event :payload => {
  78. "url" => "http://imgs.xkcd.com/comics/evolving.png",
  79. "title" => "Evolving",
  80. "hovertext" => "Biologists play reverse Pokemon, trying to avoid putting any one team member on the front lines long enough for the experience to cause evolution."
  81. }
  82. end
  83. let!(:event2) do
  84. agents(:bob_website_agent).create_event :payload => {
  85. "url" => "http://imgs.xkcd.com/comics/evolving2.png",
  86. "title" => "Evolving again",
  87. "hovertext" => "Something else"
  88. }
  89. end
  90. it "can output RSS" do
  91. stub(agent).feed_link { "https://yoursite.com" }
  92. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
  93. status.should == 200
  94. content_type.should == 'text/xml'
  95. content.gsub(/\s+/, '').should == Utils.unindent(<<-XML).gsub(/\s+/, '')
  96. <?xml version="1.0" encoding="UTF-8" ?>
  97. <rss version="2.0">
  98. <channel>
  99. <title>XKCD comics as a feed</title>
  100. <description>This is a feed of recent XKCD comics, generated by Huginn</description>
  101. <link>https://yoursite.com</link>
  102. <lastBuildDate>#{Time.now.rfc2822}</lastBuildDate>
  103. <pubDate>#{Time.now.rfc2822}</pubDate>
  104. <ttl>60</ttl>
  105. <item>
  106. <title>Evolving again</title>
  107. <description>Secret hovertext: Something else</description>
  108. <link>http://imgs.xkcd.com/comics/evolving2.png</link>
  109. <guid>#{event2.id}</guid>
  110. <pubDate>#{event2.created_at.rfc2822}</pubDate>
  111. </item>
  112. <item>
  113. <title>Evolving</title>
  114. <description>Secret hovertext: Biologists play reverse Pokemon, trying to avoid putting any one team member on the front lines long enough for the experience to cause evolution.</description>
  115. <link>http://imgs.xkcd.com/comics/evolving.png</link>
  116. <guid>#{event1.id}</guid>
  117. <pubDate>#{event1.created_at.rfc2822}</pubDate>
  118. </item>
  119. </channel>
  120. </rss>
  121. XML
  122. end
  123. it "can output JSON" do
  124. agent.options['template']['item']['foo'] = "hi"
  125. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
  126. status.should == 200
  127. content.should == {
  128. 'title' => 'XKCD comics as a feed',
  129. 'description' => 'This is a feed of recent XKCD comics, generated by Huginn',
  130. 'pubDate' => Time.now,
  131. 'items' => [
  132. {
  133. 'title' => 'Evolving again',
  134. 'description' => 'Secret hovertext: Something else',
  135. 'link' => 'http://imgs.xkcd.com/comics/evolving2.png',
  136. 'guid' => event2.id,
  137. 'pubDate' => event2.created_at.rfc2822,
  138. 'foo' => 'hi'
  139. },
  140. {
  141. 'title' => 'Evolving',
  142. 'description' => 'Secret hovertext: Biologists play reverse Pokemon, trying to avoid putting any one team member on the front lines long enough for the experience to cause evolution.',
  143. 'link' => 'http://imgs.xkcd.com/comics/evolving.png',
  144. 'guid' => event1.id,
  145. 'pubDate' => event1.created_at.rfc2822,
  146. 'foo' => 'hi'
  147. }
  148. ]
  149. }
  150. end
  151. end
  152. end
  153. end