Нет описания http://j1x-huginn.herokuapp.com

data_output_agent_spec.rb 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  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. end
  260. describe "outputting nesting" do
  261. before do
  262. agent.options['template']['item']['enclosure'] = {
  263. "_attributes" => {
  264. "type" => "audio/mpeg",
  265. "url" => "{{media_url}}"
  266. }
  267. }
  268. agent.options['template']['item']['foo'] = {
  269. "_attributes" => {
  270. "attr" => "attr-value-{{foo}}"
  271. },
  272. "_contents" => "Foo: {{foo}}"
  273. }
  274. agent.options['template']['item']['nested'] = {
  275. "_attributes" => {
  276. "key" => "value"
  277. },
  278. "_contents" => {
  279. "title" => "some title"
  280. }
  281. }
  282. agent.options['template']['item']['simpleNested'] = {
  283. "title" => "some title",
  284. "complex" => {
  285. "_attributes" => {
  286. "key" => "value"
  287. },
  288. "_contents" => {
  289. "first" => {
  290. "_attributes" => {
  291. "a" => "b"
  292. },
  293. "_contents" => {
  294. "second" => "value"
  295. }
  296. }
  297. }
  298. }
  299. }
  300. agent.save!
  301. end
  302. let!(:event) do
  303. agents(:bob_website_agent).create_event :payload => {
  304. "url" => "http://imgs.xkcd.com/comics/evolving.png",
  305. "title" => "Evolving",
  306. "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.",
  307. "media_url" => "http://google.com/audio.mpeg",
  308. "foo" => 1
  309. }
  310. end
  311. it "can output JSON" do
  312. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
  313. expect(status).to eq(200)
  314. expect(content['items'].first).to eq(
  315. {
  316. 'title' => 'Evolving',
  317. '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.',
  318. 'link' => 'http://imgs.xkcd.com/comics/evolving.png',
  319. 'guid' => {"contents" => event.id, "isPermaLink" => "false"},
  320. 'pubDate' => event.created_at.rfc2822,
  321. 'enclosure' => {
  322. "type" => "audio/mpeg",
  323. "url" => "http://google.com/audio.mpeg"
  324. },
  325. 'foo' => {
  326. 'attr' => 'attr-value-1',
  327. 'contents' => 'Foo: 1'
  328. },
  329. 'nested' => {
  330. "key" => "value",
  331. "title" => "some title"
  332. },
  333. 'simpleNested' => {
  334. "title" => "some title",
  335. "complex" => {
  336. "key"=>"value",
  337. "first" => {
  338. "a" => "b",
  339. "second"=>"value"
  340. }
  341. }
  342. }
  343. }
  344. )
  345. end
  346. it "can output RSS" do
  347. stub(agent).feed_link { "https://yoursite.com" }
  348. content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
  349. expect(status).to eq(200)
  350. expect(content_type).to eq('text/xml')
  351. expect(content.gsub(/\s+/, '')).to eq Utils.unindent(<<-XML).gsub(/\s+/, '')
  352. <?xml version="1.0" encoding="UTF-8" ?>
  353. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  354. <channel>
  355. <atom:link href="https://yoursite.com/users/#{agent.user.id}/web_requests/#{agent.id}/secret1.xml" rel="self" type="application/rss+xml"/>
  356. <atom:icon>https://yoursite.com/favicon.ico</atom:icon>
  357. <title>XKCD comics as a feed</title>
  358. <description>This is a feed of recent XKCD comics, generated by Huginn</description>
  359. <link>https://yoursite.com</link>
  360. <lastBuildDate>#{Time.now.rfc2822}</lastBuildDate>
  361. <pubDate>#{Time.now.rfc2822}</pubDate>
  362. <ttl>60</ttl>
  363. <item>
  364. <title>Evolving</title>
  365. <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>
  366. <link>http://imgs.xkcd.com/comics/evolving.png</link>
  367. <pubDate>#{event.created_at.rfc2822}</pubDate>
  368. <enclosure type="audio/mpeg" url="http://google.com/audio.mpeg" />
  369. <foo attr="attr-value-1">Foo: 1</foo>
  370. <nested key="value"><title>some title</title></nested>
  371. <simpleNested>
  372. <title>some title</title>
  373. <complex key="value">
  374. <first a="b">
  375. <second>value</second>
  376. </first>
  377. </complex>
  378. </simpleNested>
  379. <guid isPermaLink="false">#{event.id}</guid>
  380. </item>
  381. </channel>
  382. </rss>
  383. XML
  384. end
  385. end
  386. end
  387. end