require 'test_helper' class MailEngine::MailScheduleTest < ActiveSupport::TestCase context "schedule" do setup do @user = FactoryGirl.create(:user) @schedule = FactoryGirl.create(:mail_schedule, :period => "once") end should "have logs" do @schedule.send_test_mail_to!("hlxwell@gmail.com", @user.id) assert_equal 1, @schedule.logs.count end should "be able to load all payload" do assert_equal @schedule.load_payload(@user), {"lastname" => "He", "firstname" => "Michael"} end should "check email when send_test_mail_to" do assert_raise RuntimeError do @schedule.send_test_mail_to!("hlxwell", @user.id) end end should "be able to send_test_mail_to" do @schedule.send_test_mail_to!("hlxwell@gmail.com", @user.id) assert_equal 1, MailEngine::MailLog.count end should "check sample user when send_test_mail_to" do assert_raise ActiveRecord::RecordNotFound do @schedule.send_test_mail_to!("hlxwell@gmail.com", 0) end end should "be able to get next_several_schedules" do assert_equal [], @schedule.next_several_schedules end end context "once sending only schedule" do setup do clear_database @schedule = FactoryGirl.create(:mail_schedule, :period => "once") end should "send mail only once" do assert @schedule.ready_to_send? @schedule.sendmail assert_equal 1, @schedule.sent_count # should not be able to send anyway assert !@schedule.ready_to_send? Timecop.travel(1.year.since) assert !@schedule.ready_to_send? end end context "daily schedule" do setup do clear_database @schedule = FactoryGirl.create(:mail_schedule, :period => "daily") end should "send mail every day at the same time to first_send_at" do assert @schedule.ready_to_send? @schedule.sendmail assert !@schedule.ready_to_send? Timecop.travel(1.day.since) assert @schedule.ready_to_send? @schedule.sendmail assert !@schedule.ready_to_send? end end end