email_agent_spec.rb 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. end
  12. after do
  13. ActionMailer::Base.deliveries = []
  14. end
  15. describe "#receive" do
  16. it "immediately sends any payloads it receives" do
  17. ActionMailer::Base.deliveries.should == []
  18. event1 = Event.new
  19. event1.agent = agents(:bob_rain_notifier_agent)
  20. event1.payload = { :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. ActionMailer::Base.deliveries.count.should == 2
  29. ActionMailer::Base.deliveries.last.to.should == ["bob@example.com"]
  30. ActionMailer::Base.deliveries.last.subject.should == "something interesting"
  31. get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip.should == "Event\n data: Something else you should know about"
  32. get_message_part(ActionMailer::Base.deliveries.first, /plain/).strip.should == "Event\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. plain_email_text.should =~ /avehumidity/
  43. html_email_text.should =~ /avehumidity/
  44. end
  45. end
  46. end