Skip to content

Commit

Permalink
sender validation and from address could mismatch, due that reason, d…
Browse files Browse the repository at this point in the history
…elay notification now allows setting up custom from address
  • Loading branch information
ibalosh committed Feb 16, 2018
1 parent 6bda62c commit 4c389ac
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 21 deletions.
9 changes: 5 additions & 4 deletions lib/mailhandler/receiving/notification/email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ module Notification
class Email

attr_reader :sender,
:from,
:contacts,
:min_time_to_notify,
:max_time_to_notify,
:current_state

def initialize(sender, contacts, min_time_to_notify = 60)
def initialize(sender, from, to, min_time_to_notify = 60)

@min_time_to_notify = min_time_to_notify

@sender = sender
@contacts = contacts
@from = from
@contacts = to
init_state
set_content_handler(EmailContent.new)

Expand All @@ -44,8 +46,7 @@ def set_state(state)
def send_email(type, search)

verify_email_type(type)
content = @content_handler.retrieve(type, search.options, Time.now - search.started_at,
sender.dispatcher.username, contacts)
content = @content_handler.retrieve(type, search.options, Time.now - search.started_at, from, contacts)
sender.send_email content

end
Expand Down
2 changes: 1 addition & 1 deletion lib/mailhandler/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module MailHandler
VERSION = '1.0.35'
VERSION = '1.0.36'
end
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,14 @@ Console notification is a good option if you are testing email delivery and want
To add console or email notifications, to your email searching all you need to do is:

``` ruby
email_receiver.add_observer(MailHandler::Receiving::Notification::Email.new(email_sender, contacts))
email_receiver.add_observer(MailHandler::Receiving::Notification::Email.new(email_sender, from, contacts))
email_receiver.add_observer(MailHandler::Receiving::Notification::Console.new)
```

For email notifications, the parameters you need are:

* `email_sender` - email sender you will use for sending an email (it should be one of senders described below)
* `from` - email address from which email is sent
* `contacts` - list of contacts to receive the notification (for example: `[email protected], [email protected]`

# Email sending
Expand Down
16 changes: 1 addition & 15 deletions spec/unit/mailhandler/receiving/notification/email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,60 @@

let(:search) { double('search') }
let(:sender) { double('sender') }
let(:notification) { MailHandler::Receiving::Notification::Email.new(sender, '[email protected]',1) }
let(:notification) { MailHandler::Receiving::Notification::Email.new(sender, '[email protected]', '[email protected]',1) }

before(:each) do

allow(sender).to receive(:send_email) { true }
allow(search).to receive(:max_duration) { 5 }

end

it '.create' do

aggregate_failures "init details" do

expect(notification.min_time_to_notify).to eq 1
expect(notification.max_time_to_notify).to eq nil
expect(notification.contacts).to eq '[email protected]'
expect(notification.sender).to eq sender

end

end

it '.notify' do

allow(search).to receive(:started_at) { Time.now }
notification.notify(search)
expect(notification.max_time_to_notify).to eq search.max_duration

end

context 'states' do

it 'no delay' do

allow(search).to receive(:started_at) { Time.now }
notification.notify(search)
expect(notification.current_state).to be_kind_of MailHandler::Receiving::Notification::NoDelay

end

it 'delayed' do

allow(search).to receive(:started_at) { Time.now - 2}
allow(search).to receive(:result) { false }
allow(notification).to receive(:send_email) { }
notification.notify(search)
expect(notification.current_state).to be_kind_of MailHandler::Receiving::Notification::Delay

end

it 'received' do

allow(search).to receive(:started_at) { Time.now - 2}
allow(search).to receive(:result) { true }
allow(notification).to receive(:send_email) { }
notification.notify(search)
expect(notification.current_state).to be_kind_of MailHandler::Receiving::Notification::Received

end

it 'max delayed' do

allow(search).to receive(:started_at) { Time.now - 10}
allow(search).to receive(:result) { false }
allow(notification).to receive(:send_email) { }
notification.notify(search)
expect(notification.current_state).to be_kind_of MailHandler::Receiving::Notification::MaxDelay

end

end
Expand Down

0 comments on commit 4c389ac

Please sign in to comment.