Skip to content

Commit

Permalink
Feature/Avoid redefining Ruby code builtin implementations (#194)
Browse files Browse the repository at this point in the history
* Updated Truemail::Validate::Smtp::Request::Session, tests
* Updated gemspec
* Updated reek/rubocop configs
* Updated version, changelog
  • Loading branch information
bestwebua authored Jan 4, 2022
1 parent 6bed505 commit 82f3a6a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 11 deletions.
1 change: 1 addition & 0 deletions .reek.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ detectors:
- Truemail::Validate::Mx#hosts_from_cname_records
- Truemail::Configuration#logger=
- Truemail::Validate::Smtp::Request#initialize
- Truemail::Validate::Smtp::Request::Session#initialize

TooManyInstanceVariables:
exclude:
Expand Down
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ Gemspec/DateAssignment:
Gemspec/RequireMFA:
Enabled: false

Gemspec/RubyVersionGlobalsUsage:
Enabled: false

Security/IoMethods:
Enabled: true

Expand Down
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.6.1] - 2022.01.04

### Fixed

- Fixed redefining builtin implementations caused using stdlib as external dependencies. Thanks [@allard](https://github.com/allard) for report.

### Updated

- Updated `Truemail::Validate::Smtp::Request::Session#initialize`, `Truemail::Validate::Smtp::Request::Session#start`, tests
- Updated rubocop/reek configs
- Updated gem docs, version

## [2.6.0] - 2021.12.28

### Added
Expand Down Expand Up @@ -33,7 +45,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Fixed

- Ruby 3.0 stdlib SMTP client SSL certificate verification issues for cases when IP address uses as MX host. Thanks [@esb](https://github.com/esb) for bug report.
- Fixed Ruby 3.0 stdlib SMTP client SSL certificate verification issues for cases when IP address uses as MX host. Thanks [@esb](https://github.com/esb) for bug report.

### Updated

Expand Down
20 changes: 15 additions & 5 deletions lib/truemail/validate/smtp/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,34 @@ def initialize(configuration)
class Session
require 'net/smtp'

def initialize(host, port, connection_timeout, response_timeout)
@net_smtp = (old_net_smtp? ? ::Net::SMTP.new(host, port) : ::Net::SMTP.new(host, port, tls_verify: false)).tap do |settings|
UNDEFINED_VERSION = '0.0.0'

def initialize(host, port, connection_timeout, response_timeout, net_class = ::Net::SMTP)
@net_class = net_class
@net_smtp_version = resolve_net_smtp_version
@net_smtp = (old_net_smtp? ? net_class.new(host, port) : net_class.new(host, port, tls_verify: false)).tap do |settings|
settings.open_timeout = connection_timeout
settings.read_timeout = response_timeout
end
end

def start(helo_domain, &block)
return net_smtp.start(helo_domain, &block) if old_net_smtp?
return net_smtp.start(helo_domain, &block) if net_smtp_version < '0.2.0'
return net_smtp.start(helo_domain, tls_verify: false, &block) if old_net_smtp?
net_smtp.start(helo: helo_domain, &block)
end

private

attr_reader :net_smtp
attr_reader :net_class, :net_smtp_version, :net_smtp

def resolve_net_smtp_version
return net_class::VERSION if net_class.const_defined?(:VERSION)
Truemail::Validate::Smtp::Request::Session::UNDEFINED_VERSION
end

def old_net_smtp?
!::Net::SMTP.const_defined?(:VERSION) || ::Net::SMTP::VERSION < '0.3.0'
net_smtp_version < '0.3.0'
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/truemail/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Truemail
VERSION = '2.6.0'
VERSION = '2.6.1'
end
18 changes: 15 additions & 3 deletions spec/truemail/validate/smtp/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@
subject(:session_start) { request_session_instance.start(helo_domain, &session_actions) }

let(:helo_domain) { random_domain_name }
let(:session_actions) { ->(_) {} }
let(:session_actions) { proc {} }

context 'when out of the box Net::SMTP version' do
before do
Expand All @@ -380,9 +380,9 @@
end
end

context 'when Net::SMTP version < 0.3.0' do
context 'when Net::SMTP version in range 0.1.0...0.2.0' do
before do
stub_const('Net::SMTP::VERSION', '0.2.128506')
stub_const('Net::SMTP::VERSION', '0.1.314')
allow(::Net::SMTP).to receive(:new).with(host, port).and_return(net_smtp_instance)
end

Expand All @@ -392,6 +392,18 @@
end
end

context 'when Net::SMTP version in range 0.2.0...0.3.0' do
before do
stub_const('Net::SMTP::VERSION', '0.2.128506')
allow(::Net::SMTP).to receive(:new).with(host, port).and_return(net_smtp_instance)
end

it 'passes helo domain as position argument' do
expect(net_smtp_instance).to receive(:start).with(helo_domain, tls_verify: false, &session_actions)
session_start
end
end

context 'when Net::SMTP version >= 0.3.0' do
before do
stub_const('Net::SMTP::VERSION', '0.3.0')
Expand Down
2 changes: 1 addition & 1 deletion truemail.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| ::File.basename(f) }
spec.require_paths = ['lib']

spec.add_runtime_dependency 'net-smtp', '~> 0.3'
spec.add_runtime_dependency 'net-smtp', '~> 0.3' if ::RUBY_VERSION >= '3.1.0'
spec.add_runtime_dependency 'simpleidn', '~> 0.2.1'

spec.add_development_dependency 'bundler-audit', '~> 0.9.0.1'
Expand Down

0 comments on commit 82f3a6a

Please sign in to comment.