aboutsummaryrefslogtreecommitdiffstats
path: root/spec/lib/mail_handler/backends/mail_backend_spec.rb
blob: eb1d4b1677006aa562c3262734c65ec22d920fcf (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
155
156
157
158
159
160
161
162
# coding: utf-8
require File.expand_path(File.dirname(__FILE__) + '../../../../spec_helper')

describe MailHandler::Backends::MailBackend do
    include MailHandler
    include MailHandler::Backends::MailBackend

    describe :backend do

        it 'should return the name of the backend' do
            backend.should == 'Mail'
        end

    end

    describe :mail_from_raw_email do

        it 'returns a new mail instance of the email' do
            raw_mail = load_file_fixture('raw_emails/1.email')
            expected = Mail.read_from_string(raw_mail)
            mail_from_raw_email(raw_mail).should == expected
        end

    end

    describe :get_part_file_name do

        it 'returns the part file name' do
            mail = get_fixture_mail('document-pdf.email')
            part = mail.attachments.first
            get_part_file_name(part).should == 'tiny-example.pdf'
        end

        it 'returns nil if there is no file name' do
            mail = get_fixture_mail('document-pdf.email')
            part = mail.parts.first
            get_part_file_name(part).should be_nil
        end

    end

    describe :get_part_body do

        it 'returns the body of a part' do
            expected = <<-DOC
Here's a PDF attachement which has a document/pdf content-type,
when it really should be application/pdf.\n
DOC
            mail = get_fixture_mail('document-pdf.email')
            part = mail.parts.first
            get_part_body(part).should == expected
        end

    end

    describe :first_from do

        it 'finds the first from field' do
            mail = get_fixture_mail('raw_emails/1.email')
            expected = Mail::Address.new('FOI Person <foiperson@localhost>').to_s
            first_from(mail).to_s.should == expected
        end

    end

    describe :get_from_address do

        it 'finds the first address' do
            mail = get_fixture_mail('raw_emails/1.email')
            get_from_address(mail).should == 'foiperson@localhost'
        end

    end

    describe :get_from_name do

        it 'finds the first from name' do
            mail = get_fixture_mail('raw_emails/1.email')
            get_from_name(mail).should == 'FOI Person'
        end

    end

    describe :get_all_addresses do

        it 'returns all addresses present in an email' do
            mail = get_fixture_mail('raw_emails/1.email')
            mail.cc = 'bob@example.com'
            mail['envelope-to'] = 'bob@example.net'
            expected = %w(bob@localhost bob@example.com bob@example.net)
            get_all_addresses(mail).should == expected
        end

    end

    describe :empty_return_path? do

        it 'is false if the return path is nil' do
            mail = Mail.new
            empty_return_path?(mail).should be_false
        end

        it 'is false if the return path has some data' do
            mail = Mail.new
            mail['return-path'] = 'xyz'
            empty_return_path?(mail).should be_false
        end

        it 'is true if the return path is blank' do
            mail = Mail.new
            mail['return-path'] = ''
            empty_return_path?(mail).should be_true
        end

    end

    describe :get_auto_submitted do

        it 'returns the auto-submitted attribute' do
            mail = Mail.new
            mail['auto-submitted'] = 'xyz'
            get_auto_submitted(mail).should == 'xyz'
        end

        it 'returns nil if there is no auto-submitted attribute' do
            mail = Mail.new
            get_auto_submitted(mail).should be_nil
        end

    end

    describe :expand_and_normalize_parts do

        context 'when given a multipart message' do

            it 'should return a Mail::PartsList' do
                mail = get_fixture_mail('incoming-request-oft-attachments.email')
                expand_and_normalize_parts(mail, mail).class.should == Mail::PartsList
            end

        end

    end

    describe :address_from_name_and_email do

        it 'returns an address string' do
            expected = 'Test User <test@example.com>'
            address_from_name_and_email('Test User', 'test@example.com').should == expected
        end

        it 'does not change the name passed to it' do
            original = "brønn"
            name = original.dup
            address_from_name_and_email(name, 'test@example.com')
            name.should == original
        end

    end


end