data_output_agent_spec.rb 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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] = "foo/bar"
  33. expect(agent).not_to be_valid
  34. agent.options[:secrets] = "foo.xml"
  35. expect(agent).not_to be_valid
  36. agent.options[:secrets] = false
  37. expect(agent).not_to be_valid
  38. agent.options[:secrets] = []
  39. expect(agent).not_to be_valid
  40. agent.options[:secrets] = ["foo.xml"]
  41. expect(agent).not_to be_valid
  42. agent.options[:secrets] = ["hello", true]
  43. expect(agent).not_to be_valid
  44. agent.options[:secrets] = ["hello"]
  45. expect(agent).to be_valid
  46. agent.options[:secrets] = ["hello", "world"]
  47. expect(agent).to be_valid
  48. end
  49. it "should validate presence of expected_receive_period_in_days" do
  50. agent.options[:expected_receive_period_in_days] = ""
  51. expect(agent).not_to be_valid
  52. agent.options[:expected_receive_period_in_days] = 0
  53. expect(agent).not_to be_valid
  54. agent.options[:expected_receive_period_in_days] = -1
  55. expect(agent).not_to be_valid
  56. end
  57. it "should validate presence of template and template.item" do
  58. agent.options[:template] = ""
  59. expect(agent).not_to be_valid
  60. agent.options[:template] = {}
  61. expect(agent).not_to be_valid
  62. agent.options[:template] = { 'item' => 'foo' }
  63. expect(agent).not_to be_valid
  64. agent.options[:template] = { 'item' => { 'title' => 'hi' } }
  65. expect(agent).to be_valid
  66. end
  67. end
  68. describe "#receive_web_request" do
  69. before do
  70. current_time = Time.now
  71. stub(Time).now { current_time }
  72. agents(:bob_website_agent).events.destroy_all
  73. end
  74. it "requires a valid secret" do
  75. content, status, content_type = agent.receive_web_request({ 'secret' => 'fake' }, 'get', 'text/xml')
  76. expect(status).to eq(401)
  77. expect(content).to eq("Not Authorized")
  78. content, status, content_type = agent.receive_web_request({ 'secret' => 'fake' }, 'get', 'application/json')
  79. expect(status).to eq(401)
  80. expect(content).to eq({ :error => "Not Authorized" })
  81. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'application/json')
  82. expect(status).to eq(200)
  83. end
  84. describe "outputting events as RSS and JSON" do
  85. let!(:event1) do
  86. agents(:bob_website_agent).create_event :payload => {
  87. "site_title" => "XKCD",
  88. "url" => "http://imgs.xkcd.com/comics/evolving.png",
  89. "title" => "Evolving",
  90. "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."
  91. }
  92. end
  93. let!(:event2) do
  94. agents(:bob_website_agent).create_event :payload => {
  95. "site_title" => "XKCD",
  96. "url" => "http://imgs.xkcd.com/comics/evolving2.png",
  97. "title" => "Evolving again",
  98. "date" => '',
  99. "hovertext" => "Something else"
  100. }
  101. end
  102. let!(:event3) do
  103. agents(:bob_website_agent).create_event :payload => {
  104. "site_title" => "XKCD",
  105. "url" => "http://imgs.xkcd.com/comics/evolving0.png",
  106. "title" => "Evolving yet again with a past date",
  107. "date" => '2014/05/05',
  108. "hovertext" => "Something else"
  109. }
  110. end
  111. it "can output RSS" do
  112. stub(agent).feed_link { "https://yoursite.com" }
  113. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
  114. expect(status).to eq(200)
  115. expect(content_type).to eq('text/xml')
  116. expect(content.gsub(/\s+/, '')).to eq Utils.unindent(<<-XML).gsub(/\s+/, '')
  117. <?xml version="1.0" encoding="UTF-8" ?>
  118. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  119. <channel>
  120. <atom:link href="https://yoursite.com/users/#{agent.user.id}/web_requests/#{agent.id}/secret1.xml" rel="self" type="application/rss+xml"/>
  121. <atom:icon>https://yoursite.com/favicon.ico</atom:icon>
  122. <title>XKCD comics as a feed</title>
  123. <description>This is a feed of recent XKCD comics, generated by Huginn</description>
  124. <link>https://yoursite.com</link>
  125. <lastBuildDate>#{Time.now.rfc2822}</lastBuildDate>
  126. <pubDate>#{Time.now.rfc2822}</pubDate>
  127. <ttl>60</ttl>
  128. <item>
  129. <title>Evolving yet again with a past date</title>
  130. <description>Secret hovertext: Something else</description>
  131. <link>http://imgs.xkcd.com/comics/evolving0.png</link>
  132. <pubDate>#{Time.zone.parse(event3.payload['date']).rfc2822}</pubDate>
  133. <guid isPermaLink="false">#{event3.id}</guid>
  134. </item>
  135. <item>
  136. <title>Evolving again</title>
  137. <description>Secret hovertext: Something else</description>
  138. <link>http://imgs.xkcd.com/comics/evolving2.png</link>
  139. <pubDate>#{event2.created_at.rfc2822}</pubDate>
  140. <guid isPermaLink="false">#{event2.id}</guid>
  141. </item>
  142. <item>
  143. <title>Evolving</title>
  144. <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>
  145. <link>http://imgs.xkcd.com/comics/evolving.png</link>
  146. <pubDate>#{event1.created_at.rfc2822}</pubDate>
  147. <guid isPermaLink="false">#{event1.id}</guid>
  148. </item>
  149. </channel>
  150. </rss>
  151. XML
  152. end
  153. it "can output JSON" do
  154. agent.options['template']['item']['foo'] = "hi"
  155. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
  156. expect(status).to eq(200)
  157. expect(content).to eq({
  158. 'title' => 'XKCD comics as a feed',
  159. 'description' => 'This is a feed of recent XKCD comics, generated by Huginn',
  160. 'pubDate' => Time.now,
  161. 'items' => [
  162. {
  163. 'title' => 'Evolving yet again with a past date',
  164. 'description' => 'Secret hovertext: Something else',
  165. 'link' => 'http://imgs.xkcd.com/comics/evolving0.png',
  166. 'guid' => {"contents" => event3.id, "isPermaLink" => "false"},
  167. 'pubDate' => Time.zone.parse(event3.payload['date']).rfc2822,
  168. 'foo' => 'hi'
  169. },
  170. {
  171. 'title' => 'Evolving again',
  172. 'description' => 'Secret hovertext: Something else',
  173. 'link' => 'http://imgs.xkcd.com/comics/evolving2.png',
  174. 'guid' => {"contents" => event2.id, "isPermaLink" => "false"},
  175. 'pubDate' => event2.created_at.rfc2822,
  176. 'foo' => 'hi'
  177. },
  178. {
  179. 'title' => 'Evolving',
  180. '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.',
  181. 'link' => 'http://imgs.xkcd.com/comics/evolving.png',
  182. 'guid' => {"contents" => event1.id, "isPermaLink" => "false"},
  183. 'pubDate' => event1.created_at.rfc2822,
  184. 'foo' => 'hi'
  185. }
  186. ]
  187. })
  188. end
  189. describe "interpolating \"events\"" do
  190. before do
  191. agent.options['template']['title'] = "XKCD comics as a feed{% if events.first.site_title %} ({{events.first.site_title}}){% endif %}"
  192. agent.save!
  193. end
  194. it "can output RSS" do
  195. stub(agent).feed_link { "https://yoursite.com" }
  196. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
  197. expect(status).to eq(200)
  198. expect(content_type).to eq('text/xml')
  199. expect(Nokogiri(content).at('/rss/channel/title/text()').text).to eq('XKCD comics as a feed (XKCD)')
  200. end
  201. it "can output JSON" do
  202. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
  203. expect(status).to eq(200)
  204. expect(content['title']).to eq('XKCD comics as a feed (XKCD)')
  205. end
  206. end
  207. describe "with a specified icon" do
  208. before do
  209. agent.options['template']['icon'] = 'https://somesite.com/icon.png'
  210. agent.save!
  211. end
  212. it "can output RSS" do
  213. stub(agent).feed_link { "https://yoursite.com" }
  214. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
  215. expect(status).to eq(200)
  216. expect(content_type).to eq('text/xml')
  217. expect(Nokogiri(content).at('/rss/channel/atom:icon/text()').text).to eq('https://somesite.com/icon.png')
  218. end
  219. end
  220. end
  221. describe "outputting nesting" do
  222. before do
  223. agent.options['template']['item']['enclosure'] = {
  224. "_attributes" => {
  225. "type" => "audio/mpeg",
  226. "url" => "{{media_url}}"
  227. }
  228. }
  229. agent.options['template']['item']['foo'] = {
  230. "_attributes" => {
  231. "attr" => "attr-value-{{foo}}"
  232. },
  233. "_contents" => "Foo: {{foo}}"
  234. }
  235. agent.options['template']['item']['nested'] = {
  236. "_attributes" => {
  237. "key" => "value"
  238. },
  239. "_contents" => {
  240. "title" => "some title"
  241. }
  242. }
  243. agent.options['template']['item']['simpleNested'] = {
  244. "title" => "some title",
  245. "complex" => {
  246. "_attributes" => {
  247. "key" => "value"
  248. },
  249. "_contents" => {
  250. "first" => {
  251. "_attributes" => {
  252. "a" => "b"
  253. },
  254. "_contents" => {
  255. "second" => "value"
  256. }
  257. }
  258. }
  259. }
  260. }
  261. agent.save!
  262. end
  263. let!(:event) do
  264. agents(:bob_website_agent).create_event :payload => {
  265. "url" => "http://imgs.xkcd.com/comics/evolving.png",
  266. "title" => "Evolving",
  267. "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.",
  268. "media_url" => "http://google.com/audio.mpeg",
  269. "foo" => 1
  270. }
  271. end
  272. it "can output JSON" do
  273. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
  274. expect(status).to eq(200)
  275. expect(content['items'].first).to eq(
  276. {
  277. 'title' => 'Evolving',
  278. '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.',
  279. 'link' => 'http://imgs.xkcd.com/comics/evolving.png',
  280. 'guid' => {"contents" => event.id, "isPermaLink" => "false"},
  281. 'pubDate' => event.created_at.rfc2822,
  282. 'enclosure' => {
  283. "type" => "audio/mpeg",
  284. "url" => "http://google.com/audio.mpeg"
  285. },
  286. 'foo' => {
  287. 'attr' => 'attr-value-1',
  288. 'contents' => 'Foo: 1'
  289. },
  290. 'nested' => {
  291. "key" => "value",
  292. "title" => "some title"
  293. },
  294. 'simpleNested' => {
  295. "title" => "some title",
  296. "complex" => {
  297. "key"=>"value",
  298. "first" => {
  299. "a" => "b",
  300. "second"=>"value"
  301. }
  302. }
  303. }
  304. }
  305. )
  306. end
  307. it "can output RSS" do
  308. stub(agent).feed_link { "https://yoursite.com" }
  309. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
  310. expect(status).to eq(200)
  311. expect(content_type).to eq('text/xml')
  312. expect(content.gsub(/\s+/, '')).to eq Utils.unindent(<<-XML).gsub(/\s+/, '')
  313. <?xml version="1.0" encoding="UTF-8" ?>
  314. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  315. <channel>
  316. <atom:link href="https://yoursite.com/users/#{agent.user.id}/web_requests/#{agent.id}/secret1.xml" rel="self" type="application/rss+xml"/>
  317. <atom:icon>https://yoursite.com/favicon.ico</atom:icon>
  318. <title>XKCD comics as a feed</title>
  319. <description>This is a feed of recent XKCD comics, generated by Huginn</description>
  320. <link>https://yoursite.com</link>
  321. <lastBuildDate>#{Time.now.rfc2822}</lastBuildDate>
  322. <pubDate>#{Time.now.rfc2822}</pubDate>
  323. <ttl>60</ttl>
  324. <item>
  325. <title>Evolving</title>
  326. <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>
  327. <link>http://imgs.xkcd.com/comics/evolving.png</link>
  328. <pubDate>#{event.created_at.rfc2822}</pubDate>
  329. <enclosure type="audio/mpeg" url="http://google.com/audio.mpeg" />
  330. <foo attr="attr-value-1">Foo: 1</foo>
  331. <nested key="value"><title>some title</title></nested>
  332. <simpleNested>
  333. <title>some title</title>
  334. <complex key="value">
  335. <first a="b">
  336. <second>value</second>
  337. </first>
  338. </complex>
  339. </simpleNested>
  340. <guid isPermaLink="false">#{event.id}</guid>
  341. </item>
  342. </channel>
  343. </rss>
  344. XML
  345. end
  346. end
  347. end
  348. end