Keine Beschreibung http://j1x-huginn.herokuapp.com

email_agent_spec.rb 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. require 'spec_helper'
  2. describe Agents::EmailAgent do
  3. def get_message_part(mail, content_type)
  4. mail.body.parts.find { |p| p.content_type.match content_type }.body.raw_source
  5. end
  6. before do
  7. @checker = Agents::EmailAgent.new(:name => "something", :options => { :expected_receive_period_in_days => 2, :subject => "something interesting" })
  8. @checker.user = users(:bob)
  9. @checker.save!
  10. end
  11. after do
  12. ActionMailer::Base.deliveries = []
  13. end
  14. describe "#receive" do
  15. it "immediately sends any payloads it receives" do
  16. ActionMailer::Base.deliveries.should == []
  17. event1 = Event.new
  18. event1.agent = agents(:bob_rain_notifier_agent)
  19. event1.payload = { :data => "Something you should know about" }
  20. event1.save!
  21. event2 = Event.new
  22. event2.agent = agents(:bob_weather_agent)
  23. event2.payload = { :data => "Something else you should know about" }
  24. event2.save!
  25. Agents::EmailAgent.async_receive(@checker.id, [event1.id])
  26. Agents::EmailAgent.async_receive(@checker.id, [event2.id])
  27. ActionMailer::Base.deliveries.count.should == 2
  28. ActionMailer::Base.deliveries.last.to.should == ["bob@example.com"]
  29. ActionMailer::Base.deliveries.last.subject.should == "something interesting"
  30. get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip.should == "Event\n data: Something else you should know about"
  31. get_message_part(ActionMailer::Base.deliveries.first, /plain/).strip.should == "Event\n data: Something you should know about"
  32. end
  33. it "can receive complex events and send them on" do
  34. stub_request(:any, /wunderground/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/weather.json")), :status => 200)
  35. stub.any_instance_of(Agents::WeatherAgent).is_tomorrow?(anything) { true }
  36. @checker.sources << agents(:bob_weather_agent)
  37. Agent.async_check(agents(:bob_weather_agent).id)
  38. Agent.receive!
  39. plain_email_text = get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip
  40. html_email_text = get_message_part(ActionMailer::Base.deliveries.last, /html/).strip
  41. plain_email_text.should =~ /avehumidity/
  42. html_email_text.should =~ /avehumidity/
  43. end
  44. end
  45. end