boxcar_agent_spec.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. require 'rails_helper'
  2. describe Agents::BoxcarAgent do
  3. before(:each) do
  4. @valid_params = {
  5. 'user_credentials' => 'access_token',
  6. 'title' => 'Sample Title',
  7. 'body' => 'Sample Body'
  8. }
  9. @checker = Agents::BoxcarAgent.new(:name => "boxcartest", :options => @valid_params)
  10. @checker.user = users(:bob)
  11. @checker.save!
  12. @event = Event.new
  13. @event.agent = agents(:bob_weather_agent)
  14. @event.payload = { :body => 'Sample message' }
  15. @event.save!
  16. end
  17. describe 'validating' do
  18. before do
  19. expect(@checker).to be_valid
  20. end
  21. it "should require access token" do
  22. @checker.options['user_credentials'] = nil
  23. expect(@checker).not_to be_valid
  24. end
  25. end
  26. describe '#working?' do
  27. it "should not be working until the first event was received" do
  28. expect(@checker).not_to be_working
  29. @checker.last_receive_at = Time.now
  30. expect(@checker).to be_working
  31. end
  32. end
  33. describe "#receive" do
  34. it "sends a message" do
  35. stub(HTTParty).post { {"id" => 1, "message" => "blah", "title" => "blah","source_name" => "Custom Notification"} }
  36. @checker.receive([@event])
  37. end
  38. it "should raise error when invalid response arrives" do
  39. stub(HTTParty).post { {"blah" => "blah"} }
  40. expect { @checker.send_notification({}) }.to raise_error(StandardError, /Invalid response from Boxcar:/)
  41. end
  42. it "should raise error when response says unauthorized" do
  43. stub(HTTParty).post { {"Response" => "Not authorized"} }
  44. expect { @checker.send_notification({}) }.to raise_error(StandardError, /Not authorized/)
  45. end
  46. it "should raise error when response has an error" do
  47. stub(HTTParty).post { {"error" => {"message" => "Sample error"}} }
  48. expect { @checker.send_notification({}) }.to raise_error(StandardError, /Sample error/)
  49. end
  50. end
  51. end