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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# coding: utf-8
# == Schema Information
#
# Table name: info_request_events
#
# id :integer not null, primary key
# info_request_id :integer not null
# event_type :text not null
# params_yaml :text not null
# created_at :datetime not null
# described_state :string(255)
# calculated_state :string(255)
# last_described_at :datetime
# incoming_message_id :integer
# outgoing_message_id :integer
# comment_id :integer
#
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe InfoRequestEvent do
describe "when storing serialized parameters" do
it "should convert event parameters into YAML and back successfully" do
ire = InfoRequestEvent.new
example_params = { :foo => 'this is stuff', :bar => 83, :humbug => "yikes!!!" }
ire.params = example_params
ire.params_yaml.should == example_params.to_yaml
ire.params.should == example_params
end
end
describe 'when deciding if it is indexed by search' do
before do
@comment = mock_model(Comment)
@incoming_message = mock_model(IncomingMessage)
@outgoing_message = mock_model(OutgoingMessage)
@info_request = mock_model(InfoRequest, :indexed_by_search? => true)
end
it 'should return false for a comment that is not visible' do
@comment.stub!(:visible).and_return(false)
@info_request_event = InfoRequestEvent.new(:event_type => 'comment',
:comment => @comment,
:info_request => @info_request)
@info_request_event.indexed_by_search?.should be_false
end
it 'should return true for a comment that is visible' do
@comment.stub!(:visible).and_return(true)
@info_request_event = InfoRequestEvent.new(:event_type => 'comment',
:comment => @comment,
:info_request => @info_request)
@info_request_event.indexed_by_search?.should be_true
end
it 'should return false for an incoming message that is not indexed by search' do
@incoming_message.stub!(:indexed_by_search?).and_return false
@info_request_event = InfoRequestEvent.new(:event_type => 'response',
:incoming_message => @incoming_message,
:info_request => @info_request)
@info_request_event.indexed_by_search?.should be_false
end
it 'should return true for an incoming message that is indexed by search' do
@incoming_message.stub!(:indexed_by_search?).and_return true
@info_request_event = InfoRequestEvent.new(:event_type => 'response',
:incoming_message => @incoming_message,
:info_request => @info_request)
@info_request_event.indexed_by_search?.should be_true
end
it 'should return false for an outgoing message that is not indexed by search' do
@outgoing_message.stub!(:indexed_by_search?).and_return false
@info_request_event = InfoRequestEvent.new(:event_type => 'followup_sent',
:outgoing_message => @outgoing_message,
:info_request => @info_request)
@info_request_event.indexed_by_search?.should be_false
end
it 'should return true for an outgoing message that is indexed by search' do
@outgoing_message.stub!(:indexed_by_search?).and_return true
@info_request_event = InfoRequestEvent.new(:event_type => 'followup_sent',
:outgoing_message => @outgoing_message,
:info_request => @info_request)
@info_request_event.indexed_by_search?.should be_true
end
end
describe 'after saving' do
it 'should mark the model for reindexing in xapian if there is no no_xapian_reindex flag on the object' do
event = InfoRequestEvent.new(:info_request => mock_model(InfoRequest),
:event_type => 'sent',
:params => {})
event.should_receive(:xapian_mark_needs_index)
event.run_callbacks(:save)
end
end
describe "should know" do
it "that it's an incoming message" do
event = InfoRequestEvent.new()
event.stub!(:incoming_message_selective_columns).and_return(1)
event.is_incoming_message?.should be_true
event.is_outgoing_message?.should be_false
event.is_comment?.should be_false
end
it "that it's an outgoing message" do
event = InfoRequestEvent.new(:outgoing_message => mock_model(OutgoingMessage))
event.id = 1
event.is_incoming_message?.should be_false
event.is_outgoing_message?.should be_true
event.is_comment?.should be_false
end
it "that it's a comment" do
event = InfoRequestEvent.new(:comment => mock_model(Comment))
event.id = 1
event.is_incoming_message?.should be_false
event.is_outgoing_message?.should be_false
event.is_comment?.should be_true
end
end
describe "doing search/index stuff" do
before(:each) do
load_raw_emails_data
parse_all_incoming_messages
end
it 'should get search text for outgoing messages' do
event = info_request_events(:useless_outgoing_message_event)
message = outgoing_messages(:useless_outgoing_message).body
event.search_text_main.should == message + "\n\n"
end
it 'should get search text for incoming messages' do
event = info_request_events(:useless_incoming_message_event)
event.search_text_main.strip.should == "No way! I'm not going to tell you that in a month of Thursdays.\n\nThe Geraldine Quango"
end
it 'should get clipped text for incoming messages, and cache it too' do
event = info_request_events(:useless_incoming_message_event)
event.incoming_message_selective_columns("cached_main_body_text_folded").cached_main_body_text_folded = nil
event.search_text_main(true).strip.should == "No way! I'm not going to tell you that in a month of Thursdays.\n\nThe Geraldine Quango"
event.incoming_message_selective_columns("cached_main_body_text_folded").cached_main_body_text_folded.should_not == nil
end
end
describe 'when asked if it has the same email as a previous send' do
before do
@info_request_event = InfoRequestEvent.new
end
it 'should return true if the email in its params and the previous email the request was sent to are both nil' do
@info_request_event.stub!(:params).and_return({})
@info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return(nil)
@info_request_event.same_email_as_previous_send?.should be_true
end
it 'should return false if one email address exists and the other does not' do
@info_request_event.stub!(:params).and_return(:email => 'test@example.com')
@info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return(nil)
@info_request_event.same_email_as_previous_send?.should be_false
end
it 'should return true if the addresses are identical' do
@info_request_event.stub!(:params).and_return(:email => 'test@example.com')
@info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return('test@example.com')
@info_request_event.same_email_as_previous_send?.should be_true
end
it 'should return false if the addresses are different' do
@info_request_event.stub!(:params).and_return(:email => 'test@example.com')
@info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return('different@example.com')
@info_request_event.same_email_as_previous_send?.should be_false
end
it 'should return true if the addresses have different formats' do
@info_request_event.stub!(:params).and_return(:email => 'A Test <test@example.com>')
@info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return('test@example.com')
@info_request_event.same_email_as_previous_send?.should be_true
end
it 'should handle non-ascii characters in the name input' do
address = "\"Someone’s name\" <test@example.com>"
@info_request_event.stub!(:params).and_return(:email => address)
@info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return(address)
@info_request_event.same_email_as_previous_send?.should be_true
end
end
end
|