pushover_agent_spec.rb 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. require 'spec_helper'
  2. describe Agents::PushoverAgent do
  3. before do
  4. @checker = Agents::PushoverAgent.new(:name => 'Some Name',
  5. :options => { :token => 'x',
  6. :user => 'x',
  7. :message => 'Some Message',
  8. :device => 'Some Device',
  9. :title => 'Some Message Title',
  10. :url => 'http://someurl.com',
  11. :url_title => 'Some Url Title',
  12. :priority => 0,
  13. :timestamp => 'false',
  14. :sound => 'pushover',
  15. :retry => 0,
  16. :expire => 0,
  17. :expected_receive_period_in_days => '1'})
  18. @checker.user = users(:bob)
  19. @checker.save!
  20. @event = Event.new
  21. @event.agent = agents(:bob_weather_agent)
  22. @event.payload = { :message => 'Looks like its going to rain' }
  23. @event.save!
  24. @sent_notifications = []
  25. stub.any_instance_of(Agents::PushoverAgent).send_notification { |notification| @sent_notifications << notification}
  26. end
  27. describe '#receive' do
  28. it 'should make sure multiple events are being received' do
  29. event1 = Event.new
  30. event1.agent = agents(:bob_rain_notifier_agent)
  31. event1.payload = { :message => 'Some message' }
  32. event1.save!
  33. event2 = Event.new
  34. event2.agent = agents(:bob_weather_agent)
  35. event2.payload = { :message => 'Some other message' }
  36. event2.save!
  37. @checker.receive([@event,event1,event2])
  38. expect(@sent_notifications[0]['message']).to eq('Looks like its going to rain')
  39. expect(@sent_notifications[1]['message']).to eq('Some message')
  40. expect(@sent_notifications[2]['message']).to eq('Some other message')
  41. end
  42. it 'should make sure event message overrides default message' do
  43. event = Event.new
  44. event.agent = agents(:bob_rain_notifier_agent)
  45. event.payload = { :message => 'Some new message'}
  46. event.save!
  47. @checker.receive([event])
  48. expect(@sent_notifications[0]['message']).to eq('Some new message')
  49. end
  50. it 'should make sure event text overrides default message' do
  51. event = Event.new
  52. event.agent = agents(:bob_rain_notifier_agent)
  53. event.payload = { :text => 'Some new text'}
  54. event.save!
  55. @checker.receive([event])
  56. expect(@sent_notifications[0]['message']).to eq('Some new text')
  57. end
  58. it 'should make sure event title overrides default title' do
  59. event = Event.new
  60. event.agent = agents(:bob_rain_notifier_agent)
  61. event.payload = { :message => 'Some message', :title => 'Some new title' }
  62. event.save!
  63. @checker.receive([event])
  64. expect(@sent_notifications[0]['title']).to eq('Some new title')
  65. end
  66. it 'should make sure event url overrides default url' do
  67. event = Event.new
  68. event.agent = agents(:bob_rain_notifier_agent)
  69. event.payload = { :message => 'Some message', :url => 'Some new url' }
  70. event.save!
  71. @checker.receive([event])
  72. expect(@sent_notifications[0]['url']).to eq('Some new url')
  73. end
  74. it 'should make sure event url_title overrides default url_title' do
  75. event = Event.new
  76. event.agent = agents(:bob_rain_notifier_agent)
  77. event.payload = { :message => 'Some message', :url_title => 'Some new url_title' }
  78. event.save!
  79. @checker.receive([event])
  80. expect(@sent_notifications[0]['url_title']).to eq('Some new url_title')
  81. end
  82. it 'should make sure event priority overrides default priority' do
  83. event = Event.new
  84. event.agent = agents(:bob_rain_notifier_agent)
  85. event.payload = { :message => 'Some message', :priority => 1 }
  86. event.save!
  87. @checker.receive([event])
  88. expect(@sent_notifications[0]['priority']).to eq(1)
  89. end
  90. it 'should make sure event timestamp overrides default timestamp' do
  91. event = Event.new
  92. event.agent = agents(:bob_rain_notifier_agent)
  93. event.payload = { :message => 'Some message', :timestamp => 'false' }
  94. event.save!
  95. @checker.receive([event])
  96. expect(@sent_notifications[0]['timestamp']).to eq('false')
  97. end
  98. it 'should make sure event sound overrides default sound' do
  99. event = Event.new
  100. event.agent = agents(:bob_rain_notifier_agent)
  101. event.payload = { :message => 'Some message', :sound => 'Some new sound' }
  102. event.save!
  103. @checker.receive([event])
  104. expect(@sent_notifications[0]['sound']).to eq('Some new sound')
  105. end
  106. it 'should make sure event retry overrides default retry' do
  107. event = Event.new
  108. event.agent = agents(:bob_rain_notifier_agent)
  109. event.payload = { :message => 'Some message', :retry => 1 }
  110. event.save!
  111. @checker.receive([event])
  112. expect(@sent_notifications[0]['retry']).to eq(1)
  113. end
  114. it 'should make sure event expire overrides default expire' do
  115. event = Event.new
  116. event.agent = agents(:bob_rain_notifier_agent)
  117. event.payload = { :message => 'Some message', :expire => 60 }
  118. event.save!
  119. @checker.receive([event])
  120. expect(@sent_notifications[0]['expire']).to eq(60)
  121. end
  122. end
  123. describe '#working?' do
  124. it 'checks if events have been received within the expected receive period' do
  125. # No events received
  126. expect(@checker).not_to be_working
  127. Agents::PushoverAgent.async_receive @checker.id, [@event.id]
  128. # Just received events
  129. expect(@checker.reload).to be_working
  130. two_days_from_now = 2.days.from_now
  131. stub(Time).now { two_days_from_now }
  132. # More time has passed than the expected receive period without any new events
  133. expect(@checker.reload).not_to be_working
  134. end
  135. end
  136. describe "validation" do
  137. before do
  138. expect(@checker).to be_valid
  139. end
  140. it "should validate presence of token" do
  141. @checker.options[:token] = ""
  142. expect(@checker).not_to be_valid
  143. end
  144. it "should validate presence of user" do
  145. @checker.options[:user] = ""
  146. expect(@checker).not_to be_valid
  147. end
  148. it "should validate presence of expected_receive_period_in_days" do
  149. @checker.options[:expected_receive_period_in_days] = ""
  150. expect(@checker).not_to be_valid
  151. end
  152. it "should make sure device is optional" do
  153. @checker.options[:device] = ""
  154. expect(@checker).to be_valid
  155. end
  156. it "should make sure title is optional" do
  157. @checker.options[:title] = ""
  158. expect(@checker).to be_valid
  159. end
  160. it "should make sure url is optional" do
  161. @checker.options[:url] = ""
  162. expect(@checker).to be_valid
  163. end
  164. it "should make sure url_title is optional" do
  165. @checker.options[:url_title] = ""
  166. expect(@checker).to be_valid
  167. end
  168. it "should make sure priority is optional" do
  169. @checker.options[:priority] = ""
  170. expect(@checker).to be_valid
  171. end
  172. it "should make sure timestamp is optional" do
  173. @checker.options[:timestamp] = ""
  174. expect(@checker).to be_valid
  175. end
  176. it "should make sure sound is optional" do
  177. @checker.options[:sound] = ""
  178. expect(@checker).to be_valid
  179. end
  180. end
  181. end