Skip to content

Commit

Permalink
Feature/Add input email type checking (#94)
Browse files Browse the repository at this point in the history
* Added Truemail::TypeError
* Updated Truemail.validate
* Updated Truemail.valid?
  • Loading branch information
bestwebua authored Oct 2, 2020
1 parent 8977763 commit e63eabf
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

- [ ] I have updated truemail to the latest version
- [ ] I have read the [Contribution Guidelines](https://github.com/rubygarage/truemail/blob/master/CONTRIBUTING.md)
- [ ] I have read the [documentation](https://github.com/rubygarage/truemail/blob/master/README.md)
- [ ] I have read the [documentation](https://truemail-rb.org/truemail-gem)
- [ ] I have searched for [existing GitHub issues](https://github.com/rubygarage/truemail/issues)

### Issue Description
Expand Down
12 changes: 12 additions & 0 deletions 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).

## [1.9.2] - 2020.10.02

### Added

- `Truemail::TypeError`
- error handling for invalid types as input email

### Changed

- Updated `Truemail.validate`
- Updated `Truemail.valid?`

## [1.9.1] - 2020.09.21

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
truemail (1.9.1)
truemail (1.9.2)
simpleidn (~> 0.1.1)

GEM
Expand Down
11 changes: 9 additions & 2 deletions lib/truemail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module Truemail
INCOMPLETE_CONFIG = 'verifier_email is required parameter'
NOT_CONFIGURED = 'use Truemail.configure before or pass custom configuration'
INVALID_TYPE = 'email should be a String'

class << self
def configuration(&block)
Expand All @@ -25,10 +26,12 @@ def reset_configuration!
end

def validate(email, custom_configuration: nil, **options)
check_argument_type(email)
Truemail::Validator.new(email, configuration: determine_configuration(custom_configuration), **options).run
end

def valid?(email, **options)
check_argument_type(email)
validate(email, **options).result.valid?
end

Expand All @@ -38,8 +41,12 @@ def host_audit(custom_configuration: nil)

private

def raise_unless(condition, message)
raise Truemail::ConfigurationError, message unless condition
def raise_unless(condition, message, error_class = Truemail::ConfigurationError)
raise error_class, message unless condition
end

def check_argument_type(argument)
raise_unless(argument.is_a?(String), Truemail::INVALID_TYPE, Truemail::TypeError)
end

def determine_configuration(custom_configuration)
Expand Down
1 change: 1 addition & 0 deletions lib/truemail/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Truemail
require_relative '../truemail/logger'

ConfigurationError = Class.new(StandardError)
TypeError = Class.new(StandardError)

ArgumentError = Class.new(StandardError) do
def initialize(arg_value, arg_name)
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 = '1.9.1'
VERSION = '1.9.2'
end
87 changes: 53 additions & 34 deletions spec/truemail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
specify { expect(described_class).to be_const_defined(:Validator) }
specify { expect(described_class).to be_const_defined(:Logger) }
specify { expect(described_class).to be_const_defined(:ConfigurationError) }
specify { expect(described_class).to be_const_defined(:TypeError) }
specify { expect(described_class).to be_const_defined(:ArgumentError) }
specify { expect(described_class).to be_const_defined(:RegexConstant) }
specify { expect(described_class).to be_const_defined(:Audit) }
Expand Down Expand Up @@ -118,6 +119,16 @@
end
end

shared_context 'when passed email is not a String' do
context 'when passed email is not a String' do
let(:email) { nil }

specify do
expect { subject }.to raise_error(Truemail::TypeError, Truemail::INVALID_TYPE) # rubocop:disable RSpec/NamedSubject
end
end
end

describe '.validate' do
subject(:validate) { described_class.validate(email, custom_configuration: custom_configuration) }

Expand All @@ -130,38 +141,42 @@

include_examples 'configuration error'

context 'when global configuration successfully set' do
before do
described_class.configure do |config|
config.verifier_email = 'admin@bestweb.com.ua'
config.connection_timeout = 1
config.response_timeout = 1
context 'when passed email is a String' do
context 'when global configuration successfully set' do
before do
described_class.configure do |config|
config.verifier_email = 'admin@bestweb.com.ua'
config.connection_timeout = 1
config.response_timeout = 1
end
end
end

include_examples 'returns validator instance'

# TODO: should be refactored with smtp-mock server in next release
# describe 'integration tests' do
# context 'when checks real email' do
# specify do
# expect(described_class.validate('admin@bestweb.com.ua').result.valid?).to be(true)
# end
# end

# context 'when checks fake email' do
# specify do
# expect(described_class.validate('nonexistent_email@bestweb.com.ua').result.valid?).to be(false)
# end
# end
# end
end
include_examples 'returns validator instance'

# TODO: should be refactored with smtp-mock server in next release
# describe 'integration tests' do
# context 'when checks real email' do
# specify do
# expect(described_class.validate('admin@bestweb.com.ua').result.valid?).to be(true)
# end
# end

# context 'when checks fake email' do
# specify do
# expect(described_class.validate('nonexistent_email@bestweb.com.ua').result.valid?).to be(false)
# end
# end
# end
end

context 'when custom configuration passed' do
let(:custom_configuration) { create_configuration }
context 'when custom configuration passed' do
let(:custom_configuration) { create_configuration }

include_examples 'returns validator instance'
include_examples 'returns validator instance'
end
end

include_context 'when passed email is not a String'
end

describe '.valid?' do
Expand All @@ -177,17 +192,21 @@

include_examples 'configuration error'

context 'when global configuration successfully set' do
before { described_class.configure { |config| config.verifier_email = email } }
context 'when passed email is a String' do
context 'when global configuration successfully set' do
before { described_class.configure { |config| config.verifier_email = email } }

include_examples 'returns boolean'
end
include_examples 'returns boolean'
end

context 'when custom configuration passed' do
let(:custom_configuration) { create_configuration }
context 'when custom configuration passed' do
let(:custom_configuration) { create_configuration }

include_examples 'returns boolean'
include_examples 'returns boolean'
end
end

include_context 'when passed email is not a String'
end

describe '.host_audit' do
Expand Down

0 comments on commit e63eabf

Please sign in to comment.