-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
incoming_mail_spec.rb
157 lines (131 loc) · 5.28 KB
/
incoming_mail_spec.rb
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
require 'spec_helper'
require 'integration/alaveteli_dsl'
RSpec.describe 'when handling incoming mail' do
include ActiveJob::TestHelper
let(:info_request) { FactoryBot.create(:info_request) }
it "receives incoming messages, sends email to requester, and shows them" do
receive_incoming_mail('incoming-request-plain.email',
email_to: info_request.incoming_email)
deliveries = ActionMailer::Base.deliveries
expect(deliveries.size).to eq(1)
mail = deliveries[0]
expect(mail.to).to eq([info_request.user.email])
expect(mail.body).to match(/You have a new response to the Freedom of Information request/)
visit show_request_path info_request.url_title
expect(page).to have_content("No way!")
end
it "makes attachments available for download" do
receive_incoming_mail('incoming-request-two-same-name.email',
email_to: info_request.incoming_email)
attachment_1_path = get_attachment_path(
info_request.url_title,
incoming_message_id: info_request.incoming_messages.first.id,
part: 2,
file_name: 'hello world.txt',
skip_cache: 1
)
attachment_2_path = get_attachment_path(
info_request.url_title,
incoming_message_id: info_request.incoming_messages.first.id,
part: 3,
file_name: 'hello world.txt',
skip_cache: 1
)
visit attachment_1_path
visit attachment_2_path
perform_enqueued_jobs
visit attachment_1_path
expect(page.response_headers['Content-Type']).to eq(
"text/plain; charset=utf-8"
)
expect(page).to have_content "Second hello"
visit attachment_2_path
expect(page.response_headers['Content-Type']).to eq(
"text/plain; charset=utf-8"
)
expect(page).to have_content "First hello"
end
it "converts message body to UTF8" do
receive_incoming_mail('iso8859_2_raw_email.email',
email_to: info_request.incoming_email)
visit show_request_path(info_request.url_title)
expect(page).to have_content "tënde"
end
it "generates a valid HTML verson of plain text attachments" do
receive_incoming_mail('incoming-request-two-same-name.email',
email_to: info_request.incoming_email)
attachment_path = get_attachment_as_html_path(
info_request.url_title,
incoming_message_id: info_request.incoming_messages.first.id,
part: 2,
file_name: 'hello world.txt.html',
skip_cache: 1)
visit attachment_path
perform_enqueued_jobs
visit attachment_path
expect(page.response_headers['Content-Type']).to eq("text/html; charset=utf-8")
expect(page).to have_content "Second hello"
end
it "generates a valid HTML verson of PDF attachments" do
receive_incoming_mail('incoming-request-pdf-attachment.email',
email_to: info_request.incoming_email)
attachment_path = get_attachment_as_html_path(
info_request.url_title,
incoming_message_id: info_request.incoming_messages.first.id,
part: 2,
file_name: 'fs 50379341.pdf.html',
skip_cache: 1)
visit attachment_path
perform_enqueued_jobs
visit attachment_path
expect(page.response_headers['Content-Type']).to eq("text/html; charset=utf-8")
expect(page).to have_content "Walberswick Parish Council"
end
it "redirects back to incoming message when the attachment can't be found" do
receive_incoming_mail('incoming-request-two-same-name.email',
email_to: info_request.incoming_email)
# asking for an attachment by the wrong filename should result in
# redirecting back to the incoming message
visit get_attachment_as_html_path(
info_request.url_title,
incoming_message_id: info_request.incoming_messages.first.id,
part: 2,
file_name: 'hello world.txt.baz.html',
skip_cache: 1
)
expect(current_path).to eq(show_request_path(info_request.url_title))
end
it "treats attachments with unknown extensions as binary" do
receive_incoming_mail('incoming-request-attachment-unknown-extension.email',
email_to: info_request.incoming_email)
attachment_path = get_attachment_path(
info_request.url_title,
incoming_message_id: info_request.incoming_messages.first.id,
part: 2,
file_name: 'hello.qwglhm',
skip_cache: 1
)
visit attachment_path
perform_enqueued_jobs
visit attachment_path
expect(page.response_headers['Content-Type']).to eq("application/octet-stream; charset=utf-8")
expect(page).to have_content "an unusual sort of file"
end
it "does not automatically extract attachments after receiving email" do
receive_incoming_mail('incoming-request-plain.email',
email_to: info_request.incoming_email)
perform_enqueued_jobs
im = info_request.incoming_messages.first
expect(im.foi_attachments).to be_empty
end
it "extract attachments when inbound email contains an Excel spreadsheet" do
mail = Mail.new(to: info_request.incoming_email) do
body 'My excel spreadsheet'
add_file 'gems/excel_analyzer/spec/fixtures/data.xlsx'
end
receive_incoming_mail(mail.to_s)
perform_enqueued_jobs
im = info_request.incoming_messages.first
expect(im.foi_attachments).to_not be_empty
end
end