OSDN Git Service

00da6d7c6ec0248cfb97d1a3f2a1cfc31c484d44
[elecoma/elecoma.git] / spec / models / mail_spec.rb
1 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')\r
2 \r
3 describe Mail do\r
4   fixtures :customers\r
5   before(:each) do\r
6     @mails = Mail.new\r
7   end\r
8 \r
9   it "should be valid" do\r
10     @mails.should be_valid\r
11   end\r
12 \r
13   describe "post_all_mail" do\r
14     before(:each) do\r
15       #Net::SMTP = DummySMTP\r
16       @customer = customers(:valid_signup2)\r
17       @dummysmtp = DummySMTP.new('localhost')\r
18     end\r
19 \r
20     it "should be successful" do\r
21       @dummysmtp.stub!(:send_message).and_return(nil)\r
22       Net::SMTP.stub!(:start).and_yield(@dummysmtp)\r
23       @mail = Mail.new(:to_address => @customer.email,\r
24                        :from_address => "sender@example.com",\r
25                        :message => "")\r
26       @mail.save\r
27       mails = Mail.find(:all, :conditions => "sent_at is null")\r
28       mails.should_not == []\r
29       Mail.post_all_mail\r
30       mails = Mail.find(:all, :conditions => "sent_at is null")\r
31       mails.should == []\r
32     end\r
33 \r
34     it "raise 5xx error" do\r
35       @customer.reachable.should be_true\r
36       @dummysmtp.stub!(:send_message).and_raise(Net::SMTPSyntaxError)\r
37       Net::SMTP.stub!(:start).and_yield(@dummysmtp)\r
38       @mail = Mail.new(:to_address => @customer.email,\r
39                        :from_address => "sender@example.com",\r
40                        :message => "")\r
41       @mail.save\r
42       Mail.post_all_mail\r
43       customer = Customer.find(@customer.id)\r
44       customer.reachable.should be_false\r
45     end\r
46 \r
47     it "raise 4xx error" do\r
48       mail_delivery_count = @customer.mail_delivery_count\r
49       mail_delivery_count = 1 if mail_delivery_count.nil?\r
50       @dummysmtp.stub!(:send_message).and_raise(Net::SMTPServerBusy)\r
51       Net::SMTP.stub!(:start).and_yield(@dummysmtp)\r
52       @mail = Mail.new(:to_address => @customer.email,\r
53                        :from_address => "sender@example.com",\r
54                        :message => "")\r
55       @mail.save\r
56       Mail.post_all_mail\r
57       customer = Customer.find(@customer.id)\r
58       customer.reachable.should be_true\r
59       customer.mail_delivery_count.should == mail_delivery_count + 1\r
60     end\r
61 \r
62     it "over mail_delivery count" do\r
63       @customer.mail_delivery_count = 5\r
64       @customer.save\r
65       @dummysmtp.stub!(:send_message).and_raise(Net::SMTPServerBusy)\r
66       Net::SMTP.stub!(:start).and_yield(@dummysmtp)\r
67       @mail = Mail.new(:to_address => @customer.email,\r
68                        :from_address => "sender@example.com",\r
69                        :message => "")\r
70       @mail.save\r
71       Mail.post_all_mail\r
72       customer = Customer.find(@customer.id)\r
73       customer.reachable.should be_false\r
74     end\r
75 \r
76   end\r
77 end\r
78 \r
79 class DummySMTP\r
80   def initialize(address, port = nil)\r
81   end\r
82 \r
83   def send_message(message, from, to)\r
84   end\r
85 end\r