data_output_agent_spec.rb 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. # encoding: utf-8
  2. require 'rails_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" do
  69. it "should push to hubs when push_hubs is given" do
  70. agent.options[:push_hubs] = %w[http://push.example.com]
  71. agent.options[:template] = { 'link' => 'http://huginn.example.org' }
  72. alist = nil
  73. stub_request(:post, 'http://push.example.com/')
  74. .with(headers: { 'Content-Type' => %r{\Aapplication/x-www-form-urlencoded\s*(?:;|\z)} })
  75. .to_return { |request|
  76. alist = URI.decode_www_form(request.body).sort
  77. { status: 200, body: 'ok' }
  78. }
  79. agent.receive(events(:bob_website_agent_event))
  80. expect(alist).to eq [
  81. ["hub.mode", "publish"],
  82. ["hub.url", agent.feed_url(secret: agent.options[:secrets].first, format: :xml)]
  83. ]
  84. end
  85. end
  86. describe "#receive_web_request" do
  87. before do
  88. current_time = Time.now
  89. stub(Time).now { current_time }
  90. agents(:bob_website_agent).events.destroy_all
  91. end
  92. it "requires a valid secret" do
  93. content, status, content_type = agent.receive_web_request({ 'secret' => 'fake' }, 'get', 'text/xml')
  94. expect(status).to eq(401)
  95. expect(content).to eq("Not Authorized")
  96. content, status, content_type = agent.receive_web_request({ 'secret' => 'fake' }, 'get', 'application/json')
  97. expect(status).to eq(401)
  98. expect(content).to eq({ :error => "Not Authorized" })
  99. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'application/json')
  100. expect(status).to eq(200)
  101. end
  102. describe "outputting events as RSS and JSON" do
  103. let!(:event1) do
  104. agents(:bob_website_agent).create_event :payload => {
  105. "site_title" => "XKCD",
  106. "url" => "http://imgs.xkcd.com/comics/evolving.png",
  107. "title" => "Evolving",
  108. "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."
  109. }
  110. end
  111. let!(:event2) do
  112. agents(:bob_website_agent).create_event :payload => {
  113. "site_title" => "XKCD",
  114. "url" => "http://imgs.xkcd.com/comics/evolving2.png",
  115. "title" => "Evolving again",
  116. "date" => '',
  117. "hovertext" => "Something else"
  118. }
  119. end
  120. let!(:event3) do
  121. agents(:bob_website_agent).create_event :payload => {
  122. "site_title" => "XKCD",
  123. "url" => "http://imgs.xkcd.com/comics/evolving0.png",
  124. "title" => "Evolving yet again with a past date",
  125. "date" => '2014/05/05',
  126. "hovertext" => "Something else"
  127. }
  128. end
  129. it "can output RSS" do
  130. stub(agent).feed_link { "https://yoursite.com" }
  131. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
  132. expect(status).to eq(200)
  133. expect(content_type).to eq('text/xml')
  134. expect(content.gsub(/\s+/, '')).to eq Utils.unindent(<<-XML).gsub(/\s+/, '')
  135. <?xml version="1.0" encoding="UTF-8" ?>
  136. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
  137. <channel>
  138. <atom:link href="https://yoursite.com/users/#{agent.user.id}/web_requests/#{agent.id}/secret1.xml" rel="self" type="application/rss+xml"/>
  139. <atom:icon>https://yoursite.com/favicon.ico</atom:icon>
  140. <title>XKCD comics as a feed</title>
  141. <description>This is a feed of recent XKCD comics, generated by Huginn</description>
  142. <link>https://yoursite.com</link>
  143. <lastBuildDate>#{Time.now.rfc2822}</lastBuildDate>
  144. <pubDate>#{Time.now.rfc2822}</pubDate>
  145. <ttl>60</ttl>
  146. <item>
  147. <title>Evolving yet again with a past date</title>
  148. <description>Secret hovertext: Something else</description>
  149. <link>http://imgs.xkcd.com/comics/evolving0.png</link>
  150. <pubDate>#{Time.zone.parse(event3.payload['date']).rfc2822}</pubDate>
  151. <guid isPermaLink="false">#{event3.id}</guid>
  152. </item>
  153. <item>
  154. <title>Evolving again</title>
  155. <description>Secret hovertext: Something else</description>
  156. <link>http://imgs.xkcd.com/comics/evolving2.png</link>
  157. <pubDate>#{event2.created_at.rfc2822}</pubDate>
  158. <guid isPermaLink="false">#{event2.id}</guid>
  159. </item>
  160. <item>
  161. <title>Evolving</title>
  162. <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>
  163. <link>http://imgs.xkcd.com/comics/evolving.png</link>
  164. <pubDate>#{event1.created_at.rfc2822}</pubDate>
  165. <guid isPermaLink="false">#{event1.id}</guid>
  166. </item>
  167. </channel>
  168. </rss>
  169. XML
  170. end
  171. it "can output RSS with hub links when push_hubs is specified" do
  172. stub(agent).feed_link { "https://yoursite.com" }
  173. agent.options[:push_hubs] = %w[https://pubsubhubbub.superfeedr.com/ https://pubsubhubbub.appspot.com/]
  174. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
  175. expect(status).to eq(200)
  176. expect(content_type).to eq('text/xml')
  177. xml = Nokogiri::XML(content)
  178. expect(xml.xpath('/rss/channel/atom:link[@rel="hub"]/@href').map(&:text).sort).to eq agent.options[:push_hubs].sort
  179. end
  180. it "can output JSON" do
  181. agent.options['template']['item']['foo'] = "hi"
  182. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
  183. expect(status).to eq(200)
  184. expect(content).to eq({
  185. 'title' => 'XKCD comics as a feed',
  186. 'description' => 'This is a feed of recent XKCD comics, generated by Huginn',
  187. 'pubDate' => Time.now,
  188. 'items' => [
  189. {
  190. 'title' => 'Evolving yet again with a past date',
  191. 'description' => 'Secret hovertext: Something else',
  192. 'link' => 'http://imgs.xkcd.com/comics/evolving0.png',
  193. 'guid' => {"contents" => event3.id, "isPermaLink" => "false"},
  194. 'pubDate' => Time.zone.parse(event3.payload['date']).rfc2822,
  195. 'foo' => 'hi'
  196. },
  197. {
  198. 'title' => 'Evolving again',
  199. 'description' => 'Secret hovertext: Something else',
  200. 'link' => 'http://imgs.xkcd.com/comics/evolving2.png',
  201. 'guid' => {"contents" => event2.id, "isPermaLink" => "false"},
  202. 'pubDate' => event2.created_at.rfc2822,
  203. 'foo' => 'hi'
  204. },
  205. {
  206. 'title' => 'Evolving',
  207. '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.',
  208. 'link' => 'http://imgs.xkcd.com/comics/evolving.png',
  209. 'guid' => {"contents" => event1.id, "isPermaLink" => "false"},
  210. 'pubDate' => event1.created_at.rfc2822,
  211. 'foo' => 'hi'
  212. }
  213. ]
  214. })
  215. end
  216. describe 'ordering' do
  217. before do
  218. agent.options['events_order'] = ['{{title}}']
  219. end
  220. it 'can reorder the events_to_show last events based on a Liquid expression' do
  221. asc_content, _status, _content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
  222. expect(asc_content['items'].map {|i| i["title"] }).to eq(["Evolving", "Evolving again", "Evolving yet again with a past date"])
  223. agent.options['events_order'] = [['{{title}}', 'string', true]]
  224. desc_content, _status, _content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
  225. expect(desc_content['items']).to eq(asc_content['items'].reverse)
  226. end
  227. end
  228. describe "interpolating \"events\"" do
  229. before do
  230. agent.options['template']['title'] = "XKCD comics as a feed{% if events.first.site_title %} ({{events.first.site_title}}){% endif %}"
  231. agent.save!
  232. end
  233. it "can output RSS" do
  234. stub(agent).feed_link { "https://yoursite.com" }
  235. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
  236. expect(status).to eq(200)
  237. expect(content_type).to eq('text/xml')
  238. expect(Nokogiri(content).at('/rss/channel/title/text()').text).to eq('XKCD comics as a feed (XKCD)')
  239. end
  240. it "can output JSON" do
  241. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
  242. expect(status).to eq(200)
  243. expect(content['title']).to eq('XKCD comics as a feed (XKCD)')
  244. end
  245. end
  246. describe "with a specified icon" do
  247. before do
  248. agent.options['template']['icon'] = 'https://somesite.com/icon.png'
  249. agent.save!
  250. end
  251. it "can output RSS" do
  252. stub(agent).feed_link { "https://yoursite.com" }
  253. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
  254. expect(status).to eq(200)
  255. expect(content_type).to eq('text/xml')
  256. expect(Nokogiri(content).at('/rss/channel/atom:icon/text()').text).to eq('https://somesite.com/icon.png')
  257. end
  258. end
  259. describe "with media namespace not set" do
  260. before do
  261. agent.options['ns_media'] = nil
  262. agent.save!
  263. end
  264. it "can output RSS" do
  265. stub(agent).feed_link { "https://yoursite.com" }
  266. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
  267. expect(status).to eq(200)
  268. expect(content_type).to eq('text/xml')
  269. doc = Nokogiri(content)
  270. namespaces = doc.collect_namespaces
  271. expect(namespaces).not_to include("xmlns:media")
  272. end
  273. end
  274. describe "with media namespace set true" do
  275. before do
  276. agent.options['ns_media'] = 'true'
  277. agent.save!
  278. end
  279. it "can output RSS" do
  280. stub(agent).feed_link { "https://yoursite.com" }
  281. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
  282. expect(status).to eq(200)
  283. expect(content_type).to eq('text/xml')
  284. doc = Nokogiri(content)
  285. namespaces = doc.collect_namespaces
  286. expect(namespaces).to include(
  287. "xmlns:media" => 'http://search.yahoo.com/mrss/'
  288. )
  289. end
  290. end
  291. describe "with media namespace set false" do
  292. before do
  293. agent.options['ns_media'] = 'false'
  294. agent.save!
  295. end
  296. it "can output RSS" do
  297. stub(agent).feed_link { "https://yoursite.com" }
  298. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
  299. expect(status).to eq(200)
  300. expect(content_type).to eq('text/xml')
  301. doc = Nokogiri(content)
  302. namespaces = doc.collect_namespaces
  303. expect(namespaces).not_to include("xmlns:media")
  304. end
  305. end
  306. describe "with itunes namespace not set" do
  307. before do
  308. agent.options['ns_itunes'] = nil
  309. agent.save!
  310. end
  311. it "can output RSS" do
  312. stub(agent).feed_link { "https://yoursite.com" }
  313. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
  314. expect(status).to eq(200)
  315. expect(content_type).to eq('text/xml')
  316. doc = Nokogiri(content)
  317. namespaces = doc.collect_namespaces
  318. expect(namespaces).not_to include("xmlns:itunes")
  319. end
  320. end
  321. describe "with itunes namespace set true" do
  322. before do
  323. agent.options['ns_itunes'] = 'true'
  324. agent.save!
  325. end
  326. it "can output RSS" do
  327. stub(agent).feed_link { "https://yoursite.com" }
  328. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
  329. expect(status).to eq(200)
  330. expect(content_type).to eq('text/xml')
  331. doc = Nokogiri(content)
  332. namespaces = doc.collect_namespaces
  333. expect(namespaces).to include(
  334. "xmlns:itunes" => 'http://www.itunes.com/dtds/podcast-1.0.dtd'
  335. )
  336. end
  337. end
  338. describe "with itunes namespace set false" do
  339. before do
  340. agent.options['ns_itunes'] = 'false'
  341. agent.save!
  342. end
  343. it "can output RSS" do
  344. stub(agent).feed_link { "https://yoursite.com" }
  345. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
  346. expect(status).to eq(200)
  347. expect(content_type).to eq('text/xml')
  348. doc = Nokogiri(content)
  349. namespaces = doc.collect_namespaces
  350. expect(namespaces).not_to include("xmlns:itunes")
  351. end
  352. end
  353. end
  354. describe "outputting nesting" do
  355. before do
  356. agent.options['template']['item']['enclosure'] = {
  357. "_attributes" => {
  358. "type" => "audio/mpeg",
  359. "url" => "{{media_url}}"
  360. }
  361. }
  362. agent.options['template']['item']['foo'] = {
  363. "_attributes" => {
  364. "attr" => "attr-value-{{foo}}"
  365. },
  366. "_contents" => "Foo: {{foo}}"
  367. }
  368. agent.options['template']['item']['nested'] = {
  369. "_attributes" => {
  370. "key" => "value"
  371. },
  372. "_contents" => {
  373. "title" => "some title"
  374. }
  375. }
  376. agent.options['template']['item']['simpleNested'] = {
  377. "title" => "some title",
  378. "complex" => {
  379. "_attributes" => {
  380. "key" => "value"
  381. },
  382. "_contents" => {
  383. "first" => {
  384. "_attributes" => {
  385. "a" => "b"
  386. },
  387. "_contents" => {
  388. "second" => "value"
  389. }
  390. }
  391. }
  392. }
  393. }
  394. agent.save!
  395. end
  396. let!(:event) do
  397. agents(:bob_website_agent).create_event :payload => {
  398. "url" => "http://imgs.xkcd.com/comics/evolving.png",
  399. "title" => "Evolving",
  400. "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.",
  401. "media_url" => "http://google.com/audio.mpeg",
  402. "foo" => 1
  403. }
  404. end
  405. it "can output JSON" do
  406. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
  407. expect(status).to eq(200)
  408. expect(content['items'].first).to eq(
  409. {
  410. 'title' => 'Evolving',
  411. '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.',
  412. 'link' => 'http://imgs.xkcd.com/comics/evolving.png',
  413. 'guid' => {"contents" => event.id, "isPermaLink" => "false"},
  414. 'pubDate' => event.created_at.rfc2822,
  415. 'enclosure' => {
  416. "type" => "audio/mpeg",
  417. "url" => "http://google.com/audio.mpeg"
  418. },
  419. 'foo' => {
  420. 'attr' => 'attr-value-1',
  421. 'contents' => 'Foo: 1'
  422. },
  423. 'nested' => {
  424. "key" => "value",
  425. "title" => "some title"
  426. },
  427. 'simpleNested' => {
  428. "title" => "some title",
  429. "complex" => {
  430. "key"=>"value",
  431. "first" => {
  432. "a" => "b",
  433. "second"=>"value"
  434. }
  435. }
  436. }
  437. }
  438. )
  439. end
  440. it "can output RSS" do
  441. stub(agent).feed_link { "https://yoursite.com" }
  442. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
  443. expect(status).to eq(200)
  444. expect(content_type).to eq('text/xml')
  445. expect(content.gsub(/\s+/, '')).to eq Utils.unindent(<<-XML).gsub(/\s+/, '')
  446. <?xml version="1.0" encoding="UTF-8" ?>
  447. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" >
  448. <channel>
  449. <atom:link href="https://yoursite.com/users/#{agent.user.id}/web_requests/#{agent.id}/secret1.xml" rel="self" type="application/rss+xml"/>
  450. <atom:icon>https://yoursite.com/favicon.ico</atom:icon>
  451. <title>XKCD comics as a feed</title>
  452. <description>This is a feed of recent XKCD comics, generated by Huginn</description>
  453. <link>https://yoursite.com</link>
  454. <lastBuildDate>#{Time.now.rfc2822}</lastBuildDate>
  455. <pubDate>#{Time.now.rfc2822}</pubDate>
  456. <ttl>60</ttl>
  457. <item>
  458. <title>Evolving</title>
  459. <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>
  460. <link>http://imgs.xkcd.com/comics/evolving.png</link>
  461. <pubDate>#{event.created_at.rfc2822}</pubDate>
  462. <enclosure type="audio/mpeg" url="http://google.com/audio.mpeg" />
  463. <foo attr="attr-value-1">Foo: 1</foo>
  464. <nested key="value"><title>some title</title></nested>
  465. <simpleNested>
  466. <title>some title</title>
  467. <complex key="value">
  468. <first a="b">
  469. <second>value</second>
  470. </first>
  471. </complex>
  472. </simpleNested>
  473. <guid isPermaLink="false">#{event.id}</guid>
  474. </item>
  475. </channel>
  476. </rss>
  477. XML
  478. end
  479. end
  480. end
  481. end