website_agent_spec.rb 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. require 'spec_helper'
  2. describe Agents::WebsiteAgent do
  3. describe "checking without basic auth" do
  4. before do
  5. stub_request(:any, /xkcd/).to_return(body: File.read(Rails.root.join("spec/data_fixtures/xkcd.html")),
  6. status: 200,
  7. headers: {
  8. 'X-Status-Message' => 'OK'
  9. })
  10. @valid_options = {
  11. 'name' => "XKCD",
  12. 'expected_update_period_in_days' => "2",
  13. 'type' => "html",
  14. 'url' => "http://xkcd.com",
  15. 'mode' => 'on_change',
  16. 'extract' => {
  17. 'url' => { 'css' => "#comic img", 'value' => "@src" },
  18. 'title' => { 'css' => "#comic img", 'value' => "@alt" },
  19. 'hovertext' => { 'css' => "#comic img", 'value' => "@title" }
  20. }
  21. }
  22. @checker = Agents::WebsiteAgent.new(:name => "xkcd", :options => @valid_options, :keep_events_for => 2.days)
  23. @checker.user = users(:bob)
  24. @checker.save!
  25. end
  26. it_behaves_like WebRequestConcern
  27. describe "validations" do
  28. before do
  29. expect(@checker).to be_valid
  30. end
  31. it "should validate the integer fields" do
  32. @checker.options['expected_update_period_in_days'] = "2"
  33. expect(@checker).to be_valid
  34. @checker.options['expected_update_period_in_days'] = "nonsense"
  35. expect(@checker).not_to be_valid
  36. end
  37. it "should validate uniqueness_look_back" do
  38. @checker.options['uniqueness_look_back'] = "nonsense"
  39. expect(@checker).not_to be_valid
  40. @checker.options['uniqueness_look_back'] = "2"
  41. expect(@checker).to be_valid
  42. end
  43. it "should validate mode" do
  44. @checker.options['mode'] = "nonsense"
  45. expect(@checker).not_to be_valid
  46. @checker.options['mode'] = "on_change"
  47. expect(@checker).to be_valid
  48. @checker.options['mode'] = "all"
  49. expect(@checker).to be_valid
  50. @checker.options['mode'] = ""
  51. expect(@checker).to be_valid
  52. end
  53. it "should validate the force_encoding option" do
  54. @checker.options['force_encoding'] = ''
  55. expect(@checker).to be_valid
  56. @checker.options['force_encoding'] = 'UTF-8'
  57. expect(@checker).to be_valid
  58. @checker.options['force_encoding'] = ['UTF-8']
  59. expect(@checker).not_to be_valid
  60. @checker.options['force_encoding'] = 'UTF-42'
  61. expect(@checker).not_to be_valid
  62. end
  63. context "in 'json' type" do
  64. it "should ensure that all extractions have a 'path'" do
  65. @checker.options['type'] = 'json'
  66. @checker.options['extract'] = {
  67. 'url' => { 'foo' => 'bar' },
  68. }
  69. expect(@checker).to_not be_valid
  70. expect(@checker.errors_on(:base)).to include(/When type is json, all extractions must have a path attribute/)
  71. @checker.options['type'] = 'json'
  72. @checker.options['extract'] = {
  73. 'url' => { 'path' => 'bar' },
  74. }
  75. expect(@checker).to be_valid
  76. end
  77. end
  78. end
  79. describe "#check" do
  80. it "should check for changes (and update Event.expires_at)" do
  81. expect { @checker.check }.to change { Event.count }.by(1)
  82. event = Event.last
  83. sleep 2
  84. expect { @checker.check }.not_to change { Event.count }
  85. update_event = Event.last
  86. expect(update_event.expires_at).not_to eq(event.expires_at)
  87. end
  88. it "should always save events when in :all mode" do
  89. expect {
  90. @valid_options['mode'] = 'all'
  91. @checker.options = @valid_options
  92. @checker.check
  93. @checker.check
  94. }.to change { Event.count }.by(2)
  95. end
  96. it "should take uniqueness_look_back into account during deduplication" do
  97. @valid_options['mode'] = 'all'
  98. @checker.options = @valid_options
  99. @checker.check
  100. @checker.check
  101. event = Event.last
  102. event.payload = "{}"
  103. event.save
  104. expect {
  105. @valid_options['mode'] = 'on_change'
  106. @valid_options['uniqueness_look_back'] = 2
  107. @checker.options = @valid_options
  108. @checker.check
  109. }.not_to change { Event.count }
  110. expect {
  111. @valid_options['mode'] = 'on_change'
  112. @valid_options['uniqueness_look_back'] = 1
  113. @checker.options = @valid_options
  114. @checker.check
  115. }.to change { Event.count }.by(1)
  116. end
  117. it "should log an error if the number of results for a set of extraction patterns differs" do
  118. @valid_options['extract']['url']['css'] = "div"
  119. @checker.options = @valid_options
  120. @checker.check
  121. expect(@checker.logs.first.message).to match(/Got an uneven number of matches/)
  122. end
  123. it "should accept an array for url" do
  124. @valid_options['url'] = ["http://xkcd.com/1/", "http://xkcd.com/2/"]
  125. @checker.options = @valid_options
  126. expect { @checker.save! }.not_to raise_error;
  127. expect { @checker.check }.not_to raise_error;
  128. end
  129. it "should parse events from all urls in array" do
  130. expect {
  131. @valid_options['url'] = ["http://xkcd.com/", "http://xkcd.com/"]
  132. @valid_options['mode'] = 'all'
  133. @checker.options = @valid_options
  134. @checker.check
  135. }.to change { Event.count }.by(2)
  136. end
  137. it "should follow unique rules when parsing array of urls" do
  138. expect {
  139. @valid_options['url'] = ["http://xkcd.com/", "http://xkcd.com/"]
  140. @checker.options = @valid_options
  141. @checker.check
  142. }.to change { Event.count }.by(1)
  143. end
  144. end
  145. describe 'unzipping' do
  146. it 'should unzip automatically if the response has Content-Encoding: gzip' do
  147. json = {
  148. 'response' => {
  149. 'version' => 2,
  150. 'title' => "hello!"
  151. }
  152. }
  153. zipped = ActiveSupport::Gzip.compress(json.to_json)
  154. stub_request(:any, /gzip/).to_return(body: zipped, headers: { 'Content-Encoding' => 'gzip' }, status: 200)
  155. site = {
  156. 'name' => "Some JSON Response",
  157. 'expected_update_period_in_days' => "2",
  158. 'type' => "json",
  159. 'url' => "http://gzip.com",
  160. 'mode' => 'on_change',
  161. 'extract' => {
  162. 'version' => { 'path' => 'response.version' },
  163. },
  164. # no unzip option
  165. }
  166. checker = Agents::WebsiteAgent.new(:name => "Weather Site", :options => site)
  167. checker.user = users(:bob)
  168. checker.save!
  169. checker.check
  170. event = Event.last
  171. expect(event.payload['version']).to eq(2)
  172. end
  173. it 'should unzip with unzip option' do
  174. json = {
  175. 'response' => {
  176. 'version' => 2,
  177. 'title' => "hello!"
  178. }
  179. }
  180. zipped = ActiveSupport::Gzip.compress(json.to_json)
  181. stub_request(:any, /gzip/).to_return(body: zipped, status: 200)
  182. site = {
  183. 'name' => "Some JSON Response",
  184. 'expected_update_period_in_days' => "2",
  185. 'type' => "json",
  186. 'url' => "http://gzip.com",
  187. 'mode' => 'on_change',
  188. 'extract' => {
  189. 'version' => { 'path' => 'response.version' },
  190. },
  191. 'unzip' => 'gzip',
  192. }
  193. checker = Agents::WebsiteAgent.new(:name => "Weather Site", :options => site)
  194. checker.user = users(:bob)
  195. checker.save!
  196. checker.check
  197. event = Event.last
  198. expect(event.payload['version']).to eq(2)
  199. end
  200. end
  201. describe 'encoding' do
  202. it 'should be forced with force_encoding option' do
  203. huginn = "\u{601d}\u{8003}"
  204. stub_request(:any, /no-encoding/).to_return(:body => {
  205. :value => huginn,
  206. }.to_json.encode(Encoding::EUC_JP), :headers => {
  207. 'Content-Type' => 'application/json',
  208. }, :status => 200)
  209. site = {
  210. 'name' => "Some JSON Response",
  211. 'expected_update_period_in_days' => "2",
  212. 'type' => "json",
  213. 'url' => "http://no-encoding.example.com",
  214. 'mode' => 'on_change',
  215. 'extract' => {
  216. 'value' => { 'path' => 'value' },
  217. },
  218. 'force_encoding' => 'EUC-JP',
  219. }
  220. checker = Agents::WebsiteAgent.new(:name => "No Encoding Site", :options => site)
  221. checker.user = users(:bob)
  222. checker.save!
  223. checker.check
  224. event = Event.last
  225. expect(event.payload['value']).to eq(huginn)
  226. end
  227. it 'should be overridden with force_encoding option' do
  228. huginn = "\u{601d}\u{8003}"
  229. stub_request(:any, /wrong-encoding/).to_return(:body => {
  230. :value => huginn,
  231. }.to_json.encode(Encoding::EUC_JP), :headers => {
  232. 'Content-Type' => 'application/json; UTF-8',
  233. }, :status => 200)
  234. site = {
  235. 'name' => "Some JSON Response",
  236. 'expected_update_period_in_days' => "2",
  237. 'type' => "json",
  238. 'url' => "http://wrong-encoding.example.com",
  239. 'mode' => 'on_change',
  240. 'extract' => {
  241. 'value' => { 'path' => 'value' },
  242. },
  243. 'force_encoding' => 'EUC-JP',
  244. }
  245. checker = Agents::WebsiteAgent.new(:name => "Wrong Encoding Site", :options => site)
  246. checker.user = users(:bob)
  247. checker.save!
  248. checker.check
  249. event = Event.last
  250. expect(event.payload['value']).to eq(huginn)
  251. end
  252. end
  253. describe '#working?' do
  254. it 'checks if events have been received within the expected receive period' do
  255. stubbed_time = Time.now
  256. stub(Time).now { stubbed_time }
  257. expect(@checker).not_to be_working # No events created
  258. @checker.check
  259. expect(@checker.reload).to be_working # Just created events
  260. @checker.error "oh no!"
  261. expect(@checker.reload).not_to be_working # There is a recent error
  262. stubbed_time = 20.minutes.from_now
  263. @checker.events.delete_all
  264. @checker.check
  265. expect(@checker.reload).to be_working # There is a newer event now
  266. stubbed_time = 2.days.from_now
  267. expect(@checker.reload).not_to be_working # Two days have passed without a new event having been created
  268. end
  269. end
  270. describe "parsing" do
  271. it "parses CSS" do
  272. @checker.check
  273. event = Event.last
  274. expect(event.payload['url']).to eq("http://imgs.xkcd.com/comics/evolving.png")
  275. expect(event.payload['title']).to eq("Evolving")
  276. expect(event.payload['hovertext']).to match(/^Biologists play reverse/)
  277. end
  278. it "parses XPath" do
  279. @valid_options['extract'].each { |key, value|
  280. value.delete('css')
  281. value['xpath'] = "//*[@id='comic']//img"
  282. }
  283. @checker.options = @valid_options
  284. @checker.check
  285. event = Event.last
  286. expect(event.payload['url']).to eq("http://imgs.xkcd.com/comics/evolving.png")
  287. expect(event.payload['title']).to eq("Evolving")
  288. expect(event.payload['hovertext']).to match(/^Biologists play reverse/)
  289. end
  290. it "should turn relative urls to absolute" do
  291. rel_site = {
  292. 'name' => "XKCD",
  293. 'expected_update_period_in_days' => "2",
  294. 'type' => "html",
  295. 'url' => "http://xkcd.com",
  296. 'mode' => "on_change",
  297. 'extract' => {
  298. 'url' => {'css' => "#topLeft a", 'value' => "@href"},
  299. }
  300. }
  301. rel = Agents::WebsiteAgent.new(:name => "xkcd", :options => rel_site)
  302. rel.user = users(:bob)
  303. rel.save!
  304. rel.check
  305. event = Event.last
  306. expect(event.payload['url']).to eq("http://xkcd.com/about")
  307. end
  308. it "should return an integer value if XPath evaluates to one" do
  309. rel_site = {
  310. 'name' => "XKCD",
  311. 'expected_update_period_in_days' => 2,
  312. 'type' => "html",
  313. 'url' => "http://xkcd.com",
  314. 'mode' => "on_change",
  315. 'extract' => {
  316. 'num_links' => {'css' => "#comicLinks", 'value' => "count(./a)"}
  317. }
  318. }
  319. rel = Agents::WebsiteAgent.new(:name => "xkcd", :options => rel_site)
  320. rel.user = users(:bob)
  321. rel.save!
  322. rel.check
  323. event = Event.last
  324. expect(event.payload['num_links']).to eq("9")
  325. end
  326. it "should return all texts concatenated if XPath returns many text nodes" do
  327. rel_site = {
  328. 'name' => "XKCD",
  329. 'expected_update_period_in_days' => 2,
  330. 'type' => "html",
  331. 'url' => "http://xkcd.com",
  332. 'mode' => "on_change",
  333. 'extract' => {
  334. 'slogan' => {'css' => "#slogan", 'value' => ".//text()"}
  335. }
  336. }
  337. rel = Agents::WebsiteAgent.new(:name => "xkcd", :options => rel_site)
  338. rel.user = users(:bob)
  339. rel.save!
  340. rel.check
  341. event = Event.last
  342. expect(event.payload['slogan']).to eq("A webcomic of romance, sarcasm, math, and language.")
  343. end
  344. it "should interpolate _response_" do
  345. @valid_options['extract']['response_info'] =
  346. @valid_options['extract']['url'].merge(
  347. 'value' => '"{{ "The reponse was " | append:_response_.status | append:" " | append:_response_.headers.X-Status-Message | append:"." }}"'
  348. )
  349. @checker.options = @valid_options
  350. @checker.check
  351. event = Event.last
  352. expect(event.payload['response_info']).to eq('The reponse was 200 OK.')
  353. end
  354. describe "XML" do
  355. before do
  356. stub_request(:any, /github_rss/).to_return(
  357. body: File.read(Rails.root.join("spec/data_fixtures/github_rss.atom")),
  358. status: 200
  359. )
  360. @checker = Agents::WebsiteAgent.new(name: 'github', options: {
  361. 'name' => 'GitHub',
  362. 'expected_update_period_in_days' => '2',
  363. 'type' => 'xml',
  364. 'url' => 'http://example.com/github_rss.atom',
  365. 'mode' => 'on_change',
  366. 'extract' => {
  367. 'title' => { 'xpath' => '/feed/entry', 'value' => 'normalize-space(./title)' },
  368. 'url' => { 'xpath' => '/feed/entry', 'value' => './link[1]/@href' },
  369. 'thumbnail' => { 'xpath' => '/feed/entry', 'value' => './thumbnail/@url' },
  370. }
  371. }, keep_events_for: 2.days)
  372. @checker.user = users(:bob)
  373. @checker.save!
  374. end
  375. it "works with XPath" do
  376. expect {
  377. @checker.check
  378. }.to change { Event.count }.by(20)
  379. event = Event.last
  380. expect(event.payload['title']).to eq('Shift to dev group')
  381. expect(event.payload['url']).to eq('https://github.com/cantino/huginn/commit/d465158f77dcd9078697e6167b50abbfdfa8b1af')
  382. expect(event.payload['thumbnail']).to eq('https://avatars3.githubusercontent.com/u/365751?s=30')
  383. end
  384. it "works with XPath with namespaces unstripped" do
  385. @checker.options['use_namespaces'] = 'true'
  386. @checker.save!
  387. expect {
  388. @checker.check
  389. }.to change { Event.count }.by(0)
  390. @checker.options['extract'] = {
  391. 'title' => { 'xpath' => '/xmlns:feed/xmlns:entry', 'value' => 'normalize-space(./xmlns:title)' },
  392. 'url' => { 'xpath' => '/xmlns:feed/xmlns:entry', 'value' => './xmlns:link[1]/@href' },
  393. 'thumbnail' => { 'xpath' => '/xmlns:feed/xmlns:entry', 'value' => './media:thumbnail/@url' },
  394. }
  395. @checker.save!
  396. expect {
  397. @checker.check
  398. }.to change { Event.count }.by(20)
  399. event = Event.last
  400. expect(event.payload['title']).to eq('Shift to dev group')
  401. expect(event.payload['url']).to eq('https://github.com/cantino/huginn/commit/d465158f77dcd9078697e6167b50abbfdfa8b1af')
  402. expect(event.payload['thumbnail']).to eq('https://avatars3.githubusercontent.com/u/365751?s=30')
  403. end
  404. it "works with CSS selectors" do
  405. @checker.options['extract'] = {
  406. 'title' => { 'css' => 'feed > entry', 'value' => 'normalize-space(./title)' },
  407. 'url' => { 'css' => 'feed > entry', 'value' => './link[1]/@href' },
  408. 'thumbnail' => { 'css' => 'feed > entry', 'value' => './thumbnail/@url' },
  409. }
  410. @checker.save!
  411. expect {
  412. @checker.check
  413. }.to change { Event.count }.by(20)
  414. event = Event.last
  415. expect(event.payload['title']).to be_empty
  416. expect(event.payload['thumbnail']).to be_empty
  417. @checker.options['extract'] = {
  418. 'title' => { 'css' => 'feed > entry', 'value' => 'normalize-space(./xmlns:title)' },
  419. 'url' => { 'css' => 'feed > entry', 'value' => './xmlns:link[1]/@href' },
  420. 'thumbnail' => { 'css' => 'feed > entry', 'value' => './media:thumbnail/@url' },
  421. }
  422. @checker.save!
  423. expect {
  424. @checker.check
  425. }.to change { Event.count }.by(20)
  426. event = Event.last
  427. expect(event.payload['title']).to eq('Shift to dev group')
  428. expect(event.payload['url']).to eq('https://github.com/cantino/huginn/commit/d465158f77dcd9078697e6167b50abbfdfa8b1af')
  429. expect(event.payload['thumbnail']).to eq('https://avatars3.githubusercontent.com/u/365751?s=30')
  430. end
  431. it "works with CSS selectors with namespaces stripped" do
  432. @checker.options['extract'] = {
  433. 'title' => { 'css' => 'feed > entry', 'value' => 'normalize-space(./title)' },
  434. 'url' => { 'css' => 'feed > entry', 'value' => './link[1]/@href' },
  435. 'thumbnail' => { 'css' => 'feed > entry', 'value' => './thumbnail/@url' },
  436. }
  437. @checker.options['use_namespaces'] = 'false'
  438. @checker.save!
  439. expect {
  440. @checker.check
  441. }.to change { Event.count }.by(20)
  442. event = Event.last
  443. expect(event.payload['title']).to eq('Shift to dev group')
  444. expect(event.payload['url']).to eq('https://github.com/cantino/huginn/commit/d465158f77dcd9078697e6167b50abbfdfa8b1af')
  445. expect(event.payload['thumbnail']).to eq('https://avatars3.githubusercontent.com/u/365751?s=30')
  446. end
  447. end
  448. describe "JSON" do
  449. it "works with paths" do
  450. json = {
  451. 'response' => {
  452. 'version' => 2,
  453. 'title' => "hello!"
  454. }
  455. }
  456. stub_request(:any, /json-site/).to_return(:body => json.to_json, :status => 200)
  457. site = {
  458. 'name' => "Some JSON Response",
  459. 'expected_update_period_in_days' => "2",
  460. 'type' => "json",
  461. 'url' => "http://json-site.com",
  462. 'mode' => 'on_change',
  463. 'extract' => {
  464. 'version' => {'path' => "response.version"},
  465. 'title' => {'path' => "response.title"}
  466. }
  467. }
  468. checker = Agents::WebsiteAgent.new(:name => "Weather Site", :options => site)
  469. checker.user = users(:bob)
  470. checker.save!
  471. checker.check
  472. event = Event.last
  473. expect(event.payload['version']).to eq(2)
  474. expect(event.payload['title']).to eq("hello!")
  475. end
  476. it "can handle arrays" do
  477. json = {
  478. 'response' => {
  479. 'data' => [
  480. {'title' => "first", 'version' => 2},
  481. {'title' => "second", 'version' => 2.5}
  482. ]
  483. }
  484. }
  485. stub_request(:any, /json-site/).to_return(:body => json.to_json, :status => 200)
  486. site = {
  487. 'name' => "Some JSON Response",
  488. 'expected_update_period_in_days' => "2",
  489. 'type' => "json",
  490. 'url' => "http://json-site.com",
  491. 'mode' => 'on_change',
  492. 'extract' => {
  493. :title => {'path' => "response.data[*].title"},
  494. :version => {'path' => "response.data[*].version"}
  495. }
  496. }
  497. checker = Agents::WebsiteAgent.new(:name => "Weather Site", :options => site)
  498. checker.user = users(:bob)
  499. checker.save!
  500. expect {
  501. checker.check
  502. }.to change { Event.count }.by(2)
  503. (event2, event1) = Event.last(2)
  504. expect(event1.payload['version']).to eq(2.5)
  505. expect(event1.payload['title']).to eq("second")
  506. expect(event2.payload['version']).to eq(2)
  507. expect(event2.payload['title']).to eq("first")
  508. end
  509. it "stores the whole object if :extract is not specified" do
  510. json = {
  511. 'response' => {
  512. 'version' => 2,
  513. 'title' => "hello!"
  514. }
  515. }
  516. stub_request(:any, /json-site/).to_return(:body => json.to_json, :status => 200)
  517. site = {
  518. 'name' => "Some JSON Response",
  519. 'expected_update_period_in_days' => "2",
  520. 'type' => "json",
  521. 'url' => "http://json-site.com",
  522. 'mode' => 'on_change'
  523. }
  524. checker = Agents::WebsiteAgent.new(:name => "Weather Site", :options => site)
  525. checker.user = users(:bob)
  526. checker.save!
  527. checker.check
  528. event = Event.last
  529. expect(event.payload['response']['version']).to eq(2)
  530. expect(event.payload['response']['title']).to eq("hello!")
  531. end
  532. end
  533. describe "text parsing" do
  534. before do
  535. stub_request(:any, /text-site/).to_return(body: <<-EOF, status: 200)
  536. water: wet
  537. fire: hot
  538. EOF
  539. site = {
  540. 'name' => 'Some Text Response',
  541. 'expected_update_period_in_days' => '2',
  542. 'type' => 'text',
  543. 'url' => 'http://text-site.com',
  544. 'mode' => 'on_change',
  545. 'extract' => {
  546. 'word' => { 'regexp' => '^(.+?): (.+)$', index: 1 },
  547. 'property' => { 'regexp' => '^(.+?): (.+)$', index: '2' },
  548. }
  549. }
  550. @checker = Agents::WebsiteAgent.new(name: 'Text Site', options: site)
  551. @checker.user = users(:bob)
  552. @checker.save!
  553. end
  554. it "works with regexp with named capture" do
  555. @checker.options = @checker.options.merge('extract' => {
  556. 'word' => { 'regexp' => '^(?<word>.+?): (?<property>.+)$', index: 'word' },
  557. 'property' => { 'regexp' => '^(?<word>.+?): (?<property>.+)$', index: 'property' },
  558. })
  559. expect {
  560. @checker.check
  561. }.to change { Event.count }.by(2)
  562. event1, event2 = Event.last(2)
  563. expect(event1.payload['word']).to eq('water')
  564. expect(event1.payload['property']).to eq('wet')
  565. expect(event2.payload['word']).to eq('fire')
  566. expect(event2.payload['property']).to eq('hot')
  567. end
  568. it "works with regexp" do
  569. expect {
  570. @checker.check
  571. }.to change { Event.count }.by(2)
  572. event1, event2 = Event.last(2)
  573. expect(event1.payload['word']).to eq('water')
  574. expect(event1.payload['property']).to eq('wet')
  575. expect(event2.payload['word']).to eq('fire')
  576. expect(event2.payload['property']).to eq('hot')
  577. end
  578. end
  579. end
  580. describe "#receive" do
  581. before do
  582. @event = Event.new
  583. @event.agent = agents(:bob_rain_notifier_agent)
  584. @event.payload = {
  585. 'url' => 'http://xkcd.com',
  586. 'link' => 'Random',
  587. }
  588. end
  589. it "should scrape from the url element in incoming event payload" do
  590. expect {
  591. @checker.options = @valid_options
  592. @checker.receive([@event])
  593. }.to change { Event.count }.by(1)
  594. end
  595. it "should use url_from_event as url to scrape if it exists when receiving an event" do
  596. stub = stub_request(:any, 'http://example.org/?url=http%3A%2F%2Fxkcd.com')
  597. @checker.options = @valid_options.merge(
  598. 'url_from_event' => 'http://example.org/?url={{url | uri_escape}}'
  599. )
  600. @checker.receive([@event])
  601. expect(stub).to have_been_requested
  602. end
  603. it "should interpolate values from incoming event payload" do
  604. expect {
  605. @valid_options['extract'] = {
  606. 'from' => {
  607. 'xpath' => '*[1]',
  608. 'value' => '{{url | to_xpath}}'
  609. },
  610. 'to' => {
  611. 'xpath' => '(//a[@href and text()={{link | to_xpath}}])[1]',
  612. 'value' => '@href'
  613. },
  614. }
  615. @checker.options = @valid_options
  616. @checker.receive([@event])
  617. }.to change { Event.count }.by(1)
  618. expect(Event.last.payload).to eq({
  619. 'from' => 'http://xkcd.com',
  620. 'to' => 'http://dynamic.xkcd.com/random/comic/',
  621. })
  622. end
  623. it "should interpolate values from incoming event payload and _response_" do
  624. @event.payload['title'] = 'XKCD'
  625. expect {
  626. @valid_options['extract'] = {
  627. 'response_info' => @valid_options['extract']['url'].merge(
  628. 'value' => '{% capture sentence %}The reponse from {{title}} was {{_response_.status}} {{_response_.headers.X-Status-Message}}.{% endcapture %}{{sentence | to_xpath}}'
  629. )
  630. }
  631. @checker.options = @valid_options
  632. @checker.receive([@event])
  633. }.to change { Event.count }.by(1)
  634. expect(Event.last.payload['response_info']).to eq('The reponse from XKCD was 200 OK.')
  635. end
  636. it "should support merging of events" do
  637. expect {
  638. @checker.options = @valid_options
  639. @checker.options[:mode] = "merge"
  640. @checker.receive([@event])
  641. }.to change { Event.count }.by(1)
  642. last_payload = Event.last.payload
  643. expect(last_payload['link']).to eq('Random')
  644. end
  645. end
  646. end
  647. describe "checking with http basic auth" do
  648. before do
  649. stub_request(:any, /example/).
  650. with(headers: { 'Authorization' => "Basic #{['user:pass'].pack('m').chomp}" }).
  651. to_return(:body => File.read(Rails.root.join("spec/data_fixtures/xkcd.html")), :status => 200)
  652. @valid_options = {
  653. 'name' => "XKCD",
  654. 'expected_update_period_in_days' => "2",
  655. 'type' => "html",
  656. 'url' => "http://www.example.com",
  657. 'mode' => 'on_change',
  658. 'extract' => {
  659. 'url' => { 'css' => "#comic img", 'value' => "@src" },
  660. 'title' => { 'css' => "#comic img", 'value' => "@alt" },
  661. 'hovertext' => { 'css' => "#comic img", 'value' => "@title" }
  662. },
  663. 'basic_auth' => "user:pass"
  664. }
  665. @checker = Agents::WebsiteAgent.new(:name => "auth", :options => @valid_options)
  666. @checker.user = users(:bob)
  667. @checker.save!
  668. end
  669. describe "#check" do
  670. it "should check for changes" do
  671. expect { @checker.check }.to change { Event.count }.by(1)
  672. expect { @checker.check }.not_to change { Event.count }
  673. end
  674. end
  675. end
  676. describe "checking with headers" do
  677. before do
  678. stub_request(:any, /example/).
  679. with(headers: { 'foo' => 'bar' }).
  680. to_return(:body => File.read(Rails.root.join("spec/data_fixtures/xkcd.html")), :status => 200)
  681. @valid_options = {
  682. 'name' => "XKCD",
  683. 'expected_update_period_in_days' => "2",
  684. 'type' => "html",
  685. 'url' => "http://www.example.com",
  686. 'mode' => 'on_change',
  687. 'headers' => { 'foo' => 'bar' },
  688. 'extract' => {
  689. 'url' => { 'css' => "#comic img", 'value' => "@src" },
  690. }
  691. }
  692. @checker = Agents::WebsiteAgent.new(:name => "ua", :options => @valid_options)
  693. @checker.user = users(:bob)
  694. @checker.save!
  695. end
  696. describe "#check" do
  697. it "should check for changes" do
  698. expect { @checker.check }.to change { Event.count }.by(1)
  699. end
  700. end
  701. end
  702. end