mailhandler 1.0.36 → 1.0.37

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -2
  3. data/Rakefile +2 -2
  4. data/lib/mailhandler.rb +13 -37
  5. data/lib/mailhandler/errors.rb +1 -5
  6. data/lib/mailhandler/receiver.rb +4 -22
  7. data/lib/mailhandler/receiving/base.rb +16 -45
  8. data/lib/mailhandler/receiving/filelist/base.rb +25 -49
  9. data/lib/mailhandler/receiving/filelist/filter/base.rb +10 -52
  10. data/lib/mailhandler/receiving/filelist/filter/email.rb +4 -44
  11. data/lib/mailhandler/receiving/folder.rb +16 -54
  12. data/lib/mailhandler/receiving/imap.rb +34 -78
  13. data/lib/mailhandler/receiving/mail.rb +5 -19
  14. data/lib/mailhandler/receiving/notification/console.rb +2 -18
  15. data/lib/mailhandler/receiving/notification/email.rb +5 -28
  16. data/lib/mailhandler/receiving/notification/email/content.rb +9 -20
  17. data/lib/mailhandler/receiving/notification/email/states.rb +1 -36
  18. data/lib/mailhandler/receiving/observer.rb +5 -16
  19. data/lib/mailhandler/sender.rb +2 -14
  20. data/lib/mailhandler/sending/api.rb +1 -7
  21. data/lib/mailhandler/sending/api_batch.rb +1 -13
  22. data/lib/mailhandler/sending/base.rb +1 -13
  23. data/lib/mailhandler/sending/smtp.rb +20 -22
  24. data/lib/mailhandler/version.rb +2 -2
  25. data/mailhandler.gemspec +14 -17
  26. data/readme.md +33 -8
  27. data/spec/spec_helper.rb +1 -5
  28. data/spec/unit/mailhandler/receiver_spec.rb +8 -30
  29. data/spec/unit/mailhandler/receiving/base_spec.rb +4 -14
  30. data/spec/unit/mailhandler/receiving/folder_spec.rb +61 -155
  31. data/spec/unit/mailhandler/receiving/imap_spec.rb +18 -42
  32. data/spec/unit/mailhandler/receiving/notification/console_spec.rb +6 -16
  33. data/spec/unit/mailhandler/receiving/notification/email/content_spec.rb +10 -44
  34. data/spec/unit/mailhandler/receiving/notification/email_spec.rb +9 -15
  35. data/spec/unit/mailhandler/sender_spec.rb +12 -23
  36. data/spec/unit/mailhandler/sending/sender_api_batch_spec.rb +7 -19
  37. data/spec/unit/mailhandler/sending/sender_api_spec.rb +4 -14
  38. data/spec/unit/mailhandler/sending/sender_smtp_spec.rb +24 -6
  39. data/spec/unit/mailhandler_spec.rb +33 -25
  40. metadata +2 -2
@@ -1,29 +1,19 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe MailHandler::Receiving::Notification::Console do
4
-
5
- subject { MailHandler::Receiving::Notification::Console }
4
+ subject { MailHandler::Receiving::Notification::Console }
6
5
 
7
6
  context 'notify of a delay' do
8
-
9
7
  it '.notify' do
10
-
11
- search = double('Search', :started_at => Time.now - 10)
12
- expect{ subject.new.notify(search) }.to output(/.+email delay: 0(9|1)0 seconds/).to_stdout
13
-
8
+ search = double('Search', started_at: Time.now - 10)
9
+ expect { subject.new.notify(search) }.to output(/.+email delay: 0(9|1)0 seconds/).to_stdout
14
10
  end
15
-
16
11
  end
17
12
 
18
13
  context 'not notify of a delay' do
19
-
20
14
  it '.notify' do
21
-
22
- search = double('Search', :started_at => Time.now + 5)
23
- expect{ subject.new.notify(search) }.to output('').to_stdout
24
-
15
+ search = double('Search', started_at: Time.now + 5)
16
+ expect { subject.new.notify(search) }.to output('').to_stdout
25
17
  end
26
-
27
18
  end
28
-
29
- end
19
+ end
@@ -1,105 +1,71 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe MailHandler::Receiving::Notification::EmailContent do
4
-
5
4
  subject { MailHandler::Receiving::Notification::EmailContent.new }
6
5
 
7
6
  let(:to) { 'john@example.com' }
8
7
  let(:from) { 'igor@example.com' }
9
- let(:options) {{ :test => 'test' } }
8
+ let(:options) { { test: 'test' } }
10
9
 
11
10
  context '.email_received' do
12
-
13
11
  it 'create email' do
14
-
15
12
  expect(subject.retrieve(:received, options, 60, from, to)).to be_kind_of Mail::Message
16
-
17
13
  end
18
14
 
19
15
  context 'email content' do
20
-
21
16
  it 'sender' do
22
-
23
17
  expect(subject.retrieve(:received, options, 60, from, to)[:from].to_s).to eq from
24
-
25
18
  end
26
19
 
27
20
  it 'single recipient' do
28
-
29
21
  expect(subject.retrieve(:received, options, 60, from, to)[:to].to_s).to eq to
30
-
31
22
  end
32
23
 
33
24
  it 'multiple recipients' do
34
-
35
25
  to = 'john1@example.com, john2@example.com'
36
26
  expect(subject.retrieve(:received, options, 60, from, to)[:to].to_s).to eq to
37
-
38
27
  end
39
28
 
40
29
  it 'subject - 1 minute delay' do
41
-
42
- expect(subject.retrieve(:received, options, 60, from, to).subject).to eq "Received - delay was 1.0 minutes"
43
-
30
+ expect(subject.retrieve(:received, options, 60, from, to).subject).to eq 'Received - delay was 1.0 minutes'
44
31
  end
45
32
 
46
33
  it 'subject - 1.5 minute delay' do
47
-
48
- expect(subject.retrieve(:received, options, 90, from, to).subject).to eq "Received - delay was 1.5 minutes"
49
-
34
+ expect(subject.retrieve(:received, options, 90, from, to).subject).to eq 'Received - delay was 1.5 minutes'
50
35
  end
51
36
 
52
37
  it 'body' do
53
-
54
- expect(subject.retrieve(:received, options, 90, from, to).body.to_s).
55
- to eq "Received - delay was 1.5 minutes - search by #{options}"
56
-
38
+ expect(subject.retrieve(:received, options, 90, from, to).body.to_s)
39
+ .to eq "Received - delay was 1.5 minutes - search by #{options}"
57
40
  end
58
-
59
41
  end
60
-
61
42
  end
62
43
 
63
44
  context '.email_delayed' do
64
-
65
45
  it 'sender' do
66
-
67
46
  expect(subject.retrieve(:delayed, options, 60, from, to)[:from].to_s).to eq from
68
-
69
47
  end
70
48
 
71
49
  it 'single recipient' do
72
-
73
50
  expect(subject.retrieve(:delayed, options, 60, from, to)[:to].to_s).to eq to
74
-
75
51
  end
76
52
 
77
53
  it 'multiple recipients' do
78
-
79
54
  to = 'john1@example.com, john2@example.com'
80
55
  expect(subject.retrieve(:delayed, options, 60, from, to)[:to].to_s).to eq to
81
-
82
56
  end
83
57
 
84
58
  it 'subject - 1 minute delay' do
85
-
86
- expect(subject.retrieve(:delayed, options, 60, from, to).subject).to eq "Over 1.0 minutes delay"
87
-
59
+ expect(subject.retrieve(:delayed, options, 60, from, to).subject).to eq 'Over 1.0 minutes delay'
88
60
  end
89
61
 
90
62
  it 'subject - 1.5 minute delay' do
91
-
92
- expect(subject.retrieve(:delayed, options, 90, from, to).subject).to eq "Over 1.5 minutes delay"
93
-
63
+ expect(subject.retrieve(:delayed, options, 90, from, to).subject).to eq 'Over 1.5 minutes delay'
94
64
  end
95
65
 
96
66
  it 'body' do
97
-
98
- expect(subject.retrieve(:delayed, options, 90, from, to).body.to_s).
99
- to eq "Over 1.5 minutes delay - search by #{options}"
100
-
67
+ expect(subject.retrieve(:delayed, options, 90, from, to).body.to_s)
68
+ .to eq "Over 1.5 minutes delay - search by #{options}"
101
69
  end
102
-
103
70
  end
104
-
105
- end
71
+ end
@@ -1,10 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe MailHandler::Receiving::Notification::Email do
4
-
5
4
  let(:search) { double('search') }
6
5
  let(:sender) { double('sender') }
7
- let(:notification) { MailHandler::Receiving::Notification::Email.new(sender, 'from@example.com', 'igor@example.com',1) }
6
+ let(:notification) { MailHandler::Receiving::Notification::Email.new(sender, 'from@example.com', 'igor@example.com', 1) }
8
7
 
9
8
  before(:each) do
10
9
  allow(sender).to receive(:send_email) { true }
@@ -12,14 +11,12 @@ describe MailHandler::Receiving::Notification::Email do
12
11
  end
13
12
 
14
13
  it '.create' do
15
-
16
- aggregate_failures "init details" do
14
+ aggregate_failures 'init details' do
17
15
  expect(notification.min_time_to_notify).to eq 1
18
16
  expect(notification.max_time_to_notify).to eq nil
19
17
  expect(notification.contacts).to eq 'igor@example.com'
20
18
  expect(notification.sender).to eq sender
21
19
  end
22
-
23
20
  end
24
21
 
25
22
  it '.notify' do
@@ -29,7 +26,6 @@ describe MailHandler::Receiving::Notification::Email do
29
26
  end
30
27
 
31
28
  context 'states' do
32
-
33
29
  it 'no delay' do
34
30
  allow(search).to receive(:started_at) { Time.now }
35
31
  notification.notify(search)
@@ -37,29 +33,27 @@ describe MailHandler::Receiving::Notification::Email do
37
33
  end
38
34
 
39
35
  it 'delayed' do
40
- allow(search).to receive(:started_at) { Time.now - 2}
36
+ allow(search).to receive(:started_at) { Time.now - 2 }
41
37
  allow(search).to receive(:result) { false }
42
- allow(notification).to receive(:send_email) { }
38
+ allow(notification).to receive(:send_email) {}
43
39
  notification.notify(search)
44
40
  expect(notification.current_state).to be_kind_of MailHandler::Receiving::Notification::Delay
45
41
  end
46
42
 
47
43
  it 'received' do
48
- allow(search).to receive(:started_at) { Time.now - 2}
44
+ allow(search).to receive(:started_at) { Time.now - 2 }
49
45
  allow(search).to receive(:result) { true }
50
- allow(notification).to receive(:send_email) { }
46
+ allow(notification).to receive(:send_email) {}
51
47
  notification.notify(search)
52
48
  expect(notification.current_state).to be_kind_of MailHandler::Receiving::Notification::Received
53
49
  end
54
50
 
55
51
  it 'max delayed' do
56
- allow(search).to receive(:started_at) { Time.now - 10}
52
+ allow(search).to receive(:started_at) { Time.now - 10 }
57
53
  allow(search).to receive(:result) { false }
58
- allow(notification).to receive(:send_email) { }
54
+ allow(notification).to receive(:send_email) {}
59
55
  notification.notify(search)
60
56
  expect(notification.current_state).to be_kind_of MailHandler::Receiving::Notification::MaxDelay
61
57
  end
62
-
63
58
  end
64
-
65
- end
59
+ end
@@ -1,60 +1,49 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe MailHandler::Sender do
4
-
5
4
  subject { MailHandler::Sender }
6
5
 
7
6
  let(:send_duration) { 3 }
8
- let(:dispatcher) {
7
+ let(:dispatcher) do
9
8
  dispatcher = double('Dispatcher')
10
9
 
11
- allow(dispatcher).to receive(:send) { sleep send_duration ; 'Sent' }
10
+ allow(dispatcher).to receive(:send) { sleep send_duration; 'Sent' }
12
11
  dispatcher
13
-
14
- }
15
- let(:mail) {
16
-
12
+ end
13
+ let(:mail) do
17
14
  Mail.new do
18
- from "sheldon@bigbangtheory.com"
19
- to "lenard@bigbangtheory.com"
20
- subject "Hello!"
21
- body "Hello Sheldon!"
15
+ from 'sheldon@bigbangtheory.com'
16
+ to 'lenard@bigbangtheory.com'
17
+ subject 'Hello!'
18
+ body 'Hello Sheldon!'
22
19
  end
23
-
24
- }
20
+ end
25
21
 
26
22
  let(:sender) { subject.new(dispatcher) }
27
23
 
28
24
  it 'create' do
29
-
30
25
  expect(sender).not_to be nil
31
-
32
26
  end
33
27
 
34
28
  it 'init details' do
35
-
36
- aggregate_failures "sending details" do
29
+ aggregate_failures 'sending details' do
37
30
  expect(sender.sending.started_at).to be nil
38
31
  expect(sender.sending.finished_at).to be nil
39
32
  expect(sender.sending.duration).to be nil
40
33
  expect(sender.sending.response).to be nil
41
34
  expect(sender.sending.email).to be nil
42
35
  end
43
-
44
36
  end
45
37
 
46
38
  it '.send_email' do
47
-
48
39
  sender.send_email(mail)
49
40
 
50
- aggregate_failures "sending details" do
41
+ aggregate_failures 'sending details' do
51
42
  expect(sender.sending.started_at).to be_within(1).of(Time.now - send_duration)
52
43
  expect(sender.sending.finished_at).to be_within(1).of(Time.now)
53
44
  expect(sender.sending.duration).to be_within(0.5).of(send_duration)
54
45
  expect(sender.sending.response).to eq 'Sent'
55
46
  expect(sender.sending.email).to be mail
56
47
  end
57
-
58
48
  end
59
-
60
- end
49
+ end
@@ -1,49 +1,37 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe MailHandler::Sending::PostmarkBatchAPISender do
4
-
5
4
  subject { MailHandler::Sending::PostmarkBatchAPISender }
6
5
 
7
6
  let(:api_token) { '122878782' }
8
7
 
9
8
  it 'create' do
10
-
11
9
  sender = subject.new(api_token)
12
10
 
13
- aggregate_failures "init details" do
11
+ aggregate_failures 'init details' do
14
12
  expect(sender.api_token).to eq api_token
15
13
  expect(sender.type).to eq :postmark_api
16
14
  expect(sender.use_ssl).to be false
17
15
  expect(sender.host).to eq 'api.postmarkapp.com'
18
16
  end
19
-
20
17
  end
21
18
 
22
19
  it '.send - invalid auth' do
23
-
24
20
  sender = subject.new(api_token)
25
- expect{sender.send([Mail.new])}.to raise_error Postmark::InvalidApiKeyError
26
-
21
+ expect { sender.send([Mail.new]) }.to raise_error Postmark::InvalidApiKeyError
27
22
  end
28
23
 
29
24
  context 'invalid sending object' do
30
-
31
25
  it '.send with string parameter' do
32
-
33
26
  sender = subject.new(api_token)
34
- expect{sender.send('test')}.
35
- to raise_error MailHandler::TypeError, 'Invalid type error, only Array of Mail::Message object types for sending allowed'
36
-
27
+ expect { sender.send('test') }
28
+ .to raise_error MailHandler::TypeError, 'Invalid type error, only Array of Mail::Message object types for sending allowed'
37
29
  end
38
30
 
39
31
  it '.send with incorrect array parameter' do
40
-
41
32
  sender = subject.new(api_token)
42
- expect{sender.send([1,2,2])}.
43
- to raise_error MailHandler::TypeError, 'Invalid type error, only Array of Mail::Message object types for sending allowed'
44
-
33
+ expect { sender.send([1, 2, 2]) }
34
+ .to raise_error MailHandler::TypeError, 'Invalid type error, only Array of Mail::Message object types for sending allowed'
45
35
  end
46
-
47
36
  end
48
-
49
- end
37
+ end
@@ -1,40 +1,30 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe MailHandler::Sending::PostmarkAPISender do
4
-
5
4
  subject { MailHandler::Sending::PostmarkAPISender }
6
5
 
7
6
  let(:api_token) { '122878782' }
8
7
 
9
8
  it 'create' do
10
-
11
9
  sender = subject.new(api_token)
12
10
 
13
- aggregate_failures "init details" do
11
+ aggregate_failures 'init details' do
14
12
  expect(sender.api_token).to eq api_token
15
13
  expect(sender.type).to eq :postmark_api
16
14
  expect(sender.use_ssl).to be false
17
15
  expect(sender.host).to eq 'api.postmarkapp.com'
18
16
  end
19
-
20
17
  end
21
18
 
22
19
  it '.send - invalid auth' do
23
-
24
20
  sender = subject.new(api_token)
25
- expect{sender.send(Mail.new)}.to raise_error Postmark::InvalidApiKeyError
26
-
21
+ expect { sender.send(Mail.new) }.to raise_error Postmark::InvalidApiKeyError
27
22
  end
28
23
 
29
24
  context 'invalid sending object' do
30
-
31
25
  it '.send' do
32
-
33
26
  sender = subject.new(api_token)
34
- expect{sender.send('test')}.to raise_error StandardError
35
-
27
+ expect { sender.send('test') }.to raise_error StandardError
36
28
  end
37
-
38
29
  end
39
-
40
- end
30
+ end
@@ -1,22 +1,40 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe MailHandler::Sending::SMTPSender do
4
-
5
4
  subject { MailHandler::Sending::SMTPSender }
6
5
 
7
6
  context '.send' do
8
-
9
7
  context 'invalid' do
10
-
11
8
  it 'incorrect mail type' do
12
-
13
9
  sender = subject.new
14
10
  expect { sender.send('Test') }.to raise_error MailHandler::TypeError
11
+ end
12
+ end
13
+ end
14
+
15
+ context '.new' do
16
+ context 'smtp timeouts' do
17
+ it 'open' do
18
+ sender = subject.new
19
+ expect(sender.open_timeout).to be > 0
20
+ end
15
21
 
22
+ it 'read' do
23
+ sender = subject.new
24
+ expect(sender.read_timeout).to be > 0
16
25
  end
26
+ end
17
27
 
28
+ it 'save response' do
29
+ expect(subject.new.save_response).to be false
18
30
  end
19
31
 
20
- end
32
+ it 'use ssl' do
33
+ expect(subject.new.use_ssl).to be false
34
+ end
21
35
 
22
- end
36
+ it 'authentication' do
37
+ expect(subject.new.authentication).to eq 'plain'
38
+ end
39
+ end
40
+ end
@@ -1,59 +1,67 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe MailHandler::Handler do
4
-
5
4
  subject { MailHandler::Handler.new }
6
5
 
7
6
  context 'receiver' do
8
-
9
7
  it 'create - invalid type' do
10
-
11
- expect { subject.init_receiver(:test) }.
12
- to raise_error(MailHandler::TypeError, 'Unknown type - test, possible options: [:folder, :imap].')
13
-
8
+ expect { subject.init_receiver(:test) }
9
+ .to raise_error(MailHandler::TypeError, 'Unknown type - test, possible options: [:folder, :imap].')
14
10
  end
15
11
 
16
12
  it 'create - folder' do
17
-
18
13
  expect(subject.init_receiver(:folder)).to be_kind_of MailHandler::Receiver
19
-
20
14
  end
21
15
 
22
16
  it 'create - imap' do
23
-
24
17
  expect(subject.init_receiver(:imap)).to be_kind_of MailHandler::Receiver
25
-
26
18
  end
27
-
28
19
  end
29
20
 
30
21
  context 'init_sender' do
31
-
32
22
  it 'create - invalid type' do
33
-
34
- expect { subject.init_sender(:test) }.
35
- to raise_error(MailHandler::TypeError, 'Unknown type - test, possible options: [:postmark_api, :postmark_batch_api, :smtp].')
36
-
23
+ expect { subject.init_sender(:test) }
24
+ .to raise_error(MailHandler::TypeError, 'Unknown type - test, possible options: [:postmark_api, :postmark_batch_api, :smtp].')
37
25
  end
38
26
 
39
27
  it 'create - postmark api' do
40
-
41
28
  expect(subject.init_sender(:postmark_api)).to be_kind_of MailHandler::Sender
42
-
43
29
  end
44
30
 
45
31
  it 'create - postmark batch api' do
46
-
47
32
  expect(subject.init_sender(:postmark_batch_api)).to be_kind_of MailHandler::Sender
48
-
49
33
  end
50
34
 
51
- it 'create - smtp' do
35
+ context 'smtp' do
36
+ it 'create - smtp' do
37
+ expect(subject.init_sender(:smtp)).to be_kind_of MailHandler::Sender
38
+ end
52
39
 
53
- expect(subject.init_sender(:smtp)).to be_kind_of MailHandler::Sender
40
+ context 'set delivery methods' do
41
+ it 'save response' do
42
+ sender = MailHandler.sender(:smtp) do |dispatcher|
43
+ dispatcher.save_response = true
44
+ end
54
45
 
55
- end
46
+ expect(sender.dispatcher.save_response).to be true
47
+ end
56
48
 
57
- end
49
+ it 'open timeout' do
50
+ sender = MailHandler.sender(:smtp) do |dispatcher|
51
+ dispatcher.open_timeout = 30
52
+ end
53
+
54
+ expect(sender.dispatcher.open_timeout).to be 30
55
+ end
56
+
57
+ it 'read timeout' do
58
+ sender = MailHandler.sender(:smtp) do |dispatcher|
59
+ dispatcher.read_timeout = 30
60
+ end
58
61
 
59
- end
62
+ expect(sender.dispatcher.read_timeout).to be 30
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end