email_agent_spec.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. require 'rails_helper'
  2. describe Agents::EmailAgent do
  3. it_behaves_like EmailConcern
  4. def get_message_part(mail, content_type)
  5. mail.body.parts.find { |p| p.content_type.match content_type }.body.raw_source
  6. end
  7. before do
  8. @checker = Agents::EmailAgent.new(:name => "something", :options => { :expected_receive_period_in_days => "2", :subject => "something interesting" })
  9. @checker.user = users(:bob)
  10. @checker.save!
  11. expect(ActionMailer::Base.deliveries).to eq([])
  12. end
  13. after do
  14. ActionMailer::Base.deliveries = []
  15. end
  16. describe "#receive" do
  17. it "immediately sends any payloads it receives" do
  18. event1 = Event.new
  19. event1.agent = agents(:bob_rain_notifier_agent)
  20. event1.payload = { :message => "hi!", :data => "Something you should know about" }
  21. event1.save!
  22. event2 = Event.new
  23. event2.agent = agents(:bob_weather_agent)
  24. event2.payload = { :data => "Something else you should know about" }
  25. event2.save!
  26. Agents::EmailAgent.async_receive(@checker.id, [event1.id])
  27. Agents::EmailAgent.async_receive(@checker.id, [event2.id])
  28. expect(ActionMailer::Base.deliveries.count).to eq(2)
  29. expect(ActionMailer::Base.deliveries.last.to).to eq(["bob@example.com"])
  30. expect(ActionMailer::Base.deliveries.last.subject).to eq("something interesting")
  31. expect(get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip).to eq("Event\r\n data: Something else you should know about")
  32. expect(get_message_part(ActionMailer::Base.deliveries.first, /plain/).strip).to eq("hi!\r\n data: Something you should know about")
  33. end
  34. it "logs and re-raises any mailer errors" do
  35. event1 = Event.new
  36. event1.agent = agents(:bob_rain_notifier_agent)
  37. event1.payload = { :message => "hi!", :data => "Something you should know about" }
  38. event1.save!
  39. mock(SystemMailer).send_message(anything) { raise Net::SMTPAuthenticationError.new("Wrong password") }
  40. expect {
  41. Agents::EmailAgent.async_receive(@checker.id, [event1.id])
  42. }.to raise_error(/Wrong password/)
  43. expect(@checker.logs.last.message).to match(/Error sending mail .* Wrong password/)
  44. end
  45. it "can receive complex events and send them on" do
  46. stub_request(:any, /darksky/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/weather.json")), :status => 200)
  47. stub.any_instance_of(Agents::WeatherAgent).is_tomorrow?(anything) { true }
  48. @checker.sources << agents(:bob_weather_agent)
  49. Agent.async_check(agents(:bob_weather_agent).id)
  50. Agent.receive!
  51. plain_email_text = get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip
  52. html_email_text = get_message_part(ActionMailer::Base.deliveries.last, /html/).strip
  53. expect(plain_email_text).to match(/avehumidity/)
  54. expect(html_email_text).to match(/avehumidity/)
  55. end
  56. it "can take body option for selecting the resulting email's body" do
  57. @checker.update :options => @checker.options.merge({
  58. 'subject' => '{{foo.subject}}',
  59. 'body' => '{{some_html}}'
  60. })
  61. event = Event.new
  62. event.agent = agents(:bob_rain_notifier_agent)
  63. event.payload = { :foo => { :subject => "Something you should know about" }, :some_html => "<script>console.log('hello, world.')</script><strong>rain!</strong>" }
  64. event.save!
  65. Agents::EmailAgent.async_receive(@checker.id, [event.id])
  66. expect(ActionMailer::Base.deliveries.count).to eq(1)
  67. expect(ActionMailer::Base.deliveries.last.to).to eq(["bob@example.com"])
  68. expect(ActionMailer::Base.deliveries.last.subject).to eq("Something you should know about")
  69. expect(get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip).to match(/\A\s*#{Regexp.escape("<script>console.log('hello, world.')</script><strong>rain!</strong>")}\s*\z/)
  70. expect(get_message_part(ActionMailer::Base.deliveries.last, /html/).strip).to match(/<body>\s*#{Regexp.escape("console.log('hello, world.')<strong>rain!</strong>")}\s*<\/body>/)
  71. end
  72. it "can take content type option to set content type of email sent" do
  73. @checker.update :options => @checker.options.merge({
  74. 'content_type' => 'text/plain'
  75. })
  76. event2 = Event.new
  77. event2.agent = agents(:bob_rain_notifier_agent)
  78. event2.payload = { :foo => { :subject => "Something you should know about" }, :some_html => "<strong>rain!</strong>" }
  79. event2.save!
  80. Agents::EmailAgent.async_receive(@checker.id, [event2.id])
  81. expect(ActionMailer::Base.deliveries.last.content_type).to eq("text/plain; charset=UTF-8")
  82. end
  83. end
  84. end