1
0

pushbullet_agent_spec.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. require 'rails_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. 'url' => 'url',
  9. 'name' => 'name',
  10. 'address' => 'address',
  11. 'title' => 'hello from huginn',
  12. 'type' => 'note'
  13. }
  14. @checker = Agents::PushbulletAgent.new(:name => "somename", :options => @valid_params)
  15. @checker.user = users(:jane)
  16. @checker.save!
  17. @event = Event.new
  18. @event.agent = agents(:bob_weather_agent)
  19. @event.payload = { :body => 'One two test' }
  20. @event.save!
  21. end
  22. describe "validating" do
  23. before do
  24. expect(@checker).to be_valid
  25. end
  26. it "should require the api_key" do
  27. @checker.options['api_key'] = nil
  28. expect(@checker).not_to be_valid
  29. end
  30. it "should try do create a device_id" do
  31. @checker.options['device_id'] = nil
  32. expect(@checker).not_to be_valid
  33. end
  34. it "should require fields based on the type" do
  35. @checker.options['type'] = 'address'
  36. @checker.options['address'] = nil
  37. expect(@checker).not_to be_valid
  38. end
  39. end
  40. describe "helpers" do
  41. before(:each) do
  42. @base_options = {
  43. body: { device_iden: @checker.options[:device_id] },
  44. basic_auth: { username: @checker.options[:api_key], :password=>'' }
  45. }
  46. end
  47. context "#query_options" do
  48. it "should work for a note" do
  49. options = @base_options.deep_merge({
  50. body: {title: 'hello from huginn', body: 'One two test', type: 'note'}
  51. })
  52. expect(@checker.send(:query_options, @event)).to eq(options)
  53. end
  54. it "should work for a link" do
  55. @checker.options['type'] = 'link'
  56. options = @base_options.deep_merge({
  57. body: {title: 'hello from huginn', body: 'One two test', type: 'link', url: 'url'}
  58. })
  59. expect(@checker.send(:query_options, @event)).to eq(options)
  60. end
  61. it "should work for an address" do
  62. @checker.options['type'] = 'address'
  63. options = @base_options.deep_merge({
  64. body: {name: 'name', address: 'address', type: 'address'}
  65. })
  66. expect(@checker.send(:query_options, @event)).to eq(options)
  67. end
  68. end
  69. end
  70. describe '#validate_api_key' do
  71. it "should return true when working" do
  72. mock(@checker).devices
  73. expect(@checker.validate_api_key).to be_truthy
  74. end
  75. it "should return true when working" do
  76. mock(@checker).devices { raise Agents::PushbulletAgent::Unauthorized }
  77. expect(@checker.validate_api_key).to be_falsy
  78. end
  79. end
  80. describe '#complete_device_id' do
  81. it "should return an array" do
  82. mock(@checker).devices { [{'iden' => '12345', 'nickname' => 'huginn'}] }
  83. expect(@checker.complete_device_id).to eq([{:text=>"All Devices", :id=>"__ALL__"}, {:text=>"huginn", :id=>"12345"}])
  84. end
  85. end
  86. describe "#receive" do
  87. it "send a note" do
  88. stub_request(:post, "https://api.pushbullet.com/v2/pushes").
  89. with(basic_auth: [@checker.options[:api_key], ''],
  90. body: "device_iden=124&type=note&title=hello%20from%20huginn&body=One%20two%20test").
  91. to_return(status: 200, body: "{}", headers: {})
  92. dont_allow(@checker).error
  93. @checker.receive([@event])
  94. end
  95. it "should log resquests which return an error" do
  96. stub_request(:post, "https://api.pushbullet.com/v2/pushes").
  97. with(basic_auth: [@checker.options[:api_key], ''],
  98. body: "device_iden=124&type=note&title=hello%20from%20huginn&body=One%20two%20test").
  99. to_return(status: 200, body: '{"error": {"message": "error"}}', headers: {})
  100. mock(@checker).error("error")
  101. @checker.receive([@event])
  102. end
  103. end
  104. describe "#working?" do
  105. it "should not be working until the first event was received" do
  106. expect(@checker).not_to be_working
  107. @checker.last_receive_at = Time.now
  108. expect(@checker).to be_working
  109. end
  110. end
  111. describe '#devices' do
  112. it "should return an array of devices" do
  113. stub_request(:get, "https://api.pushbullet.com/v2/devices").
  114. with(basic_auth: [@checker.options[:api_key], '']).
  115. to_return(status: 200,
  116. body: '{"devices": [{"pushable": false}, {"nickname": "test", "iden": "iden", "pushable": true}]}',
  117. headers: {})
  118. expect(@checker.send(:devices)).to eq([{"nickname"=>"test", "iden"=>"iden", "pushable"=>true}])
  119. end
  120. it "should return an empty array on error" do
  121. stub(@checker).request { raise Agents::PushbulletAgent::Unauthorized }
  122. expect(@checker.send(:devices)).to eq([])
  123. end
  124. end
  125. describe '#create_device' do
  126. it "should create a new device and assign it to the options" do
  127. stub_request(:post, "https://api.pushbullet.com/v2/devices").
  128. with(basic_auth: [@checker.options[:api_key], ''],
  129. body: "nickname=Huginn&type=stream").
  130. to_return(status: 200,
  131. body: '{"iden": "udm0Tdjz5A7bL4NM"}',
  132. headers: {})
  133. @checker.options['device_id'] = nil
  134. @checker.send(:create_device)
  135. expect(@checker.options[:device_id]).to eq('udm0Tdjz5A7bL4NM')
  136. end
  137. end
  138. end