phantom_js_cloud_agent_spec.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. require 'rails_helper'
  2. describe Agents::PhantomJsCloudAgent do
  3. before do
  4. @valid_options = {
  5. 'name' => "XKCD",
  6. 'render_type' => "html",
  7. 'url' => "http://xkcd.com",
  8. 'mode' => 'clean',
  9. 'api_key' => '1234567890'
  10. }
  11. @checker = Agents::PhantomJsCloudAgent.new(:name => "xkcd", :options => @valid_options, :keep_events_for => 2.days)
  12. @checker.user = users(:jane)
  13. @checker.save!
  14. end
  15. describe "validations" do
  16. before do
  17. expect(@checker).to be_valid
  18. end
  19. it "should validate the presence of url" do
  20. @checker.options['url'] = "http://google.com"
  21. expect(@checker).to be_valid
  22. @checker.options['url'] = ""
  23. expect(@checker).not_to be_valid
  24. @checker.options['url'] = nil
  25. expect(@checker).not_to be_valid
  26. end
  27. end
  28. describe "emitting event" do
  29. it "should emit url as event" do
  30. expect {
  31. @checker.check
  32. }.to change { @checker.events.count }.by(1)
  33. item,* = @checker.events.last(1)
  34. expect(item.payload['url']).to eq("https://phantomjscloud.com/api/browser/v2/1234567890/?request=%7B%22url%22%3A%22http%3A%2F%2Fxkcd.com%22%2C%22renderType%22%3A%22html%22%2C%22requestSettings%22%3A%7B%22userAgent%22%3A%22Huginn%20-%20https%3A%2F%2Fgithub.com%2Fhuginn%2Fhuginn%22%7D%7D")
  35. end
  36. it "should set render type as plain text" do
  37. @checker.options['render_type'] = 'plainText'
  38. expect {
  39. @checker.check
  40. }.to change { @checker.events.count }.by(1)
  41. item,* = @checker.events.last(1)
  42. expect(item.payload['url']).to eq("https://phantomjscloud.com/api/browser/v2/1234567890/?request=%7B%22url%22%3A%22http%3A%2F%2Fxkcd.com%22%2C%22renderType%22%3A%22plainText%22%2C%22requestSettings%22%3A%7B%22userAgent%22%3A%22Huginn%20-%20https%3A%2F%2Fgithub.com%2Fhuginn%2Fhuginn%22%7D%7D")
  43. end
  44. it "should set render type as jpg" do
  45. @checker.options['render_type'] = 'jpg'
  46. expect {
  47. @checker.check
  48. }.to change { @checker.events.count }.by(1)
  49. item,* = @checker.events.last(1)
  50. expect(item.payload['url']).to eq("https://phantomjscloud.com/api/browser/v2/1234567890/?request=%7B%22url%22%3A%22http%3A%2F%2Fxkcd.com%22%2C%22renderType%22%3A%22jpg%22%2C%22requestSettings%22%3A%7B%22userAgent%22%3A%22Huginn%20-%20https%3A%2F%2Fgithub.com%2Fhuginn%2Fhuginn%22%7D%7D")
  51. end
  52. it "should set output as json" do
  53. @checker.options['output_as_json'] = true
  54. expect {
  55. @checker.check
  56. }.to change { @checker.events.count }.by(1)
  57. item,* = @checker.events.last(1)
  58. expect(item.payload['url']).to eq("https://phantomjscloud.com/api/browser/v2/1234567890/?request=%7B%22url%22%3A%22http%3A%2F%2Fxkcd.com%22%2C%22renderType%22%3A%22html%22%2C%22outputAsJson%22%3Atrue%2C%22requestSettings%22%3A%7B%22userAgent%22%3A%22Huginn%20-%20https%3A%2F%2Fgithub.com%2Fhuginn%2Fhuginn%22%7D%7D")
  59. end
  60. it "should not set ignore images" do
  61. @checker.options['ignore_images'] = false
  62. expect {
  63. @checker.check
  64. }.to change { @checker.events.count }.by(1)
  65. item,* = @checker.events.last(1)
  66. expect(item.payload['url']).to eq("https://phantomjscloud.com/api/browser/v2/1234567890/?request=%7B%22url%22%3A%22http%3A%2F%2Fxkcd.com%22%2C%22renderType%22%3A%22html%22%2C%22requestSettings%22%3A%7B%22userAgent%22%3A%22Huginn%20-%20https%3A%2F%2Fgithub.com%2Fhuginn%2Fhuginn%22%7D%7D")
  67. end
  68. it "should set ignore images" do
  69. @checker.options['ignore_images'] = true
  70. expect {
  71. @checker.check
  72. }.to change { @checker.events.count }.by(1)
  73. item,* = @checker.events.last(1)
  74. expect(item.payload['url']).to eq("https://phantomjscloud.com/api/browser/v2/1234567890/?request=%7B%22url%22%3A%22http%3A%2F%2Fxkcd.com%22%2C%22renderType%22%3A%22html%22%2C%22requestSettings%22%3A%7B%22ignoreImages%22%3Atrue%2C%22userAgent%22%3A%22Huginn%20-%20https%3A%2F%2Fgithub.com%2Fhuginn%2Fhuginn%22%7D%7D")
  75. end
  76. it "should set wait interval to zero" do
  77. @checker.options['wait_interval'] = '0'
  78. expect {
  79. @checker.check
  80. }.to change { @checker.events.count }.by(1)
  81. item,* = @checker.events.last(1)
  82. expect(item.payload['url']).to eq("https://phantomjscloud.com/api/browser/v2/1234567890/?request=%7B%22url%22%3A%22http%3A%2F%2Fxkcd.com%22%2C%22renderType%22%3A%22html%22%2C%22requestSettings%22%3A%7B%22userAgent%22%3A%22Huginn%20-%20https%3A%2F%2Fgithub.com%2Fhuginn%2Fhuginn%22%2C%22wait_interval%22%3A%220%22%7D%7D")
  83. end
  84. it "should set user agent to BlackBerry" do
  85. @checker.options['user_agent'] = 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+'
  86. expect {
  87. @checker.check
  88. }.to change { @checker.events.count }.by(1)
  89. item,* = @checker.events.last(1)
  90. expect(item.payload['url']).to eq("https://phantomjscloud.com/api/browser/v2/1234567890/?request=%7B%22url%22%3A%22http%3A%2F%2Fxkcd.com%22%2C%22renderType%22%3A%22html%22%2C%22requestSettings%22%3A%7B%22userAgent%22%3A%22Mozilla%2F5.0%20%28BlackBerry%3B%20U%3B%20BlackBerry%209900%3B%20en%29%20AppleWebKit%2F534.11%2B%20%28KHTML%2C%20like%20Gecko%29%20Version%2F7.1.0.346%20Mobile%20Safari%2F534.11%2B%22%7D%7D")
  91. end
  92. end
  93. end