Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build and migrate to GitHub Actions #125

Merged
merged 12 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: CI
on:
workflow_dispatch:
push:

permissions:
checks: write
contents: read
pull-requests: write

jobs:
rspec:
name: RSpec (ruby ${{ matrix.ruby-version }})
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
ruby-version:
- '1.9.3'
- '2.0'
- '2.1'
- '2.2'
- '2.3'
- '2.4'
- '2.5'
- '2.6'
- '2.7'
- '3.0'
- '3.1'
- '3.2'
- 'jruby-head'
- 'truffleruby-head'
env:
CI: true
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true
- name: Run tests
run: bundle exec rake
1 change: 0 additions & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
--warning
21 changes: 0 additions & 21 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ nil.email? # => false

## Code Status

[![Build Status](https://travis-ci.org/hallelujah/valid_email.svg?branch=master)](https://travis-ci.org/hallelujah/valid_email)
[![Build Status](https://github.com/hallelujah/valid_email/actions/workflows/ci.yaml/badge.svg)](https://github.com/hallelujah/valid_email/actions/workflows/ci.yaml)

## Credits

Expand Down
2 changes: 1 addition & 1 deletion lib/valid_email/email_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def validate_each(record,attribute,value)
end
unless r
msg = (options[:message] || I18n.t(:invalid, :scope => "valid_email.validations.email"))
record.errors.add attribute, message: I18n.interpolate(msgs, value: value)
record.errors.add attribute, message: I18n.interpolate(msg, value: value)
end
end
end
62 changes: 44 additions & 18 deletions spec/email_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
require 'spec_helper'

RSpec::Matchers.define :have_error_messages do |field, expected|
match do |model|
errors = model.errors[field]

messages = errors.map do |error|
case error
when String then error
when Hash then error[:message]
else fail ArgumentError, "Unknown model error type #{error.class}"
end
end

expect(messages).to eq expected
end
end
Comment on lines +3 to +17
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of adding a matcher 👍


describe EmailValidator do
email_class = Class.new do
include ActiveModel::Validations
Expand Down Expand Up @@ -69,43 +85,43 @@ def self.model_name

it "fails when email empty" do
expect(subject.valid?).to be_falsey
expect(subject.errors[:email]).to eq errors
expect(subject).to have_error_messages(:email, errors)
end

it "fails when email is not valid" do
subject.email = 'joh@doe'
expect(subject.valid?).to be_falsey
expect(subject.errors[:email]).to eq errors
expect(subject).to have_error_messages(:email, errors)
end

it "fails when email domain is prefixed with dot" do
subject.email = '[email protected]'
expect(subject.valid?).to be_falsey
expect(subject.errors[:email]).to eq errors
expect(subject).to have_error_messages(:email, errors)
end

it "fails when email domain contains two consecutive dots" do
subject.email = '[email protected]'
expect(subject.valid?).to be_falsey
expect(subject.errors[:email]).to eq errors
expect(subject).to have_error_messages(:email, errors)
end

it "fails when email ends with a period" do
subject.email = '[email protected].'
expect(subject.valid?).to be_falsey
expect(subject.errors[:email]).to eq errors
expect(subject).to have_error_messages(:email, errors)
end

it "fails when email ends with special characters" do
subject.email = '[email protected]&'
expect(subject.valid?).to be_falsey
expect(subject.errors[:email]).to eq errors
expect(subject).to have_error_messages(:email, errors)
end

it "fails when email is valid with information" do
subject.email = '"John Doe" <[email protected]>'
expect(subject.valid?).to be_falsey
expect(subject.errors[:email]).to eq errors
expect(subject).to have_error_messages(:email, errors)
end

it "passes when email is simple email address" do
Expand All @@ -117,42 +133,52 @@ def self.model_name
it "fails when email is simple email address not stripped" do
subject.email = '[email protected] '
expect(subject.valid?).to be_falsey
expect(subject.errors[:email]).to eq errors
expect(subject).to have_error_messages(:email, errors)
end

it "fails when domain contains a space" do
subject.email = 'john@doe .com'
expect(subject.valid?).to be_falsey
expect(subject.errors[:email]).to eq errors
expect(subject).to have_error_messages(:email, errors)
end

it "fails when passing multiple simple email addresses" do
subject.email = '[email protected], [email protected]'
expect(subject.valid?).to be_falsey
expect(subject.errors[:email]).to eq errors
expect(subject).to have_error_messages(:email, errors)
end

end

describe "validating email with MX and fallback to A" do
let(:dns) { instance_double('Resolv::DNS', :dns) }

subject { person_class_mx_with_fallback.new }

before do
allow(Resolv::DNS).to receive(:open).and_yield(dns)
end

it "passes when email domain has MX record" do
subject.email = '[email protected]'
allow(dns).to receive(:getresources).with('has-mx-record.org', Resolv::DNS::Resource::IN::MX).and_return(['1.2.3.4'])
subject.email = '[email protected]'
expect(subject.valid?).to be_truthy
expect(subject.errors[:email]).to be_empty
end

it "passes when email domain has no MX record but has an A record" do
subject.email = '[email protected]'
allow(dns).to receive(:getresources).with('has-a-record.org', Resolv::DNS::Resource::IN::MX).and_return([])
allow(dns).to receive(:getresources).with('has-a-record.org', Resolv::DNS::Resource::IN::A).and_return(['1.2.3.4'])
subject.email = '[email protected]'
expect(subject.valid?).to be_truthy
expect(subject.errors[:email]).to be_empty
end

it "fails when domain does not exists" do
subject.email = '[email protected]'
allow(dns).to receive(:getresources).with('does-not-exist.org', anything).and_return([])
subject.email = '[email protected]'
expect(subject.valid?).to be_falsey
expect(subject.errors[:email]).to eq errors
expect(subject).to have_error_messages(:email, errors)
end
end

Expand All @@ -168,13 +194,13 @@ def self.model_name
it "fails when email domain has no MX record" do
subject.email = '[email protected]'
expect(subject.valid?).to be_falsey
expect(subject.errors[:email]).to eq errors
expect(subject).to have_error_messages(:email, errors)
end

it "fails when domain does not exists" do
subject.email = '[email protected]'
expect(subject.valid?).to be_falsey
expect(subject.errors[:email]).to eq errors
expect(subject).to have_error_messages(:email, errors)
end
end

Expand Down Expand Up @@ -218,7 +244,7 @@ def self.model_name
it "fails when email from disposable email services" do
subject.email = '[email protected]'
expect(subject.valid?).to be_falsey
expect(subject.errors[:email]).to eq errors
expect(subject).to have_error_messages(:email, errors)
end
end

Expand All @@ -228,7 +254,7 @@ def self.model_name
it "does not pass with an invalid domain" do
subject.email = "[email protected]$\'"
expect(subject.valid?).to be_falsey
expect(subject.errors[:email]).to eq errors
expect(subject).to have_error_messages(:email, errors)
end

it "passes with valid domain" do
Expand Down
5 changes: 4 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

RSpec.configure do |config|
config.order = :random
config.profile_examples = 10
config.raise_errors_for_deprecations!
Kernel.srand config.seed

if ENV['CI']
config.warnings = true
end
end
6 changes: 3 additions & 3 deletions valid_email.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ Gem::Specification.new do |s|

# specify any dependencies here; for example:
s.add_development_dependency "rspec", "~> 3.10"
s.add_development_dependency "rake", '< 11.0'
s.add_development_dependency "rake"
s.add_runtime_dependency "mail", ">= 2.6.1"
s.add_runtime_dependency "simpleidn"
if Gem::Version.new(RUBY_VERSION) <= Gem::Version.new('2.0')
if Gem::Version.new(RUBY_VERSION.dup) <= Gem::Version.new('2.0')
s.add_runtime_dependency 'mime-types', '<= 2.6.2'
end
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.2.2')
if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.2.2')
s.add_runtime_dependency "activemodel", '< 5.0.0'
else
s.add_runtime_dependency "activemodel"
Expand Down