Skip to content

Commit

Permalink
Merge pull request #150 from nicolas-besnard/EmailValidation
Browse files Browse the repository at this point in the history
Check email format on registration
  • Loading branch information
lynndylanhurley committed Feb 17, 2015
2 parents ef33757 + 0f494ad commit 2c4931f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/devise_token_auth/concerns/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module DeviseTokenAuth::Concerns::User

serialize :tokens, JSON

validates_presence_of :email, if: Proc.new { |u| u.provider == 'email' }
validates :email, presence: true, email: true, if: Proc.new { |u| u.provider == 'email' }
validates_presence_of :uid, if: Proc.new { |u| u.provider != 'email' }

# only validate unique emails among email registration users
Expand Down
7 changes: 7 additions & 0 deletions app/validators/email_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
record.errors[attribute] << (options[:message] || 'is not an email')
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,36 @@ class DeviseTokenAuth::RegistrationsControllerTest < ActionDispatch::Integration
end
end

describe 'bad email' do
before do
post '/auth', {
email: "false_email@",
password: "secret123",
password_confirmation: "secret123",
confirm_success_url: Faker::Internet.url
}

@resource = assigns(:resource)
@data = JSON.parse(response.body)
end

test "request should not be successful" do
assert_equal 403, response.status
end

test "user should not have been created" do
assert_nil @resource.id
end

test "error should be returned in the response" do
assert @data['errors'].length
end

test "full_messages should be included in error hash" do
assert @data['errors']['full_messages'].length
end
end

describe "Mismatched passwords" do
before do
post '/auth', {
Expand Down

0 comments on commit 2c4931f

Please sign in to comment.