1
0

email_digest_agent_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. require 'rails_helper'
  2. describe Agents::EmailDigestAgent 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::EmailDigestAgent.new(:name => "something", :options => {:expected_receive_period_in_days => "2", :subject => "something interesting"})
  9. @checker.user = users(:bob)
  10. @checker.save!
  11. @checker1 = Agents::EmailDigestAgent.new(:name => "something", :options => {:expected_receive_period_in_days => "2", :subject => "something interesting", :content_type => "text/plain"})
  12. @checker1.user = users(:bob)
  13. @checker1.save!
  14. end
  15. after do
  16. ActionMailer::Base.deliveries = []
  17. end
  18. describe "#receive" do
  19. it "queues any payloads it receives" do
  20. event1 = Event.new
  21. event1.agent = agents(:bob_rain_notifier_agent)
  22. event1.payload = {:data => "Something you should know about"}
  23. event1.save!
  24. event2 = Event.new
  25. event2.agent = agents(:bob_weather_agent)
  26. event2.payload = {:data => "Something else you should know about"}
  27. event2.save!
  28. Agents::EmailDigestAgent.async_receive(@checker.id, [event1.id, event2.id])
  29. expect(@checker.reload.memory['events']).to match([event1.id, event2.id])
  30. end
  31. end
  32. describe "#check" do
  33. it "should send an email" do
  34. Agents::EmailDigestAgent.async_check(@checker.id)
  35. expect(ActionMailer::Base.deliveries).to eq([])
  36. payloads = [
  37. {:data => "Something you should know about"},
  38. {:title => "Foo", :url => "http://google.com", :bar => 2},
  39. {"message" => "hi", :woah => "there"},
  40. {"test" => 2}
  41. ]
  42. events = payloads.map do |payload|
  43. Event.new.tap do |event|
  44. event.agent = agents(:bob_weather_agent)
  45. event.payload = payload
  46. event.save!
  47. end
  48. end
  49. Agents::DigestAgent.async_receive(@checker.id, events.map(&:id))
  50. @checker.sources << agents(:bob_weather_agent)
  51. Agents::DigestAgent.async_check(@checker.id)
  52. expect(ActionMailer::Base.deliveries.last.to).to eq(["bob@example.com"])
  53. expect(ActionMailer::Base.deliveries.last.subject).to eq("something interesting")
  54. expect(get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip).to eq("Event\r\n data: Something you should know about\r\n\r\nFoo\r\n bar: 2\r\n url: http://google.com\r\n\r\nhi\r\n woah: there\r\n\r\nEvent\r\n test: 2")
  55. expect(@checker.reload.memory[:events]).to be_empty
  56. end
  57. it "logs and re-raises mailer errors" do
  58. expect(SystemMailer).to receive(:send_message).with(anything) { raise Net::SMTPAuthenticationError.new("Wrong password") }
  59. @checker.memory[:events] = [1]
  60. @checker.save!
  61. expect {
  62. Agents::EmailDigestAgent.async_check(@checker.id)
  63. }.to raise_error(/Wrong password/)
  64. expect(@checker.reload.memory[:events]).not_to be_empty
  65. expect(@checker.logs.last.message).to match(/Error sending digest mail .* Wrong password/)
  66. end
  67. it "can receive complex events and send them on" do
  68. stub_request(:any, /pirateweather/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/weather.json")), :status => 200)
  69. @checker.sources << agents(:bob_weather_agent)
  70. Agent.async_check(agents(:bob_weather_agent).id)
  71. Agent.receive!
  72. expect(@checker.reload.memory[:events]).not_to be_empty
  73. Agents::EmailDigestAgent.async_check(@checker.id)
  74. plain_email_text = get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip
  75. html_email_text = get_message_part(ActionMailer::Base.deliveries.last, /html/).strip
  76. expect(plain_email_text).to match(/avehumidity/)
  77. expect(html_email_text).to match(/avehumidity/)
  78. expect(@checker.reload.memory[:events]).to be_empty
  79. end
  80. it "should send email with correct content type" do
  81. Agents::EmailDigestAgent.async_check(@checker1.id)
  82. expect(ActionMailer::Base.deliveries).to eq([])
  83. @checker1.memory[:events] = [1, 2, 3, 4]
  84. @checker1.save!
  85. Agents::EmailDigestAgent.async_check(@checker1.id)
  86. expect(ActionMailer::Base.deliveries.last.content_type).to eq("text/plain; charset=UTF-8")
  87. end
  88. end
  89. end