pushbullet_agent_spec.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. require 'spec_helper'
  2. describe Agents::PushbulletAgent do
  3. before(:each) do
  4. @valid_params = {
  5. 'api_key' => 'token',
  6. 'device_id' => '124',
  7. 'body' => '{{body}}',
  8. 'title' => 'hello from huginn'
  9. }
  10. @checker = Agents::PushbulletAgent.new(:name => "somename", :options => @valid_params)
  11. @checker.user = users(:jane)
  12. @checker.save!
  13. @event = Event.new
  14. @event.agent = agents(:bob_weather_agent)
  15. @event.payload = { :body => 'One two test' }
  16. @event.save!
  17. end
  18. describe "validating" do
  19. before do
  20. expect(@checker).to be_valid
  21. end
  22. it "should require the api_key" do
  23. @checker.options['api_key'] = nil
  24. expect(@checker).not_to be_valid
  25. end
  26. it "should require the device_id" do
  27. @checker.options['device_id'] = nil
  28. expect(@checker).not_to be_valid
  29. end
  30. end
  31. describe "helpers" do
  32. it "should return the query_options" do
  33. expect(@checker.send(:query_options, @event)).to eq({
  34. :body => {:title => 'hello from huginn', :body => 'One two test', :device_iden => @checker.options[:device_id], :type => 'note'},
  35. :basic_auth => {:username =>@checker.options[:api_key], :password=>''}
  36. })
  37. end
  38. end
  39. describe "#receive" do
  40. it "send a message to the hipchat" do
  41. stub_request(:post, "https://token:@api.pushbullet.com/api/pushes").
  42. with(:body => "device_iden=124&title=hello%20from%20huginn&body=One%20two%20test&type=note").
  43. to_return(:status => 200, :body => "ok", :headers => {})
  44. dont_allow(@checker).error
  45. @checker.receive([@event])
  46. end
  47. it "should log resquests which return an error" do
  48. stub_request(:post, "https://token:@api.pushbullet.com/api/pushes").
  49. with(:body => "device_iden=124&title=hello%20from%20huginn&body=One%20two%20test&type=note").
  50. to_return(:status => 200, :body => "error", :headers => {})
  51. mock(@checker).error("error")
  52. @checker.receive([@event])
  53. end
  54. end
  55. describe "#working?" do
  56. it "should not be working until the first event was received" do
  57. expect(@checker).not_to be_working
  58. @checker.last_receive_at = Time.now
  59. expect(@checker).to be_working
  60. end
  61. end
  62. end