kenhirakawa-astrotrain 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/.gitignore +26 -0
  2. data/LICENSE +20 -0
  3. data/README +47 -0
  4. data/Rakefile +124 -0
  5. data/VERSION +1 -0
  6. data/astrotrain.gemspec +141 -0
  7. data/config/sample.rb +12 -0
  8. data/lib/astrotrain.rb +56 -0
  9. data/lib/astrotrain/api.rb +52 -0
  10. data/lib/astrotrain/logged_mail.rb +48 -0
  11. data/lib/astrotrain/mapping.rb +162 -0
  12. data/lib/astrotrain/mapping/http_post.rb +18 -0
  13. data/lib/astrotrain/mapping/jabber.rb +28 -0
  14. data/lib/astrotrain/mapping/transport.rb +55 -0
  15. data/lib/astrotrain/message.rb +342 -0
  16. data/lib/astrotrain/tmail.rb +58 -0
  17. data/lib/astrotrain/worker.rb +65 -0
  18. data/lib/vendor/rest-client/README.rdoc +104 -0
  19. data/lib/vendor/rest-client/Rakefile +84 -0
  20. data/lib/vendor/rest-client/bin/restclient +65 -0
  21. data/lib/vendor/rest-client/foo.diff +66 -0
  22. data/lib/vendor/rest-client/lib/rest_client.rb +188 -0
  23. data/lib/vendor/rest-client/lib/rest_client/net_http_ext.rb +23 -0
  24. data/lib/vendor/rest-client/lib/rest_client/payload.rb +185 -0
  25. data/lib/vendor/rest-client/lib/rest_client/request_errors.rb +75 -0
  26. data/lib/vendor/rest-client/lib/rest_client/resource.rb +103 -0
  27. data/lib/vendor/rest-client/rest-client.gemspec +18 -0
  28. data/lib/vendor/rest-client/spec/base.rb +5 -0
  29. data/lib/vendor/rest-client/spec/master_shake.jpg +0 -0
  30. data/lib/vendor/rest-client/spec/payload_spec.rb +71 -0
  31. data/lib/vendor/rest-client/spec/request_errors_spec.rb +44 -0
  32. data/lib/vendor/rest-client/spec/resource_spec.rb +52 -0
  33. data/lib/vendor/rest-client/spec/rest_client_spec.rb +219 -0
  34. data/test/api_test.rb +32 -0
  35. data/test/fixtures/apple_multipart.txt +100 -0
  36. data/test/fixtures/bad_content_type.txt +27 -0
  37. data/test/fixtures/basic.txt +14 -0
  38. data/test/fixtures/custom.txt +15 -0
  39. data/test/fixtures/fwd.txt +0 -0
  40. data/test/fixtures/gb2312_encoding.txt +16 -0
  41. data/test/fixtures/gb2312_encoding_invalid.txt +15 -0
  42. data/test/fixtures/html.txt +10 -0
  43. data/test/fixtures/html_multipart.txt +16 -0
  44. data/test/fixtures/iso-8859-1.txt +13 -0
  45. data/test/fixtures/mapped.txt +13 -0
  46. data/test/fixtures/multipart.txt +213 -0
  47. data/test/fixtures/multipart2.txt +213 -0
  48. data/test/fixtures/multiple.txt +13 -0
  49. data/test/fixtures/multiple_delivered_to.txt +14 -0
  50. data/test/fixtures/multiple_with_body_recipients.txt +15 -0
  51. data/test/fixtures/reply.txt +16 -0
  52. data/test/fixtures/undisclosed.txt +14 -0
  53. data/test/fixtures/utf-8.txt +13 -0
  54. data/test/logged_mail_test.rb +67 -0
  55. data/test/mapping_test.rb +129 -0
  56. data/test/message_test.rb +492 -0
  57. data/test/test_helper.rb +56 -0
  58. data/test/transport_test.rb +113 -0
  59. metadata +330 -0
@@ -0,0 +1,492 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class Astrotrain::MessageTest < Astrotrain::TestCase
4
+ describe "mapping" do
5
+ describe "against default domain" do
6
+ before :all do
7
+ Astrotrain::Mapping.transaction do
8
+ Astrotrain::LoggedMail.all.destroy!
9
+ Astrotrain::Mapping.all.destroy!
10
+ @mapping = Astrotrain::Mapping.create!(:email_user => 'xyz')
11
+ @mapping2 = Astrotrain::Mapping.create!(:email_user => 'xyz', :email_domain => 'sample.com')
12
+ end
13
+ end
14
+
15
+ describe "without mapping" do
16
+ before do
17
+ Astrotrain::LoggedMail.all.destroy!
18
+ end
19
+
20
+ it "doesn't log message" do
21
+ @msg = Astrotrain::Message.receive(mail(:basic))
22
+ @log = Astrotrain::LoggedMail.first
23
+ assert_nil @log
24
+ end
25
+
26
+ it "logs message if Astrotrain::LoggedMail.log_processed" do
27
+ Astrotrain::LoggedMail.log_processed = true
28
+ @msg = Astrotrain::Message.receive(mail(:basic))
29
+ @log = Astrotrain::LoggedMail.first
30
+ assert @log
31
+ assert @log.error_message.blank?
32
+ end
33
+
34
+ it "calls pre_mapping callback" do
35
+ Astrotrain::LoggedMail.log_processed = true
36
+ callback_msg = nil
37
+ Astrotrain.callback(:pre_mapping) do |message|
38
+ callback_msg = message
39
+ end
40
+
41
+ @msg = Astrotrain::Message.receive(mail(:mapped))
42
+ assert_equal callback_msg, @msg
43
+ end
44
+
45
+ it "it allows pre_mapping callback to cancel processing" do
46
+ Astrotrain::LoggedMail.log_processed = true
47
+ Astrotrain.callback(:pre_mapping) do |message|
48
+ raise Astrotrain::ProcessingCancelled
49
+ end
50
+
51
+ @msg = Astrotrain::Message.receive(mail(:mapped))
52
+ assert_equal 0, Astrotrain::LoggedMail.count
53
+ end
54
+
55
+ it "calls pre_processing callback" do
56
+ Astrotrain::LoggedMail.log_processed = true
57
+ callback_msg, callback_map = nil
58
+ Astrotrain.callback(:pre_processing) do |message, mapping|
59
+ callback_msg = message
60
+ callback_map = mapping
61
+ end
62
+
63
+ @msg = Astrotrain::Message.receive(mail(:mapped))
64
+ @log = Astrotrain::LoggedMail.first
65
+ assert_equal callback_msg, @msg
66
+ assert_equal callback_map, @log.mapping
67
+ end
68
+
69
+ it "it allows pre_processing callback to cancel processing" do
70
+ Astrotrain::LoggedMail.log_processed = true
71
+ Astrotrain.callback(:pre_processing) do |message, mapping|
72
+ raise Astrotrain::ProcessingCancelled
73
+ end
74
+
75
+ @msg = Astrotrain::Message.receive(mail(:mapped))
76
+ @log = Astrotrain::LoggedMail.first
77
+ assert_equal "Cancelled.", @log.error_message
78
+ end
79
+
80
+ it "calls post_processing callback" do
81
+ Astrotrain::LoggedMail.log_processed = true
82
+ callback_msg, callback_map, callback_log = nil
83
+ Astrotrain.callback(:post_processing) do |message, mapping, log|
84
+ callback_msg = message
85
+ callback_map = mapping
86
+ callback_log = log
87
+ end
88
+
89
+ @msg = Astrotrain::Message.receive(mail(:mapped))
90
+ @log = Astrotrain::LoggedMail.first
91
+ assert_equal callback_msg, @msg
92
+ assert_equal callback_map, @log.mapping
93
+ assert_equal callback_log, @log
94
+ end
95
+
96
+ after do
97
+ Astrotrain::LoggedMail.log_processed = false
98
+ Astrotrain.clear_callbacks
99
+ end
100
+ end
101
+
102
+ describe "erroring" do
103
+ before do
104
+ Astrotrain::LoggedMail.all.destroy!
105
+ end
106
+
107
+ it "logs message without mappping" do
108
+ stub(Astrotrain::Mapping).match { raise RuntimeError }
109
+ @msg = Astrotrain::Message.receive(mail(:basic))
110
+ @log = Astrotrain::LoggedMail.first
111
+ assert @log
112
+ assert_nil @log.delivered_at
113
+ assert_match /RuntimeError/, @log.error_message
114
+ assert_nil @log.mapping
115
+ end
116
+
117
+ it "logs message with mappping" do
118
+ stub(Astrotrain::Mapping).match {@mapping}
119
+ stub(@mapping).process { raise RuntimeError }
120
+ @msg = Astrotrain::Message.receive(mail(:basic))
121
+ @log = Astrotrain::LoggedMail.first
122
+ assert @log
123
+ assert_nil @log.delivered_at
124
+ assert_match /RuntimeError/, @log.error_message
125
+ assert_equal @mapping, @log.mapping
126
+ end
127
+ end
128
+
129
+ describe "with mapping" do
130
+ before :all do
131
+ @msg = Astrotrain::Message.receive(mail(:mapped))
132
+ @log = Astrotrain::LoggedMail.first
133
+ end
134
+
135
+ it "does not log message" do
136
+ assert_nil @log
137
+ end
138
+ end
139
+ end
140
+ end
141
+
142
+ describe "parsing" do
143
+ before :all do
144
+ @body = "---------- Forwarded message ----------\nblah blah"
145
+ end
146
+
147
+ describe "basic, with bad content type header" do
148
+ before :all do
149
+ @raw = mail(:bad_content_type)
150
+ @message = Astrotrain::Message.parse(@raw)
151
+ end
152
+
153
+ it "parses body" do
154
+ expected = "--====boundary====\nContent-Type: text/plain; charset=\"us-ascii\"\n\nThis message is being generated automatically to notify you\nthat PowerMTA has crashed on mtasv.net.\n\nAs the information below is likely to be essential for debugging\nthe problem, please forward this message to <support@port25.com>.\nThank you.\n\n--====boundary====\nContent-Type: text/plain; charset=\"us-ascii\"\n\nYo\n--====boundary====--"
155
+ assert_equal expected, @message.body
156
+ end
157
+
158
+ it "attempts parsing bad header" do
159
+ assert_equal "multipart/mixed; boundary=\"====boundary=\"===\"\"", @message.header('content-type')
160
+ end
161
+ end
162
+
163
+ describe "basic, single sender/recipient" do
164
+ before :all do
165
+ @raw = mail(:basic)
166
+ @message = Astrotrain::Message.parse(@raw)
167
+ end
168
+
169
+ it "parses message-id and headers" do
170
+ assert_equal 'a16be7390810161014n52b603e9k1aa6bb803c6735aa@mail.gmail.com', @message.message_id
171
+ expected = {'mime-version' => '1.0', 'content-type' => 'text/plain; charset=ISO-8859-1', 'to' => 'Processor <processor@astrotrain.com>',
172
+ 'x-custom' => 'reply', 'content-transfer-encoding' => '7bit', 'content-disposition' => 'inline', 'message-id' => '<a16be7390810161014n52b603e9k1aa6bb803c6735aa@mail.gmail.com>'}
173
+ assert_equal expected, @message.headers
174
+ end
175
+
176
+ it "#parse parses TMail::Mail object from raw text" do
177
+ assert_kind_of TMail::Mail, @message.mail
178
+ end
179
+
180
+ it "recognizes Delivered-To and To: headers as recipients" do
181
+ assert_equal %w(processor@astrotrain.com), @message.recipients
182
+ end
183
+
184
+ it "recognizes From: header as sender" do
185
+ assert_equal %(Bob <user@example.com>), @message.sender
186
+ end
187
+
188
+ it "recognizes Subject: header" do
189
+ assert_equal 'Fwd: blah blah', @message.subject
190
+ end
191
+
192
+ it "recognizes message body" do
193
+ assert_equal @body, @message.body
194
+ end
195
+
196
+ it "retains raw message" do
197
+ assert_equal @raw, @message.raw
198
+ end
199
+ end
200
+
201
+ describe "iso 8859 1 encoded headers" do
202
+ before :all do
203
+ @raw = mail("iso-8859-1")
204
+ @message = Astrotrain::Message.parse(@raw)
205
+ end
206
+
207
+ it "recognizes From: header with strange encoding" do
208
+ assert_equal %(Matthéw <user@example.com>), @message.sender
209
+ end
210
+ end
211
+
212
+ describe "gb2312 encoded body" do
213
+ before :all do
214
+ @raw = mail("gb2312_encoding")
215
+ @message = Astrotrain::Message.parse(@raw)
216
+ end
217
+
218
+ it "converts to UTF-8" do
219
+ assert_equal "Dear Sirs, \r\nWe are given to understand that you are Manufacturer of plstic Bottles\r\nAdd: blah China",
220
+ @message.body
221
+ end
222
+ end
223
+
224
+ describe "gb2312 encoded body with invalid charset in mime version header" do
225
+ before :all do
226
+ @raw = mail("gb2312_encoding_invalid")
227
+ @message = Astrotrain::Message.parse(@raw)
228
+ end
229
+
230
+ it "converts to UTF-8" do
231
+ assert_equal "Dear Sirs, \r\nWe are given to understand that you are Manufacturer of plstic Bottles\r\nAdd: blah China",
232
+ @message.body
233
+ end
234
+ end
235
+
236
+ describe "utf 8 encoded headers" do
237
+ before :all do
238
+ @raw = mail("utf-8")
239
+ @message = Astrotrain::Message.parse(@raw)
240
+ end
241
+
242
+ it "recognizes From: header with strange encoding" do
243
+ assert_equal %(isnard naiké <user@example.com>), @message.sender
244
+ end
245
+ end
246
+
247
+ describe "multipart message with name property on Content Type" do
248
+ before :all do
249
+ @raw = mail(:multipart)
250
+ @message = Astrotrain::Message.parse(@raw)
251
+ end
252
+
253
+ it "#parse parses TMail::Mail object from raw text" do
254
+ assert_kind_of TMail::Mail, @message.mail
255
+ end
256
+
257
+ it "recognizes Delivered-To/To: headers as recipient" do
258
+ assert_equal %w(foo@example.com), @message.recipients
259
+ end
260
+
261
+ it "recognizes message body" do
262
+ assert_equal "Testing out rich emails with attachments!\nThis one has a name property on Content-Type.\n[state:hold responsible:rick]\n\n",
263
+ @message.body
264
+ end
265
+
266
+ it "retrieves attachments" do
267
+ assert_equal 1, @message.attachments.size
268
+ end
269
+
270
+ it "retrieves attachment filename" do
271
+ assert_equal 'bandit.jpg', @message.attachments.first.filename
272
+ end
273
+
274
+ it "retrieves attachment content_type" do
275
+ assert_equal 'image/jpeg', @message.attachments.first.content_type
276
+ end
277
+ end
278
+
279
+ describe "multipart message with filename property on Content Disposition" do
280
+ before :all do
281
+ @raw = mail(:multipart2)
282
+ @message = Astrotrain::Message.parse(@raw)
283
+ end
284
+
285
+ it "#parse parses TMail::Mail object from raw text" do
286
+ assert_kind_of TMail::Mail, @message.mail
287
+ end
288
+
289
+ it "recognizes Delivered-To/To: headers as recipient" do
290
+ assert_equal %w(foo@example.com), @message.recipients
291
+ end
292
+
293
+ it "recognizes message body" do
294
+ assert_equal "Testing out rich emails with attachments!\nThis one has NO name property on Content-Type.\n[state:hold responsible:rick]\n\n",
295
+ @message.body
296
+ end
297
+
298
+ it "retrieves attachments" do
299
+ assert_equal 1, @message.attachments.size
300
+ end
301
+
302
+ it "retrieves attachment filename" do
303
+ assert_equal 'bandit.jpg', @message.attachments.first.filename
304
+ end
305
+
306
+ it "retrieves attachment content_type" do
307
+ assert_equal 'image/jpeg', @message.attachments.first.content_type
308
+ end
309
+ end
310
+
311
+ describe "apple multipart message" do
312
+ before :all do
313
+ @raw = mail(:apple_multipart)
314
+ @message = Astrotrain::Message.parse(@raw)
315
+ end
316
+
317
+ it "#parse parses TMail::Mail object from raw text" do
318
+ assert_kind_of TMail::Mail, @message.mail
319
+ end
320
+
321
+ it "recognizes To: header as recipient" do
322
+ assert_equal %w(foo@example.com), @message.recipients
323
+ end
324
+
325
+ it "recognizes message body" do
326
+ assert_equal "Let's have a test here:\r\n\r\n\r\n\nYum\r\n\r\n\r\nOn Feb 10, 2009, at 3:37 PM, Tender Support wrote:\r\n\r\n> // Add your reply above here\r\n> ==================================================\r\n> From: Tyler Durden\r\n> Subject: Email attachments and file upload\r\n>\r\n> not at the moment ... let me test\r\n>\r\n> View this Discussion online: http://foobar.com\r\n> .\r\n\r\n\r\n\r\n\r\n--Apple-Mail-7-451386929--",
327
+ @message.body
328
+ end
329
+
330
+ it "retrieves attachments" do
331
+ assert_equal 1, @message.attachments.size
332
+ end
333
+
334
+ it "retrieves attachment filename" do
335
+ assert_equal 'logo.gif', @message.attachments.first.filename
336
+ end
337
+
338
+ it "retrieves attachment content_type" do
339
+ assert_equal 'image/gif', @message.attachments.first.content_type
340
+ end
341
+ end
342
+
343
+ describe "multiple sender/recipients" do
344
+ before :all do
345
+ @raw = mail(:multiple)
346
+ @message = Astrotrain::Message.parse(@raw)
347
+ end
348
+
349
+ it "#parse parses TMail::Mail object from raw text" do
350
+ assert_kind_of TMail::Mail, @message.mail
351
+ end
352
+
353
+ it "recognizes To: headers as recipients" do
354
+ assert_equal %w(processor@astrotrain.com other@example.com), @message.recipients
355
+ end
356
+
357
+ it "recognizes To: headers as recipients with custom header order" do
358
+ assert_equal %w(other@example.com processor@astrotrain.com), @message.recipients(%w(to original_to delivered_to))
359
+ end
360
+
361
+ it "recognizes From: header as sender" do
362
+ assert_equal %(user@example.com, boss@example.com), @message.sender
363
+ end
364
+
365
+ it "recognizes Subject: header" do
366
+ assert_equal 'Fwd: blah blah', @message.subject
367
+ end
368
+
369
+ it "recognizes message body" do
370
+ assert_equal @body, @message.body
371
+ end
372
+
373
+ it "retains raw message" do
374
+ assert_equal @raw, @message.raw
375
+ end
376
+ end
377
+
378
+ describe "recipients in the body" do
379
+ before :all do
380
+ @raw = mail(:multiple_with_body_recipients)
381
+ @message = Astrotrain::Message.parse(@raw)
382
+ end
383
+
384
+ it "recognizes in-body emails and To: headers as recipients" do
385
+ assert_equal %w(processor@astrotrain.com other@example.com processor+foobar@astrotrain.com processor+blah@astrotrain.com),
386
+ @message.recipients
387
+ end
388
+ end
389
+
390
+ describe "with only HTML body in a multipart message" do
391
+ before :all do
392
+ @raw = mail(:html_multipart)
393
+ @message = Astrotrain::Message.parse(@raw)
394
+ end
395
+
396
+ it "parses emtpy body" do
397
+ assert_equal '', @message.body
398
+ end
399
+
400
+ it "parses HTML body" do
401
+ assert_equal "<p>ABC</p>\n------", @message.html
402
+ end
403
+ end
404
+
405
+ describe "with only HTML body" do
406
+ before :all do
407
+ @raw = mail(:html)
408
+ @message = Astrotrain::Message.parse(@raw)
409
+ end
410
+
411
+ it "parses emtpy body" do
412
+ assert_equal '', @message.body
413
+ end
414
+
415
+ it "parses HTML body" do
416
+ assert_equal "<p>ABC</p>", @message.html
417
+ end
418
+ end
419
+
420
+ describe "with X Original To header" do
421
+ before :all do
422
+ @raw = mail(:custom)
423
+ @message = Astrotrain::Message.parse(@raw)
424
+ end
425
+
426
+ it "#parse parses TMail::Mail object from raw text" do
427
+ assert_kind_of TMail::Mail, @message.mail
428
+ end
429
+
430
+ it "recognizes X-Original-to: header as recipient" do
431
+ assert_equal %w(processor-reply-57@custom.com processor-delivered@astrotrain.com processor@astrotrain.com), @message.recipients
432
+ end
433
+
434
+ it "recognizes Delivered-To: header as recipient with custom header order" do
435
+ assert_equal %w(processor-delivered@astrotrain.com processor-reply-57@custom.com processor@astrotrain.com), @message.recipients(%w(delivered_to original_to to))
436
+ end
437
+
438
+ it "recognizes To: header as recipient with custom header order" do
439
+ assert_equal %w(processor@astrotrain.com processor-reply-57@custom.com processor-delivered@astrotrain.com), @message.recipients(%w(to original_to delivered_to))
440
+ end
441
+
442
+ it "recognizes From: header as sender" do
443
+ assert_equal %(user@example.com, boss@example.com), @message.sender
444
+ end
445
+
446
+ it "recognizes Subject: header" do
447
+ assert_equal 'Fwd: blah blah', @message.subject
448
+ end
449
+
450
+ it "recognizes message body" do
451
+ assert_equal @body, @message.body
452
+ end
453
+
454
+ it "retains raw message" do
455
+ assert_equal @raw, @message.raw
456
+ end
457
+ end
458
+
459
+ describe "with multiple Delivered To headers" do
460
+ before :all do
461
+ @raw = mail(:multiple_delivered_to)
462
+ @message = Astrotrain::Message.parse(@raw)
463
+ end
464
+
465
+ it "recognizes Delivered-to: header as recipient" do
466
+ assert_equal %w(processor-reply-57@custom.com processor-delivered@astrotrain.com processor@astrotrain.com), @message.recipients
467
+ end
468
+ end
469
+
470
+ it "parses invalid email collection" do
471
+ assert_equal %w(ricky@foo.com bobby@foo.com), Astrotrain::Message.parse_email_addresses("Ricky <ricky@foo.com>, Bobby:bobby@foo.com")
472
+ end
473
+
474
+ it "parses invalid email" do
475
+ assert_equal({:name => "Name", :email => "email@server.com"}, Astrotrain::Message.parse_email_address("Name:email@server.com"))
476
+ end
477
+
478
+ it "parses undisclosed recipients" do
479
+ raw = mail(:undisclosed)
480
+ m = Astrotrain::Message.parse(raw)
481
+ assert_equal(["undisclosed-recipients: ;"], m.recipients_from_to)
482
+ assert_equal({:name => "undisclosed-recipients"}, Astrotrain::Message.parse_email_address(m.recipients_from_to.first))
483
+ end
484
+ end
485
+
486
+ describe "queueing" do
487
+ it "writes contents queue path" do
488
+ filename = Astrotrain::Message.queue("boo!")
489
+ assert_equal 'boo!', IO.read(filename)
490
+ end
491
+ end
492
+ end