aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/outgoing_mailer_spec.rb
blob: 83da7a553884af0b84597e9f44822e0fd13f59c0 (plain)
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
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe OutgoingMailer, " when working out follow up addresses" do
    # This is done with fixtures as the code is a bit tangled with the way it
    # calls TMail.  XXX untangle it and make these tests spread out and using
    # mocks. Put parts of the tests in spec/lib/tmail_extensions.rb
    fixtures :info_requests, :incoming_messages, :raw_emails, :public_bodies, :public_body_translations

    before do
        # XXX this is a hack around the fact that our raw_email model
        # is in transition to something that doesn't actually live in
        # the database at all.  The raw_email fixture saves to the
        # model, the model then needs to be told to save itself on the
        # filesystem.
        raw_email = raw_emails(:useless_raw_email)
        raw_email.data=raw_email.dbdata
    end

    after do
        # And this is a hack around the fact that Rails fixtures don't
        # have teardowns happen on them; we need to ensure no emails
        # are left lying around
        raw_emails(:useless_raw_email).destroy_file_representation!
    end

    it "should parse them right" do
        ir = info_requests(:fancy_dog_request) 
        im = ir.incoming_messages[0]

        # check the basic entry in the fixture is fine
        OutgoingMailer.name_and_email_for_followup(ir, im).should == "FOI Person <foiperson@localhost>"
        OutgoingMailer.name_for_followup(ir, im).should == "FOI Person"
        OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost"
    end

    it "should work when there is only an email address" do
        ir = info_requests(:fancy_dog_request) 
        im = ir.incoming_messages[0]

        im.raw_email.data = im.raw_email.data.sub("\"FOI Person\" <foiperson@localhost>", "foiperson@localhost")

        # check the basic entry in the fixture is fine
        OutgoingMailer.name_and_email_for_followup(ir, im).should == "foiperson@localhost"
        OutgoingMailer.name_for_followup(ir, im).should == "Geraldine Quango"
        OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost"
    end

    it "should quote funny characters" do
        ir = info_requests(:fancy_dog_request) 
        im = ir.incoming_messages[0]

        im.raw_email.data = im.raw_email.data.sub("FOI Person", "FOI [ Person")

        # check the basic entry in the fixture is fine
        OutgoingMailer.name_and_email_for_followup(ir, im).should == "\"FOI [ Person\" <foiperson@localhost>"
        OutgoingMailer.name_for_followup(ir, im).should == "FOI [ Person"
        OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost"
    end

    it "should quote quotes" do
        ir = info_requests(:fancy_dog_request) 
        im = ir.incoming_messages[0]

        im.raw_email.data = im.raw_email.data.sub("FOI Person", "FOI \\\" Person")

        # check the basic entry in the fixture is fine
        OutgoingMailer.name_and_email_for_followup(ir, im).should == "\"FOI \\\" Person\" <foiperson@localhost>"
        OutgoingMailer.name_for_followup(ir, im).should == "FOI \" Person"
        OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost"
    end

    it "should quote @ signs" do
        ir = info_requests(:fancy_dog_request) 
        im = ir.incoming_messages[0]

        im.raw_email.data = im.raw_email.data.sub("FOI Person", "FOI @ Person")

        # check the basic entry in the fixture is fine
        OutgoingMailer.name_and_email_for_followup(ir, im).should == "\"FOI @ Person\" <foiperson@localhost>"
        OutgoingMailer.name_for_followup(ir, im).should == "FOI @ Person"
        OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost"
    end

end

describe OutgoingMailer, "when working out follow up subjects" do
    fixtures :info_requests, :incoming_messages, :outgoing_messages, :raw_emails

    before do
        raw_email = raw_emails(:useless_raw_email)
        raw_email.data=raw_email.dbdata
    end

    after do
        raw_emails(:useless_raw_email).destroy_file_representation!
    end

    it "should prefix the title with 'Freedom of Information request -' for initial requests" do
        ir = info_requests(:fancy_dog_request) 
        im = ir.incoming_messages[0]

        ir.email_subject_request.should == "Freedom of Information request - Why do you have & such a fancy dog?"
    end

    it "should use 'Re:' and inital request subject for followups which aren't replies to particular messages" do
        ir = info_requests(:fancy_dog_request) 
        om = outgoing_messages(:useless_outgoing_message)

        OutgoingMailer.subject_for_followup(ir, om).should == "Re: Freedom of Information request - Why do you have & such a fancy dog?"
    end

    it "should prefix with Re: the subject of the message being replied to" do
        ir = info_requests(:fancy_dog_request) 
        im = ir.incoming_messages[0]
        om = outgoing_messages(:useless_outgoing_message)
        om.incoming_message_followup = im

        OutgoingMailer.subject_for_followup(ir, om).should == "Re: Geraldine FOI Code AZXB421"
    end

    it "should not add Re: prefix if there already is such a prefix" do
        ir = info_requests(:fancy_dog_request) 
        im = ir.incoming_messages[0]
        om = outgoing_messages(:useless_outgoing_message)
        om.incoming_message_followup = im

        im.raw_email.data = im.raw_email.data.sub("Subject: Geraldine FOI Code AZXB421", "Subject: Re: Geraldine FOI Code AZXB421")
        OutgoingMailer.subject_for_followup(ir, om).should == "Re: Geraldine FOI Code AZXB421"
    end

    it "should not add Re: prefix if there already is a lower case re: prefix" do
        ir = info_requests(:fancy_dog_request) 
        im = ir.incoming_messages[0]
        puts im.raw_email.data
        om = outgoing_messages(:useless_outgoing_message)
        om.incoming_message_followup = im

        im.raw_email.data = im.raw_email.data.sub("Subject: Geraldine FOI Code AZXB421", "Subject: re: Geraldine FOI Code AZXB421")
        OutgoingMailer.subject_for_followup(ir, om).should == "re: Geraldine FOI Code AZXB421"
    end

    it "should use 'Re:' and initial request subject when replying to failed delivery notifications" do
        ir = info_requests(:fancy_dog_request) 
        im = ir.incoming_messages[0]
        om = outgoing_messages(:useless_outgoing_message)
        om.incoming_message_followup = im

        im.raw_email.data = im.raw_email.data.sub("foiperson@localhost", "postmaster@localhost")
        im.raw_email.data = im.raw_email.data.sub("Subject: Geraldine FOI Code AZXB421", "Subject: Delivery Failed")
        OutgoingMailer.subject_for_followup(ir, om).should == "Re: Freedom of Information request - Why do you have & such a fancy dog?"
    end
end