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

Check email format on registration #150

Merged
merged 1 commit into from
Feb 17, 2015
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
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