Aucune description http://j1x-huginn.herokuapp.com

data_output_agent_spec.rb 6.2KB

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