data_output_agent_spec.rb 7.4 KB

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