Skip to content

Commit

Permalink
Technical/Refactor tests (#172)
Browse files Browse the repository at this point in the history
* Updated Truemail::Validate::Smtp#attempts, tests
* Updated test helpers
  • Loading branch information
bestwebua authored Aug 17, 2021
1 parent e47bf19 commit d2c143a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/truemail/validate/smtp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def filtered_mail_servers_by_fail_fast_scenario
def attempts
@attempts ||= begin
return {} if fail_fast? || !mail_servers.one?
{ attempts: configuration.connection_attempts }
{ attempts: configuration.connection_attempts } # attempts should used for case with one mail server only
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/support/helpers/context_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def email_punycode_domain(email)
DnsMock::Representer::Punycode.call(domain_from_email(email))
end

def attempts_getter
->(smtp_request_instance) { smtp_request_instance.send(:attempts) }
end

private

def ffaker
Expand Down
10 changes: 10 additions & 0 deletions spec/support/helpers/context_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,14 @@
expect(random_internationalized_email).to eq("#{user}@#{ascii_word}.#{domain_zone}")
end
end

describe '#attempts_getter' do
let(:smtp_request_instance) { instance_double('SmtpRequestInstance') }

specify do
expect(attempts_getter).to be_an_instance_of(::Proc)
expect(smtp_request_instance).to receive(:attempts)
attempts_getter.call(smtp_request_instance)
end
end
end
70 changes: 47 additions & 23 deletions spec/truemail/validate/smtp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
described_class.new(
Truemail::Validator::Result.new(
email: email,
mail_servers: create_servers_list(2),
mail_servers: mail_servers,
configuration: configuration_instance
)
)
end

let(:mail_servers) { create_servers_list(2) }
let(:result_instance) { smtp_validator_instance.result }
let(:smtp_results) { smtp_validator_instance.smtp_results }

Expand Down Expand Up @@ -86,19 +87,41 @@
.from(0).to(result_instance.mail_servers.size)

expect(smtp_results.map(&:host)).to eq(result_instance.mail_servers)
expect(smtp_results.map(&attempts_getter).none?).to be(true)
end
end

context 'until request run fails' do
context 'until request run fails, more then one mail server' do
it 'creates smtp request instances' do
allow_any_instance_of(Truemail::Validate::Smtp::Request).to receive(:check_port).and_return(true)
allow_any_instance_of(Truemail::Validate::Smtp::Request).to receive(:run).and_return(false)

expect(result_instance)
.to receive(:punycode_email).exactly(result_instance.mail_servers.size).and_call_original
expect { smtp_validator_instance.send(:establish_smtp_connection) }
.to change(smtp_results, :size)
.from(0).to(result_instance.mail_servers.size)

expect(smtp_results.map(&:host)).to eq(result_instance.mail_servers)
expect(smtp_results.map(&attempts_getter).none?).to be(true)
end
end

context 'until request run fails, one mail server' do
let(:mail_servers) { create_servers_list(1) }

it 'creates smtp request instances' do
allow_any_instance_of(Truemail::Validate::Smtp::Request).to receive(:check_port).and_return(true)
allow_any_instance_of(Truemail::Validate::Smtp::Request).to receive(:run).and_return(false)

expect(result_instance)
.to receive(:punycode_email).exactly(result_instance.mail_servers.size).and_call_original
expect { smtp_validator_instance.send(:establish_smtp_connection) }
.to change(smtp_results, :size)
.from(0).to(result_instance.mail_servers.size)

expect(smtp_results.map(&:host)).to eq(result_instance.mail_servers)
expect(smtp_results.map(&attempts_getter).none?).to be(false)
end
end

Expand All @@ -108,11 +131,13 @@
allow_any_instance_of(Truemail::Validate::Smtp::Request).to receive(:run).and_return(false)
allow_any_instance_of(Truemail::Validate::Smtp::Response).to receive(:errors).and_return(rcptto: 'error')

expect(result_instance).to receive(:punycode_email).and_call_original
expect { smtp_validator_instance.send(:establish_smtp_connection) }
.to change(smtp_results, :size)
.from(0).to(1)

expect(smtp_results.last.send(:host)).to eq(result_instance.mail_servers.first)
expect(smtp_results.map(&attempts_getter).none?).to be(true)
end
end
end
Expand All @@ -125,12 +150,13 @@
allow_any_instance_of(Truemail::Validate::Smtp::Request).to receive(:check_port).and_return(false)
allow(Truemail::Validate::Smtp::Request).to receive(:new).and_call_original

expect(result_instance).to receive(:punycode_email).exactly(1).and_call_original
expect(result_instance).to receive(:punycode_email).and_call_original
expect { smtp_validator_instance.send(:establish_smtp_connection) }
.to change(smtp_results, :size)
.from(0).to(1)

expect(smtp_results.last.send(:host)).to eq(result_instance.mail_servers.first)
expect(smtp_results.map(&attempts_getter).none?).to be(true)
end
end

Expand All @@ -139,11 +165,13 @@
allow_any_instance_of(Truemail::Validate::Smtp::Request).to receive(:check_port).and_return(true)
allow_any_instance_of(Truemail::Validate::Smtp::Request).to receive(:run).and_return(false)

expect(result_instance).to receive(:punycode_email).and_call_original
expect { smtp_validator_instance.send(:establish_smtp_connection) }
.to change(smtp_results, :size)
.from(0).to(1)

expect(smtp_results.last.send(:host)).to eq(result_instance.mail_servers.first)
expect(smtp_results.map(&attempts_getter).none?).to be(true)
end
end
end
Expand All @@ -160,6 +188,7 @@
.to change(smtp_results, :size).from(0).to(1)

expect(smtp_results.last.send(:host)).to eq(result_instance.mail_servers.first)
expect(smtp_results.map(&attempts_getter).none?).to be(true)
end
end
end
Expand All @@ -185,16 +214,15 @@
end

specify do
allow(result_instance.mail_servers).to receive(:each)
allow(smtp_validator_instance.send(:filtered_mail_servers_by_fail_fast_scenario))
.to receive(:each)

expect { smtp_validator_instance.run }
.to not_change(result_instance, :success)
.and not_change(result_instance, :errors)
end

it 'returns true' do
expect(smtp_validator_instance.run).to be(true)
end
specify { expect(smtp_validator_instance.run).to be(true) }
end

context 'when smtp validation has only failed attempts' do
Expand Down Expand Up @@ -241,7 +269,8 @@

context 'wihout smtp safe check' do
specify do
allow(result_instance.mail_servers).to receive(:each)
allow(smtp_validator_instance.send(:filtered_mail_servers_by_fail_fast_scenario))
.to receive(:each)

expect { smtp_validator_instance.run }
.to change(result_instance, :success)
Expand All @@ -252,27 +281,24 @@
.from(nil).to(smtp_results)
end

it 'returns false' do
expect(smtp_validator_instance.run).to be(false)
end
specify { expect(smtp_validator_instance.run).to be(false) }
end

context 'with smtp safe check' do
before { configuration_instance.smtp_safe_check = true }

context 'when smtp user error has been not detected' do
specify do
allow(result_instance.mail_servers).to receive(:each)
allow(smtp_validator_instance.send(:filtered_mail_servers_by_fail_fast_scenario))
.to receive(:each)

expect { smtp_validator_instance.run }
.to not_change(result_instance, :success)
.and not_change(result_instance, :errors)
.and change(result_instance, :smtp_debug).from(nil).to(smtp_results)
end

it 'returns false' do
expect(smtp_validator_instance.run).to be(true)
end
specify { expect(smtp_validator_instance.run).to be(true) }
end

context 'when smtp user error has been detected' do
Expand All @@ -282,7 +308,8 @@
let(:smtp_error_context_2) { smtp_error_context }

specify do
allow(result_instance.mail_servers).to receive(:each)
allow(smtp_validator_instance.send(:filtered_mail_servers_by_fail_fast_scenario))
.to receive(:each)

expect { smtp_validator_instance.run }
.to change(result_instance, :success)
Expand All @@ -293,27 +320,24 @@
.from(nil).to(smtp_results)
end

it 'returns false' do
expect(smtp_validator_instance.run).to be(false)
end
specify { expect(smtp_validator_instance.run).to be(false) }
end

context 'with error in others smtp responses' do
context 'with error in rcptto response' do
let(:smtp_error_context_1) { smtp_error_context }

specify do
allow(result_instance.mail_servers).to receive(:each)
allow(smtp_validator_instance.send(:filtered_mail_servers_by_fail_fast_scenario))
.to receive(:each)

expect { smtp_validator_instance.run }
.to not_change(result_instance, :success)
.and not_change(result_instance, :errors)
.and change(result_instance, :smtp_debug).from(nil).to(smtp_results)
end

it 'returns false' do
expect(smtp_validator_instance.run).to be(true)
end
specify { expect(smtp_validator_instance.run).to be(true) }
end
end
end
Expand Down

0 comments on commit d2c143a

Please sign in to comment.