1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe TrackMailer do
describe 'when sending email alerts for tracked things' do
before do
TrackMailer.stub!(:deliver_event_digest)
Time.stub!(:now).and_return(Time.utc(2007, 11, 12, 23, 59))
end
it 'should ask for all the users whose last daily track email was sent more than a day ago' do
expected_conditions = [ "last_daily_track_email < ?", Time.utc(2007, 11, 11, 23, 59)]
User.should_receive(:find).with(:all, :conditions => expected_conditions).and_return([])
TrackMailer.alert_tracks
end
describe 'for each user' do
before do
@user = mock_model(User, :no_xapian_reindex= => false,
:last_daily_track_email= => true,
:save! => true,
:url_name => 'test-name')
User.stub!(:find).and_return([@user])
@user.stub!(:no_xapian_reindex=)
end
it 'should ask for any daily track things for the user' do
expected_conditions = [ "tracking_user_id = ? and track_medium = ?", @user.id, 'email_daily' ]
TrackThing.should_receive(:find).with(:all, :conditions => expected_conditions).and_return([])
TrackMailer.alert_tracks
end
it 'should set the no_xapian_reindex flag on the user' do
@user.should_receive(:no_xapian_reindex=).with(true)
TrackMailer.alert_tracks
end
it 'should update the time of the user\'s last daily tracking email' do
@user.should_receive(:last_daily_track_email=).with(Time.now)
@user.should_receive(:save!)
TrackMailer.alert_tracks
end
describe 'for each tracked thing' do
before do
@track_things_sent_emails_array = []
@track_things_sent_emails_array.stub!(:find).and_return([]) # this is for the date range find (created in last 14 days)
@track_thing = mock_model(TrackThing, :track_query => 'test query',
:track_things_sent_emails => @track_things_sent_emails_array,
:created_at => Time.utc(2007, 11, 9, 23, 59))
TrackThing.stub!(:find).and_return([@track_thing])
@xapian_search = mock('xapian search', :results => [])
@found_event = mock_model(InfoRequestEvent, :described_at => @track_thing.created_at + 1.day)
@search_result = {:model => @found_event}
InfoRequest.stub!(:full_search).and_return(@xapian_search)
end
it 'should ask for the events returned by the tracking query' do
InfoRequest.should_receive(:full_search).with([InfoRequestEvent], 'test query', 'described_at', true, nil, 100, 1).and_return(@xapian_search)
TrackMailer.alert_tracks
end
it 'should not include in the email any events that the user has already been sent a tracking email about' do
sent_email = mock_model(TrackThingsSentEmail, :info_request_event_id => @found_event.id)
@track_things_sent_emails_array.stub!(:find).and_return([sent_email]) # this is for the date range find (created in last 14 days)
@xapian_search.stub!(:results).and_return([@search_result])
TrackMailer.should_not_receive(:deliver_event_digest)
TrackMailer.alert_tracks
end
it 'should not include in the email any events not sent in a previous tracking email that were described before the track was set up' do
@found_event.stub!(:described_at).and_return(@track_thing.created_at - 1.day)
@xapian_search.stub!(:results).and_return([@search_result])
TrackMailer.should_not_receive(:deliver_event_digest)
TrackMailer.alert_tracks
end
it 'should include in the email any events that the user has not been sent a tracking email on that have been described since the track was set up' do
@found_event.stub!(:described_at).and_return(@track_thing.created_at + 1.day)
@xapian_search.stub!(:results).and_return([@search_result])
TrackMailer.should_receive(:deliver_event_digest)
TrackMailer.alert_tracks
end
it 'should raise an error if a non-event class is returned by the tracking query' do
@xapian_search.stub!(:results).and_return([{:model => 'string class'}])
lambda{ TrackMailer.alert_tracks }.should raise_error('need to add other types to TrackMailer.alert_tracks (unalerted)')
end
it 'should record that a tracking email has been sent for each event that has been included in the email' do
@xapian_search.stub!(:results).and_return([@search_result])
sent_email = mock_model(TrackThingsSentEmail)
TrackThingsSentEmail.should_receive(:new).and_return(sent_email)
sent_email.should_receive(:track_thing_id=).with(@track_thing.id)
sent_email.should_receive(:info_request_event_id=).with(@found_event.id)
sent_email.should_receive(:save!)
TrackMailer.alert_tracks
end
end
end
end
describe 'delivering the email' do
it 'should deliver one email, with right headers' do
@user = mock_model(User,
:name_and_email => TMail::Address.address_from_name_and_email('Tippy Test', 'tippy@localhost'),
:url_name => 'tippy_test'
)
TrackMailer.deliver_event_digest(@user, []) # no items in it email for minimal test
deliveries = ActionMailer::Base.deliveries
if deliveries.size > 1 # debugging if there is an error
deliveries.each do |d|
STDERR.puts "------------------------------"
STDERR.puts d.body
STDERR.puts "------------------------------"
end
end
deliveries.size.should == 1
mail = deliveries[0]
mail['Auto-Submitted'].to_s.should == 'auto-generated'
mail['Precedence'].to_s.should == 'bulk'
deliveries.clear
end
end
end
|