pushover_agent_spec.rb 7.6 KB

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