improve user email validation

pull/493/head
Alex Turchyn 5 months ago committed by Pete Matsyburka
parent 8b9678a434
commit fa0733f480

@ -73,6 +73,8 @@ class User < ApplicationRecord
scope :archived, -> { where.not(archived_at: nil) } scope :archived, -> { where.not(archived_at: nil) }
scope :admins, -> { where(role: ADMIN_ROLE) } scope :admins, -> { where(role: ADMIN_ROLE) }
validates :email, format: { with: /\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\z/ }
def access_token def access_token
super || build_access_token.tap(&:save!) super || build_access_token.tap(&:save!)
end end

@ -33,6 +33,14 @@ RSpec.describe 'Profile Settings' do
expect(user.last_name).to eq('Beckham') expect(user.last_name).to eq('Beckham')
expect(user.email).to eq('david.beckham@example.com') expect(user.email).to eq('david.beckham@example.com')
end end
it 'does not update if email is invalid' do
fill_in 'Email', with: 'devid+test@example'
all(:button, 'Update')[0].click
expect(page).to have_content('Email is invalid')
end
end end
context 'when changes password' do context 'when changes password' do

@ -51,6 +51,16 @@ RSpec.describe 'App Setup' do
end end
context 'when invalid information' do context 'when invalid information' do
it 'does not setup the app if the email is invalid' do
fill_setup_form(form_data.merge(email: 'bob@example-com'))
expect do
click_button 'Submit'
end.not_to(change(User, :count))
expect(page).to have_content('Email is invalid')
end
it 'does not setup the app if the password is too short' do it 'does not setup the app if the password is too short' do
fill_setup_form(form_data.merge(password: 'pass')) fill_setup_form(form_data.merge(password: 'pass'))

@ -92,6 +92,23 @@ RSpec.describe 'Team Settings' do
end end
end end
it 'does not allow to create a new user with an invalid email' do
click_link 'New User'
within '#modal' do
fill_in 'First name', with: 'Joseph'
fill_in 'Last name', with: 'Smith'
fill_in 'Email', with: 'joseph.smith@gmail'
fill_in 'Password', with: 'password'
expect do
click_button 'Submit'
end.not_to change(User, :count)
expect(page).to have_content('Email is invalid')
end
end
it 'updates a user' do it 'updates a user' do
first(:link, 'Edit').click first(:link, 'Edit').click

Loading…
Cancel
Save