email_agent_spec.rb 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. require 'spec_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\n data: Something else you should know about")
  32. expect(get_message_part(ActionMailer::Base.deliveries.first, /plain/).strip).to eq("hi!\n data: Something you should know about")
  33. end
  34. it "can receive complex events and send them on" do
  35. stub_request(:any, /wunderground/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/weather.json")), :status => 200)
  36. stub.any_instance_of(Agents::WeatherAgent).is_tomorrow?(anything) { true }
  37. @checker.sources << agents(:bob_weather_agent)
  38. Agent.async_check(agents(:bob_weather_agent).id)
  39. Agent.receive!
  40. plain_email_text = get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip
  41. html_email_text = get_message_part(ActionMailer::Base.deliveries.last, /html/).strip
  42. expect(plain_email_text).to match(/avehumidity/)
  43. expect(html_email_text).to match(/avehumidity/)
  44. end
  45. it "can take body option for selecting the resulting email's body" do
  46. @checker.update_attributes :options => @checker.options.merge({
  47. 'subject' => '{{foo.subject}}',
  48. 'body' => '{{some_html}}'
  49. })
  50. event = Event.new
  51. event.agent = agents(:bob_rain_notifier_agent)
  52. event.payload = { :foo => { :subject => "Something you should know about" }, :some_html => "<strong>rain!</strong>" }
  53. event.save!
  54. Agents::EmailAgent.async_receive(@checker.id, [event.id])
  55. expect(ActionMailer::Base.deliveries.count).to eq(1)
  56. expect(ActionMailer::Base.deliveries.last.to).to eq(["bob@example.com"])
  57. expect(ActionMailer::Base.deliveries.last.subject).to eq("Something you should know about")
  58. expect(get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip).to match(/\A\s*<strong>rain\!<\/strong>\s*\z/)
  59. expect(get_message_part(ActionMailer::Base.deliveries.last, /html/).strip).to match(/<body>\s*<strong>rain\!<\/strong>\s*<\/body>/)
  60. end
  61. end
  62. end